blob: ae91455e0ebb4a337a9ae547fea80edb0111bc93 [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
Damien George6be0b0a2014-08-15 14:30:52 +0100321STATIC void c_assign_power(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];
328 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
329 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
Damiend99b0522013-12-21 18:17:45 +0000369 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100370 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100371 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100372
373 return;
374
375cannot_assign:
376 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100377}
378
Damien George0288cf02014-04-11 11:53:00 +0000379// 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 +0100380STATIC 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 +0000381 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
382
383 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000384 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000385 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
386 EMIT_ARG(unpack_ex, 0, num_tail);
387 have_star_index = 0;
388 }
Damien George963a5a32015-01-16 17:47:07 +0000389 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000390 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000391 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000392 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
393 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100394 } else {
Damien George9d181f62014-04-27 16:55:27 +0100395 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100396 return;
397 }
398 }
399 }
Damien George963a5a32015-01-16 17:47:07 +0000400 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000401 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100402 }
Damien George0288cf02014-04-11 11:53:00 +0000403 if (num_head != 0) {
404 if (0 == have_star_index) {
405 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damien George0288cf02014-04-11 11:53:00 +0000407 c_assign(comp, node_head, ASSIGN_STORE);
408 }
409 }
Damien George963a5a32015-01-16 17:47:07 +0000410 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000411 if (num_head + i == have_star_index) {
412 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
413 } else {
414 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100415 }
416 }
417}
418
419// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100420STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000421 assert(!MP_PARSE_NODE_IS_NULL(pn));
422 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000423 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000424 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100425 switch (assign_kind) {
426 case ASSIGN_STORE:
427 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000428 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100429 break;
430 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000431 default:
Damien George542bd6b2015-03-26 14:42:40 +0000432 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100433 break;
434 }
435 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100436 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100437 return;
438 }
439 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000440 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000441 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
442 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100443 case PN_power:
444 // lhs is an index or attribute
445 c_assign_power(comp, pns, assign_kind);
446 break;
447
448 case PN_testlist_star_expr:
449 case PN_exprlist:
450 // lhs is a tuple
451 if (assign_kind != ASSIGN_STORE) {
452 goto bad_aug;
453 }
Damien George0288cf02014-04-11 11:53:00 +0000454 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100455 break;
456
457 case PN_atom_paren:
458 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000459 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100460 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100461 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000462 } else {
463 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000464 if (assign_kind != ASSIGN_STORE) {
465 goto bad_aug;
466 }
Damiend99b0522013-12-21 18:17:45 +0000467 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100468 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100469 }
470 break;
471
472 case PN_atom_bracket:
473 // lhs is something in brackets
474 if (assign_kind != ASSIGN_STORE) {
475 goto bad_aug;
476 }
Damiend99b0522013-12-21 18:17:45 +0000477 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100478 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000479 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000480 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
481 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100482 goto testlist_comp;
483 } else {
484 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000485 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100486 }
487 break;
488
489 default:
Damien George9d181f62014-04-27 16:55:27 +0100490 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100491 }
492 return;
493
494 testlist_comp:
495 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000496 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
497 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
498 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100499 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000500 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000501 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000502 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100503 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000504 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
505 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000506 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100507 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100508 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100509 } else {
510 // sequence with 2 items
511 goto sequence_with_2_items;
512 }
513 } else {
514 // sequence with 2 items
515 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000516 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100517 }
518 return;
519 }
520 return;
521
Damien George9d181f62014-04-27 16:55:27 +0100522 cannot_assign:
523 compile_syntax_error(comp, pn, "can't assign to expression");
524 return;
525
Damien429d7192013-10-04 19:53:11 +0100526 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100527 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100528}
529
Damien George65dc9602015-08-14 12:24:11 +0100530// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100531// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
532// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
533// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100534STATIC 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 +0100535 assert(n_pos_defaults >= 0);
536 assert(n_kw_defaults >= 0);
537
Damien George3a3db4d2015-10-22 23:45:37 +0100538 // set flags
539 if (n_kw_defaults > 0) {
540 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
541 }
542 this_scope->num_def_pos_args = n_pos_defaults;
543
Damien429d7192013-10-04 19:53:11 +0100544 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000545 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100546 int nfree = 0;
547 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000548 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
549 id_info_t *id = &comp->scope_cur->id_info[i];
550 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
551 for (int j = 0; j < this_scope->id_info_len; j++) {
552 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100553 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000554 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000555 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000556 nfree += 1;
557 }
558 }
Damien429d7192013-10-04 19:53:11 +0100559 }
560 }
561 }
Damien429d7192013-10-04 19:53:11 +0100562
563 // make the function/closure
564 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100565 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100566 } else {
Damien George3558f622014-04-20 17:50:40 +0100567 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100568 }
569}
570
Damien George2c838942015-11-17 14:00:14 +0000571STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
572 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
573 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100574 comp->have_star = true;
575 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000576 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
577 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100578 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100579 } else {
580 // named star
Damien429d7192013-10-04 19:53:11 +0100581 }
Damien George8b19db02014-04-11 23:25:34 +0100582 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000583
Damien George2c838942015-11-17 14:00:14 +0000584 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
585 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100586 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000587 // TODO do we need to do anything with this?
588
589 } else {
590 mp_parse_node_t pn_id;
591 mp_parse_node_t pn_colon;
592 mp_parse_node_t pn_equal;
593 if (MP_PARSE_NODE_IS_ID(pn)) {
594 // this parameter is just an id
595
596 pn_id = pn;
597 pn_colon = MP_PARSE_NODE_NULL;
598 pn_equal = MP_PARSE_NODE_NULL;
599
Damien George2c838942015-11-17 14:00:14 +0000600 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000601 // this parameter has a colon and/or equal specifier
602
603 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
604 pn_id = pns->nodes[0];
605 pn_colon = pns->nodes[1];
606 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000607
608 } else {
609 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
610 // this parameter has an equal specifier
611
612 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
613 pn_id = pns->nodes[0];
614 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000615 }
616
617 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
618 // this parameter does not have a default value
619
620 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100621 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100622 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000623 return;
624 }
625
626 } else {
627 // this parameter has a default value
628 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
629
Damien George8b19db02014-04-11 23:25:34 +0100630 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000631 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000632 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
633 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100634 // in Micro Python we put the default positional parameters into a tuple using the bytecode
635 // we need to do this here before we start building the map for the default keywords
636 if (comp->num_default_params > 0) {
637 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100638 } else {
639 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100640 }
Damien George69b89d22014-04-11 13:38:30 +0000641 // first default dict param, so make the map
642 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000643 }
Damien Georgef0778a72014-06-07 22:01:00 +0100644
645 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000646 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000647 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000648 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000649 } else {
Damien George69b89d22014-04-11 13:38:30 +0000650 comp->num_default_params += 1;
651 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000652 }
653 }
654
655 // TODO pn_colon not implemented
656 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100657 }
658}
659
Damien George2c838942015-11-17 14:00:14 +0000660STATIC 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 +0000661 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
662 // expression for default arguments, which may contain a lambda. The lambda will
663 // call here in a nested way, so we must save and restore the relevant state.
664 bool orig_have_star = comp->have_star;
665 uint16_t orig_num_dict_params = comp->num_dict_params;
666 uint16_t orig_num_default_params = comp->num_default_params;
667
Damien George2c838942015-11-17 14:00:14 +0000668 // compile default parameters
669 comp->have_star = false;
670 comp->num_dict_params = 0;
671 comp->num_default_params = 0;
672 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
673
674 if (comp->compile_error != MP_OBJ_NULL) {
675 return;
676 }
677
678 // in Micro Python we put the default positional parameters into a tuple using the bytecode
679 // the default keywords args may have already made the tuple; if not, do it now
680 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
681 EMIT_ARG(build_tuple, comp->num_default_params);
682 EMIT(load_null); // sentinel indicating empty default keyword args
683 }
684
685 // make the function
686 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000687
688 // restore state
689 comp->have_star = orig_have_star;
690 comp->num_dict_params = orig_num_dict_params;
691 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000692}
693
Damien429d7192013-10-04 19:53:11 +0100694// leaves function object on stack
695// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000696STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100697 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100698 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000699 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100700 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000701 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100702 }
703
Damien429d7192013-10-04 19:53:11 +0100704 // get the scope for this function
705 scope_t *fscope = (scope_t*)pns->nodes[4];
706
Damien George2c838942015-11-17 14:00:14 +0000707 // compile the function definition
708 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100709
Damien429d7192013-10-04 19:53:11 +0100710 // return its name (the 'f' in "def f(...):")
711 return fscope->simple_name;
712}
713
714// leaves class object on stack
715// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000716STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100717 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100718 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000719 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100720 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000721 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100722 }
723
724 EMIT(load_build_class);
725
726 // scope for this class
727 scope_t *cscope = (scope_t*)pns->nodes[3];
728
729 // compile the class
730 close_over_variables_etc(comp, cscope, 0, 0);
731
732 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000733 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100734
735 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100736 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
737 mp_parse_node_t parents = pns->nodes[1];
738 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
739 parents = MP_PARSE_NODE_NULL;
740 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000741 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100742 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100743
744 // return its name (the 'C' in class C(...):")
745 return cscope->simple_name;
746}
747
Damien6cdd3af2013-10-05 18:08:26 +0100748// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200749STATIC 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 +0000750 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100751 return false;
752 }
753
754 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100755 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100756 return true;
757 }
758
Damiend99b0522013-12-21 18:17:45 +0000759 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100760 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100761 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100762#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000763 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100764 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000765 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100766 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100767#endif
Damien3ef4abb2013-10-12 16:53:13 +0100768#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000769 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100770 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100771#endif
Damien6cdd3af2013-10-05 18:08:26 +0100772 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100773 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100774 }
775
776 return true;
777}
778
Damien George969a6b32014-12-10 22:07:04 +0000779STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100780 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000781 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000782 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100783
Damien6cdd3af2013-10-05 18:08:26 +0100784 // inherit emit options for this function/class definition
785 uint emit_options = comp->scope_cur->emit_options;
786
787 // compile each decorator
788 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100789 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000790 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
791 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100792
793 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000794 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000795 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100796
797 // check for built-in decorators
798 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
799 // this was a built-in
800 num_built_in_decorators += 1;
801
802 } else {
803 // not a built-in, compile normally
804
805 // compile the decorator function
806 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000807 for (int j = 1; j < name_len; j++) {
808 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
809 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100810 }
811
812 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000813 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100814 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000815 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100816 compile_node(comp, pns_decorator->nodes[1]);
817 }
Damien429d7192013-10-04 19:53:11 +0100818 }
819 }
820
821 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000822 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100823 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000824 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100825 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100826 } else {
Damien George0bb97132015-02-27 14:25:47 +0000827 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
828 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100829 }
830
831 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100832 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100833 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100834 }
835
836 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000837 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100838}
839
Damien George969a6b32014-12-10 22:07:04 +0000840STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100841 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100842 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000843 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100844}
845
Damien George6be0b0a2014-08-15 14:30:52 +0100846STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000847 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000848 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +0000849 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
850 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100851
852 compile_node(comp, pns->nodes[0]); // base of the power node
853
Damiend99b0522013-12-21 18:17:45 +0000854 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
855 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
856 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
857 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100858 for (int i = 0; i < n - 1; i++) {
859 compile_node(comp, pns1->nodes[i]);
860 }
Damiend99b0522013-12-21 18:17:45 +0000861 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
862 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100863 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000864 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100865 compile_node(comp, pns1->nodes[0]);
866 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000867 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
868 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000869 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100870 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100871 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100872 }
873 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100874 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100875 }
876
Damiend99b0522013-12-21 18:17:45 +0000877 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100878 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100879 }
Damiend99b0522013-12-21 18:17:45 +0000880 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
881 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000882 if (MP_PARSE_NODE_IS_NULL(pn)) {
883 goto cannot_delete;
884 } else {
885 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000886 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100887 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
888
Damiend99b0522013-12-21 18:17:45 +0000889 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
890 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
891 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100892 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000893 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100894 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000895 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100896 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000897 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100898 c_del_stmt(comp, pns->nodes[0]);
899 for (int i = 0; i < n; i++) {
900 c_del_stmt(comp, pns1->nodes[i]);
901 }
Damiend99b0522013-12-21 18:17:45 +0000902 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000903 // TODO not implemented; can't del comprehension? can we get here?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100904 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100905 } else {
906 // sequence with 2 items
907 goto sequence_with_2_items;
908 }
909 } else {
910 // sequence with 2 items
911 sequence_with_2_items:
912 c_del_stmt(comp, pns->nodes[0]);
913 c_del_stmt(comp, pns->nodes[1]);
914 }
Damien429d7192013-10-04 19:53:11 +0100915 }
916 } else {
Damien George93b37262016-01-07 13:07:52 +0000917 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100918 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100919 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100920
921 return;
922
923cannot_delete:
924 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100925}
926
Damien George969a6b32014-12-10 22:07:04 +0000927STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100928 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
929}
930
Damien George969a6b32014-12-10 22:07:04 +0000931STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100932 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100933 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100934 }
Damien George090c9232014-10-17 14:08:49 +0000935 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000936 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100937}
938
Damien George969a6b32014-12-10 22:07:04 +0000939STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100940 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100941 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100942 }
Damien George090c9232014-10-17 14:08:49 +0000943 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000944 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100945}
946
Damien George969a6b32014-12-10 22:07:04 +0000947STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100948 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100949 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100950 return;
951 }
Damiend99b0522013-12-21 18:17:45 +0000952 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100953 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000954 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000955 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100956 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000957 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
958 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 +0100959
Damien George6f355fd2014-04-10 14:11:31 +0100960 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100961 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
962 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
963 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000964 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100965 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
966 } else {
967 compile_node(comp, pns->nodes[0]);
968 }
969 EMIT(return_value);
970}
971
Damien George969a6b32014-12-10 22:07:04 +0000972STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100973 compile_node(comp, pns->nodes[0]);
974 EMIT(pop_top);
975}
976
Damien George969a6b32014-12-10 22:07:04 +0000977STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000978 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100979 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000980 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +0000981 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +0100982 // raise x from y
Damiend99b0522013-12-21 18:17:45 +0000983 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100984 compile_node(comp, pns->nodes[0]);
985 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000986 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +0100987 } else {
988 // raise x
989 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +0000990 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +0100991 }
992}
993
Damien George635543c2014-04-10 12:56:52 +0100994// q_base holds the base of the name
995// eg a -> q_base=a
996// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +0100997STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +0100998 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +0000999 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1000 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001001 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001002 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001003 pn = pns->nodes[0];
1004 is_as = true;
1005 }
Damien George635543c2014-04-10 12:56:52 +01001006 if (MP_PARSE_NODE_IS_NULL(pn)) {
1007 // empty name (eg, from . import x)
1008 *q_base = MP_QSTR_;
1009 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1010 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001011 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001012 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001013 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001014 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001015 }
Damien George635543c2014-04-10 12:56:52 +01001016 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001017 } else {
1018 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001019 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001020 {
Damien429d7192013-10-04 19:53:11 +01001021 // a name of the form a.b.c
1022 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001023 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001024 }
Damiend99b0522013-12-21 18:17:45 +00001025 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001026 int len = n - 1;
1027 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001028 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001029 }
Damien George55baff42014-01-21 21:40:13 +00001030 byte *q_ptr;
1031 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001032 for (int i = 0; i < n; i++) {
1033 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001034 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001035 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001036 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001037 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001038 memcpy(str_dest, str_src, str_src_len);
1039 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001040 }
Damien George635543c2014-04-10 12:56:52 +01001041 qstr q_full = qstr_build_end(q_ptr);
1042 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001043 if (is_as) {
1044 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001045 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001046 }
1047 }
Damien429d7192013-10-04 19:53:11 +01001048 }
Damien429d7192013-10-04 19:53:11 +01001049 }
1050}
1051
Damien George6be0b0a2014-08-15 14:30:52 +01001052STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001053 EMIT_ARG(load_const_small_int, 0); // level 0 import
1054 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1055 qstr q_base;
1056 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001057 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001058}
1059
Damien George969a6b32014-12-10 22:07:04 +00001060STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001061 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1062}
1063
Damien George969a6b32014-12-10 22:07:04 +00001064STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001065 mp_parse_node_t pn_import_source = pns->nodes[0];
1066
1067 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1068 uint import_level = 0;
1069 do {
1070 mp_parse_node_t pn_rel;
1071 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)) {
1072 // This covers relative imports with dots only like "from .. import"
1073 pn_rel = pn_import_source;
1074 pn_import_source = MP_PARSE_NODE_NULL;
1075 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1076 // This covers relative imports starting with dot(s) like "from .foo import"
1077 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1078 pn_rel = pns_2b->nodes[0];
1079 pn_import_source = pns_2b->nodes[1];
1080 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1081 } else {
1082 // Not a relative import
1083 break;
1084 }
1085
1086 // get the list of . and/or ...'s
1087 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001088 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001089
1090 // count the total number of .'s
1091 for (int i = 0; i < n; i++) {
1092 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1093 import_level++;
1094 } else {
1095 // should be an MP_TOKEN_ELLIPSIS
1096 import_level += 3;
1097 }
1098 }
1099 } while (0);
1100
Damiend99b0522013-12-21 18:17:45 +00001101 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001102 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001103
1104 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001105 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001106 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001107
1108 // do the import
Damien George635543c2014-04-10 12:56:52 +01001109 qstr dummy_q;
1110 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001111 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001112
Damien429d7192013-10-04 19:53:11 +01001113 } else {
Damien George635543c2014-04-10 12:56:52 +01001114 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001115
1116 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001117 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001118 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001119 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001120 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1121 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1122 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001123 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001124 }
Damien Georgeb9791222014-01-23 00:34:21 +00001125 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001126
1127 // do the import
Damien George635543c2014-04-10 12:56:52 +01001128 qstr dummy_q;
1129 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001130 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001131 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1132 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1133 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001134 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001135 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001136 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001137 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001138 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001139 }
1140 }
1141 EMIT(pop_top);
1142 }
1143}
1144
Damien George584ba672014-12-21 17:26:45 +00001145STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1146 bool added;
1147 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001148 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1149 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001150 return;
1151 }
1152 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1153
1154 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1155 id_info = scope_find_global(comp->scope_cur, qst);
1156 if (id_info != NULL) {
1157 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1158 }
1159}
1160
Damien George969a6b32014-12-10 22:07:04 +00001161STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001162 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001163 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001164 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001165 for (int i = 0; i < n; i++) {
1166 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001167 }
1168 }
1169}
1170
Damien George584ba672014-12-21 17:26:45 +00001171STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1172 bool added;
1173 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001174 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1175 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001176 return;
1177 }
1178 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1179 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)) {
1180 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1181 return;
1182 }
1183 id_info->kind = ID_INFO_KIND_FREE;
1184 scope_close_over_in_parents(comp->scope_cur, qst);
1185}
1186
Damien George969a6b32014-12-10 22:07:04 +00001187STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001188 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001189 if (comp->scope_cur->kind == SCOPE_MODULE) {
1190 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1191 return;
1192 }
1193 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001194 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001195 for (int i = 0; i < n; i++) {
1196 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001197 }
1198 }
1199}
1200
Damien George969a6b32014-12-10 22:07:04 +00001201STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001202 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001203 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001204 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001205 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001206 // assertion message
1207 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001208 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001209 }
Damien Georgeb9791222014-01-23 00:34:21 +00001210 EMIT_ARG(raise_varargs, 1);
1211 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001212}
1213
Damien George969a6b32014-12-10 22:07:04 +00001214STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001215 // TODO proper and/or short circuiting
1216
Damien George6f355fd2014-04-10 14:11:31 +01001217 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001218
Damien George65dc9602015-08-14 12:24:11 +01001219 // optimisation: don't emit anything when "if False"
1220 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001221 uint l_fail = comp_next_label(comp);
1222 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001223
Damien George391db862014-10-17 17:57:33 +00001224 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001225
Damien George65dc9602015-08-14 12:24:11 +01001226 // optimisation: skip everything else when "if True"
1227 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001228 goto done;
1229 }
1230
1231 if (
Damien George65dc9602015-08-14 12:24:11 +01001232 // optimisation: don't jump over non-existent elif/else blocks
1233 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001234 // optimisation: don't jump if last instruction was return
1235 && !EMIT(last_emit_was_return_value)
1236 ) {
1237 // jump over elif/else blocks
1238 EMIT_ARG(jump, l_end);
1239 }
1240
1241 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001242 }
1243
Damien George235f9b32014-10-17 17:30:16 +00001244 // compile elif blocks (if any)
1245 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001246 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 +00001247 for (int i = 0; i < n_elif; i++) {
1248 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1249 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001250
Damien George65dc9602015-08-14 12:24:11 +01001251 // optimisation: don't emit anything when "if False"
1252 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001253 uint l_fail = comp_next_label(comp);
1254 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001255
Damien George391db862014-10-17 17:57:33 +00001256 compile_node(comp, pns_elif->nodes[1]); // elif block
1257
Damien George65dc9602015-08-14 12:24:11 +01001258 // optimisation: skip everything else when "elif True"
1259 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001260 goto done;
1261 }
1262
1263 // optimisation: don't jump if last instruction was return
1264 if (!EMIT(last_emit_was_return_value)) {
1265 EMIT_ARG(jump, l_end);
1266 }
1267 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001268 }
1269 }
1270
1271 // compile else block
1272 compile_node(comp, pns->nodes[3]); // can be null
1273
Damien George391db862014-10-17 17:57:33 +00001274done:
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001276}
1277
Damien Georgecbddb272014-02-01 20:08:18 +00001278#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001279 uint16_t old_break_label = comp->break_label; \
1280 uint16_t old_continue_label = comp->continue_label; \
1281 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001282 uint break_label = comp_next_label(comp); \
1283 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001284 comp->break_label = break_label; \
1285 comp->continue_label = continue_label; \
1286 comp->break_continue_except_level = comp->cur_except_level;
1287
1288#define END_BREAK_CONTINUE_BLOCK \
1289 comp->break_label = old_break_label; \
1290 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001291 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001292
Damien George969a6b32014-12-10 22:07:04 +00001293STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001294 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001295
Damien George391db862014-10-17 17:57:33 +00001296 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1297 uint top_label = comp_next_label(comp);
1298 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1299 EMIT_ARG(jump, continue_label);
1300 }
1301 EMIT_ARG(label_assign, top_label);
1302 compile_node(comp, pns->nodes[1]); // body
1303 EMIT_ARG(label_assign, continue_label);
1304 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1305 }
Damience89a212013-10-15 22:25:17 +01001306
1307 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001308 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001309
1310 compile_node(comp, pns->nodes[2]); // else
1311
Damien Georgeb9791222014-01-23 00:34:21 +00001312 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001313}
1314
Damien Georgee181c0d2014-12-12 17:19:56 +00001315// This function compiles an optimised for-loop of the form:
1316// for <var> in range(<start>, <end>, <step>):
1317// <body>
1318// else:
1319// <else>
1320// <var> must be an identifier and <step> must be a small-int.
1321//
1322// Semantics of for-loop require:
1323// - final failing value should not be stored in the loop variable
1324// - if the loop never runs, the loop variable should never be assigned
1325// - assignments to <var>, <end> or <step> in the body do not alter the loop
1326// (<step> is a constant for us, so no need to worry about it changing)
1327//
1328// If <end> is a small-int, then the stack during the for-loop contains just
1329// the current value of <var>. Otherwise, the stack contains <end> then the
1330// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001331STATIC 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 +00001332 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001333
Damien George6f355fd2014-04-10 14:11:31 +01001334 uint top_label = comp_next_label(comp);
1335 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001336
Damien Georgee181c0d2014-12-12 17:19:56 +00001337 // put the end value on the stack if it's not a small-int constant
1338 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1339 if (end_on_stack) {
1340 compile_node(comp, pn_end);
1341 }
1342
1343 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001344 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001345
Damien Georgeb9791222014-01-23 00:34:21 +00001346 EMIT_ARG(jump, entry_label);
1347 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001348
Damien Georgec33ce602014-12-11 17:35:23 +00001349 // duplicate next value and store it to var
1350 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001351 c_assign(comp, pn_var, ASSIGN_STORE);
1352
Damienf3822fc2013-11-09 20:12:03 +00001353 // compile body
1354 compile_node(comp, pn_body);
1355
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001357
Damien Georgee181c0d2014-12-12 17:19:56 +00001358 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001359 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001360 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001361
Damien Georgeb9791222014-01-23 00:34:21 +00001362 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001363
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001364 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001365 if (end_on_stack) {
1366 EMIT(dup_top_two);
1367 EMIT(rot_two);
1368 } else {
1369 EMIT(dup_top);
1370 compile_node(comp, pn_end);
1371 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001372 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1373 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001374 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001375 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001376 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001377 }
Damien George63f38322015-02-28 15:04:06 +00001378 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001379
1380 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001381 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001382
1383 compile_node(comp, pn_else);
1384
Damien Georgeb9791222014-01-23 00:34:21 +00001385 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001386
1387 // discard final value of var that failed the loop condition
1388 EMIT(pop_top);
1389
1390 // discard <end> value if it's on the stack
1391 if (end_on_stack) {
1392 EMIT(pop_top);
1393 }
Damienf72fd0e2013-11-06 20:20:49 +00001394}
1395
Damien George969a6b32014-12-10 22:07:04 +00001396STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001397 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1398 // this is actually slower, but uses no heap memory
1399 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001400 if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
Damiend99b0522013-12-21 18:17:45 +00001401 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001402 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1403 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1404 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1405 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001406 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1407 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001408 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001409 mp_parse_node_t pn_range_start;
1410 mp_parse_node_t pn_range_end;
1411 mp_parse_node_t pn_range_step;
1412 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001413 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001414 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001415 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001416 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001417 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001418 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001419 } else if (n_args == 2) {
1420 pn_range_start = args[0];
1421 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001422 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001423 } else {
1424 pn_range_start = args[0];
1425 pn_range_end = args[1];
1426 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001427 // We need to know sign of step. This is possible only if it's constant
1428 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1429 optimize = false;
1430 }
Damienf72fd0e2013-11-06 20:20:49 +00001431 }
Damien George33ac0fd2015-12-08 21:05:14 +00001432 // arguments must be able to be compiled as standard expressions
1433 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1434 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1435 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1436 optimize = false;
1437 }
1438 }
1439 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1440 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1441 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1442 optimize = false;
1443 }
1444 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001445 }
1446 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001447 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1448 return;
1449 }
1450 }
1451 }
Damienf72fd0e2013-11-06 20:20:49 +00001452
Damien Georgecbddb272014-02-01 20:08:18 +00001453 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001454 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001455
Damien George6f355fd2014-04-10 14:11:31 +01001456 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001457
Damien429d7192013-10-04 19:53:11 +01001458 compile_node(comp, pns->nodes[1]); // iterator
1459 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001460 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001461 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001462 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1463 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001464 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001465 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001466 }
Damien Georgeb9791222014-01-23 00:34:21 +00001467 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001468 EMIT(for_iter_end);
1469
1470 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001471 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001472
Damien429d7192013-10-04 19:53:11 +01001473 compile_node(comp, pns->nodes[3]); // else (not tested)
1474
Damien Georgeb9791222014-01-23 00:34:21 +00001475 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001476}
1477
Damien George6be0b0a2014-08-15 14:30:52 +01001478STATIC 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 +01001479 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001480 uint l1 = comp_next_label(comp);
1481 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001482
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001484 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001485
Damien429d7192013-10-04 19:53:11 +01001486 compile_node(comp, pn_body); // body
1487 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001488 EMIT_ARG(jump, success_label); // jump over exception handler
1489
1490 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001491 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001492
Damien George6f355fd2014-04-10 14:11:31 +01001493 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001494
1495 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001496 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1497 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001498
1499 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001500 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001501
Damiend99b0522013-12-21 18:17:45 +00001502 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001503 // this is a catch all exception handler
1504 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001505 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001506 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001507 return;
1508 }
1509 } else {
1510 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001511 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1512 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1513 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1514 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001515 // handler binds the exception to a local
1516 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001517 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001518 }
1519 }
1520 EMIT(dup_top);
1521 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001522 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001523 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001524 }
1525
1526 EMIT(pop_top);
1527
1528 if (qstr_exception_local == 0) {
1529 EMIT(pop_top);
1530 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001531 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001532 }
1533
1534 EMIT(pop_top);
1535
Damien George6f355fd2014-04-10 14:11:31 +01001536 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001537 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001538 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001539 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001540 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001541 }
1542 compile_node(comp, pns_except->nodes[1]);
1543 if (qstr_exception_local != 0) {
1544 EMIT(pop_block);
1545 }
1546 EMIT(pop_except);
1547 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1549 EMIT_ARG(label_assign, l3);
1550 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001551 compile_store_id(comp, qstr_exception_local);
1552 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001553
Damien George8dcc0c72014-03-27 10:55:21 +00001554 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001555 EMIT(end_finally);
1556 }
Damien Georgeb9791222014-01-23 00:34:21 +00001557 EMIT_ARG(jump, l2);
1558 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001559 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001560 }
1561
Damien George8dcc0c72014-03-27 10:55:21 +00001562 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001563 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001564 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001565
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001567 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001569}
1570
Damien George6be0b0a2014-08-15 14:30:52 +01001571STATIC 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 +01001572 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001573
Damien Georgeb9791222014-01-23 00:34:21 +00001574 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001575 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001576
Damien429d7192013-10-04 19:53:11 +01001577 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001578 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001579 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001580 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001581 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001582 } else {
1583 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1584 }
1585 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1587 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001588 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001589
Damien George8dcc0c72014-03-27 10:55:21 +00001590 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001591 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001592}
1593
Damien George969a6b32014-12-10 22:07:04 +00001594STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001595 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1596 {
Damiend99b0522013-12-21 18:17:45 +00001597 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1598 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001599 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001600 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001602 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001603 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001604 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001605 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001606 // no finally
1607 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1608 } else {
1609 // have finally
Damiend99b0522013-12-21 18:17:45 +00001610 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 +01001611 }
1612 } else {
1613 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001614 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001615 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001616 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001617 }
Damien429d7192013-10-04 19:53:11 +01001618 }
1619}
1620
Damien George6be0b0a2014-08-15 14:30:52 +01001621STATIC 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 +01001622 if (n == 0) {
1623 // no more pre-bits, compile the body of the with
1624 compile_node(comp, body);
1625 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001626 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001627 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1628 // we need to allocate an extra label for the native emitter
1629 // it will use l_end+1 as an auxiliary label
1630 comp_next_label(comp);
1631 }
Damiend99b0522013-12-21 18:17:45 +00001632 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001633 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001634 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001635 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001636 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001637 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1638 } else {
1639 // this pre-bit is just an expression
1640 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001641 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001642 EMIT(pop_top);
1643 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001644 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001645 // compile additional pre-bits and the body
1646 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1647 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001648 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001649 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001650 EMIT(end_finally);
1651 }
1652}
1653
Damien George969a6b32014-12-10 22:07:04 +00001654STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001655 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001656 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001657 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001658 assert(n > 0);
1659
1660 // compile in a nested fashion
1661 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1662}
1663
Damien George969a6b32014-12-10 22:07:04 +00001664STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001665 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001666 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1667 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001668 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001669 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001670 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001671 EMIT(pop_top);
1672
Damien429d7192013-10-04 19:53:11 +01001673 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001674 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001675 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001676 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001677 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1678 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001679 // do nothing with a lonely constant
1680 } else {
1681 compile_node(comp, pns->nodes[0]); // just an expression
1682 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1683 }
Damien429d7192013-10-04 19:53:11 +01001684 }
Damien Georgeb948de32015-10-08 14:26:01 +01001685 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001686 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1687 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001688 if (kind == PN_expr_stmt_augassign) {
1689 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1690 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001691 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001692 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001693 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001694 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1695 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1696 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1697 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1698 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1699 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1700 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1701 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1702 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1703 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1704 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001705 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001706 }
Damien George7e5fb242014-02-01 22:18:47 +00001707 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001708 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1709 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001710 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001711 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001712 // following CPython, we store left-most first
1713 if (rhs > 0) {
1714 EMIT(dup_top);
1715 }
1716 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1717 for (int i = 0; i < rhs; i++) {
1718 if (i + 1 < rhs) {
1719 EMIT(dup_top);
1720 }
Damien Georgeb948de32015-10-08 14:26:01 +01001721 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001722 }
Damien George0bb97132015-02-27 14:25:47 +00001723 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001724 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001725 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001726 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001727 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001728 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001729 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001730 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001731 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001732 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001733 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1734 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1735 // can't optimise when it's a star expression on the lhs
1736 goto no_optimisation;
1737 }
Damien429d7192013-10-04 19:53:11 +01001738 compile_node(comp, pns10->nodes[0]); // rhs
1739 compile_node(comp, pns10->nodes[1]); // rhs
1740 EMIT(rot_two);
1741 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1742 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001743 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001744 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001745 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001746 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001747 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001748 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001749 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001750 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001751 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1752 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1753 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1754 // can't optimise when it's a star expression on the lhs
1755 goto no_optimisation;
1756 }
Damien429d7192013-10-04 19:53:11 +01001757 compile_node(comp, pns10->nodes[0]); // rhs
1758 compile_node(comp, pns10->nodes[1]); // rhs
1759 compile_node(comp, pns10->nodes[2]); // rhs
1760 EMIT(rot_three);
1761 EMIT(rot_two);
1762 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1763 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1764 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1765 } else {
Damien George495d7812014-04-08 17:51:47 +01001766 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001767 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001768 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1769 }
Damien429d7192013-10-04 19:53:11 +01001770 }
Damien Georgeb948de32015-10-08 14:26:01 +01001771 } else {
1772 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001773 }
1774}
1775
Damien George6be0b0a2014-08-15 14:30:52 +01001776STATIC 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 +00001777 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001778 compile_node(comp, pns->nodes[0]);
1779 for (int i = 1; i < num_nodes; i += 1) {
1780 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001782 }
1783}
1784
Damien George969a6b32014-12-10 22:07:04 +00001785STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001786 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1787 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001788
Damien George6f355fd2014-04-10 14:11:31 +01001789 uint l_fail = comp_next_label(comp);
1790 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001791 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1792 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001793 EMIT_ARG(jump, l_end);
1794 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001795 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001796 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001797 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001798}
1799
Damien George969a6b32014-12-10 22:07:04 +00001800STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001801 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001802 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001803 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 +01001804 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001805 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001806 }
1807
1808 // get the scope for this lambda
1809 scope_t *this_scope = (scope_t*)pns->nodes[2];
1810
Damien George2c838942015-11-17 14:00:14 +00001811 // compile the lambda definition
1812 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01001813}
1814
Damien George7711afb2015-02-28 15:10:18 +00001815STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01001816 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001817 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001818 for (int i = 0; i < n; i += 1) {
1819 compile_node(comp, pns->nodes[i]);
1820 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00001821 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01001822 }
1823 }
Damien Georgeb9791222014-01-23 00:34:21 +00001824 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001825}
1826
Damien George7711afb2015-02-28 15:10:18 +00001827STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
1828 compile_or_and_test(comp, pns, true);
1829}
1830
Damien George969a6b32014-12-10 22:07:04 +00001831STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00001832 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01001833}
1834
Damien George969a6b32014-12-10 22:07:04 +00001835STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001836 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01001837 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001838}
1839
Damien George969a6b32014-12-10 22:07:04 +00001840STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001841 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001842 compile_node(comp, pns->nodes[0]);
1843 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01001844 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01001845 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001846 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001847 }
1848 for (int i = 1; i + 1 < num_nodes; i += 2) {
1849 compile_node(comp, pns->nodes[i + 1]);
1850 if (i + 2 < num_nodes) {
1851 EMIT(dup_top);
1852 EMIT(rot_three);
1853 }
Damien George7e5fb242014-02-01 22:18:47 +00001854 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01001855 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00001856 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01001857 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
1858 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
1859 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
1860 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
1861 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
1862 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001863 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00001864 }
1865 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00001866 } else {
1867 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00001868 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1869 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001870 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01001871 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00001872 } else {
1873 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00001874 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001875 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001876 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001877 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001878 }
Damien429d7192013-10-04 19:53:11 +01001879 }
Damien429d7192013-10-04 19:53:11 +01001880 }
1881 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00001882 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01001883 }
1884 }
1885 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01001886 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001887 EMIT_ARG(jump, l_end);
1888 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001889 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01001890 EMIT(rot_two);
1891 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001892 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001893 }
1894}
1895
Damien George969a6b32014-12-10 22:07:04 +00001896STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01001897 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01001898}
1899
Damien George969a6b32014-12-10 22:07:04 +00001900STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001901 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01001902}
1903
Damien George969a6b32014-12-10 22:07:04 +00001904STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001905 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01001906}
1907
Damien George969a6b32014-12-10 22:07:04 +00001908STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001909 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01001910}
1911
Damien George969a6b32014-12-10 22:07:04 +00001912STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001913 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001914 compile_node(comp, pns->nodes[0]);
1915 for (int i = 1; i + 1 < num_nodes; i += 2) {
1916 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001917 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001918 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01001919 } else {
Damien George0bb97132015-02-27 14:25:47 +00001920 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
1921 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01001922 }
1923 }
1924}
1925
Damien George969a6b32014-12-10 22:07:04 +00001926STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001927 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001928 compile_node(comp, pns->nodes[0]);
1929 for (int i = 1; i + 1 < num_nodes; i += 2) {
1930 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001931 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001932 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01001933 } else {
Damien George0bb97132015-02-27 14:25:47 +00001934 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
1935 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01001936 }
1937 }
1938}
1939
Damien George969a6b32014-12-10 22:07:04 +00001940STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001941 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001942 compile_node(comp, pns->nodes[0]);
1943 for (int i = 1; i + 1 < num_nodes; i += 2) {
1944 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001945 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01001946 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00001947 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01001948 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00001949 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01001950 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01001951 } else {
Damien George0bb97132015-02-27 14:25:47 +00001952 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
1953 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01001954 }
1955 }
1956}
1957
Damien George969a6b32014-12-10 22:07:04 +00001958STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001959 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00001960 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001961 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00001962 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001963 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01001964 } else {
Damien George0bb97132015-02-27 14:25:47 +00001965 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
1966 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01001967 }
1968}
1969
Damien George969a6b32014-12-10 22:07:04 +00001970STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00001971 // this is to handle special super() call
1972 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
1973
1974 compile_generic_all_nodes(comp, pns);
Damien George3acaa282016-03-16 13:04:51 +00001975
1976 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
1977 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
1978 }
Damien George35e2a4e2014-02-05 00:51:47 +00001979}
1980
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001981STATIC 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 +01001982 // function to call is on top of stack
1983
Damien George35e2a4e2014-02-05 00:51:47 +00001984 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00001985 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 +00001986 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00001987 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00001988 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01001989 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00001990 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00001991 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00001992 EMIT_ARG(call_function, 2, 0, 0);
1993 return;
Damien George35e2a4e2014-02-05 00:51:47 +00001994 }
1995 }
Damien George01039b52014-12-21 17:44:27 +00001996 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00001997 return;
1998 }
Damien George35e2a4e2014-02-05 00:51:47 +00001999
Damien George2c0842b2014-04-27 16:46:51 +01002000 // get the list of arguments
2001 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002002 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002003
Damien George2c0842b2014-04-27 16:46:51 +01002004 // compile the arguments
2005 // Rather than calling compile_node on the list, we go through the list of args
2006 // explicitly here so that we can count the number of arguments and give sensible
2007 // error messages.
2008 int n_positional = n_positional_extra;
2009 uint n_keyword = 0;
2010 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002011 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002012 for (int i = 0; i < n_args; i++) {
2013 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2014 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2015 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2016 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2017 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2018 return;
2019 }
2020 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002021 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002022 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2023 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2024 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2025 return;
2026 }
2027 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002028 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002029 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002030 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002031 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2032 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2033 return;
2034 }
Damien George59fba2d2015-06-25 14:42:13 +00002035 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002036 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002037 n_keyword += 1;
2038 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002039 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2040 n_positional++;
2041 }
2042 } else {
2043 goto normal_argument;
2044 }
2045 } else {
2046 normal_argument:
2047 if (n_keyword > 0) {
2048 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2049 return;
2050 }
2051 compile_node(comp, args[i]);
2052 n_positional++;
2053 }
Damien429d7192013-10-04 19:53:11 +01002054 }
2055
Delio Brignolie6978a42015-09-17 12:07:06 +02002056 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002057 // if we had one but not the other then we load "null" as a place holder
2058 if (star_flags != 0) {
2059 if (star_args_node == NULL) {
2060 EMIT(load_null);
2061 } else {
2062 compile_node(comp, star_args_node->nodes[0]);
2063 }
2064 if (dblstar_args_node == NULL) {
2065 EMIT(load_null);
2066 } else {
2067 compile_node(comp, dblstar_args_node->nodes[0]);
2068 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002069 }
2070
Damien George2c0842b2014-04-27 16:46:51 +01002071 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002072 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002073 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002074 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002075 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002076 }
Damien429d7192013-10-04 19:53:11 +01002077}
2078
Damien George969a6b32014-12-10 22:07:04 +00002079STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002080 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002081 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002082 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 +01002083 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002084 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2085 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002087 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002088 i += 1;
2089 } else {
2090 compile_node(comp, pns->nodes[i]);
2091 }
Damien George35e2a4e2014-02-05 00:51:47 +00002092 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002093 }
2094}
2095
Damien George969a6b32014-12-10 22:07:04 +00002096STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002097 // a list of strings
Damien63321742013-12-10 17:41:49 +00002098
2099 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002100 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002101 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002102 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002103 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002104 int pn_kind;
2105 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2106 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2107 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2108 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2109 } else {
2110 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2111 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002112 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2113 pn_kind = MP_PARSE_NODE_STRING;
2114 } else {
2115 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2116 pn_kind = MP_PARSE_NODE_BYTES;
2117 }
Damien George831137b2015-12-17 13:13:18 +00002118 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002119 }
Damien63321742013-12-10 17:41:49 +00002120 if (i == 0) {
2121 string_kind = pn_kind;
2122 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002123 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002124 return;
2125 }
Damien429d7192013-10-04 19:53:11 +01002126 }
Damien63321742013-12-10 17:41:49 +00002127
Damien George65ef6b72015-01-14 21:17:27 +00002128 // if we are not in the last pass, just load a dummy object
2129 if (comp->pass != MP_PASS_EMIT) {
2130 EMIT_ARG(load_const_obj, mp_const_none);
2131 return;
2132 }
2133
Damien63321742013-12-10 17:41:49 +00002134 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002135 vstr_t vstr;
2136 vstr_init_len(&vstr, n_bytes);
2137 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002138 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002139 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002140 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002141 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2142 memcpy(s_dest, s, s_len);
2143 s_dest += s_len;
2144 } else {
2145 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002146 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2147 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002148 }
Damien63321742013-12-10 17:41:49 +00002149 }
Damien George65ef6b72015-01-14 21:17:27 +00002150
2151 // load the object
Damien George05005f62015-01-21 22:48:37 +00002152 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 +01002153}
2154
2155// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002156STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002157 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2158 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2159 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002160
Damien George36db6bc2014-05-07 17:24:22 +01002161 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002162 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002163 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 +01002164 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002165 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002166 }
2167
2168 // get the scope for this comprehension
2169 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2170
2171 // compile the comprehension
2172 close_over_variables_etc(comp, this_scope, 0, 0);
2173
2174 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2175 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002176 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002177}
2178
Damien George969a6b32014-12-10 22:07:04 +00002179STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002180 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002181 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002182 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002183 } else {
2184 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002185 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2186 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2187 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2188 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2189 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002190 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002191 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002192 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002193 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002194 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002195 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002196 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002197 // generator expression
2198 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2199 } else {
2200 // tuple with 2 items
2201 goto tuple_with_2_items;
2202 }
2203 } else {
2204 // tuple with 2 items
2205 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002206 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002207 }
Damien429d7192013-10-04 19:53:11 +01002208 }
2209}
2210
Damien George969a6b32014-12-10 22:07:04 +00002211STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002212 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002213 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002214 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002215 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2216 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2217 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2218 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2219 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002220 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002221 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002222 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002223 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002224 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002225 // list of many items
2226 compile_node(comp, pns2->nodes[0]);
2227 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002229 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002230 // list comprehension
2231 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2232 } else {
2233 // list with 2 items
2234 goto list_with_2_items;
2235 }
2236 } else {
2237 // list with 2 items
2238 list_with_2_items:
2239 compile_node(comp, pns2->nodes[0]);
2240 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002241 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002242 }
2243 } else {
2244 // list with 1 item
2245 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002246 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002247 }
2248}
2249
Damien George969a6b32014-12-10 22:07:04 +00002250STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002251 mp_parse_node_t pn = pns->nodes[0];
2252 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002253 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002254 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002255 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2256 pns = (mp_parse_node_struct_t*)pn;
2257 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002258 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002260 compile_node(comp, pn);
2261 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002262 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2263 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2264 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2265 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002266 // dict/set with multiple elements
2267
2268 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002269 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002270 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002271
2272 // first element sets whether it's a dict or set
2273 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002274 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002275 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002276 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002277 compile_node(comp, pns->nodes[0]);
2278 EMIT(store_map);
2279 is_dict = true;
2280 } else {
2281 // a set
2282 compile_node(comp, pns->nodes[0]); // 1st value of set
2283 is_dict = false;
2284 }
2285
2286 // process rest of elements
2287 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002288 mp_parse_node_t pn_i = nodes[i];
2289 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2290 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002291 if (is_dict) {
2292 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002293 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002294 return;
2295 }
2296 EMIT(store_map);
2297 } else {
2298 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002299 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002300 return;
2301 }
2302 }
2303 }
2304
Damien Georgee37dcaa2014-12-27 17:07:16 +00002305 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002306 // if it's a set, build it
2307 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002308 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002309 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002310 #endif
Damien George0bb97132015-02-27 14:25:47 +00002311 } else {
2312 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002313 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002314 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002315 // a dictionary comprehension
2316 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2317 } else {
2318 // a set comprehension
2319 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2320 }
Damien429d7192013-10-04 19:53:11 +01002321 }
2322 } else {
2323 // set with one element
2324 goto set_with_one_element;
2325 }
2326 } else {
2327 // set with one element
2328 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002329 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002330 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002331 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002332 #else
2333 assert(0);
2334 #endif
Damien429d7192013-10-04 19:53:11 +01002335 }
2336}
2337
Damien George969a6b32014-12-10 22:07:04 +00002338STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002339 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002340}
2341
Damien George969a6b32014-12-10 22:07:04 +00002342STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002343 // object who's index we want is on top of stack
2344 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002345 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damien George969a6b32014-12-10 22:07:04 +00002348STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002349 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002350 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002351}
2352
Damien George83204f32014-12-27 17:20:41 +00002353#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002354STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002355 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2356 mp_parse_node_t pn = pns->nodes[0];
2357 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002358 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002359 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2360 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002361 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2362 pns = (mp_parse_node_struct_t*)pn;
2363 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002364 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002365 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002366 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002367 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002368 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002369 } else {
2370 // [?::x]
2371 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002372 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002373 }
Damiend99b0522013-12-21 18:17:45 +00002374 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002375 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002376 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2377 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2378 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2379 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002380 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002381 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002382 } else {
2383 // [?:x:x]
2384 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002385 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002386 }
2387 } else {
2388 // [?:x]
2389 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002391 }
2392 } else {
2393 // [?:x]
2394 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002395 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002396 }
2397}
2398
Damien George969a6b32014-12-10 22:07:04 +00002399STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002400 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002401 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2402 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002403}
2404
Damien George969a6b32014-12-10 22:07:04 +00002405STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002406 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002407 compile_subscript_3_helper(comp, pns);
2408}
Damien George83204f32014-12-27 17:20:41 +00002409#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002410
Damien George969a6b32014-12-10 22:07:04 +00002411STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002412 // if this is called then we are compiling a dict key:value pair
2413 compile_node(comp, pns->nodes[1]); // value
2414 compile_node(comp, pns->nodes[0]); // key
2415}
2416
Damien George969a6b32014-12-10 22:07:04 +00002417STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002418 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002419 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002420 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002421}
2422
Damien George969a6b32014-12-10 22:07:04 +00002423STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002424 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002425 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002426 return;
2427 }
Damiend99b0522013-12-21 18:17:45 +00002428 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002429 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002430 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002431 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2432 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002433 compile_node(comp, pns->nodes[0]);
2434 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002435 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002436 EMIT(yield_from);
2437 } else {
2438 compile_node(comp, pns->nodes[0]);
2439 EMIT(yield_value);
2440 }
2441}
2442
Damien George65ef6b72015-01-14 21:17:27 +00002443STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2444 // only create and load the actual str object on the last pass
2445 if (comp->pass != MP_PASS_EMIT) {
2446 EMIT_ARG(load_const_obj, mp_const_none);
2447 } else {
Damien George831137b2015-12-17 13:13:18 +00002448 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 +00002449 }
2450}
2451
2452STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2453 // only create and load the actual bytes object on the last pass
2454 if (comp->pass != MP_PASS_EMIT) {
2455 EMIT_ARG(load_const_obj, mp_const_none);
2456 } else {
Damien George831137b2015-12-17 13:13:18 +00002457 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002458 }
2459}
2460
Damien George7d414a12015-02-08 01:57:40 +00002461STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002462 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2463 // nodes are 32-bit pointers, but need to extract 64-bit object
2464 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2465 #else
Damien George7d414a12015-02-08 01:57:40 +00002466 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002467 #endif
Damien George7d414a12015-02-08 01:57:40 +00002468}
2469
Damiend99b0522013-12-21 18:17:45 +00002470typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002471STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002472#define nc NULL
2473#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002474#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002475#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002476#undef nc
2477#undef c
2478#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002479 NULL,
2480 compile_string,
2481 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002482 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002483};
2484
Damien George6be0b0a2014-08-15 14:30:52 +01002485STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002486 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002487 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002488 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002489 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002490 #if MICROPY_DYNAMIC_COMPILER
2491 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2492 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2493 // integer fits in target runtime's small-int
2494 EMIT_ARG(load_const_small_int, arg);
2495 } else {
2496 // integer doesn't fit, so create a multi-precision int object
2497 // (but only create the actual object on the last pass)
2498 if (comp->pass != MP_PASS_EMIT) {
2499 EMIT_ARG(load_const_obj, mp_const_none);
2500 } else {
2501 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2502 }
2503 }
2504 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002505 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002506 #endif
Damiend99b0522013-12-21 18:17:45 +00002507 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002508 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002509 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002510 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002511 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2512 case MP_PARSE_NODE_BYTES:
2513 // only create and load the actual bytes object on the last pass
2514 if (comp->pass != MP_PASS_EMIT) {
2515 EMIT_ARG(load_const_obj, mp_const_none);
2516 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002517 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002518 const byte *data = qstr_data(arg, &len);
2519 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2520 }
2521 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002522 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002523 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002524 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002525 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002526 // do nothing
2527 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002529 }
2530 break;
Damien429d7192013-10-04 19:53:11 +01002531 }
2532 } else {
Damiend99b0522013-12-21 18:17:45 +00002533 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002534 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002535 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2536 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002537#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002538 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2539 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002540#endif
Damien George65ef6b72015-01-14 21:17:27 +00002541 compile_syntax_error(comp, pn, "internal compiler error");
2542 } else {
2543 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002544 }
2545 }
2546}
2547
Damien George2ac4af62014-08-15 16:45:41 +01002548STATIC 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 +00002549 // check that **kw is last
2550 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2551 compile_syntax_error(comp, pn, "invalid syntax");
2552 return;
2553 }
2554
Damien George2827d622014-04-27 15:50:52 +01002555 qstr param_name = MP_QSTR_NULL;
2556 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002557 if (MP_PARSE_NODE_IS_ID(pn)) {
2558 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002559 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002560 // comes after a star, so counts as a keyword-only parameter
2561 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002562 } else {
Damien George2827d622014-04-27 15:50:52 +01002563 // comes before a star, so counts as a positional parameter
2564 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002565 }
Damienb14de212013-10-06 00:28:28 +01002566 } else {
Damiend99b0522013-12-21 18:17:45 +00002567 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2568 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2569 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2570 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002571 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002572 // comes after a star, so counts as a keyword-only parameter
2573 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002574 } else {
Damien George2827d622014-04-27 15:50:52 +01002575 // comes before a star, so counts as a positional parameter
2576 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002577 }
Damiend99b0522013-12-21 18:17:45 +00002578 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002579 if (comp->have_star) {
2580 // more than one star
2581 compile_syntax_error(comp, pn, "invalid syntax");
2582 return;
2583 }
Damien George8b19db02014-04-11 23:25:34 +01002584 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002585 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002586 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002587 // bare star
2588 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002589 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002590 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002591 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002592 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002593 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002594 } else {
2595 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002596 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002597 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002598 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2599 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002600 }
Damien George0bb97132015-02-27 14:25:47 +00002601 } else {
2602 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002603 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002604 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002605 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002606 }
Damien429d7192013-10-04 19:53:11 +01002607 }
2608
Damien George2827d622014-04-27 15:50:52 +01002609 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002610 bool added;
2611 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2612 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002613 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002614 return;
2615 }
Damien429d7192013-10-04 19:53:11 +01002616 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002617 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002618 }
2619}
2620
Damien George2827d622014-04-27 15:50:52 +01002621STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002622 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002623}
2624
Damien George2827d622014-04-27 15:50:52 +01002625STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002626 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2627}
2628
Damien Georgeea5b59b2015-09-04 16:44:14 +01002629#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002630STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2631 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2632 // no annotation
2633 return;
2634 }
2635
2636 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2637 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2638 // named parameter with possible annotation
2639 // fallthrough
2640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2641 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2642 // named star with possible annotation
2643 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2644 // fallthrough
2645 } else {
2646 // no annotation
2647 return;
2648 }
2649 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2650 // double star with possible annotation
2651 // fallthrough
2652 } else {
2653 // no annotation
2654 return;
2655 }
2656
2657 mp_parse_node_t pn_annotation = pns->nodes[1];
2658
2659 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002660 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2661 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2662 assert(id_info != NULL);
2663
Damien Georgeea5b59b2015-09-04 16:44:14 +01002664 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2665 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2666 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2667 } else {
2668 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002669 }
Damien George2ac4af62014-08-15 16:45:41 +01002670 }
Damien429d7192013-10-04 19:53:11 +01002671}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002672#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002673
Damien Georgea8312432015-12-18 01:37:55 +00002674STATIC 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) {
2675 uint l_top = comp_next_label(comp);
2676 uint l_end = comp_next_label(comp);
2677 EMIT_ARG(label_assign, l_top);
2678 EMIT_ARG(for_iter, l_end);
2679 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2680 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2681
Damien429d7192013-10-04 19:53:11 +01002682 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002683 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002684 // no more nested if/for; compile inner expression
2685 compile_node(comp, pn_inner_expr);
2686 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002687 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002688 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002690 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002691 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002692 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002693 #endif
Damien429d7192013-10-04 19:53:11 +01002694 } else {
2695 EMIT(yield_value);
2696 EMIT(pop_top);
2697 }
Damiend99b0522013-12-21 18:17:45 +00002698 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002699 // if condition
Damiend99b0522013-12-21 18:17:45 +00002700 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002701 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2702 pn_iter = pns_comp_if->nodes[1];
2703 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002704 } else {
2705 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002706 // for loop
Damiend99b0522013-12-21 18:17:45 +00002707 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002708 compile_node(comp, pns_comp_for2->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002709 EMIT(get_iter);
Damien Georgea8312432015-12-18 01:37:55 +00002710 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002711 }
Damien Georgea8312432015-12-18 01:37:55 +00002712
2713 EMIT_ARG(jump, l_top);
2714 EMIT_ARG(label_assign, l_end);
2715 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002716}
2717
Damien George1463c1f2014-04-25 23:52:57 +01002718STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002719#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002720 // see http://www.python.org/dev/peps/pep-0257/
2721
2722 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002723 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002724 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002725 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002726 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002727 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2728 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002729 for (int i = 0; i < num_nodes; i++) {
2730 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002731 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 +00002732 // not a newline, so this is the first statement; finish search
2733 break;
2734 }
2735 }
2736 // 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 +00002737 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002738 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002739 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002740 } else {
2741 return;
2742 }
2743
2744 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002745 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002746 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002747 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2748 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2749 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2750 // compile the doc string
2751 compile_node(comp, pns->nodes[0]);
2752 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002753 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002754 }
2755 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002756#else
2757 (void)comp;
2758 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002759#endif
Damien429d7192013-10-04 19:53:11 +01002760}
2761
Damien George1463c1f2014-04-25 23:52:57 +01002762STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002763 comp->pass = pass;
2764 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002765 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002766 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002767
Damien George36db6bc2014-05-07 17:24:22 +01002768 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002769 // reset maximum stack sizes in scope
2770 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002771 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002772 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002773 }
2774
Damien429d7192013-10-04 19:53:11 +01002775 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002776 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2777 assert(scope->kind == SCOPE_MODULE);
2778 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2779 compile_node(comp, pns->nodes[0]); // compile the expression
2780 EMIT(return_value);
2781 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002782 if (!comp->is_repl) {
2783 check_for_doc_string(comp, scope->pn);
2784 }
Damien429d7192013-10-04 19:53:11 +01002785 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002786 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002787 EMIT(return_value);
2788 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002789 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2790 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2791 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002792
2793 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002794 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002795 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002796 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002797 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002798 }
2799 #if MICROPY_EMIT_NATIVE
2800 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002801 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002802 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002803
Damien George2ac4af62014-08-15 16:45:41 +01002804 // argument annotations
2805 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2806
2807 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002808 mp_parse_node_t pn_annotation = pns->nodes[2];
2809 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002810 // nodes[2] can be null or a test-expr
2811 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2812 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2813 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2814 } else {
2815 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002816 }
2817 }
Damien George2ac4af62014-08-15 16:45:41 +01002818 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002819 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002820
2821 compile_node(comp, pns->nodes[3]); // 3 is function body
2822 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002823 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002824 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002825 EMIT(return_value);
2826 }
2827 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002828 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2829 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2830 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002831
2832 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002833 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002834 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002835 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002836 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2837 }
2838
2839 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002840
2841 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2842 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2843 EMIT(pop_top);
2844 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2845 }
Damien429d7192013-10-04 19:53:11 +01002846 EMIT(return_value);
2847 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2848 // a bit of a hack at the moment
2849
Damiend99b0522013-12-21 18:17:45 +00002850 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2851 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2852 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2853 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2854 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002855
Damien George708c0732014-04-27 19:23:46 +01002856 // We need a unique name for the comprehension argument (the iterator).
2857 // CPython uses .0, but we should be able to use anything that won't
2858 // clash with a user defined variable. Best to use an existing qstr,
2859 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01002860 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01002861 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002862 bool added;
2863 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2864 assert(added);
2865 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002866 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01002867 }
2868
2869 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002870 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002871 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002873 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002874 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002875 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002876 #endif
Damien429d7192013-10-04 19:53:11 +01002877 }
2878
Damien George542bd6b2015-03-26 14:42:40 +00002879 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00002880 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01002881
2882 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002883 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002884 }
2885 EMIT(return_value);
2886 } else {
2887 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002888 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2889 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2890 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002891
Damien George36db6bc2014-05-07 17:24:22 +01002892 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002893 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002894 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002895 assert(added);
2896 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002897 }
2898
Damien George542bd6b2015-03-26 14:42:40 +00002899 compile_load_id(comp, MP_QSTR___name__);
2900 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00002901 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00002902 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002903
2904 check_for_doc_string(comp, pns->nodes[2]);
2905 compile_node(comp, pns->nodes[2]); // 2 is class body
2906
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002907 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002908 assert(id != NULL);
2909 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002910 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002911 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002912 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01002913 }
2914 EMIT(return_value);
2915 }
2916
Damien415eb6f2013-10-05 12:19:06 +01002917 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002918
2919 // make sure we match all the exception levels
2920 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002921}
2922
Damien George094d4502014-04-02 17:31:27 +01002923#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01002924// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01002925STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01002926 comp->pass = pass;
2927 comp->scope_cur = scope;
2928 comp->next_label = 1;
2929
2930 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00002931 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01002932 return;
2933 }
2934
Damien George36db6bc2014-05-07 17:24:22 +01002935 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00002936 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01002937 }
2938
Damien826005c2013-10-05 23:17:28 +01002939 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002940 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2941 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2942 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002943
Damiend99b0522013-12-21 18:17:45 +00002944 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002945
Damiena2f2f7d2013-10-06 00:14:13 +01002946 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01002947 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00002948 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00002949 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01002950 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00002951 if (comp->compile_error != MP_OBJ_NULL) {
2952 goto inline_asm_error;
2953 }
Damiena2f2f7d2013-10-06 00:14:13 +01002954 }
2955
Damien George8f54c082016-01-15 15:20:43 +00002956 // pns->nodes[2] is function return annotation
2957 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
2958 mp_parse_node_t pn_annotation = pns->nodes[2];
2959 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
2960 // nodes[2] can be null or a test-expr
2961 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2962 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2963 switch (ret_type) {
2964 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
2965 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
2966 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
2967 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
2968 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
2969 }
2970 } else {
2971 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
2972 }
2973 }
Damien826005c2013-10-05 23:17:28 +01002974
Damiend99b0522013-12-21 18:17:45 +00002975 mp_parse_node_t pn_body = pns->nodes[3]; // body
2976 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002977 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01002978
Damien826005c2013-10-05 23:17:28 +01002979 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002980 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2981 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01002982 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
2983 // no instructions
2984 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00002985 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01002986 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00002987 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00002988 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01002989 return;
2990 }
Damien George3d7bf5d2015-02-16 17:46:28 +00002991
2992 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00002993 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00002994 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
2995 goto not_an_instruction;
2996 }
Damiend99b0522013-12-21 18:17:45 +00002997 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00002998 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
2999 goto not_an_instruction;
3000 }
3001 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3002 goto not_an_instruction;
3003 }
3004 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3005 goto not_an_instruction;
3006 }
Damiend99b0522013-12-21 18:17:45 +00003007 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003008
3009 // parse node looks like an instruction
3010 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003011 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3012 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3013 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003014 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003015
3016 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003017 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003018 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003019 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003020 return;
3021 }
Damien George6f355fd2014-04-10 14:11:31 +01003022 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003023 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003024 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3025 compile_syntax_error(comp, nodes[i], "label redefined");
3026 return;
3027 }
Damien826005c2013-10-05 23:17:28 +01003028 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003029 } else if (op == MP_QSTR_align) {
3030 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003031 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003032 return;
3033 }
Damien George36db6bc2014-05-07 17:24:22 +01003034 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003035 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3036 }
3037 } else if (op == MP_QSTR_data) {
3038 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003039 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003040 return;
3041 }
Damien George36db6bc2014-05-07 17:24:22 +01003042 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003043 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003044 for (uint j = 1; j < n_args; j++) {
3045 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003046 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003047 return;
3048 }
Damien George50912e72015-01-20 11:55:10 +00003049 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003050 }
3051 }
Damien826005c2013-10-05 23:17:28 +01003052 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003053 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003055 }
3056 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003057
3058 if (comp->compile_error != MP_OBJ_NULL) {
3059 pns = pns2; // this is the parse node that had the error
3060 goto inline_asm_error;
3061 }
Damien826005c2013-10-05 23:17:28 +01003062 }
3063
Damien George36db6bc2014-05-07 17:24:22 +01003064 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003065 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien George8dfbd2d2015-02-13 01:00:51 +00003066 }
3067
3068 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003069 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003070 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003071 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003072 }
Damien429d7192013-10-04 19:53:11 +01003073}
Damien George094d4502014-04-02 17:31:27 +01003074#endif
Damien429d7192013-10-04 19:53:11 +01003075
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003076STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003077 // in Micro Python we put the *x parameter after all other parameters (except **y)
3078 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3079 id_info_t *id_param = NULL;
3080 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3081 id_info_t *id = &scope->id_info[i];
3082 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3083 if (id_param != NULL) {
3084 // swap star param with last param
3085 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3086 }
3087 break;
3088 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3089 id_param = id;
3090 }
3091 }
3092 }
Damien George2827d622014-04-27 15:50:52 +01003093
Damien429d7192013-10-04 19:53:11 +01003094 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003095 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003096 scope->num_locals = 0;
3097 for (int i = 0; i < scope->id_info_len; i++) {
3098 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003099 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003100 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3101 continue;
3102 }
3103 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3104 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3105 }
Damien George2827d622014-04-27 15:50:52 +01003106 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003107 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003108 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003109 }
3110 }
3111
Damien George65dc9602015-08-14 12:24:11 +01003112 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003113 for (int i = 0; i < scope->id_info_len; i++) {
3114 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003115 // in Micro Python the cells come right after the fast locals
3116 // parameters are not counted here, since they remain at the start
3117 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003118 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003119 id->local_num = scope->num_locals;
3120 scope->num_locals += 1;
3121 }
Damien9ecbcff2013-12-11 00:41:43 +00003122 }
Damien9ecbcff2013-12-11 00:41:43 +00003123
Damien George65dc9602015-08-14 12:24:11 +01003124 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003125 // make sure they are in the order of the parent scope
3126 if (scope->parent != NULL) {
3127 int num_free = 0;
3128 for (int i = 0; i < scope->parent->id_info_len; i++) {
3129 id_info_t *id = &scope->parent->id_info[i];
3130 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3131 for (int j = 0; j < scope->id_info_len; j++) {
3132 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003133 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003134 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003135 // in Micro Python the frees come first, before the params
3136 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003137 num_free += 1;
3138 }
3139 }
3140 }
Damien429d7192013-10-04 19:53:11 +01003141 }
Damien George6baf76e2013-12-30 22:32:17 +00003142 // in Micro Python shift all other locals after the free locals
3143 if (num_free > 0) {
3144 for (int i = 0; i < scope->id_info_len; i++) {
3145 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003146 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003147 id->local_num += num_free;
3148 }
3149 }
Damien George2827d622014-04-27 15:50:52 +01003150 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 +00003151 scope->num_locals += num_free;
3152 }
Damien429d7192013-10-04 19:53:11 +01003153 }
Damien429d7192013-10-04 19:53:11 +01003154}
3155
Damien Georged4dba882015-11-13 13:38:28 +00003156#if !MICROPY_PERSISTENT_CODE_SAVE
3157STATIC
3158#endif
3159mp_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 +01003160 // put compiler state on the stack, it's relatively small
3161 compiler_t comp_state = {0};
3162 compiler_t *comp = &comp_state;
3163
Damien Georgecbd2f742014-01-19 11:48:48 +00003164 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003165 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003166
Damien George62a3a282015-03-01 12:04:05 +00003167 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003168 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003169
Damien Georgea210c772015-03-26 15:49:53 +00003170 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003171 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003172
Damien826005c2013-10-05 23:17:28 +01003173 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003174 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003175 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003176 comp->emit_method_table = &emit_bc_method_table;
3177 #endif
Damien826005c2013-10-05 23:17:28 +01003178 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003179 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003180 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003181#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003182 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003183 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003184#endif
Damien826005c2013-10-05 23:17:28 +01003185 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003186 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003187 }
3188
3189 // update maximim number of labels needed
3190 if (comp->next_label > max_num_labels) {
3191 max_num_labels = comp->next_label;
3192 }
Damien429d7192013-10-04 19:53:11 +01003193 }
3194
Damien826005c2013-10-05 23:17:28 +01003195 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003196 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 +00003197 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003198 }
3199
Damien Georgea210c772015-03-26 15:49:53 +00003200 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003201 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003202
Damien826005c2013-10-05 23:17:28 +01003203 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003204#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003205 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003206#endif
Damien3ef4abb2013-10-12 16:53:13 +01003207#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003208 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003209#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003210 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003211 if (false) {
3212 // dummy
3213
Damien3ef4abb2013-10-12 16:53:13 +01003214#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003215 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003216 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003217 if (emit_inline_thumb == NULL) {
3218 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3219 }
3220 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003221 comp->emit_inline_asm = emit_inline_thumb;
3222 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003223 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003224 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003225 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003226 }
Damienc025ebb2013-10-12 14:30:21 +01003227#endif
3228
Damien826005c2013-10-05 23:17:28 +01003229 } else {
Damienc025ebb2013-10-12 14:30:21 +01003230
3231 // choose the emit type
3232
Damien826005c2013-10-05 23:17:28 +01003233 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003234
3235#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003236 case MP_EMIT_OPT_NATIVE_PYTHON:
3237 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003238#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003239 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003240 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003241 }
Damien13ed3a62013-10-08 09:05:10 +01003242 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003243#elif MICROPY_EMIT_X86
3244 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003245 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003246 }
3247 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003248#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003249 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003250 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003251 }
3252 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003253#elif MICROPY_EMIT_ARM
3254 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003255 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003256 }
3257 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003258#endif
3259 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003260 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003261 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003262#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003263
Damien826005c2013-10-05 23:17:28 +01003264 default:
Damien826005c2013-10-05 23:17:28 +01003265 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003266 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003267 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003268 #endif
Damien826005c2013-10-05 23:17:28 +01003269 break;
3270 }
Damienc025ebb2013-10-12 14:30:21 +01003271
Damien George1e1779e2015-01-14 00:20:28 +00003272 // need a pass to compute stack size
3273 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3274
Damien George36db6bc2014-05-07 17:24:22 +01003275 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003276 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003277 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3278 }
3279
3280 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003281 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003282 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003283 }
Damien6cdd3af2013-10-05 18:08:26 +01003284 }
Damien429d7192013-10-04 19:53:11 +01003285 }
3286
Damien Georgecfc4c332015-07-29 22:16:01 +00003287 if (comp->compile_error != MP_OBJ_NULL) {
3288 // if there is no line number for the error then use the line
3289 // number for the start of this scope
3290 compile_error_set_line(comp, comp->scope_cur->pn);
3291 // add a traceback to the exception using relevant source info
3292 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3293 comp->compile_error_line, comp->scope_cur->simple_name);
3294 }
3295
Damien George41d02b62014-01-24 22:42:28 +00003296 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003297
Damien Georgea210c772015-03-26 15:49:53 +00003298 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003299#if MICROPY_EMIT_NATIVE
3300 if (emit_native != NULL) {
3301#if MICROPY_EMIT_X64
3302 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003303#elif MICROPY_EMIT_X86
3304 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003305#elif MICROPY_EMIT_THUMB
3306 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003307#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003308 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003309#endif
3310 }
3311#endif
3312#if MICROPY_EMIT_INLINE_THUMB
3313 if (emit_inline_thumb != NULL) {
3314 emit_inline_thumb_free(emit_inline_thumb);
3315 }
3316#endif
Damien George41d02b62014-01-24 22:42:28 +00003317
Damien George52b5d762014-09-23 15:31:56 +00003318 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003319 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003320
Damien George41d02b62014-01-24 22:42:28 +00003321 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003322 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003323 for (scope_t *s = module_scope; s;) {
3324 scope_t *next = s->next;
3325 scope_free(s);
3326 s = next;
3327 }
Damien5ac1b2e2013-10-18 19:58:12 +01003328
Damien George9d5e5c02015-09-24 13:15:57 +01003329 if (comp->compile_error != MP_OBJ_NULL) {
3330 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003331 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003332 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003333 }
Damien429d7192013-10-04 19:53:11 +01003334}
Damien Georged4dba882015-11-13 13:38:28 +00003335
3336mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3337 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3338 // return function that executes the outer module
3339 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3340}
Damien Georgedd5353a2015-12-18 12:35:44 +00003341
3342#endif // MICROPY_ENABLE_COMPILER