blob: 418c1100bf0ec6f53e508fe9966cd1b40275222d [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 George080a78b2016-12-07 11:17:17 +110072#if MICROPY_EMIT_NATIVE
73// define a macro to access external native emitter
74#if MICROPY_EMIT_X64
75#define NATIVE_EMITTER(f) emit_native_x64_##f
76#elif MICROPY_EMIT_X86
77#define NATIVE_EMITTER(f) emit_native_x86_##f
78#elif MICROPY_EMIT_THUMB
79#define NATIVE_EMITTER(f) emit_native_thumb_##f
80#elif MICROPY_EMIT_ARM
81#define NATIVE_EMITTER(f) emit_native_arm_##f
82#else
83#error "unknown native emitter"
84#endif
85#endif
86
Damien George0496de22015-10-03 17:07:54 +010087#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
88#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
89
Damien Georgea91ac202014-10-05 19:01:34 +010090// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010091typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000092 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010093
Damien George78035b92014-04-09 12:27:39 +010094 uint8_t is_repl;
95 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010096 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010097 uint8_t have_star;
98
99 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +0000100 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +0000101 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +0100102
Damien George6f355fd2014-04-10 14:11:31 +0100103 uint next_label;
Damienb05d7072013-10-05 13:37:10 +0100104
Damien George69b89d22014-04-11 13:38:30 +0000105 uint16_t num_dict_params;
106 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +0100107
Damien Georgea91ac202014-10-05 19:01:34 +0100108 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
109 uint16_t continue_label;
110 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +0000111 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +0100112
Damien429d7192013-10-04 19:53:11 +0100113 scope_t *scope_head;
114 scope_t *scope_cur;
115
Damien6cdd3af2013-10-05 18:08:26 +0100116 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000117 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100118 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000119 #endif
Damien826005c2013-10-05 23:17:28 +0100120
Damien Georgea77ffe62015-03-14 12:59:31 +0000121 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100122 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
123 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 +0000124 #endif
Damien429d7192013-10-04 19:53:11 +0100125} compiler_t;
126
Damien Georgecfc4c332015-07-29 22:16:01 +0000127STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
128 // if the line of the error is unknown then try to update it from the pn
129 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000130 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100131 }
Damien George84d59c22015-07-27 22:20:00 +0100132}
133
134STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000135 // only register the error if there has been no other error
136 if (comp->compile_error == MP_OBJ_NULL) {
137 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
138 compile_error_set_line(comp, pn);
139 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000140}
141
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200142STATIC 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 +0100143STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000144STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100145
Damien George6f355fd2014-04-10 14:11:31 +0100146STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100147 return comp->next_label++;
148}
149
Damien George8dcc0c72014-03-27 10:55:21 +0000150STATIC void compile_increase_except_level(compiler_t *comp) {
151 comp->cur_except_level += 1;
152 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
153 comp->scope_cur->exc_stack_size = comp->cur_except_level;
154 }
155}
156
157STATIC void compile_decrease_except_level(compiler_t *comp) {
158 assert(comp->cur_except_level > 0);
159 comp->cur_except_level -= 1;
160}
161
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200162STATIC 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 +0100163 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100164 scope->parent = comp->scope_cur;
165 scope->next = NULL;
166 if (comp->scope_head == NULL) {
167 comp->scope_head = scope;
168 } else {
169 scope_t *s = comp->scope_head;
170 while (s->next != NULL) {
171 s = s->next;
172 }
173 s->next = scope;
174 }
175 return scope;
176}
177
Damien George91bc32d2015-04-09 15:31:53 +0000178typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
179
180STATIC 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 +0000181 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000182 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
183 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100184 for (int i = 0; i < num_nodes; i++) {
185 f(comp, pns->nodes[i]);
186 }
Damiend99b0522013-12-21 18:17:45 +0000187 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100188 f(comp, pn);
189 }
190}
191
Damien George969a6b32014-12-10 22:07:04 +0000192STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000193 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100194 for (int i = 0; i < num_nodes; i++) {
195 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000196 if (comp->compile_error != MP_OBJ_NULL) {
197 // add line info for the error in case it didn't have a line number
198 compile_error_set_line(comp, pns->nodes[i]);
199 return;
200 }
Damien429d7192013-10-04 19:53:11 +0100201 }
202}
203
Damien George542bd6b2015-03-26 14:42:40 +0000204STATIC void compile_load_id(compiler_t *comp, qstr qst) {
205 if (comp->pass == MP_PASS_SCOPE) {
206 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
207 } else {
Damien George41125902015-03-26 16:44:14 +0000208 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000209 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000210 #else
211 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
212 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000213 }
214}
215
216STATIC void compile_store_id(compiler_t *comp, qstr qst) {
217 if (comp->pass == MP_PASS_SCOPE) {
218 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
219 } else {
Damien George41125902015-03-26 16:44:14 +0000220 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000221 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000222 #else
223 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
224 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000225 }
226}
227
228STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
229 if (comp->pass == MP_PASS_SCOPE) {
230 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
231 } else {
Damien George41125902015-03-26 16:44:14 +0000232 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000233 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000234 #else
235 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
236 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000237 }
238}
239
Damien George2c0842b2014-04-27 16:46:51 +0100240STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100241 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000242 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100243 compile_node(comp, pn);
244 total += 1;
245 }
246 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000247 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100248 for (int i = 0; i < n; i++) {
249 compile_node(comp, pns_list->nodes[i]);
250 }
251 total += n;
252 }
Damien Georgeb9791222014-01-23 00:34:21 +0000253 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100254}
Damien429d7192013-10-04 19:53:11 +0100255
Damien George969a6b32014-12-10 22:07:04 +0000256STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100257 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000258 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100259}
260
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200261STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100262 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100263 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000264 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100265 }
266 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100267 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100268 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000269 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100270 }
271 return;
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
273 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
274 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
275 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100276 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000277 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100278 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100279 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000280 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100281 }
Damien George0b2fd912015-02-28 14:37:54 +0000282 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000283 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100284 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000285 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100286 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000287 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100288 }
289 }
290 return;
Damiend99b0522013-12-21 18:17:45 +0000291 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100292 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000293 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100294 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000295 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100296 }
Damiend99b0522013-12-21 18:17:45 +0000297 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100298 c_if_cond(comp, pns->nodes[0], !jump_if, label);
299 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100300 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
301 // cond is something in parenthesis
302 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
303 // empty tuple, acts as false for the condition
304 if (jump_if == false) {
305 EMIT_ARG(jump, label);
306 }
Damien George93b37262016-01-07 13:07:52 +0000307 } else {
308 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100309 // non-empty tuple, acts as true for the condition
310 if (jump_if == true) {
311 EMIT_ARG(jump, label);
312 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100313 }
314 return;
Damien3a205172013-10-12 15:01:56 +0100315 }
316 }
317
318 // nothing special, fall back to default compiling for node and jump
319 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000320 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100321}
322
323typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100324STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100325
pohmelie81ebba72016-01-27 23:23:11 +0300326STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100327 if (assign_kind != ASSIGN_AUG_STORE) {
328 compile_node(comp, pns->nodes[0]);
329 }
330
Damiend99b0522013-12-21 18:17:45 +0000331 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
332 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300333 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000334 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100335 if (assign_kind != ASSIGN_AUG_STORE) {
336 for (int i = 0; i < n - 1; i++) {
337 compile_node(comp, pns1->nodes[i]);
338 }
339 }
Damiend99b0522013-12-21 18:17:45 +0000340 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
341 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100342 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000343 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100344 if (assign_kind == ASSIGN_AUG_STORE) {
345 EMIT(rot_three);
346 EMIT(store_subscr);
347 } else {
348 compile_node(comp, pns1->nodes[0]);
349 if (assign_kind == ASSIGN_AUG_LOAD) {
350 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100351 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100352 } else {
353 EMIT(store_subscr);
354 }
355 }
Damiend99b0522013-12-21 18:17:45 +0000356 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
357 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100358 if (assign_kind == ASSIGN_AUG_LOAD) {
359 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000360 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100361 } else {
362 if (assign_kind == ASSIGN_AUG_STORE) {
363 EMIT(rot_two);
364 }
Damien Georgeb9791222014-01-23 00:34:21 +0000365 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100366 }
367 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100368 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100369 }
370 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100371 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100372 }
373
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100374 return;
375
376cannot_assign:
377 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100378}
379
Damien George0288cf02014-04-11 11:53:00 +0000380// 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 +0100381STATIC 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 +0000382 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
383
384 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000385 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000386 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
387 EMIT_ARG(unpack_ex, 0, num_tail);
388 have_star_index = 0;
389 }
Damien George963a5a32015-01-16 17:47:07 +0000390 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000391 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000392 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000393 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
394 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100395 } else {
Damien George9d181f62014-04-27 16:55:27 +0100396 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100397 return;
398 }
399 }
400 }
Damien George963a5a32015-01-16 17:47:07 +0000401 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000402 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100403 }
Damien George0288cf02014-04-11 11:53:00 +0000404 if (num_head != 0) {
405 if (0 == have_star_index) {
406 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100407 } else {
Damien George0288cf02014-04-11 11:53:00 +0000408 c_assign(comp, node_head, ASSIGN_STORE);
409 }
410 }
Damien George963a5a32015-01-16 17:47:07 +0000411 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000412 if (num_head + i == have_star_index) {
413 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
414 } else {
415 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100416 }
417 }
418}
419
420// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100421STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000422 assert(!MP_PARSE_NODE_IS_NULL(pn));
423 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000424 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000425 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100426 switch (assign_kind) {
427 case ASSIGN_STORE:
428 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000429 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100430 break;
431 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000432 default:
Damien George542bd6b2015-03-26 14:42:40 +0000433 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100434 break;
435 }
436 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100437 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100438 return;
439 }
440 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000441 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000442 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
443 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300444 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100445 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300446 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100447 break;
448
449 case PN_testlist_star_expr:
450 case PN_exprlist:
451 // lhs is a tuple
452 if (assign_kind != ASSIGN_STORE) {
453 goto bad_aug;
454 }
Damien George0288cf02014-04-11 11:53:00 +0000455 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100456 break;
457
458 case PN_atom_paren:
459 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000460 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100461 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100462 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000463 } else {
464 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000465 if (assign_kind != ASSIGN_STORE) {
466 goto bad_aug;
467 }
Damiend99b0522013-12-21 18:17:45 +0000468 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100469 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100470 }
471 break;
472
473 case PN_atom_bracket:
474 // lhs is something in brackets
475 if (assign_kind != ASSIGN_STORE) {
476 goto bad_aug;
477 }
Damiend99b0522013-12-21 18:17:45 +0000478 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100479 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000480 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000481 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
482 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100483 goto testlist_comp;
484 } else {
485 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000486 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100487 }
488 break;
489
490 default:
Damien George9d181f62014-04-27 16:55:27 +0100491 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100492 }
493 return;
494
495 testlist_comp:
496 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000497 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
498 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
499 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100500 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000501 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000502 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000503 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100504 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000505 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
506 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000507 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
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
pohmelie81ebba72016-01-27 23:23:11 +0300821 // compile the body (funcdef, async 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);
pohmelie81ebba72016-01-27 23:23:11 +0300826 #if MICROPY_PY_ASYNC_AWAIT
827 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
828 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
829 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
830 body_name = compile_funcdef_helper(comp, pns0, emit_options);
831 scope_t *fscope = (scope_t*)pns0->nodes[4];
832 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
833 #endif
Damien429d7192013-10-04 19:53:11 +0100834 } else {
Damien George0bb97132015-02-27 14:25:47 +0000835 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
836 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100837 }
838
839 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100840 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100841 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100842 }
843
844 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000845 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100846}
847
Damien George969a6b32014-12-10 22:07:04 +0000848STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100849 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100850 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000851 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100852}
853
Damien George6be0b0a2014-08-15 14:30:52 +0100854STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000855 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000856 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300857 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000858 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100859
pohmelie81ebba72016-01-27 23:23:11 +0300860 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100861
Damiend99b0522013-12-21 18:17:45 +0000862 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
863 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300864 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000865 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100866 for (int i = 0; i < n - 1; i++) {
867 compile_node(comp, pns1->nodes[i]);
868 }
Damiend99b0522013-12-21 18:17:45 +0000869 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
870 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100871 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000872 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100873 compile_node(comp, pns1->nodes[0]);
874 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000875 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
876 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000877 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100878 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100879 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100880 }
881 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100882 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100883 }
884
Damiend99b0522013-12-21 18:17:45 +0000885 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
886 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000887 if (MP_PARSE_NODE_IS_NULL(pn)) {
888 goto cannot_delete;
889 } else {
890 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000891 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100892 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
893
Damiend99b0522013-12-21 18:17:45 +0000894 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
895 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
896 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100897 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000898 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100899 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000900 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100901 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000902 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100903 c_del_stmt(comp, pns->nodes[0]);
904 for (int i = 0; i < n; i++) {
905 c_del_stmt(comp, pns1->nodes[i]);
906 }
Damien George216a7112016-09-30 14:48:06 +1000907 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100908 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100909 } else {
910 // sequence with 2 items
911 goto sequence_with_2_items;
912 }
913 } else {
914 // sequence with 2 items
915 sequence_with_2_items:
916 c_del_stmt(comp, pns->nodes[0]);
917 c_del_stmt(comp, pns->nodes[1]);
918 }
Damien429d7192013-10-04 19:53:11 +0100919 }
920 } else {
Damien George93b37262016-01-07 13:07:52 +0000921 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100922 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100923 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100924
925 return;
926
927cannot_delete:
928 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100929}
930
Damien George969a6b32014-12-10 22:07:04 +0000931STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100932 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
933}
934
Damien George969a6b32014-12-10 22:07:04 +0000935STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100936 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100937 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100938 }
Damien George090c9232014-10-17 14:08:49 +0000939 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000940 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100941}
942
Damien George969a6b32014-12-10 22:07:04 +0000943STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100944 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100945 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100946 }
Damien George090c9232014-10-17 14:08:49 +0000947 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000948 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100949}
950
Damien George969a6b32014-12-10 22:07:04 +0000951STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100952 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100953 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100954 return;
955 }
Damiend99b0522013-12-21 18:17:45 +0000956 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100957 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000958 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000959 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100960 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000961 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
962 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 +0100963
Damien George6f355fd2014-04-10 14:11:31 +0100964 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100965 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
966 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
967 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000968 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100969 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
970 } else {
971 compile_node(comp, pns->nodes[0]);
972 }
973 EMIT(return_value);
974}
975
Damien George969a6b32014-12-10 22:07:04 +0000976STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100977 compile_node(comp, pns->nodes[0]);
978 EMIT(pop_top);
979}
980
Damien George969a6b32014-12-10 22:07:04 +0000981STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000982 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100983 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000984 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +0000985 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +0100986 // raise x from y
Damiend99b0522013-12-21 18:17:45 +0000987 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100988 compile_node(comp, pns->nodes[0]);
989 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000990 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +0100991 } else {
992 // raise x
993 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +0000994 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +0100995 }
996}
997
Damien George635543c2014-04-10 12:56:52 +0100998// q_base holds the base of the name
999// eg a -> q_base=a
1000// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001001STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001002 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001003 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001005 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001006 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001007 pn = pns->nodes[0];
1008 is_as = true;
1009 }
Damien George635543c2014-04-10 12:56:52 +01001010 if (MP_PARSE_NODE_IS_NULL(pn)) {
1011 // empty name (eg, from . import x)
1012 *q_base = MP_QSTR_;
1013 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1014 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001015 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001016 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001017 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001018 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001019 }
Damien George635543c2014-04-10 12:56:52 +01001020 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001021 } else {
1022 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001023 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001024 {
Damien429d7192013-10-04 19:53:11 +01001025 // a name of the form a.b.c
1026 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001027 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001028 }
Damiend99b0522013-12-21 18:17:45 +00001029 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001030 int len = n - 1;
1031 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001032 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001033 }
Damien George55baff42014-01-21 21:40:13 +00001034 byte *q_ptr;
1035 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001036 for (int i = 0; i < n; i++) {
1037 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001038 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001039 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001040 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001041 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001042 memcpy(str_dest, str_src, str_src_len);
1043 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001044 }
Damien George635543c2014-04-10 12:56:52 +01001045 qstr q_full = qstr_build_end(q_ptr);
1046 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001047 if (is_as) {
1048 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001049 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001050 }
1051 }
Damien429d7192013-10-04 19:53:11 +01001052 }
Damien429d7192013-10-04 19:53:11 +01001053 }
1054}
1055
Damien George6be0b0a2014-08-15 14:30:52 +01001056STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001057 EMIT_ARG(load_const_small_int, 0); // level 0 import
1058 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1059 qstr q_base;
1060 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001061 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001062}
1063
Damien George969a6b32014-12-10 22:07:04 +00001064STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001065 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1066}
1067
Damien George969a6b32014-12-10 22:07:04 +00001068STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001069 mp_parse_node_t pn_import_source = pns->nodes[0];
1070
1071 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1072 uint import_level = 0;
1073 do {
1074 mp_parse_node_t pn_rel;
1075 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)) {
1076 // This covers relative imports with dots only like "from .. import"
1077 pn_rel = pn_import_source;
1078 pn_import_source = MP_PARSE_NODE_NULL;
1079 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1080 // This covers relative imports starting with dot(s) like "from .foo import"
1081 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1082 pn_rel = pns_2b->nodes[0];
1083 pn_import_source = pns_2b->nodes[1];
1084 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1085 } else {
1086 // Not a relative import
1087 break;
1088 }
1089
1090 // get the list of . and/or ...'s
1091 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001092 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001093
1094 // count the total number of .'s
1095 for (int i = 0; i < n; i++) {
1096 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1097 import_level++;
1098 } else {
1099 // should be an MP_TOKEN_ELLIPSIS
1100 import_level += 3;
1101 }
1102 }
1103 } while (0);
1104
Damiend99b0522013-12-21 18:17:45 +00001105 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001106 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001107
1108 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001109 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001110 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001111
1112 // do the import
Damien George635543c2014-04-10 12:56:52 +01001113 qstr dummy_q;
1114 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001115 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001116
Damien429d7192013-10-04 19:53:11 +01001117 } else {
Damien George635543c2014-04-10 12:56:52 +01001118 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001119
1120 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001121 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001122 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001123 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001124 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1125 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1126 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001127 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001128 }
Damien Georgeb9791222014-01-23 00:34:21 +00001129 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001130
1131 // do the import
Damien George635543c2014-04-10 12:56:52 +01001132 qstr dummy_q;
1133 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001134 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001135 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1136 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1137 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001138 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001139 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001140 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001141 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001142 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001143 }
1144 }
1145 EMIT(pop_top);
1146 }
1147}
1148
Damien George584ba672014-12-21 17:26:45 +00001149STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1150 bool added;
1151 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001152 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1153 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001154 return;
1155 }
1156 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1157
1158 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1159 id_info = scope_find_global(comp->scope_cur, qst);
1160 if (id_info != NULL) {
1161 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1162 }
1163}
1164
Damien George969a6b32014-12-10 22:07:04 +00001165STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001166 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001167 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001168 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001169 for (int i = 0; i < n; i++) {
1170 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001171 }
1172 }
1173}
1174
Damien George584ba672014-12-21 17:26:45 +00001175STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1176 bool added;
1177 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001178 if (added) {
1179 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1180 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1181 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1182 }
1183 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001184 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001185 }
Damien George584ba672014-12-21 17:26:45 +00001186}
1187
Damien George969a6b32014-12-10 22:07:04 +00001188STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001189 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001190 if (comp->scope_cur->kind == SCOPE_MODULE) {
1191 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1192 return;
1193 }
1194 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001195 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001196 for (int i = 0; i < n; i++) {
1197 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001198 }
1199 }
1200}
1201
Damien George969a6b32014-12-10 22:07:04 +00001202STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001203 // with optimisations enabled we don't compile assertions
1204 if (MP_STATE_VM(mp_optimise_value) != 0) {
1205 return;
1206 }
1207
Damien George6f355fd2014-04-10 14:11:31 +01001208 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001209 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001210 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001211 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001212 // assertion message
1213 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001214 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001215 }
Damien Georgeb9791222014-01-23 00:34:21 +00001216 EMIT_ARG(raise_varargs, 1);
1217 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001218}
1219
Damien George969a6b32014-12-10 22:07:04 +00001220STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001221 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001222
Damien George65dc9602015-08-14 12:24:11 +01001223 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001224 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001225 uint l_fail = comp_next_label(comp);
1226 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001227
Damien George391db862014-10-17 17:57:33 +00001228 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001229
Damien George65dc9602015-08-14 12:24:11 +01001230 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001231 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001232 goto done;
1233 }
1234
1235 if (
Damien George65dc9602015-08-14 12:24:11 +01001236 // optimisation: don't jump over non-existent elif/else blocks
1237 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001238 // optimisation: don't jump if last instruction was return
1239 && !EMIT(last_emit_was_return_value)
1240 ) {
1241 // jump over elif/else blocks
1242 EMIT_ARG(jump, l_end);
1243 }
1244
1245 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001246 }
1247
Damien George235f9b32014-10-17 17:30:16 +00001248 // compile elif blocks (if any)
1249 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001250 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 +00001251 for (int i = 0; i < n_elif; i++) {
1252 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1253 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001254
Damien George65dc9602015-08-14 12:24:11 +01001255 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001256 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001257 uint l_fail = comp_next_label(comp);
1258 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001259
Damien George391db862014-10-17 17:57:33 +00001260 compile_node(comp, pns_elif->nodes[1]); // elif block
1261
Damien George65dc9602015-08-14 12:24:11 +01001262 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001263 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001264 goto done;
1265 }
1266
1267 // optimisation: don't jump if last instruction was return
1268 if (!EMIT(last_emit_was_return_value)) {
1269 EMIT_ARG(jump, l_end);
1270 }
1271 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001272 }
1273 }
1274
1275 // compile else block
1276 compile_node(comp, pns->nodes[3]); // can be null
1277
Damien George391db862014-10-17 17:57:33 +00001278done:
Damien Georgeb9791222014-01-23 00:34:21 +00001279 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001280}
1281
Damien Georgecbddb272014-02-01 20:08:18 +00001282#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001283 uint16_t old_break_label = comp->break_label; \
1284 uint16_t old_continue_label = comp->continue_label; \
1285 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001286 uint break_label = comp_next_label(comp); \
1287 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001288 comp->break_label = break_label; \
1289 comp->continue_label = continue_label; \
1290 comp->break_continue_except_level = comp->cur_except_level;
1291
1292#define END_BREAK_CONTINUE_BLOCK \
1293 comp->break_label = old_break_label; \
1294 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001295 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001296
Damien George969a6b32014-12-10 22:07:04 +00001297STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001298 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001299
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001300 if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
Damien George391db862014-10-17 17:57:33 +00001301 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001302 if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
Damien George391db862014-10-17 17:57:33 +00001303 EMIT_ARG(jump, continue_label);
1304 }
1305 EMIT_ARG(label_assign, top_label);
1306 compile_node(comp, pns->nodes[1]); // body
1307 EMIT_ARG(label_assign, continue_label);
1308 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1309 }
Damience89a212013-10-15 22:25:17 +01001310
1311 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001312 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001313
1314 compile_node(comp, pns->nodes[2]); // else
1315
Damien Georgeb9791222014-01-23 00:34:21 +00001316 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001317}
1318
Damien Georgee181c0d2014-12-12 17:19:56 +00001319// This function compiles an optimised for-loop of the form:
1320// for <var> in range(<start>, <end>, <step>):
1321// <body>
1322// else:
1323// <else>
1324// <var> must be an identifier and <step> must be a small-int.
1325//
1326// Semantics of for-loop require:
1327// - final failing value should not be stored in the loop variable
1328// - if the loop never runs, the loop variable should never be assigned
1329// - assignments to <var>, <end> or <step> in the body do not alter the loop
1330// (<step> is a constant for us, so no need to worry about it changing)
1331//
1332// If <end> is a small-int, then the stack during the for-loop contains just
1333// the current value of <var>. Otherwise, the stack contains <end> then the
1334// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001335STATIC 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 +00001336 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001337
Damien George6f355fd2014-04-10 14:11:31 +01001338 uint top_label = comp_next_label(comp);
1339 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001340
Damien Georgee181c0d2014-12-12 17:19:56 +00001341 // put the end value on the stack if it's not a small-int constant
1342 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1343 if (end_on_stack) {
1344 compile_node(comp, pn_end);
1345 }
1346
1347 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001348 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001349
Damien Georgeb9791222014-01-23 00:34:21 +00001350 EMIT_ARG(jump, entry_label);
1351 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001352
Damien Georgec33ce602014-12-11 17:35:23 +00001353 // duplicate next value and store it to var
1354 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001355 c_assign(comp, pn_var, ASSIGN_STORE);
1356
Damienf3822fc2013-11-09 20:12:03 +00001357 // compile body
1358 compile_node(comp, pn_body);
1359
Damien Georgeb9791222014-01-23 00:34:21 +00001360 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001361
Damien Georgee181c0d2014-12-12 17:19:56 +00001362 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001363 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001364 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001365
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001367
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001368 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001369 if (end_on_stack) {
1370 EMIT(dup_top_two);
1371 EMIT(rot_two);
1372 } else {
1373 EMIT(dup_top);
1374 compile_node(comp, pn_end);
1375 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001376 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1377 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001378 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001379 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001380 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001381 }
Damien George63f38322015-02-28 15:04:06 +00001382 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001383
1384 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001385 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001386
1387 compile_node(comp, pn_else);
1388
Damien Georgeb9791222014-01-23 00:34:21 +00001389 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001390
1391 // discard final value of var that failed the loop condition
1392 EMIT(pop_top);
1393
1394 // discard <end> value if it's on the stack
1395 if (end_on_stack) {
1396 EMIT(pop_top);
1397 }
Damienf72fd0e2013-11-06 20:20:49 +00001398}
1399
Damien George969a6b32014-12-10 22:07:04 +00001400STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001401 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1402 // this is actually slower, but uses no heap memory
1403 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001404 if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +00001405 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001406 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001407 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001408 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001409 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1410 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001411 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001412 mp_parse_node_t pn_range_start;
1413 mp_parse_node_t pn_range_end;
1414 mp_parse_node_t pn_range_step;
1415 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001416 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001417 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001418 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001419 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001420 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001421 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001422 } else if (n_args == 2) {
1423 pn_range_start = args[0];
1424 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001425 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001426 } else {
1427 pn_range_start = args[0];
1428 pn_range_end = args[1];
1429 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001430 // We need to know sign of step. This is possible only if it's constant
1431 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1432 optimize = false;
1433 }
Damienf72fd0e2013-11-06 20:20:49 +00001434 }
Damien George33ac0fd2015-12-08 21:05:14 +00001435 // arguments must be able to be compiled as standard expressions
1436 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1437 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1438 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1439 optimize = false;
1440 }
1441 }
1442 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1443 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1444 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1445 optimize = false;
1446 }
1447 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001448 }
1449 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001450 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1451 return;
1452 }
1453 }
1454 }
Damienf72fd0e2013-11-06 20:20:49 +00001455
Damien Georgecbddb272014-02-01 20:08:18 +00001456 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001457 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001458
Damien George6f355fd2014-04-10 14:11:31 +01001459 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001460
Damien429d7192013-10-04 19:53:11 +01001461 compile_node(comp, pns->nodes[1]); // iterator
1462 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001463 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001464 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001465 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1466 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001467 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001468 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien Georgeb9791222014-01-23 00:34:21 +00001470 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001471 EMIT(for_iter_end);
1472
1473 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001474 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001475
Damien429d7192013-10-04 19:53:11 +01001476 compile_node(comp, pns->nodes[3]); // else (not tested)
1477
Damien Georgeb9791222014-01-23 00:34:21 +00001478 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001479}
1480
Damien George6be0b0a2014-08-15 14:30:52 +01001481STATIC 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 +01001482 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001483 uint l1 = comp_next_label(comp);
1484 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001485
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001487 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001488
Damien429d7192013-10-04 19:53:11 +01001489 compile_node(comp, pn_body); // body
1490 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001491 EMIT_ARG(jump, success_label); // jump over exception handler
1492
1493 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001494 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001495
Damien Georgef0406852016-09-27 12:37:21 +10001496 // at this point the top of the stack contains the exception instance that was raised
1497
Damien George6f355fd2014-04-10 14:11:31 +01001498 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001499
1500 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001501 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1502 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001503
1504 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001505 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001506
Damiend99b0522013-12-21 18:17:45 +00001507 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001508 // this is a catch all exception handler
1509 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001510 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001511 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001512 return;
1513 }
1514 } else {
1515 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001516 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1517 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1518 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1519 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001520 // handler binds the exception to a local
1521 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001522 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001523 }
1524 }
1525 EMIT(dup_top);
1526 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001527 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001528 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001529 }
1530
Damien Georgef0406852016-09-27 12:37:21 +10001531 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001532 if (qstr_exception_local == 0) {
1533 EMIT(pop_top);
1534 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001535 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001536 }
1537
Damien George6f355fd2014-04-10 14:11:31 +01001538 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001539 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001540 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001541 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001542 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001543 }
1544 compile_node(comp, pns_except->nodes[1]);
1545 if (qstr_exception_local != 0) {
1546 EMIT(pop_block);
1547 }
1548 EMIT(pop_except);
1549 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001550 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1551 EMIT_ARG(label_assign, l3);
1552 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001553 compile_store_id(comp, qstr_exception_local);
1554 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001555
Damien George8dcc0c72014-03-27 10:55:21 +00001556 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001557 EMIT(end_finally);
1558 }
Damien Georgeb9791222014-01-23 00:34:21 +00001559 EMIT_ARG(jump, l2);
1560 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001561 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001562 }
1563
Damien George8dcc0c72014-03-27 10:55:21 +00001564 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001565 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001566 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001567
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001569 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001571}
1572
Damien George6be0b0a2014-08-15 14:30:52 +01001573STATIC 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 +01001574 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001575
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001577 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001578
Damien429d7192013-10-04 19:53:11 +01001579 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001580 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001581 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001582 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001583 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001584 } else {
1585 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1586 }
1587 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001588 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1589 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001590 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001591
Damien George8dcc0c72014-03-27 10:55:21 +00001592 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001593 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001594}
1595
Damien George969a6b32014-12-10 22:07:04 +00001596STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001597 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1598 {
Damiend99b0522013-12-21 18:17:45 +00001599 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1600 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001601 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001602 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1603 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001604 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001605 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001606 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001607 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001608 // no finally
1609 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1610 } else {
1611 // have finally
Damiend99b0522013-12-21 18:17:45 +00001612 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 +01001613 }
1614 } else {
1615 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001616 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001617 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001618 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001619 }
Damien429d7192013-10-04 19:53:11 +01001620 }
1621}
1622
Damien George6be0b0a2014-08-15 14:30:52 +01001623STATIC 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 +01001624 if (n == 0) {
1625 // no more pre-bits, compile the body of the with
1626 compile_node(comp, body);
1627 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001628 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001629 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1630 // we need to allocate an extra label for the native emitter
1631 // it will use l_end+1 as an auxiliary label
1632 comp_next_label(comp);
1633 }
Damiend99b0522013-12-21 18:17:45 +00001634 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001635 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001636 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001637 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001638 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001639 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1640 } else {
1641 // this pre-bit is just an expression
1642 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001643 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001644 EMIT(pop_top);
1645 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001646 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001647 // compile additional pre-bits and the body
1648 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1649 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001650 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001651 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001652 EMIT(end_finally);
1653 }
1654}
1655
Damien George969a6b32014-12-10 22:07:04 +00001656STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001657 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001658 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001659 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001660 assert(n > 0);
1661
1662 // compile in a nested fashion
1663 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1664}
1665
pohmelie81ebba72016-01-27 23:23:11 +03001666STATIC void compile_yield_from(compiler_t *comp) {
1667 EMIT(get_iter);
1668 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1669 EMIT(yield_from);
1670}
1671
1672#if MICROPY_PY_ASYNC_AWAIT
1673STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1674 EMIT_ARG(load_method, method);
1675 EMIT_ARG(call_method, 0, 0, 0);
1676 compile_yield_from(comp);
1677}
1678
1679STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1680 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1681
1682 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1683 uint while_else_label = comp_next_label(comp);
1684 uint try_exception_label = comp_next_label(comp);
1685 uint try_else_label = comp_next_label(comp);
1686 uint try_finally_label = comp_next_label(comp);
1687
1688 compile_node(comp, pns->nodes[1]); // iterator
1689 compile_await_object_method(comp, MP_QSTR___aiter__);
1690 compile_store_id(comp, context);
1691
1692 START_BREAK_CONTINUE_BLOCK
1693
1694 EMIT_ARG(label_assign, continue_label);
1695
1696 EMIT_ARG(setup_except, try_exception_label);
1697 compile_increase_except_level(comp);
1698
1699 compile_load_id(comp, context);
1700 compile_await_object_method(comp, MP_QSTR___anext__);
1701 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1702 EMIT(pop_block);
1703 EMIT_ARG(jump, try_else_label);
1704
1705 EMIT_ARG(label_assign, try_exception_label);
1706 EMIT(start_except_handler);
1707 EMIT(dup_top);
1708 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1709 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1710 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001711 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001712 EMIT(pop_except);
1713 EMIT_ARG(jump, while_else_label);
1714
1715 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001716 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001717 compile_decrease_except_level(comp);
1718 EMIT(end_finally);
1719 EMIT(end_except_handler);
1720
1721 EMIT_ARG(label_assign, try_else_label);
1722 compile_node(comp, pns->nodes[2]); // body
1723
1724 EMIT_ARG(jump, continue_label);
1725 // break/continue apply to outer loop (if any) in the else block
1726 END_BREAK_CONTINUE_BLOCK
1727
1728 EMIT_ARG(label_assign, while_else_label);
1729 compile_node(comp, pns->nodes[3]); // else
1730
1731 EMIT_ARG(label_assign, break_label);
1732}
1733
1734STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1735 if (n == 0) {
1736 // no more pre-bits, compile the body of the with
1737 compile_node(comp, body);
1738 } else {
1739 uint try_exception_label = comp_next_label(comp);
1740 uint no_reraise_label = comp_next_label(comp);
1741 uint try_else_label = comp_next_label(comp);
1742 uint end_label = comp_next_label(comp);
1743 qstr context;
1744
1745 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1746 // this pre-bit is of the form "a as b"
1747 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1748 compile_node(comp, pns->nodes[0]);
1749 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1750 compile_store_id(comp, context);
1751 compile_load_id(comp, context);
1752 compile_await_object_method(comp, MP_QSTR___aenter__);
1753 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1754 } else {
1755 // this pre-bit is just an expression
1756 compile_node(comp, nodes[0]);
1757 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1758 compile_store_id(comp, context);
1759 compile_load_id(comp, context);
1760 compile_await_object_method(comp, MP_QSTR___aenter__);
1761 EMIT(pop_top);
1762 }
1763
1764 compile_load_id(comp, context);
1765 EMIT_ARG(load_method, MP_QSTR___aexit__);
1766
1767 EMIT_ARG(setup_except, try_exception_label);
1768 compile_increase_except_level(comp);
1769 // compile additional pre-bits and the body
1770 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1771 // finish this with block
1772 EMIT(pop_block);
1773 EMIT_ARG(jump, try_else_label); // jump over exception handler
1774
1775 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1776 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001777
1778 // at this point the stack contains: ..., __aexit__, self, exc
1779 EMIT(dup_top);
1780 #if MICROPY_CPYTHON_COMPAT
1781 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1782 #else
1783 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001784 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001785 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1786 #endif
1787 EMIT(rot_two);
1788 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1789 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001790 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001791
pohmelie81ebba72016-01-27 23:23:11 +03001792 compile_yield_from(comp);
1793 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1794 EMIT_ARG(raise_varargs, 0);
1795
1796 EMIT_ARG(label_assign, no_reraise_label);
1797 EMIT(pop_except);
1798 EMIT_ARG(jump, end_label);
1799
Damien Georgeb32c01b2016-09-28 11:52:13 +10001800 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001801 compile_decrease_except_level(comp);
1802 EMIT(end_finally);
1803 EMIT(end_except_handler);
1804
1805 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1806 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1807 EMIT(dup_top);
1808 EMIT(dup_top);
1809 EMIT_ARG(call_method, 3, 0, 0);
1810 compile_yield_from(comp);
1811 EMIT(pop_top);
1812
1813 EMIT_ARG(label_assign, end_label);
1814
1815 }
1816}
1817
1818STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1819 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1820 mp_parse_node_t *nodes;
1821 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1822 assert(n > 0);
1823
1824 // compile in a nested fashion
1825 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1826}
1827
1828STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1829 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1830 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1831 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1832 // async def
1833 compile_funcdef(comp, pns0);
1834 scope_t *fscope = (scope_t*)pns0->nodes[4];
1835 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1836 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1837 // async for
1838 compile_async_for_stmt(comp, pns0);
1839 } else {
1840 // async with
1841 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1842 compile_async_with_stmt(comp, pns0);
1843 }
1844}
1845#endif
1846
Damien George969a6b32014-12-10 22:07:04 +00001847STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001848 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001849 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1850 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001851 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001852 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001853 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001854 EMIT(pop_top);
1855
Damien429d7192013-10-04 19:53:11 +01001856 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001857 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001858 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001859 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001860 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1861 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001862 // do nothing with a lonely constant
1863 } else {
1864 compile_node(comp, pns->nodes[0]); // just an expression
1865 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1866 }
Damien429d7192013-10-04 19:53:11 +01001867 }
Damien Georgeb948de32015-10-08 14:26:01 +01001868 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001869 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1870 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001871 if (kind == PN_expr_stmt_augassign) {
1872 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1873 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001874 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001875 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001876 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001877 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1878 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1879 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1880 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1881 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1882 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1883 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1884 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1885 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1886 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1887 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001888 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001889 }
Damien George7e5fb242014-02-01 22:18:47 +00001890 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001891 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1892 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001893 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001894 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001895 // following CPython, we store left-most first
1896 if (rhs > 0) {
1897 EMIT(dup_top);
1898 }
1899 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1900 for (int i = 0; i < rhs; i++) {
1901 if (i + 1 < rhs) {
1902 EMIT(dup_top);
1903 }
Damien Georgeb948de32015-10-08 14:26:01 +01001904 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001905 }
Damien George0bb97132015-02-27 14:25:47 +00001906 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001907 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001908 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001909 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001910 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001911 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001912 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001913 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001914 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001915 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001916 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1917 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1918 // can't optimise when it's a star expression on the lhs
1919 goto no_optimisation;
1920 }
Damien429d7192013-10-04 19:53:11 +01001921 compile_node(comp, pns10->nodes[0]); // rhs
1922 compile_node(comp, pns10->nodes[1]); // rhs
1923 EMIT(rot_two);
1924 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1925 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001926 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001927 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001928 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001929 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001930 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001931 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001932 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001933 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001934 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1935 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1936 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1937 // can't optimise when it's a star expression on the lhs
1938 goto no_optimisation;
1939 }
Damien429d7192013-10-04 19:53:11 +01001940 compile_node(comp, pns10->nodes[0]); // rhs
1941 compile_node(comp, pns10->nodes[1]); // rhs
1942 compile_node(comp, pns10->nodes[2]); // rhs
1943 EMIT(rot_three);
1944 EMIT(rot_two);
1945 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1946 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1947 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1948 } else {
Damien George495d7812014-04-08 17:51:47 +01001949 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001950 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001951 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1952 }
Damien429d7192013-10-04 19:53:11 +01001953 }
Damien Georgeb948de32015-10-08 14:26:01 +01001954 } else {
1955 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001956 }
1957}
1958
Damien George6be0b0a2014-08-15 14:30:52 +01001959STATIC 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 +00001960 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001961 compile_node(comp, pns->nodes[0]);
1962 for (int i = 1; i < num_nodes; i += 1) {
1963 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001964 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001965 }
1966}
1967
Damien George969a6b32014-12-10 22:07:04 +00001968STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001969 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1970 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001971
Damien George6f355fd2014-04-10 14:11:31 +01001972 uint l_fail = comp_next_label(comp);
1973 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001974 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1975 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001976 EMIT_ARG(jump, l_end);
1977 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001978 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001979 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001981}
1982
Damien George969a6b32014-12-10 22:07:04 +00001983STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001984 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001985 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001986 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 +01001987 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001988 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001989 }
1990
1991 // get the scope for this lambda
1992 scope_t *this_scope = (scope_t*)pns->nodes[2];
1993
Damien George2c838942015-11-17 14:00:14 +00001994 // compile the lambda definition
1995 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01001996}
1997
Damien George7711afb2015-02-28 15:10:18 +00001998STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01001999 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002000 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002001 for (int i = 0; i < n; i += 1) {
2002 compile_node(comp, pns->nodes[i]);
2003 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002004 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002005 }
2006 }
Damien Georgeb9791222014-01-23 00:34:21 +00002007 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002008}
2009
Damien George7711afb2015-02-28 15:10:18 +00002010STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2011 compile_or_and_test(comp, pns, true);
2012}
2013
Damien George969a6b32014-12-10 22:07:04 +00002014STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002015 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002016}
2017
Damien George969a6b32014-12-10 22:07:04 +00002018STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002019 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002020 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002021}
2022
Damien George969a6b32014-12-10 22:07:04 +00002023STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002024 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002025 compile_node(comp, pns->nodes[0]);
2026 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002027 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002028 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002029 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002030 }
2031 for (int i = 1; i + 1 < num_nodes; i += 2) {
2032 compile_node(comp, pns->nodes[i + 1]);
2033 if (i + 2 < num_nodes) {
2034 EMIT(dup_top);
2035 EMIT(rot_three);
2036 }
Damien George7e5fb242014-02-01 22:18:47 +00002037 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002038 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002039 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002040 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2041 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2042 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2043 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2044 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2045 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002046 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002047 }
2048 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002049 } else {
2050 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002051 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2052 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002053 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002054 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002055 } else {
2056 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002057 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002058 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002059 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002060 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002061 }
Damien429d7192013-10-04 19:53:11 +01002062 }
Damien429d7192013-10-04 19:53:11 +01002063 }
2064 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002065 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002066 }
2067 }
2068 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002069 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002070 EMIT_ARG(jump, l_end);
2071 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002072 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002073 EMIT(rot_two);
2074 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002075 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002076 }
2077}
2078
Damien George969a6b32014-12-10 22:07:04 +00002079STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002080 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002081}
2082
Damien George969a6b32014-12-10 22:07:04 +00002083STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002084 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002085}
2086
Damien George969a6b32014-12-10 22:07:04 +00002087STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002088 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002089}
2090
Damien George969a6b32014-12-10 22:07:04 +00002091STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002092 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002093}
2094
Damien George969a6b32014-12-10 22:07:04 +00002095STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002096 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002097 compile_node(comp, pns->nodes[0]);
2098 for (int i = 1; i + 1 < num_nodes; i += 2) {
2099 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002100 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002101 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002102 } else {
Damien George0bb97132015-02-27 14:25:47 +00002103 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2104 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002105 }
2106 }
2107}
2108
Damien George969a6b32014-12-10 22:07:04 +00002109STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002110 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002111 compile_node(comp, pns->nodes[0]);
2112 for (int i = 1; i + 1 < num_nodes; i += 2) {
2113 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002114 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002115 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002116 } else {
Damien George0bb97132015-02-27 14:25:47 +00002117 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2118 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002119 }
2120 }
2121}
2122
Damien George969a6b32014-12-10 22:07:04 +00002123STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002124 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002125 compile_node(comp, pns->nodes[0]);
2126 for (int i = 1; i + 1 < num_nodes; i += 2) {
2127 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002128 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002129 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002132 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002133 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002134 } else {
Damien George0bb97132015-02-27 14:25:47 +00002135 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2136 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002137 }
2138 }
2139}
2140
Damien George969a6b32014-12-10 22:07:04 +00002141STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002142 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002143 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002144 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002146 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002147 } else {
Damien George0bb97132015-02-27 14:25:47 +00002148 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2149 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002150 }
2151}
2152
pohmelie81ebba72016-01-27 23:23:11 +03002153STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002154 // this is to handle special super() call
2155 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2156
2157 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002158}
Damien George3acaa282016-03-16 13:04:51 +00002159
pohmelie81ebba72016-01-27 23:23:11 +03002160STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2161 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2162 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002163}
2164
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002165STATIC 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 +01002166 // function to call is on top of stack
2167
Damien George35e2a4e2014-02-05 00:51:47 +00002168 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002169 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 +00002170 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002171 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002172 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002173 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002174 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002175 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002176 EMIT_ARG(call_function, 2, 0, 0);
2177 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002178 }
2179 }
Damien George01039b52014-12-21 17:44:27 +00002180 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002181 return;
2182 }
Damien George35e2a4e2014-02-05 00:51:47 +00002183
Damien George2c0842b2014-04-27 16:46:51 +01002184 // get the list of arguments
2185 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002186 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002187
Damien George2c0842b2014-04-27 16:46:51 +01002188 // compile the arguments
2189 // Rather than calling compile_node on the list, we go through the list of args
2190 // explicitly here so that we can count the number of arguments and give sensible
2191 // error messages.
2192 int n_positional = n_positional_extra;
2193 uint n_keyword = 0;
2194 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002195 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002196 for (int i = 0; i < n_args; i++) {
2197 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2198 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2199 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2200 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2201 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2202 return;
2203 }
2204 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002205 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002206 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2207 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2208 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2209 return;
2210 }
2211 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002212 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002213 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002214 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002215 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2216 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2217 return;
2218 }
Damien George59fba2d2015-06-25 14:42:13 +00002219 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002220 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002221 n_keyword += 1;
2222 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002223 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2224 n_positional++;
2225 }
2226 } else {
2227 goto normal_argument;
2228 }
2229 } else {
2230 normal_argument:
2231 if (n_keyword > 0) {
2232 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2233 return;
2234 }
2235 compile_node(comp, args[i]);
2236 n_positional++;
2237 }
Damien429d7192013-10-04 19:53:11 +01002238 }
2239
Delio Brignolie6978a42015-09-17 12:07:06 +02002240 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002241 // if we had one but not the other then we load "null" as a place holder
2242 if (star_flags != 0) {
2243 if (star_args_node == NULL) {
2244 EMIT(load_null);
2245 } else {
2246 compile_node(comp, star_args_node->nodes[0]);
2247 }
2248 if (dblstar_args_node == NULL) {
2249 EMIT(load_null);
2250 } else {
2251 compile_node(comp, dblstar_args_node->nodes[0]);
2252 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002253 }
2254
Damien George2c0842b2014-04-27 16:46:51 +01002255 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002256 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002257 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002258 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002259 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002260 }
Damien429d7192013-10-04 19:53:11 +01002261}
2262
pohmelie81ebba72016-01-27 23:23:11 +03002263STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002264 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002265 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002266 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 +01002267 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002268 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2269 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002270 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002271 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002272 i += 1;
2273 } else {
2274 compile_node(comp, pns->nodes[i]);
2275 }
Damien George35e2a4e2014-02-05 00:51:47 +00002276 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002277 }
2278}
2279
Damien George969a6b32014-12-10 22:07:04 +00002280STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002281 // a list of strings
Damien63321742013-12-10 17:41:49 +00002282
2283 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002284 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002285 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002286 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002287 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002288 int pn_kind;
2289 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2290 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2291 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2292 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2293 } else {
2294 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2295 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002296 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2297 pn_kind = MP_PARSE_NODE_STRING;
2298 } else {
2299 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2300 pn_kind = MP_PARSE_NODE_BYTES;
2301 }
Damien George831137b2015-12-17 13:13:18 +00002302 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002303 }
Damien63321742013-12-10 17:41:49 +00002304 if (i == 0) {
2305 string_kind = pn_kind;
2306 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002307 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002308 return;
2309 }
Damien429d7192013-10-04 19:53:11 +01002310 }
Damien63321742013-12-10 17:41:49 +00002311
Damien George65ef6b72015-01-14 21:17:27 +00002312 // if we are not in the last pass, just load a dummy object
2313 if (comp->pass != MP_PASS_EMIT) {
2314 EMIT_ARG(load_const_obj, mp_const_none);
2315 return;
2316 }
2317
Damien63321742013-12-10 17:41:49 +00002318 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002319 vstr_t vstr;
2320 vstr_init_len(&vstr, n_bytes);
2321 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002322 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002323 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002324 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002325 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2326 memcpy(s_dest, s, s_len);
2327 s_dest += s_len;
2328 } else {
2329 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002330 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2331 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002332 }
Damien63321742013-12-10 17:41:49 +00002333 }
Damien George65ef6b72015-01-14 21:17:27 +00002334
2335 // load the object
Damien George05005f62015-01-21 22:48:37 +00002336 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 +01002337}
2338
2339// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002340STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002341 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2342 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2343 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002344
Damien George36db6bc2014-05-07 17:24:22 +01002345 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002346 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002347 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 +01002348 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002349 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002350 }
2351
2352 // get the scope for this comprehension
2353 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2354
2355 // compile the comprehension
2356 close_over_variables_etc(comp, this_scope, 0, 0);
2357
2358 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2359 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002360 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002361}
2362
Damien George969a6b32014-12-10 22:07:04 +00002363STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002364 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002365 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002366 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002367 } else {
2368 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002369 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2370 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2371 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2372 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2373 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002374 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002375 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002376 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002377 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002378 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002379 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002380 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002381 // generator expression
2382 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2383 } else {
2384 // tuple with 2 items
2385 goto tuple_with_2_items;
2386 }
2387 } else {
2388 // tuple with 2 items
2389 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002390 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002391 }
Damien429d7192013-10-04 19:53:11 +01002392 }
2393}
2394
Damien George969a6b32014-12-10 22:07:04 +00002395STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002396 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002397 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002398 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002399 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2400 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2401 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2402 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2403 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002404 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002405 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002406 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002407 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002408 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002409 // list of many items
2410 compile_node(comp, pns2->nodes[0]);
2411 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002412 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002413 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002414 // list comprehension
2415 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2416 } else {
2417 // list with 2 items
2418 goto list_with_2_items;
2419 }
2420 } else {
2421 // list with 2 items
2422 list_with_2_items:
2423 compile_node(comp, pns2->nodes[0]);
2424 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002425 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002426 }
2427 } else {
2428 // list with 1 item
2429 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002430 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002431 }
2432}
2433
Damien George969a6b32014-12-10 22:07:04 +00002434STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002435 mp_parse_node_t pn = pns->nodes[0];
2436 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002437 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002438 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002439 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2440 pns = (mp_parse_node_struct_t*)pn;
2441 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002442 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002443 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002444 compile_node(comp, pn);
2445 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2447 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2448 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2449 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002450 // dict/set with multiple elements
2451
2452 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002453 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002454 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002455
2456 // first element sets whether it's a dict or set
2457 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002458 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002459 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002461 compile_node(comp, pns->nodes[0]);
2462 EMIT(store_map);
2463 is_dict = true;
2464 } else {
2465 // a set
2466 compile_node(comp, pns->nodes[0]); // 1st value of set
2467 is_dict = false;
2468 }
2469
2470 // process rest of elements
2471 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002472 mp_parse_node_t pn_i = nodes[i];
2473 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2474 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002475 if (is_dict) {
2476 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002477 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002478 return;
2479 }
2480 EMIT(store_map);
2481 } else {
2482 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002483 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002484 return;
2485 }
2486 }
2487 }
2488
Damien Georgee37dcaa2014-12-27 17:07:16 +00002489 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002490 // if it's a set, build it
2491 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002492 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002493 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002494 #endif
Damien George0bb97132015-02-27 14:25:47 +00002495 } else {
2496 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002497 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002498 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002499 // a dictionary comprehension
2500 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2501 } else {
2502 // a set comprehension
2503 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2504 }
Damien429d7192013-10-04 19:53:11 +01002505 }
2506 } else {
2507 // set with one element
2508 goto set_with_one_element;
2509 }
2510 } else {
2511 // set with one element
2512 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002513 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002514 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002516 #else
2517 assert(0);
2518 #endif
Damien429d7192013-10-04 19:53:11 +01002519 }
2520}
2521
Damien George969a6b32014-12-10 22:07:04 +00002522STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002523 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002524}
2525
Damien George969a6b32014-12-10 22:07:04 +00002526STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002527 // object who's index we want is on top of stack
2528 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002529 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002530}
2531
Damien George969a6b32014-12-10 22:07:04 +00002532STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002533 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002534 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002535}
2536
Damien George83204f32014-12-27 17:20:41 +00002537#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002538STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002539 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2540 mp_parse_node_t pn = pns->nodes[0];
2541 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002542 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002543 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2544 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002545 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2546 pns = (mp_parse_node_struct_t*)pn;
2547 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002548 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002549 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002550 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002551 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002552 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002553 } else {
2554 // [?::x]
2555 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002556 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002557 }
Damiend99b0522013-12-21 18:17:45 +00002558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002559 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002560 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2561 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2562 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2563 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002564 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002565 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002566 } else {
2567 // [?:x:x]
2568 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002570 }
2571 } else {
2572 // [?:x]
2573 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002575 }
2576 } else {
2577 // [?:x]
2578 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002580 }
2581}
2582
Damien George969a6b32014-12-10 22:07:04 +00002583STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002584 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002585 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2586 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002587}
2588
Damien George969a6b32014-12-10 22:07:04 +00002589STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002591 compile_subscript_3_helper(comp, pns);
2592}
Damien George83204f32014-12-27 17:20:41 +00002593#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002594
Damien George969a6b32014-12-10 22:07:04 +00002595STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002596 // if this is called then we are compiling a dict key:value pair
2597 compile_node(comp, pns->nodes[1]); // value
2598 compile_node(comp, pns->nodes[0]); // key
2599}
2600
Damien George969a6b32014-12-10 22:07:04 +00002601STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002602 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002603 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002604 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002605}
2606
Damien George969a6b32014-12-10 22:07:04 +00002607STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002608 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002609 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002610 return;
2611 }
Damiend99b0522013-12-21 18:17:45 +00002612 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002613 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002614 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002615 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2616 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002617 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002618 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002619 } else {
2620 compile_node(comp, pns->nodes[0]);
2621 EMIT(yield_value);
2622 }
2623}
2624
pohmelie81ebba72016-01-27 23:23:11 +03002625#if MICROPY_PY_ASYNC_AWAIT
2626STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2627 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2628 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2629 return;
2630 }
2631 compile_atom_expr_normal(comp, pns);
2632 compile_yield_from(comp);
2633}
2634#endif
2635
Damien George65ef6b72015-01-14 21:17:27 +00002636STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2637 // only create and load the actual str object on the last pass
2638 if (comp->pass != MP_PASS_EMIT) {
2639 EMIT_ARG(load_const_obj, mp_const_none);
2640 } else {
Damien George831137b2015-12-17 13:13:18 +00002641 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 +00002642 }
2643}
2644
2645STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2646 // only create and load the actual bytes object on the last pass
2647 if (comp->pass != MP_PASS_EMIT) {
2648 EMIT_ARG(load_const_obj, mp_const_none);
2649 } else {
Damien George831137b2015-12-17 13:13:18 +00002650 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002651 }
2652}
2653
Damien George7d414a12015-02-08 01:57:40 +00002654STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002655 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2656 // nodes are 32-bit pointers, but need to extract 64-bit object
2657 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2658 #else
Damien George7d414a12015-02-08 01:57:40 +00002659 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002660 #endif
Damien George7d414a12015-02-08 01:57:40 +00002661}
2662
Damiend99b0522013-12-21 18:17:45 +00002663typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002664STATIC const compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002665#define nc NULL
2666#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002667#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002668#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002669#undef nc
2670#undef c
2671#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002672 NULL,
2673 compile_string,
2674 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002675 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002676};
2677
Damien George6be0b0a2014-08-15 14:30:52 +01002678STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002679 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002680 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002681 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002682 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002683 #if MICROPY_DYNAMIC_COMPILER
2684 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2685 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2686 // integer fits in target runtime's small-int
2687 EMIT_ARG(load_const_small_int, arg);
2688 } else {
2689 // integer doesn't fit, so create a multi-precision int object
2690 // (but only create the actual object on the last pass)
2691 if (comp->pass != MP_PASS_EMIT) {
2692 EMIT_ARG(load_const_obj, mp_const_none);
2693 } else {
2694 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2695 }
2696 }
2697 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002698 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002699 #endif
Damiend99b0522013-12-21 18:17:45 +00002700 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002701 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002702 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002703 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002704 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2705 case MP_PARSE_NODE_BYTES:
2706 // only create and load the actual bytes object on the last pass
2707 if (comp->pass != MP_PASS_EMIT) {
2708 EMIT_ARG(load_const_obj, mp_const_none);
2709 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002710 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002711 const byte *data = qstr_data(arg, &len);
2712 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2713 }
2714 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002715 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002716 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002717 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002718 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002719 // do nothing
2720 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002721 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002722 }
2723 break;
Damien429d7192013-10-04 19:53:11 +01002724 }
2725 } else {
Damiend99b0522013-12-21 18:17:45 +00002726 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002727 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002728 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002729 assert(f != NULL);
2730 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002731 }
2732}
2733
Damien George2ac4af62014-08-15 16:45:41 +01002734STATIC 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 +00002735 // check that **kw is last
2736 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2737 compile_syntax_error(comp, pn, "invalid syntax");
2738 return;
2739 }
2740
Damien George2827d622014-04-27 15:50:52 +01002741 qstr param_name = MP_QSTR_NULL;
2742 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002743 if (MP_PARSE_NODE_IS_ID(pn)) {
2744 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002745 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002746 // comes after a star, so counts as a keyword-only parameter
2747 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002748 } else {
Damien George2827d622014-04-27 15:50:52 +01002749 // comes before a star, so counts as a positional parameter
2750 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002751 }
Damienb14de212013-10-06 00:28:28 +01002752 } else {
Damiend99b0522013-12-21 18:17:45 +00002753 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2754 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2755 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2756 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002757 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002758 // comes after a star, so counts as a keyword-only parameter
2759 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002760 } else {
Damien George2827d622014-04-27 15:50:52 +01002761 // comes before a star, so counts as a positional parameter
2762 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002763 }
Damiend99b0522013-12-21 18:17:45 +00002764 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002765 if (comp->have_star) {
2766 // more than one star
2767 compile_syntax_error(comp, pn, "invalid syntax");
2768 return;
2769 }
Damien George8b19db02014-04-11 23:25:34 +01002770 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002771 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002772 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002773 // bare star
2774 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002775 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002776 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002777 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002778 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002779 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002780 } else {
2781 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002782 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002783 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002784 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2785 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002786 }
Damien George0bb97132015-02-27 14:25:47 +00002787 } else {
2788 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002789 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002790 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002791 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002792 }
Damien429d7192013-10-04 19:53:11 +01002793 }
2794
Damien George2827d622014-04-27 15:50:52 +01002795 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002796 bool added;
2797 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2798 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002799 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002800 return;
2801 }
Damien429d7192013-10-04 19:53:11 +01002802 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002803 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002804 }
2805}
2806
Damien George2827d622014-04-27 15:50:52 +01002807STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002808 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002809}
2810
Damien George2827d622014-04-27 15:50:52 +01002811STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002812 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2813}
2814
Damien Georgeea5b59b2015-09-04 16:44:14 +01002815#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002816STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2817 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2818 // no annotation
2819 return;
2820 }
2821
2822 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2823 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2824 // named parameter with possible annotation
2825 // fallthrough
2826 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2827 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2828 // named star with possible annotation
2829 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2830 // fallthrough
2831 } else {
2832 // no annotation
2833 return;
2834 }
Damien Georgee49153f2016-10-11 12:29:54 +11002835 } else {
2836 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002837 // double star with possible annotation
2838 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002839 }
2840
2841 mp_parse_node_t pn_annotation = pns->nodes[1];
2842
2843 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002844 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2845 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2846 assert(id_info != NULL);
2847
Damien Georgeea5b59b2015-09-04 16:44:14 +01002848 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2849 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2850 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2851 } else {
2852 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002853 }
Damien George2ac4af62014-08-15 16:45:41 +01002854 }
Damien429d7192013-10-04 19:53:11 +01002855}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002856#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002857
Damien Georgea8312432015-12-18 01:37:55 +00002858STATIC 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) {
2859 uint l_top = comp_next_label(comp);
2860 uint l_end = comp_next_label(comp);
2861 EMIT_ARG(label_assign, l_top);
2862 EMIT_ARG(for_iter, l_end);
2863 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2864 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2865
Damien429d7192013-10-04 19:53:11 +01002866 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002867 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002868 // no more nested if/for; compile inner expression
2869 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002870 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002871 EMIT(yield_value);
2872 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002873 } else {
2874 EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002875 }
Damiend99b0522013-12-21 18:17:45 +00002876 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002877 // if condition
Damiend99b0522013-12-21 18:17:45 +00002878 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002879 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2880 pn_iter = pns_comp_if->nodes[1];
2881 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002882 } else {
2883 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002884 // for loop
Damiend99b0522013-12-21 18:17:45 +00002885 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002886 compile_node(comp, pns_comp_for2->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002887 EMIT(get_iter);
Damien Georgea8312432015-12-18 01:37:55 +00002888 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002889 }
Damien Georgea8312432015-12-18 01:37:55 +00002890
2891 EMIT_ARG(jump, l_top);
2892 EMIT_ARG(label_assign, l_end);
2893 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002894}
2895
Damien George1463c1f2014-04-25 23:52:57 +01002896STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002897#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002898 // see http://www.python.org/dev/peps/pep-0257/
2899
2900 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002901 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002902 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002903 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002904 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002905 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2906 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002907 for (int i = 0; i < num_nodes; i++) {
2908 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002909 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 +00002910 // not a newline, so this is the first statement; finish search
2911 break;
2912 }
2913 }
2914 // 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 +00002915 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002916 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002917 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002918 } else {
2919 return;
2920 }
2921
2922 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002923 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002924 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002925 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2926 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2927 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2928 // compile the doc string
2929 compile_node(comp, pns->nodes[0]);
2930 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002931 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002932 }
2933 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002934#else
2935 (void)comp;
2936 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002937#endif
Damien429d7192013-10-04 19:53:11 +01002938}
2939
Damien George1463c1f2014-04-25 23:52:57 +01002940STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002941 comp->pass = pass;
2942 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002943 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002945
Damien George36db6bc2014-05-07 17:24:22 +01002946 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002947 // reset maximum stack sizes in scope
2948 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002949 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002950 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002951 }
2952
Damien429d7192013-10-04 19:53:11 +01002953 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002954 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2955 assert(scope->kind == SCOPE_MODULE);
2956 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2957 compile_node(comp, pns->nodes[0]); // compile the expression
2958 EMIT(return_value);
2959 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002960 if (!comp->is_repl) {
2961 check_for_doc_string(comp, scope->pn);
2962 }
Damien429d7192013-10-04 19:53:11 +01002963 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002964 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002965 EMIT(return_value);
2966 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002967 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2968 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2969 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002970
2971 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002972 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002973 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002974 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002975 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002976 }
2977 #if MICROPY_EMIT_NATIVE
2978 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002979 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002980 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002981
Damien George2ac4af62014-08-15 16:45:41 +01002982 // argument annotations
2983 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2984
2985 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002986 mp_parse_node_t pn_annotation = pns->nodes[2];
2987 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002988 // nodes[2] can be null or a test-expr
2989 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2990 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2991 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2992 } else {
2993 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002994 }
2995 }
Damien George2ac4af62014-08-15 16:45:41 +01002996 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002997 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002998
2999 compile_node(comp, pns->nodes[3]); // 3 is function body
3000 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003001 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003002 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003003 EMIT(return_value);
3004 }
3005 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003006 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3007 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3008 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003009
3010 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003011 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003012 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003013 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003014 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3015 }
3016
3017 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003018
3019 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3020 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3021 EMIT(pop_top);
3022 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3023 }
Damien429d7192013-10-04 19:53:11 +01003024 EMIT(return_value);
3025 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3026 // a bit of a hack at the moment
3027
Damiend99b0522013-12-21 18:17:45 +00003028 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3029 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3030 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3031 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3032 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003033
Damien George708c0732014-04-27 19:23:46 +01003034 // We need a unique name for the comprehension argument (the iterator).
3035 // CPython uses .0, but we should be able to use anything that won't
3036 // clash with a user defined variable. Best to use an existing qstr,
3037 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003038 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003039 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003040 bool added;
3041 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3042 assert(added);
3043 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003044 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003045 }
3046
3047 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003048 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003049 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003050 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003051 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003052 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003054 #endif
Damien429d7192013-10-04 19:53:11 +01003055 }
3056
Damien George542bd6b2015-03-26 14:42:40 +00003057 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00003058 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003059
3060 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003061 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003062 }
3063 EMIT(return_value);
3064 } else {
3065 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003066 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3067 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3068 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003069
Damien George36db6bc2014-05-07 17:24:22 +01003070 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003071 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003072 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003073 assert(added);
3074 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003075 }
3076
Damien George542bd6b2015-03-26 14:42:40 +00003077 compile_load_id(comp, MP_QSTR___name__);
3078 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003079 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003080 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003081
3082 check_for_doc_string(comp, pns->nodes[2]);
3083 compile_node(comp, pns->nodes[2]); // 2 is class body
3084
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003085 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003086 assert(id != NULL);
3087 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003088 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003089 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003090 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003091 }
3092 EMIT(return_value);
3093 }
3094
Damien415eb6f2013-10-05 12:19:06 +01003095 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003096
3097 // make sure we match all the exception levels
3098 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003099}
3100
Damien George094d4502014-04-02 17:31:27 +01003101#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003102// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003103STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003104 comp->pass = pass;
3105 comp->scope_cur = scope;
3106 comp->next_label = 1;
3107
3108 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003109 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003110 return;
3111 }
3112
Damien George36db6bc2014-05-07 17:24:22 +01003113 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003114 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003115 }
3116
Damien826005c2013-10-05 23:17:28 +01003117 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003118 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3119 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3120 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003121
Damiend99b0522013-12-21 18:17:45 +00003122 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003123
Damiena2f2f7d2013-10-06 00:14:13 +01003124 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003125 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003126 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003127 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003128 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003129 if (comp->compile_error != MP_OBJ_NULL) {
3130 goto inline_asm_error;
3131 }
Damiena2f2f7d2013-10-06 00:14:13 +01003132 }
3133
Damien George8f54c082016-01-15 15:20:43 +00003134 // pns->nodes[2] is function return annotation
3135 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3136 mp_parse_node_t pn_annotation = pns->nodes[2];
3137 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3138 // nodes[2] can be null or a test-expr
3139 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3140 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3141 switch (ret_type) {
3142 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3143 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3144 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3145 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3146 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3147 }
3148 } else {
3149 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3150 }
3151 }
Damien826005c2013-10-05 23:17:28 +01003152
Damiend99b0522013-12-21 18:17:45 +00003153 mp_parse_node_t pn_body = pns->nodes[3]; // body
3154 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003155 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003156
Damien826005c2013-10-05 23:17:28 +01003157 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003158 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3159 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003160 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3161 // no instructions
3162 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003163 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003164 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003165 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003166 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003167 return;
3168 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003169
3170 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003171 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003172 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3173 goto not_an_instruction;
3174 }
Damiend99b0522013-12-21 18:17:45 +00003175 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003176 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003177 goto not_an_instruction;
3178 }
3179 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3180 goto not_an_instruction;
3181 }
3182 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3183 goto not_an_instruction;
3184 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003185
3186 // parse node looks like an instruction
3187 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003188 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3189 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3190 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003191 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003192
3193 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003194 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003195 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003196 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003197 return;
3198 }
Damien George6f355fd2014-04-10 14:11:31 +01003199 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003200 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003201 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3202 compile_syntax_error(comp, nodes[i], "label redefined");
3203 return;
3204 }
Damien826005c2013-10-05 23:17:28 +01003205 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003206 } else if (op == MP_QSTR_align) {
3207 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003208 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003209 return;
3210 }
Damien George36db6bc2014-05-07 17:24:22 +01003211 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003212 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3213 }
3214 } else if (op == MP_QSTR_data) {
3215 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003216 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003217 return;
3218 }
Damien George36db6bc2014-05-07 17:24:22 +01003219 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003220 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003221 for (uint j = 1; j < n_args; j++) {
3222 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003223 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003224 return;
3225 }
Damien George50912e72015-01-20 11:55:10 +00003226 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003227 }
3228 }
Damien826005c2013-10-05 23:17:28 +01003229 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003230 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003231 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003232 }
3233 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003234
3235 if (comp->compile_error != MP_OBJ_NULL) {
3236 pns = pns2; // this is the parse node that had the error
3237 goto inline_asm_error;
3238 }
Damien826005c2013-10-05 23:17:28 +01003239 }
3240
Damien George36db6bc2014-05-07 17:24:22 +01003241 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003242 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien George8dfbd2d2015-02-13 01:00:51 +00003243 }
3244
3245 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003246 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003247 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003248 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003249 }
Damien429d7192013-10-04 19:53:11 +01003250}
Damien George094d4502014-04-02 17:31:27 +01003251#endif
Damien429d7192013-10-04 19:53:11 +01003252
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003253STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003254 // in Micro Python we put the *x parameter after all other parameters (except **y)
3255 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3256 id_info_t *id_param = NULL;
3257 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3258 id_info_t *id = &scope->id_info[i];
3259 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3260 if (id_param != NULL) {
3261 // swap star param with last param
3262 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3263 }
3264 break;
3265 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3266 id_param = id;
3267 }
3268 }
3269 }
Damien George2827d622014-04-27 15:50:52 +01003270
Damien429d7192013-10-04 19:53:11 +01003271 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003272 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003273 scope->num_locals = 0;
3274 for (int i = 0; i < scope->id_info_len; i++) {
3275 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003276 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003277 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3278 continue;
3279 }
Damien George3dea8c92016-09-30 12:34:05 +10003280 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003281 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3282 }
Damien George2827d622014-04-27 15:50:52 +01003283 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003284 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003285 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003286 }
3287 }
3288
Damien George65dc9602015-08-14 12:24:11 +01003289 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003290 for (int i = 0; i < scope->id_info_len; i++) {
3291 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003292 // in Micro Python the cells come right after the fast locals
3293 // parameters are not counted here, since they remain at the start
3294 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003295 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003296 id->local_num = scope->num_locals;
3297 scope->num_locals += 1;
3298 }
Damien9ecbcff2013-12-11 00:41:43 +00003299 }
Damien9ecbcff2013-12-11 00:41:43 +00003300
Damien George65dc9602015-08-14 12:24:11 +01003301 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003302 // make sure they are in the order of the parent scope
3303 if (scope->parent != NULL) {
3304 int num_free = 0;
3305 for (int i = 0; i < scope->parent->id_info_len; i++) {
3306 id_info_t *id = &scope->parent->id_info[i];
3307 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3308 for (int j = 0; j < scope->id_info_len; j++) {
3309 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003310 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003311 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003312 // in Micro Python the frees come first, before the params
3313 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003314 num_free += 1;
3315 }
3316 }
3317 }
Damien429d7192013-10-04 19:53:11 +01003318 }
Damien George6baf76e2013-12-30 22:32:17 +00003319 // in Micro Python shift all other locals after the free locals
3320 if (num_free > 0) {
3321 for (int i = 0; i < scope->id_info_len; i++) {
3322 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003323 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003324 id->local_num += num_free;
3325 }
3326 }
Damien George2827d622014-04-27 15:50:52 +01003327 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 +00003328 scope->num_locals += num_free;
3329 }
Damien429d7192013-10-04 19:53:11 +01003330 }
Damien429d7192013-10-04 19:53:11 +01003331}
3332
Damien Georged4dba882015-11-13 13:38:28 +00003333#if !MICROPY_PERSISTENT_CODE_SAVE
3334STATIC
3335#endif
3336mp_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 +01003337 // put compiler state on the stack, it's relatively small
3338 compiler_t comp_state = {0};
3339 compiler_t *comp = &comp_state;
3340
Damien Georgecbd2f742014-01-19 11:48:48 +00003341 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003342 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003343
Damien George62a3a282015-03-01 12:04:05 +00003344 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003345 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003346
Damien Georgea210c772015-03-26 15:49:53 +00003347 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003348 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003349
Damien826005c2013-10-05 23:17:28 +01003350 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003351 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003352 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003353 comp->emit_method_table = &emit_bc_method_table;
3354 #endif
Damien826005c2013-10-05 23:17:28 +01003355 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003356 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003357 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003358#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003359 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003360 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003361#endif
Damien826005c2013-10-05 23:17:28 +01003362 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003363 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003364 }
3365
3366 // update maximim number of labels needed
3367 if (comp->next_label > max_num_labels) {
3368 max_num_labels = comp->next_label;
3369 }
Damien429d7192013-10-04 19:53:11 +01003370 }
3371
Damien826005c2013-10-05 23:17:28 +01003372 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003373 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 +00003374 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003375 }
3376
Damien Georgea210c772015-03-26 15:49:53 +00003377 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003378 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003379
Damien826005c2013-10-05 23:17:28 +01003380 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003381#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003382 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003383#endif
Damien3ef4abb2013-10-12 16:53:13 +01003384#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003385 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003386#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003387 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003388 if (false) {
3389 // dummy
3390
Damien3ef4abb2013-10-12 16:53:13 +01003391#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003392 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003393 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003394 if (emit_inline_thumb == NULL) {
3395 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3396 }
3397 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003398 comp->emit_inline_asm = emit_inline_thumb;
3399 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003400 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003401 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003402 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003403 }
Damienc025ebb2013-10-12 14:30:21 +01003404#endif
3405
Damien826005c2013-10-05 23:17:28 +01003406 } else {
Damienc025ebb2013-10-12 14:30:21 +01003407
3408 // choose the emit type
3409
Damien826005c2013-10-05 23:17:28 +01003410 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003411
3412#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003413 case MP_EMIT_OPT_NATIVE_PYTHON:
3414 case MP_EMIT_OPT_VIPER:
Damiendc833822013-10-06 01:01:01 +01003415 if (emit_native == NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003416 emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003417 }
Damien George080a78b2016-12-07 11:17:17 +11003418 comp->emit_method_table = &NATIVE_EMITTER(method_table);
Damienc025ebb2013-10-12 14:30:21 +01003419 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003420 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003421 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003422#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003423
Damien826005c2013-10-05 23:17:28 +01003424 default:
Damien826005c2013-10-05 23:17:28 +01003425 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003426 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003427 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003428 #endif
Damien826005c2013-10-05 23:17:28 +01003429 break;
3430 }
Damienc025ebb2013-10-12 14:30:21 +01003431
Damien George1e1779e2015-01-14 00:20:28 +00003432 // need a pass to compute stack size
3433 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3434
Damien George36db6bc2014-05-07 17:24:22 +01003435 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003436 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003437 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3438 }
3439
3440 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003441 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003442 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003443 }
Damien6cdd3af2013-10-05 18:08:26 +01003444 }
Damien429d7192013-10-04 19:53:11 +01003445 }
3446
Damien Georgecfc4c332015-07-29 22:16:01 +00003447 if (comp->compile_error != MP_OBJ_NULL) {
3448 // if there is no line number for the error then use the line
3449 // number for the start of this scope
3450 compile_error_set_line(comp, comp->scope_cur->pn);
3451 // add a traceback to the exception using relevant source info
3452 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3453 comp->compile_error_line, comp->scope_cur->simple_name);
3454 }
3455
Damien George41d02b62014-01-24 22:42:28 +00003456 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003457
Damien Georgea210c772015-03-26 15:49:53 +00003458 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003459#if MICROPY_EMIT_NATIVE
3460 if (emit_native != NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003461 NATIVE_EMITTER(free)(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003462 }
3463#endif
3464#if MICROPY_EMIT_INLINE_THUMB
3465 if (emit_inline_thumb != NULL) {
3466 emit_inline_thumb_free(emit_inline_thumb);
3467 }
3468#endif
Damien George41d02b62014-01-24 22:42:28 +00003469
Damien George52b5d762014-09-23 15:31:56 +00003470 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003471 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003472
Damien George41d02b62014-01-24 22:42:28 +00003473 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003474 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003475 for (scope_t *s = module_scope; s;) {
3476 scope_t *next = s->next;
3477 scope_free(s);
3478 s = next;
3479 }
Damien5ac1b2e2013-10-18 19:58:12 +01003480
Damien George9d5e5c02015-09-24 13:15:57 +01003481 if (comp->compile_error != MP_OBJ_NULL) {
3482 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003483 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003484 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003485 }
Damien429d7192013-10-04 19:53:11 +01003486}
Damien Georged4dba882015-11-13 13:38:28 +00003487
3488mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3489 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3490 // return function that executes the outer module
3491 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3492}
Damien Georgedd5353a2015-12-18 12:35:44 +00003493
3494#endif // MICROPY_ENABLE_COMPILER