blob: 3fb45cd69c51c45b47b7677b883a8b9ccd3257ed [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"
Damien Georgedd53b122016-12-09 20:54:54 +110037#include "py/asmbase.h"
Damien429d7192013-10-04 19:53:11 +010038
Damien Georgedd5353a2015-12-18 12:35:44 +000039#if MICROPY_ENABLE_COMPILER
40
Damien429d7192013-10-04 19:53:11 +010041// TODO need to mangle __attr names
42
43typedef enum {
Damien George71019ae2017-02-15 10:58:05 +110044// define rules with a compile function
Damien George00208ce2014-01-23 00:00:53 +000045#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George71019ae2017-02-15 10:58:05 +110046#define DEF_RULE_NC(rule, kind, ...)
Damien George51dfcb42015-01-01 20:27:54 +000047#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010048#undef DEF_RULE
Damien George71019ae2017-02-15 10:58:05 +110049#undef DEF_RULE_NC
Damien George5042bce2014-05-25 22:06:06 +010050 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000051 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000052 PN_const_object, // special node for a constant, generic Python object
Damien George71019ae2017-02-15 10:58:05 +110053// define rules without a compile function
54#define DEF_RULE(rule, comp, kind, ...)
55#define DEF_RULE_NC(rule, kind, ...) PN_##rule,
56#include "py/grammar.h"
57#undef DEF_RULE
58#undef DEF_RULE_NC
Damien429d7192013-10-04 19:53:11 +010059} pn_kind_t;
60
Damien George65dc9602015-08-14 12:24:11 +010061#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000062
63#if NEED_METHOD_TABLE
64
65// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000066#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
67#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000068#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
69#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010070
Damien George41125902015-03-26 16:44:14 +000071#else
72
73// if we only have the bytecode emitter enabled then we can do a direct call to the functions
74#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
75#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
76#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
77#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
78
79#endif
80
Damien George080a78b2016-12-07 11:17:17 +110081#if MICROPY_EMIT_NATIVE
82// define a macro to access external native emitter
83#if MICROPY_EMIT_X64
84#define NATIVE_EMITTER(f) emit_native_x64_##f
85#elif MICROPY_EMIT_X86
86#define NATIVE_EMITTER(f) emit_native_x86_##f
87#elif MICROPY_EMIT_THUMB
88#define NATIVE_EMITTER(f) emit_native_thumb_##f
89#elif MICROPY_EMIT_ARM
90#define NATIVE_EMITTER(f) emit_native_arm_##f
Damien George8e5aced2016-12-09 16:39:39 +110091#elif MICROPY_EMIT_XTENSA
92#define NATIVE_EMITTER(f) emit_native_xtensa_##f
Damien George080a78b2016-12-07 11:17:17 +110093#else
94#error "unknown native emitter"
95#endif
96#endif
97
Damien Georgead297a12016-12-09 13:17:49 +110098#if MICROPY_EMIT_INLINE_ASM
99// define macros for inline assembler
100#if MICROPY_EMIT_INLINE_THUMB
101#define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
102#define ASM_EMITTER(f) emit_inline_thumb_##f
Damien Georgef76b1bf2016-12-09 17:03:33 +1100103#elif MICROPY_EMIT_INLINE_XTENSA
104#define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa
105#define ASM_EMITTER(f) emit_inline_xtensa_##f
Damien Georgead297a12016-12-09 13:17:49 +1100106#else
107#error "unknown asm emitter"
108#endif
109#endif
110
Damien George0496de22015-10-03 17:07:54 +0100111#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
112#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
113
Damien Georgea91ac202014-10-05 19:01:34 +0100114// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +0100115typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +0000116 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +0100117
Damien George78035b92014-04-09 12:27:39 +0100118 uint8_t is_repl;
119 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +0100120 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +0100121 uint8_t have_star;
122
123 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +0000124 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +0000125 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +0100126
Damien George6f355fd2014-04-10 14:11:31 +0100127 uint next_label;
Damienb05d7072013-10-05 13:37:10 +0100128
Damien George69b89d22014-04-11 13:38:30 +0000129 uint16_t num_dict_params;
130 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +0100131
Damien Georgea91ac202014-10-05 19:01:34 +0100132 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
133 uint16_t continue_label;
134 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +0000135 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +0100136
Damien429d7192013-10-04 19:53:11 +0100137 scope_t *scope_head;
138 scope_t *scope_cur;
139
Damien6cdd3af2013-10-05 18:08:26 +0100140 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000141 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100142 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000143 #endif
Damien826005c2013-10-05 23:17:28 +0100144
Damien Georgead297a12016-12-09 13:17:49 +1100145 #if MICROPY_EMIT_INLINE_ASM
Damien826005c2013-10-05 23:17:28 +0100146 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
147 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 +0000148 #endif
Damien429d7192013-10-04 19:53:11 +0100149} compiler_t;
150
Damien Georgecfc4c332015-07-29 22:16:01 +0000151STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
152 // if the line of the error is unknown then try to update it from the pn
153 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000154 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100155 }
Damien George84d59c22015-07-27 22:20:00 +0100156}
157
158STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000159 // only register the error if there has been no other error
160 if (comp->compile_error == MP_OBJ_NULL) {
161 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
162 compile_error_set_line(comp, pn);
163 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000164}
165
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200166STATIC 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 +0100167STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000168STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100169
Damien George6f355fd2014-04-10 14:11:31 +0100170STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100171 return comp->next_label++;
172}
173
Damien George8dcc0c72014-03-27 10:55:21 +0000174STATIC void compile_increase_except_level(compiler_t *comp) {
175 comp->cur_except_level += 1;
176 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
177 comp->scope_cur->exc_stack_size = comp->cur_except_level;
178 }
179}
180
181STATIC void compile_decrease_except_level(compiler_t *comp) {
182 assert(comp->cur_except_level > 0);
183 comp->cur_except_level -= 1;
184}
185
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200186STATIC 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 +0100187 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100188 scope->parent = comp->scope_cur;
189 scope->next = NULL;
190 if (comp->scope_head == NULL) {
191 comp->scope_head = scope;
192 } else {
193 scope_t *s = comp->scope_head;
194 while (s->next != NULL) {
195 s = s->next;
196 }
197 s->next = scope;
198 }
199 return scope;
200}
201
Damien George91bc32d2015-04-09 15:31:53 +0000202typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
203
204STATIC 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 +0000205 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
207 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100208 for (int i = 0; i < num_nodes; i++) {
209 f(comp, pns->nodes[i]);
210 }
Damiend99b0522013-12-21 18:17:45 +0000211 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100212 f(comp, pn);
213 }
214}
215
Damien George969a6b32014-12-10 22:07:04 +0000216STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000217 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100218 for (int i = 0; i < num_nodes; i++) {
219 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000220 if (comp->compile_error != MP_OBJ_NULL) {
221 // add line info for the error in case it didn't have a line number
222 compile_error_set_line(comp, pns->nodes[i]);
223 return;
224 }
Damien429d7192013-10-04 19:53:11 +0100225 }
226}
227
Damien George542bd6b2015-03-26 14:42:40 +0000228STATIC void compile_load_id(compiler_t *comp, qstr qst) {
229 if (comp->pass == MP_PASS_SCOPE) {
230 mp_emit_common_get_id_for_load(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->load_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_load_id_ops, comp->scope_cur, qst);
236 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000237 }
238}
239
240STATIC void compile_store_id(compiler_t *comp, qstr qst) {
241 if (comp->pass == MP_PASS_SCOPE) {
242 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
243 } else {
Damien George41125902015-03-26 16:44:14 +0000244 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000245 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000246 #else
247 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
248 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000249 }
250}
251
252STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
253 if (comp->pass == MP_PASS_SCOPE) {
254 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
255 } else {
Damien George41125902015-03-26 16:44:14 +0000256 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000257 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000258 #else
259 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
260 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000261 }
262}
263
Damien George2c0842b2014-04-27 16:46:51 +0100264STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100265 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000266 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100267 compile_node(comp, pn);
268 total += 1;
269 }
270 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000271 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100272 for (int i = 0; i < n; i++) {
273 compile_node(comp, pns_list->nodes[i]);
274 }
275 total += n;
276 }
Damien Georgeb9791222014-01-23 00:34:21 +0000277 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100278}
Damien429d7192013-10-04 19:53:11 +0100279
Damien George969a6b32014-12-10 22:07:04 +0000280STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100281 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000282 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100283}
284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100286 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100287 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000288 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100289 }
290 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100291 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100292 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000293 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100294 }
295 return;
Damiend99b0522013-12-21 18:17:45 +0000296 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
297 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
298 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
299 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100300 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000301 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100302 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100303 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000304 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100305 }
Damien George0b2fd912015-02-28 14:37:54 +0000306 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000307 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100308 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000309 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100310 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000311 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100312 }
313 }
314 return;
Damiend99b0522013-12-21 18:17:45 +0000315 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100316 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000317 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100318 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000319 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100320 }
Damiend99b0522013-12-21 18:17:45 +0000321 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100322 c_if_cond(comp, pns->nodes[0], !jump_if, label);
323 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100324 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
325 // cond is something in parenthesis
326 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
327 // empty tuple, acts as false for the condition
328 if (jump_if == false) {
329 EMIT_ARG(jump, label);
330 }
Damien George93b37262016-01-07 13:07:52 +0000331 } else {
332 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100333 // non-empty tuple, acts as true for the condition
334 if (jump_if == true) {
335 EMIT_ARG(jump, label);
336 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100337 }
338 return;
Damien3a205172013-10-12 15:01:56 +0100339 }
340 }
341
342 // nothing special, fall back to default compiling for node and jump
343 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000344 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100345}
346
347typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100348STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100349
pohmelie81ebba72016-01-27 23:23:11 +0300350STATIC 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 +0100351 if (assign_kind != ASSIGN_AUG_STORE) {
352 compile_node(comp, pns->nodes[0]);
353 }
354
Damiend99b0522013-12-21 18:17:45 +0000355 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
356 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300357 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000358 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100359 if (assign_kind != ASSIGN_AUG_STORE) {
360 for (int i = 0; i < n - 1; i++) {
361 compile_node(comp, pns1->nodes[i]);
362 }
363 }
Damiend99b0522013-12-21 18:17:45 +0000364 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
365 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100366 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000367 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100368 if (assign_kind == ASSIGN_AUG_STORE) {
369 EMIT(rot_three);
370 EMIT(store_subscr);
371 } else {
372 compile_node(comp, pns1->nodes[0]);
373 if (assign_kind == ASSIGN_AUG_LOAD) {
374 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100375 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100376 } else {
377 EMIT(store_subscr);
378 }
379 }
Damiend99b0522013-12-21 18:17:45 +0000380 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
381 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100382 if (assign_kind == ASSIGN_AUG_LOAD) {
383 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000384 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100385 } else {
386 if (assign_kind == ASSIGN_AUG_STORE) {
387 EMIT(rot_two);
388 }
Damien Georgeb9791222014-01-23 00:34:21 +0000389 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100390 }
391 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100392 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100393 }
394 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100395 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100396 }
397
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100398 return;
399
400cannot_assign:
401 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100402}
403
Damien George0288cf02014-04-11 11:53:00 +0000404// 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 +0100405STATIC 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 +0000406 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
407
408 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000409 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000410 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
411 EMIT_ARG(unpack_ex, 0, num_tail);
412 have_star_index = 0;
413 }
Damien George963a5a32015-01-16 17:47:07 +0000414 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000415 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000416 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000417 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
418 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100419 } else {
Damien George9d181f62014-04-27 16:55:27 +0100420 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100421 return;
422 }
423 }
424 }
Damien George963a5a32015-01-16 17:47:07 +0000425 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000426 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100427 }
Damien George0288cf02014-04-11 11:53:00 +0000428 if (num_head != 0) {
429 if (0 == have_star_index) {
430 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100431 } else {
Damien George0288cf02014-04-11 11:53:00 +0000432 c_assign(comp, node_head, ASSIGN_STORE);
433 }
434 }
Damien George963a5a32015-01-16 17:47:07 +0000435 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000436 if (num_head + i == have_star_index) {
437 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
438 } else {
439 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100440 }
441 }
442}
443
444// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100445STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000446 assert(!MP_PARSE_NODE_IS_NULL(pn));
447 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000448 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000449 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100450 switch (assign_kind) {
451 case ASSIGN_STORE:
452 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000453 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100454 break;
455 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000456 default:
Damien George542bd6b2015-03-26 14:42:40 +0000457 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100458 break;
459 }
460 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100461 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100462 return;
463 }
464 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000465 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000466 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
467 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300468 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100469 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300470 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100471 break;
472
473 case PN_testlist_star_expr:
474 case PN_exprlist:
475 // lhs is a tuple
476 if (assign_kind != ASSIGN_STORE) {
477 goto bad_aug;
478 }
Damien George0288cf02014-04-11 11:53:00 +0000479 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100480 break;
481
482 case PN_atom_paren:
483 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000484 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100485 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100486 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000487 } else {
488 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000489 if (assign_kind != ASSIGN_STORE) {
490 goto bad_aug;
491 }
Damiend99b0522013-12-21 18:17:45 +0000492 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100493 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100494 }
495 break;
496
497 case PN_atom_bracket:
498 // lhs is something in brackets
499 if (assign_kind != ASSIGN_STORE) {
500 goto bad_aug;
501 }
Damiend99b0522013-12-21 18:17:45 +0000502 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100503 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000504 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000505 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
506 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100507 goto testlist_comp;
508 } else {
509 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000510 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100511 }
512 break;
513
514 default:
Damien George9d181f62014-04-27 16:55:27 +0100515 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100516 }
517 return;
518
519 testlist_comp:
520 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000521 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
522 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
523 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100524 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000525 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000526 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100528 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000529 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
530 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000531 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien George9d181f62014-04-27 16:55:27 +0100532 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100533 } else {
534 // sequence with 2 items
535 goto sequence_with_2_items;
536 }
537 } else {
538 // sequence with 2 items
539 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000540 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100541 }
542 return;
543 }
544 return;
545
Damien George9d181f62014-04-27 16:55:27 +0100546 cannot_assign:
547 compile_syntax_error(comp, pn, "can't assign to expression");
548 return;
549
Damien429d7192013-10-04 19:53:11 +0100550 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100551 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100552}
553
Damien George65dc9602015-08-14 12:24:11 +0100554// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100555// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
556// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
557// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100558STATIC 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 +0100559 assert(n_pos_defaults >= 0);
560 assert(n_kw_defaults >= 0);
561
Damien George3a3db4d2015-10-22 23:45:37 +0100562 // set flags
563 if (n_kw_defaults > 0) {
564 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
565 }
566 this_scope->num_def_pos_args = n_pos_defaults;
567
Damien429d7192013-10-04 19:53:11 +0100568 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000569 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100570 int nfree = 0;
571 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000572 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
573 id_info_t *id = &comp->scope_cur->id_info[i];
574 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
575 for (int j = 0; j < this_scope->id_info_len; j++) {
576 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100577 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000578 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000579 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000580 nfree += 1;
581 }
582 }
Damien429d7192013-10-04 19:53:11 +0100583 }
584 }
585 }
Damien429d7192013-10-04 19:53:11 +0100586
587 // make the function/closure
588 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100589 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100590 } else {
Damien George3558f622014-04-20 17:50:40 +0100591 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100592 }
593}
594
Damien George2c838942015-11-17 14:00:14 +0000595STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
596 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
597 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100598 comp->have_star = true;
599 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000600 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
601 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100602 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100603 } else {
604 // named star
Damien429d7192013-10-04 19:53:11 +0100605 }
Damien George8b19db02014-04-11 23:25:34 +0100606 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000607
Damien George2c838942015-11-17 14:00:14 +0000608 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
609 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100610 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000611 // TODO do we need to do anything with this?
612
613 } else {
614 mp_parse_node_t pn_id;
615 mp_parse_node_t pn_colon;
616 mp_parse_node_t pn_equal;
617 if (MP_PARSE_NODE_IS_ID(pn)) {
618 // this parameter is just an id
619
620 pn_id = pn;
621 pn_colon = MP_PARSE_NODE_NULL;
622 pn_equal = MP_PARSE_NODE_NULL;
623
Damien George2c838942015-11-17 14:00:14 +0000624 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000625 // this parameter has a colon and/or equal specifier
626
627 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
628 pn_id = pns->nodes[0];
629 pn_colon = pns->nodes[1];
630 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000631
632 } else {
633 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
634 // this parameter has an equal specifier
635
636 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
637 pn_id = pns->nodes[0];
638 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000639 }
640
641 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
642 // this parameter does not have a default value
643
644 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100645 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100646 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000647 return;
648 }
649
650 } else {
651 // this parameter has a default value
652 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
653
Damien George8b19db02014-04-11 23:25:34 +0100654 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000655 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000656 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
657 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100658 // in Micro Python we put the default positional parameters into a tuple using the bytecode
659 // we need to do this here before we start building the map for the default keywords
660 if (comp->num_default_params > 0) {
661 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100662 } else {
663 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100664 }
Damien George69b89d22014-04-11 13:38:30 +0000665 // first default dict param, so make the map
666 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000667 }
Damien Georgef0778a72014-06-07 22:01:00 +0100668
669 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000670 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000671 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000672 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000673 } else {
Damien George69b89d22014-04-11 13:38:30 +0000674 comp->num_default_params += 1;
675 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000676 }
677 }
678
679 // TODO pn_colon not implemented
680 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100681 }
682}
683
Damien George2c838942015-11-17 14:00:14 +0000684STATIC 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 +0000685 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
686 // expression for default arguments, which may contain a lambda. The lambda will
687 // call here in a nested way, so we must save and restore the relevant state.
688 bool orig_have_star = comp->have_star;
689 uint16_t orig_num_dict_params = comp->num_dict_params;
690 uint16_t orig_num_default_params = comp->num_default_params;
691
Damien George2c838942015-11-17 14:00:14 +0000692 // compile default parameters
693 comp->have_star = false;
694 comp->num_dict_params = 0;
695 comp->num_default_params = 0;
696 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
697
698 if (comp->compile_error != MP_OBJ_NULL) {
699 return;
700 }
701
702 // in Micro Python we put the default positional parameters into a tuple using the bytecode
703 // the default keywords args may have already made the tuple; if not, do it now
704 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
705 EMIT_ARG(build_tuple, comp->num_default_params);
706 EMIT(load_null); // sentinel indicating empty default keyword args
707 }
708
709 // make the function
710 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000711
712 // restore state
713 comp->have_star = orig_have_star;
714 comp->num_dict_params = orig_num_dict_params;
715 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000716}
717
Damien429d7192013-10-04 19:53:11 +0100718// leaves function object on stack
719// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000720STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100721 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100722 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000723 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100724 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000725 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100726 }
727
Damien429d7192013-10-04 19:53:11 +0100728 // get the scope for this function
729 scope_t *fscope = (scope_t*)pns->nodes[4];
730
Damien George2c838942015-11-17 14:00:14 +0000731 // compile the function definition
732 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100733
Damien429d7192013-10-04 19:53:11 +0100734 // return its name (the 'f' in "def f(...):")
735 return fscope->simple_name;
736}
737
738// leaves class object on stack
739// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000740STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100741 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100742 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000743 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100744 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000745 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100746 }
747
748 EMIT(load_build_class);
749
750 // scope for this class
751 scope_t *cscope = (scope_t*)pns->nodes[3];
752
753 // compile the class
754 close_over_variables_etc(comp, cscope, 0, 0);
755
756 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000757 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100758
759 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100760 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
761 mp_parse_node_t parents = pns->nodes[1];
762 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
763 parents = MP_PARSE_NODE_NULL;
764 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000765 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100766 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100767
768 // return its name (the 'C' in class C(...):")
769 return cscope->simple_name;
770}
771
Damien6cdd3af2013-10-05 18:08:26 +0100772// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC 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 +0000774 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100775 return false;
776 }
777
778 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100779 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100780 return true;
781 }
782
Damiend99b0522013-12-21 18:17:45 +0000783 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100784 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100785 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100786#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000787 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100788 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000789 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100790 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100791#endif
Damien Georgead297a12016-12-09 13:17:49 +1100792 #if MICROPY_EMIT_INLINE_ASM
793 } else if (attr == ASM_DECORATOR_QSTR) {
794 *emit_options = MP_EMIT_OPT_ASM;
795 #endif
Damien6cdd3af2013-10-05 18:08:26 +0100796 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100797 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100798 }
799
800 return true;
801}
802
Damien George969a6b32014-12-10 22:07:04 +0000803STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100804 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000805 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000806 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100807
Damien6cdd3af2013-10-05 18:08:26 +0100808 // inherit emit options for this function/class definition
809 uint emit_options = comp->scope_cur->emit_options;
810
811 // compile each decorator
812 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100813 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000814 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
815 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100816
817 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000818 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000819 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100820
821 // check for built-in decorators
822 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
823 // this was a built-in
824 num_built_in_decorators += 1;
825
826 } else {
827 // not a built-in, compile normally
828
829 // compile the decorator function
830 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000831 for (int j = 1; j < name_len; j++) {
832 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
833 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100834 }
835
836 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000837 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100838 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000839 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100840 compile_node(comp, pns_decorator->nodes[1]);
841 }
Damien429d7192013-10-04 19:53:11 +0100842 }
843 }
844
pohmelie81ebba72016-01-27 23:23:11 +0300845 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000846 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100847 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000848 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100849 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300850 #if MICROPY_PY_ASYNC_AWAIT
851 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
852 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
853 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
854 body_name = compile_funcdef_helper(comp, pns0, emit_options);
855 scope_t *fscope = (scope_t*)pns0->nodes[4];
856 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
857 #endif
Damien429d7192013-10-04 19:53:11 +0100858 } else {
Damien George0bb97132015-02-27 14:25:47 +0000859 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
860 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100861 }
862
863 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100864 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100865 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100866 }
867
868 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000869 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100870}
871
Damien George969a6b32014-12-10 22:07:04 +0000872STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100873 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100874 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000875 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100876}
877
Damien George6be0b0a2014-08-15 14:30:52 +0100878STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000879 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000880 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300881 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000882 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100883
pohmelie81ebba72016-01-27 23:23:11 +0300884 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100885
Damiend99b0522013-12-21 18:17:45 +0000886 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
887 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300888 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000889 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100890 for (int i = 0; i < n - 1; i++) {
891 compile_node(comp, pns1->nodes[i]);
892 }
Damiend99b0522013-12-21 18:17:45 +0000893 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
894 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100895 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000896 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100897 compile_node(comp, pns1->nodes[0]);
898 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000899 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
900 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000901 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100902 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100903 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100904 }
905 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100906 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100907 }
908
Damiend99b0522013-12-21 18:17:45 +0000909 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
910 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000911 if (MP_PARSE_NODE_IS_NULL(pn)) {
912 goto cannot_delete;
913 } else {
914 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000915 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100916 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
917
Damiend99b0522013-12-21 18:17:45 +0000918 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
919 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
920 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100921 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000922 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100923 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000924 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100925 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000926 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100927 c_del_stmt(comp, pns->nodes[0]);
928 for (int i = 0; i < n; i++) {
929 c_del_stmt(comp, pns1->nodes[i]);
930 }
Damien George216a7112016-09-30 14:48:06 +1000931 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100932 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100933 } else {
934 // sequence with 2 items
935 goto sequence_with_2_items;
936 }
937 } else {
938 // sequence with 2 items
939 sequence_with_2_items:
940 c_del_stmt(comp, pns->nodes[0]);
941 c_del_stmt(comp, pns->nodes[1]);
942 }
Damien429d7192013-10-04 19:53:11 +0100943 }
944 } else {
Damien George93b37262016-01-07 13:07:52 +0000945 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100946 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100947 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100948
949 return;
950
951cannot_delete:
952 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100953}
954
Damien George969a6b32014-12-10 22:07:04 +0000955STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100956 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
957}
958
Damien George969a6b32014-12-10 22:07:04 +0000959STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100960 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100961 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100962 }
Damien George090c9232014-10-17 14:08:49 +0000963 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000964 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100965}
966
Damien George969a6b32014-12-10 22:07:04 +0000967STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100968 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100969 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100970 }
Damien George090c9232014-10-17 14:08:49 +0000971 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000972 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100973}
974
Damien George969a6b32014-12-10 22:07:04 +0000975STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100976 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100977 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100978 return;
979 }
Damiend99b0522013-12-21 18:17:45 +0000980 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100981 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000982 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000983 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100984 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000985 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
986 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 +0100987
Damien George6f355fd2014-04-10 14:11:31 +0100988 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100989 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
990 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
991 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000992 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100993 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
994 } else {
995 compile_node(comp, pns->nodes[0]);
996 }
997 EMIT(return_value);
998}
999
Damien George969a6b32014-12-10 22:07:04 +00001000STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001001 compile_node(comp, pns->nodes[0]);
1002 EMIT(pop_top);
1003}
1004
Damien George969a6b32014-12-10 22:07:04 +00001005STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001006 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001007 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001008 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001009 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001010 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001011 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001012 compile_node(comp, pns->nodes[0]);
1013 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001014 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001015 } else {
1016 // raise x
1017 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001018 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001019 }
1020}
1021
Damien George635543c2014-04-10 12:56:52 +01001022// q_base holds the base of the name
1023// eg a -> q_base=a
1024// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001025STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001026 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001027 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1028 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001029 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001030 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001031 pn = pns->nodes[0];
1032 is_as = true;
1033 }
Damien George635543c2014-04-10 12:56:52 +01001034 if (MP_PARSE_NODE_IS_NULL(pn)) {
1035 // empty name (eg, from . import x)
1036 *q_base = MP_QSTR_;
1037 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1038 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001039 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001040 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001041 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001042 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001043 }
Damien George635543c2014-04-10 12:56:52 +01001044 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001045 } else {
1046 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001047 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001048 {
Damien429d7192013-10-04 19:53:11 +01001049 // a name of the form a.b.c
1050 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001051 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001052 }
Damiend99b0522013-12-21 18:17:45 +00001053 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001054 int len = n - 1;
1055 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001056 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001057 }
Damien George55baff42014-01-21 21:40:13 +00001058 byte *q_ptr;
1059 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001060 for (int i = 0; i < n; i++) {
1061 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001062 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001063 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001064 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001065 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001066 memcpy(str_dest, str_src, str_src_len);
1067 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001068 }
Damien George635543c2014-04-10 12:56:52 +01001069 qstr q_full = qstr_build_end(q_ptr);
1070 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001071 if (is_as) {
1072 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001073 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001074 }
1075 }
Damien429d7192013-10-04 19:53:11 +01001076 }
Damien429d7192013-10-04 19:53:11 +01001077 }
1078}
1079
Damien George6be0b0a2014-08-15 14:30:52 +01001080STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001081 EMIT_ARG(load_const_small_int, 0); // level 0 import
1082 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1083 qstr q_base;
1084 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001085 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001086}
1087
Damien George969a6b32014-12-10 22:07:04 +00001088STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001089 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1090}
1091
Damien George969a6b32014-12-10 22:07:04 +00001092STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001093 mp_parse_node_t pn_import_source = pns->nodes[0];
1094
1095 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1096 uint import_level = 0;
1097 do {
1098 mp_parse_node_t pn_rel;
1099 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)) {
1100 // This covers relative imports with dots only like "from .. import"
1101 pn_rel = pn_import_source;
1102 pn_import_source = MP_PARSE_NODE_NULL;
1103 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1104 // This covers relative imports starting with dot(s) like "from .foo import"
1105 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1106 pn_rel = pns_2b->nodes[0];
1107 pn_import_source = pns_2b->nodes[1];
1108 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1109 } else {
1110 // Not a relative import
1111 break;
1112 }
1113
1114 // get the list of . and/or ...'s
1115 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001116 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001117
1118 // count the total number of .'s
1119 for (int i = 0; i < n; i++) {
1120 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1121 import_level++;
1122 } else {
1123 // should be an MP_TOKEN_ELLIPSIS
1124 import_level += 3;
1125 }
1126 }
1127 } while (0);
1128
Damiend99b0522013-12-21 18:17:45 +00001129 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001130 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001131
1132 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001133 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001134 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001135
1136 // do the import
Damien George635543c2014-04-10 12:56:52 +01001137 qstr dummy_q;
1138 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001139 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001140
Damien429d7192013-10-04 19:53:11 +01001141 } else {
Damien George635543c2014-04-10 12:56:52 +01001142 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001143
1144 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001145 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001146 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001147 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001148 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1149 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1150 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001151 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001152 }
Damien Georgeb9791222014-01-23 00:34:21 +00001153 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001154
1155 // do the import
Damien George635543c2014-04-10 12:56:52 +01001156 qstr dummy_q;
1157 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001158 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001159 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1160 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1161 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001162 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001163 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001164 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001165 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001166 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001167 }
1168 }
1169 EMIT(pop_top);
1170 }
1171}
1172
Damien George584ba672014-12-21 17:26:45 +00001173STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1174 bool added;
1175 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001176 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1177 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001178 return;
1179 }
1180 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1181
1182 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1183 id_info = scope_find_global(comp->scope_cur, qst);
1184 if (id_info != NULL) {
1185 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1186 }
1187}
1188
Damien George969a6b32014-12-10 22:07:04 +00001189STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001190 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001191 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001192 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001193 for (int i = 0; i < n; i++) {
1194 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001195 }
1196 }
1197}
1198
Damien George584ba672014-12-21 17:26:45 +00001199STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1200 bool added;
1201 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001202 if (added) {
1203 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1204 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1205 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1206 }
1207 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001208 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001209 }
Damien George584ba672014-12-21 17:26:45 +00001210}
1211
Damien George969a6b32014-12-10 22:07:04 +00001212STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001213 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001214 if (comp->scope_cur->kind == SCOPE_MODULE) {
1215 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1216 return;
1217 }
1218 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001219 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001220 for (int i = 0; i < n; i++) {
1221 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001222 }
1223 }
1224}
1225
Damien George969a6b32014-12-10 22:07:04 +00001226STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001227 // with optimisations enabled we don't compile assertions
1228 if (MP_STATE_VM(mp_optimise_value) != 0) {
1229 return;
1230 }
1231
Damien George6f355fd2014-04-10 14:11:31 +01001232 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001233 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001234 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001235 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001236 // assertion message
1237 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001238 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001239 }
Damien Georgeb9791222014-01-23 00:34:21 +00001240 EMIT_ARG(raise_varargs, 1);
1241 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001242}
1243
Damien George969a6b32014-12-10 22:07:04 +00001244STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001245 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001246
Damien George65dc9602015-08-14 12:24:11 +01001247 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001248 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001249 uint l_fail = comp_next_label(comp);
1250 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001251
Damien George391db862014-10-17 17:57:33 +00001252 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001253
Damien George65dc9602015-08-14 12:24:11 +01001254 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001255 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001256 goto done;
1257 }
1258
1259 if (
Damien George65dc9602015-08-14 12:24:11 +01001260 // optimisation: don't jump over non-existent elif/else blocks
1261 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001262 // optimisation: don't jump if last instruction was return
1263 && !EMIT(last_emit_was_return_value)
1264 ) {
1265 // jump over elif/else blocks
1266 EMIT_ARG(jump, l_end);
1267 }
1268
1269 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001270 }
1271
Damien George235f9b32014-10-17 17:30:16 +00001272 // compile elif blocks (if any)
1273 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001274 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 +00001275 for (int i = 0; i < n_elif; i++) {
1276 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1277 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001278
Damien George65dc9602015-08-14 12:24:11 +01001279 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001280 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001281 uint l_fail = comp_next_label(comp);
1282 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001283
Damien George391db862014-10-17 17:57:33 +00001284 compile_node(comp, pns_elif->nodes[1]); // elif block
1285
Damien George65dc9602015-08-14 12:24:11 +01001286 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001287 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001288 goto done;
1289 }
1290
1291 // optimisation: don't jump if last instruction was return
1292 if (!EMIT(last_emit_was_return_value)) {
1293 EMIT_ARG(jump, l_end);
1294 }
1295 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001296 }
1297 }
1298
1299 // compile else block
1300 compile_node(comp, pns->nodes[3]); // can be null
1301
Damien George391db862014-10-17 17:57:33 +00001302done:
Damien Georgeb9791222014-01-23 00:34:21 +00001303 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001304}
1305
Damien Georgecbddb272014-02-01 20:08:18 +00001306#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001307 uint16_t old_break_label = comp->break_label; \
1308 uint16_t old_continue_label = comp->continue_label; \
1309 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001310 uint break_label = comp_next_label(comp); \
1311 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001312 comp->break_label = break_label; \
1313 comp->continue_label = continue_label; \
1314 comp->break_continue_except_level = comp->cur_except_level;
1315
1316#define END_BREAK_CONTINUE_BLOCK \
1317 comp->break_label = old_break_label; \
1318 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001319 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001320
Damien George969a6b32014-12-10 22:07:04 +00001321STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001322 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001323
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001324 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 +00001325 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001326 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 +00001327 EMIT_ARG(jump, continue_label);
1328 }
1329 EMIT_ARG(label_assign, top_label);
1330 compile_node(comp, pns->nodes[1]); // body
1331 EMIT_ARG(label_assign, continue_label);
1332 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1333 }
Damience89a212013-10-15 22:25:17 +01001334
1335 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001336 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001337
1338 compile_node(comp, pns->nodes[2]); // else
1339
Damien Georgeb9791222014-01-23 00:34:21 +00001340 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001341}
1342
Damien Georgee181c0d2014-12-12 17:19:56 +00001343// This function compiles an optimised for-loop of the form:
1344// for <var> in range(<start>, <end>, <step>):
1345// <body>
1346// else:
1347// <else>
1348// <var> must be an identifier and <step> must be a small-int.
1349//
1350// Semantics of for-loop require:
1351// - final failing value should not be stored in the loop variable
1352// - if the loop never runs, the loop variable should never be assigned
1353// - assignments to <var>, <end> or <step> in the body do not alter the loop
1354// (<step> is a constant for us, so no need to worry about it changing)
1355//
1356// If <end> is a small-int, then the stack during the for-loop contains just
1357// the current value of <var>. Otherwise, the stack contains <end> then the
1358// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001359STATIC 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 +00001360 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001361
Damien George6f355fd2014-04-10 14:11:31 +01001362 uint top_label = comp_next_label(comp);
1363 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001364
Damien Georgee181c0d2014-12-12 17:19:56 +00001365 // put the end value on the stack if it's not a small-int constant
1366 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1367 if (end_on_stack) {
1368 compile_node(comp, pn_end);
1369 }
1370
1371 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001372 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001373
Damien Georgeb9791222014-01-23 00:34:21 +00001374 EMIT_ARG(jump, entry_label);
1375 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001376
Damien Georgec33ce602014-12-11 17:35:23 +00001377 // duplicate next value and store it to var
1378 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001379 c_assign(comp, pn_var, ASSIGN_STORE);
1380
Damienf3822fc2013-11-09 20:12:03 +00001381 // compile body
1382 compile_node(comp, pn_body);
1383
Damien Georgeb9791222014-01-23 00:34:21 +00001384 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001385
Damien Georgee181c0d2014-12-12 17:19:56 +00001386 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001387 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001388 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001389
Damien Georgeb9791222014-01-23 00:34:21 +00001390 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001391
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001392 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001393 if (end_on_stack) {
1394 EMIT(dup_top_two);
1395 EMIT(rot_two);
1396 } else {
1397 EMIT(dup_top);
1398 compile_node(comp, pn_end);
1399 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001400 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1401 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001402 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001403 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001404 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001405 }
Damien George63f38322015-02-28 15:04:06 +00001406 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001407
1408 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001409 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001410
1411 compile_node(comp, pn_else);
1412
Damien Georgeb9791222014-01-23 00:34:21 +00001413 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001414
1415 // discard final value of var that failed the loop condition
1416 EMIT(pop_top);
1417
1418 // discard <end> value if it's on the stack
1419 if (end_on_stack) {
1420 EMIT(pop_top);
1421 }
Damienf72fd0e2013-11-06 20:20:49 +00001422}
1423
Damien George969a6b32014-12-10 22:07:04 +00001424STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001425 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1426 // this is actually slower, but uses no heap memory
1427 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001428 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 +00001429 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001430 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001431 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001432 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001433 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1434 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001435 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001436 mp_parse_node_t pn_range_start;
1437 mp_parse_node_t pn_range_end;
1438 mp_parse_node_t pn_range_step;
1439 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001440 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001441 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001442 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001443 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001444 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001445 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001446 } else if (n_args == 2) {
1447 pn_range_start = args[0];
1448 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001449 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001450 } else {
1451 pn_range_start = args[0];
1452 pn_range_end = args[1];
1453 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001454 // We need to know sign of step. This is possible only if it's constant
1455 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1456 optimize = false;
1457 }
Damienf72fd0e2013-11-06 20:20:49 +00001458 }
Damien George33ac0fd2015-12-08 21:05:14 +00001459 // arguments must be able to be compiled as standard expressions
1460 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1461 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1462 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1463 optimize = false;
1464 }
1465 }
1466 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1467 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1468 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1469 optimize = false;
1470 }
1471 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001472 }
1473 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001474 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1475 return;
1476 }
1477 }
1478 }
Damienf72fd0e2013-11-06 20:20:49 +00001479
Damien Georgecbddb272014-02-01 20:08:18 +00001480 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001481 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001482
Damien George6f355fd2014-04-10 14:11:31 +01001483 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001484
Damien429d7192013-10-04 19:53:11 +01001485 compile_node(comp, pns->nodes[1]); // iterator
Damien Georgef4df3aa2016-01-09 23:59:52 +00001486 EMIT_ARG(get_iter, true);
Damien Georgecbddb272014-02-01 20:08:18 +00001487 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001489 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1490 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001491 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001492 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001493 }
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(label_assign, pop_label);
Damien George30b42dd2017-01-17 15:30:18 +11001495 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01001496
1497 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001498 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001499
Damien429d7192013-10-04 19:53:11 +01001500 compile_node(comp, pns->nodes[3]); // else (not tested)
1501
Damien Georgeb9791222014-01-23 00:34:21 +00001502 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001503}
1504
Damien George6be0b0a2014-08-15 14:30:52 +01001505STATIC 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 +01001506 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001507 uint l1 = comp_next_label(comp);
1508 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001509
Damien Georgeb9791222014-01-23 00:34:21 +00001510 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001511 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001512
Damien429d7192013-10-04 19:53:11 +01001513 compile_node(comp, pn_body); // body
1514 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001515 EMIT_ARG(jump, success_label); // jump over exception handler
1516
1517 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001518 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001519
Damien Georgef0406852016-09-27 12:37:21 +10001520 // at this point the top of the stack contains the exception instance that was raised
1521
Damien George6f355fd2014-04-10 14:11:31 +01001522 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001523
1524 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001525 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1526 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001527
1528 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001529 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001530
Damiend99b0522013-12-21 18:17:45 +00001531 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001532 // this is a catch all exception handler
1533 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001534 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001535 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001536 return;
1537 }
1538 } else {
1539 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001540 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1541 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1542 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1543 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001544 // handler binds the exception to a local
1545 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001546 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001547 }
1548 }
1549 EMIT(dup_top);
1550 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001551 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001552 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001553 }
1554
Damien Georgef0406852016-09-27 12:37:21 +10001555 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001556 if (qstr_exception_local == 0) {
1557 EMIT(pop_top);
1558 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001559 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001560 }
1561
Damien George6f355fd2014-04-10 14:11:31 +01001562 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001563 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001564 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001565 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001566 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001567 }
1568 compile_node(comp, pns_except->nodes[1]);
1569 if (qstr_exception_local != 0) {
1570 EMIT(pop_block);
1571 }
1572 EMIT(pop_except);
1573 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001574 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1575 EMIT_ARG(label_assign, l3);
1576 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001577 compile_store_id(comp, qstr_exception_local);
1578 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001579
Damien George8dcc0c72014-03-27 10:55:21 +00001580 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001581 EMIT(end_finally);
1582 }
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(jump, l2);
1584 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001585 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001586 }
1587
Damien George8dcc0c72014-03-27 10:55:21 +00001588 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001589 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001590 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001591
Damien Georgeb9791222014-01-23 00:34:21 +00001592 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001593 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001595}
1596
Damien George6be0b0a2014-08-15 14:30:52 +01001597STATIC 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 +01001598 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001599
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001601 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001602
Damien429d7192013-10-04 19:53:11 +01001603 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001604 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001605 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001606 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001607 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001608 } else {
1609 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1610 }
1611 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1613 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001614 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001615
Damien George8dcc0c72014-03-27 10:55:21 +00001616 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001617 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001618}
1619
Damien George969a6b32014-12-10 22:07:04 +00001620STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001621 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1622 {
Damiend99b0522013-12-21 18:17:45 +00001623 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1624 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001625 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001626 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001628 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001629 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001630 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001631 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001632 // no finally
1633 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1634 } else {
1635 // have finally
Damiend99b0522013-12-21 18:17:45 +00001636 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 +01001637 }
1638 } else {
1639 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001640 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001641 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001642 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001643 }
Damien429d7192013-10-04 19:53:11 +01001644 }
1645}
1646
Damien George6be0b0a2014-08-15 14:30:52 +01001647STATIC 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 +01001648 if (n == 0) {
1649 // no more pre-bits, compile the body of the with
1650 compile_node(comp, body);
1651 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001652 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001653 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1654 // we need to allocate an extra label for the native emitter
1655 // it will use l_end+1 as an auxiliary label
1656 comp_next_label(comp);
1657 }
Damiend99b0522013-12-21 18:17:45 +00001658 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001659 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001660 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001661 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001662 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001663 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1664 } else {
1665 // this pre-bit is just an expression
1666 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001667 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001668 EMIT(pop_top);
1669 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001670 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001671 // compile additional pre-bits and the body
1672 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1673 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001674 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001675 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001676 EMIT(end_finally);
1677 }
1678}
1679
Damien George969a6b32014-12-10 22:07:04 +00001680STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001681 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001682 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001683 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001684 assert(n > 0);
1685
1686 // compile in a nested fashion
1687 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1688}
1689
pohmelie81ebba72016-01-27 23:23:11 +03001690STATIC void compile_yield_from(compiler_t *comp) {
Damien Georgef4df3aa2016-01-09 23:59:52 +00001691 EMIT_ARG(get_iter, false);
pohmelie81ebba72016-01-27 23:23:11 +03001692 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1693 EMIT(yield_from);
1694}
1695
1696#if MICROPY_PY_ASYNC_AWAIT
1697STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1698 EMIT_ARG(load_method, method);
1699 EMIT_ARG(call_method, 0, 0, 0);
1700 compile_yield_from(comp);
1701}
1702
1703STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1704 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1705
1706 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1707 uint while_else_label = comp_next_label(comp);
1708 uint try_exception_label = comp_next_label(comp);
1709 uint try_else_label = comp_next_label(comp);
1710 uint try_finally_label = comp_next_label(comp);
1711
1712 compile_node(comp, pns->nodes[1]); // iterator
1713 compile_await_object_method(comp, MP_QSTR___aiter__);
1714 compile_store_id(comp, context);
1715
1716 START_BREAK_CONTINUE_BLOCK
1717
1718 EMIT_ARG(label_assign, continue_label);
1719
1720 EMIT_ARG(setup_except, try_exception_label);
1721 compile_increase_except_level(comp);
1722
1723 compile_load_id(comp, context);
1724 compile_await_object_method(comp, MP_QSTR___anext__);
1725 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1726 EMIT(pop_block);
1727 EMIT_ARG(jump, try_else_label);
1728
1729 EMIT_ARG(label_assign, try_exception_label);
1730 EMIT(start_except_handler);
1731 EMIT(dup_top);
1732 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1733 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1734 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001735 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001736 EMIT(pop_except);
1737 EMIT_ARG(jump, while_else_label);
1738
1739 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001740 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001741 compile_decrease_except_level(comp);
1742 EMIT(end_finally);
1743 EMIT(end_except_handler);
1744
1745 EMIT_ARG(label_assign, try_else_label);
1746 compile_node(comp, pns->nodes[2]); // body
1747
1748 EMIT_ARG(jump, continue_label);
1749 // break/continue apply to outer loop (if any) in the else block
1750 END_BREAK_CONTINUE_BLOCK
1751
1752 EMIT_ARG(label_assign, while_else_label);
1753 compile_node(comp, pns->nodes[3]); // else
1754
1755 EMIT_ARG(label_assign, break_label);
1756}
1757
1758STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1759 if (n == 0) {
1760 // no more pre-bits, compile the body of the with
1761 compile_node(comp, body);
1762 } else {
1763 uint try_exception_label = comp_next_label(comp);
1764 uint no_reraise_label = comp_next_label(comp);
1765 uint try_else_label = comp_next_label(comp);
1766 uint end_label = comp_next_label(comp);
1767 qstr context;
1768
1769 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1770 // this pre-bit is of the form "a as b"
1771 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1772 compile_node(comp, pns->nodes[0]);
1773 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1774 compile_store_id(comp, context);
1775 compile_load_id(comp, context);
1776 compile_await_object_method(comp, MP_QSTR___aenter__);
1777 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1778 } else {
1779 // this pre-bit is just an expression
1780 compile_node(comp, nodes[0]);
1781 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1782 compile_store_id(comp, context);
1783 compile_load_id(comp, context);
1784 compile_await_object_method(comp, MP_QSTR___aenter__);
1785 EMIT(pop_top);
1786 }
1787
1788 compile_load_id(comp, context);
1789 EMIT_ARG(load_method, MP_QSTR___aexit__);
1790
1791 EMIT_ARG(setup_except, try_exception_label);
1792 compile_increase_except_level(comp);
1793 // compile additional pre-bits and the body
1794 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1795 // finish this with block
1796 EMIT(pop_block);
1797 EMIT_ARG(jump, try_else_label); // jump over exception handler
1798
1799 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1800 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001801
1802 // at this point the stack contains: ..., __aexit__, self, exc
1803 EMIT(dup_top);
1804 #if MICROPY_CPYTHON_COMPAT
1805 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1806 #else
1807 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001808 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001809 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1810 #endif
1811 EMIT(rot_two);
1812 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1813 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001814 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001815
pohmelie81ebba72016-01-27 23:23:11 +03001816 compile_yield_from(comp);
1817 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1818 EMIT_ARG(raise_varargs, 0);
1819
1820 EMIT_ARG(label_assign, no_reraise_label);
1821 EMIT(pop_except);
1822 EMIT_ARG(jump, end_label);
1823
Damien Georgeb32c01b2016-09-28 11:52:13 +10001824 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001825 compile_decrease_except_level(comp);
1826 EMIT(end_finally);
1827 EMIT(end_except_handler);
1828
1829 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1830 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1831 EMIT(dup_top);
1832 EMIT(dup_top);
1833 EMIT_ARG(call_method, 3, 0, 0);
1834 compile_yield_from(comp);
1835 EMIT(pop_top);
1836
1837 EMIT_ARG(label_assign, end_label);
1838
1839 }
1840}
1841
1842STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1843 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1844 mp_parse_node_t *nodes;
1845 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1846 assert(n > 0);
1847
1848 // compile in a nested fashion
1849 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1850}
1851
1852STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1853 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1854 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1855 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1856 // async def
1857 compile_funcdef(comp, pns0);
1858 scope_t *fscope = (scope_t*)pns0->nodes[4];
1859 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1860 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1861 // async for
1862 compile_async_for_stmt(comp, pns0);
1863 } else {
1864 // async with
1865 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1866 compile_async_with_stmt(comp, pns0);
1867 }
1868}
1869#endif
1870
Damien George969a6b32014-12-10 22:07:04 +00001871STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001872 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001873 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1874 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001875 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001876 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001877 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001878 EMIT(pop_top);
1879
Damien429d7192013-10-04 19:53:11 +01001880 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001881 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001882 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001883 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001884 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1885 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001886 // do nothing with a lonely constant
1887 } else {
1888 compile_node(comp, pns->nodes[0]); // just an expression
1889 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1890 }
Damien429d7192013-10-04 19:53:11 +01001891 }
Damien Georgeb948de32015-10-08 14:26:01 +01001892 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001893 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1894 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001895 if (kind == PN_expr_stmt_augassign) {
1896 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1897 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001898 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001899 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001900 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001901 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1902 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1903 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1904 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1905 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1906 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1907 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1908 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1909 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1910 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1911 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001912 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001913 }
Damien George7e5fb242014-02-01 22:18:47 +00001914 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001915 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1916 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001917 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001918 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001919 // following CPython, we store left-most first
1920 if (rhs > 0) {
1921 EMIT(dup_top);
1922 }
1923 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1924 for (int i = 0; i < rhs; i++) {
1925 if (i + 1 < rhs) {
1926 EMIT(dup_top);
1927 }
Damien Georgeb948de32015-10-08 14:26:01 +01001928 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001929 }
Damien George0bb97132015-02-27 14:25:47 +00001930 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001931 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001932 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001933 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001934 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001935 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001936 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001937 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001938 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001939 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001940 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1941 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1942 // can't optimise when it's a star expression on the lhs
1943 goto no_optimisation;
1944 }
Damien429d7192013-10-04 19:53:11 +01001945 compile_node(comp, pns10->nodes[0]); // rhs
1946 compile_node(comp, pns10->nodes[1]); // rhs
1947 EMIT(rot_two);
1948 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1949 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001950 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001951 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001952 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001953 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001954 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001955 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001956 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001957 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001958 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1959 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1960 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1961 // can't optimise when it's a star expression on the lhs
1962 goto no_optimisation;
1963 }
Damien429d7192013-10-04 19:53:11 +01001964 compile_node(comp, pns10->nodes[0]); // rhs
1965 compile_node(comp, pns10->nodes[1]); // rhs
1966 compile_node(comp, pns10->nodes[2]); // rhs
1967 EMIT(rot_three);
1968 EMIT(rot_two);
1969 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1970 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1971 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1972 } else {
Damien George495d7812014-04-08 17:51:47 +01001973 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001974 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001975 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1976 }
Damien429d7192013-10-04 19:53:11 +01001977 }
Damien Georgeb948de32015-10-08 14:26:01 +01001978 } else {
1979 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001980 }
1981}
1982
Damien George6be0b0a2014-08-15 14:30:52 +01001983STATIC 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 +00001984 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001985 compile_node(comp, pns->nodes[0]);
1986 for (int i = 1; i < num_nodes; i += 1) {
1987 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001988 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001989 }
1990}
1991
Damien George969a6b32014-12-10 22:07:04 +00001992STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001993 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1994 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001995
Damien George6f355fd2014-04-10 14:11:31 +01001996 uint l_fail = comp_next_label(comp);
1997 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001998 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1999 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002000 EMIT_ARG(jump, l_end);
2001 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002002 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002003 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002004 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002005}
2006
Damien George969a6b32014-12-10 22:07:04 +00002007STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01002008 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002009 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002010 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 +01002011 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002012 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002013 }
2014
2015 // get the scope for this lambda
2016 scope_t *this_scope = (scope_t*)pns->nodes[2];
2017
Damien George2c838942015-11-17 14:00:14 +00002018 // compile the lambda definition
2019 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01002020}
2021
Damien George7711afb2015-02-28 15:10:18 +00002022STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002023 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002024 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002025 for (int i = 0; i < n; i += 1) {
2026 compile_node(comp, pns->nodes[i]);
2027 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002028 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002029 }
2030 }
Damien Georgeb9791222014-01-23 00:34:21 +00002031 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002032}
2033
Damien George7711afb2015-02-28 15:10:18 +00002034STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2035 compile_or_and_test(comp, pns, true);
2036}
2037
Damien George969a6b32014-12-10 22:07:04 +00002038STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002039 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002040}
2041
Damien George969a6b32014-12-10 22:07:04 +00002042STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002043 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002044 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002045}
2046
Damien George969a6b32014-12-10 22:07:04 +00002047STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002048 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002049 compile_node(comp, pns->nodes[0]);
2050 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002051 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002052 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002053 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002054 }
2055 for (int i = 1; i + 1 < num_nodes; i += 2) {
2056 compile_node(comp, pns->nodes[i + 1]);
2057 if (i + 2 < num_nodes) {
2058 EMIT(dup_top);
2059 EMIT(rot_three);
2060 }
Damien George7e5fb242014-02-01 22:18:47 +00002061 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002062 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002063 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002064 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2065 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2066 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2067 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2068 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2069 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002070 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002071 }
2072 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002073 } else {
2074 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002075 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2076 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002077 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002078 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002079 } else {
2080 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002081 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002082 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002083 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002084 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002085 }
Damien429d7192013-10-04 19:53:11 +01002086 }
Damien429d7192013-10-04 19:53:11 +01002087 }
2088 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002089 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 }
2092 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002093 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002094 EMIT_ARG(jump, l_end);
2095 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002096 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002097 EMIT(rot_two);
2098 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002099 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002100 }
2101}
2102
Damien George969a6b32014-12-10 22:07:04 +00002103STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002104 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002105}
2106
Damien George969a6b32014-12-10 22:07:04 +00002107STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002108 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damien George969a6b32014-12-10 22:07:04 +00002111STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002112 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002113}
2114
Damien George969a6b32014-12-10 22:07:04 +00002115STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002116 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002117}
2118
Damien George969a6b32014-12-10 22:07:04 +00002119STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002120 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002121 compile_node(comp, pns->nodes[0]);
2122 for (int i = 1; i + 1 < num_nodes; i += 2) {
2123 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002124 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002126 } else {
Damien George0bb97132015-02-27 14:25:47 +00002127 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2128 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002129 }
2130 }
2131}
2132
Damien George969a6b32014-12-10 22:07:04 +00002133STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002134 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002135 compile_node(comp, pns->nodes[0]);
2136 for (int i = 1; i + 1 < num_nodes; i += 2) {
2137 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002139 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002140 } else {
Damien George0bb97132015-02-27 14:25:47 +00002141 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2142 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002143 }
2144 }
2145}
2146
Damien George969a6b32014-12-10 22:07:04 +00002147STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002148 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002149 compile_node(comp, pns->nodes[0]);
2150 for (int i = 1; i + 1 < num_nodes; i += 2) {
2151 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002152 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002153 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002154 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002155 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002156 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002157 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002158 } else {
Damien George0bb97132015-02-27 14:25:47 +00002159 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2160 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002161 }
2162 }
2163}
2164
Damien George969a6b32014-12-10 22:07:04 +00002165STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002166 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002167 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002168 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002169 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002171 } else {
Damien George0bb97132015-02-27 14:25:47 +00002172 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2173 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002174 }
2175}
2176
pohmelie81ebba72016-01-27 23:23:11 +03002177STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002178 // this is to handle special super() call
2179 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2180
2181 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002182}
Damien George3acaa282016-03-16 13:04:51 +00002183
pohmelie81ebba72016-01-27 23:23:11 +03002184STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2185 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2186 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002187}
2188
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002189STATIC 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 +01002190 // function to call is on top of stack
2191
Damien George35e2a4e2014-02-05 00:51:47 +00002192 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002193 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 +00002194 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002195 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002196 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002197 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002198 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002199 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002200 EMIT_ARG(call_function, 2, 0, 0);
2201 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002202 }
2203 }
Damien George01039b52014-12-21 17:44:27 +00002204 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002205 return;
2206 }
Damien George35e2a4e2014-02-05 00:51:47 +00002207
Damien George2c0842b2014-04-27 16:46:51 +01002208 // get the list of arguments
2209 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002210 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002211
Damien George2c0842b2014-04-27 16:46:51 +01002212 // compile the arguments
2213 // Rather than calling compile_node on the list, we go through the list of args
2214 // explicitly here so that we can count the number of arguments and give sensible
2215 // error messages.
2216 int n_positional = n_positional_extra;
2217 uint n_keyword = 0;
2218 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002219 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002220 for (int i = 0; i < n_args; i++) {
2221 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2222 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2223 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2224 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2225 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2226 return;
2227 }
2228 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002229 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002230 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2231 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2232 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2233 return;
2234 }
2235 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002236 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002237 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002238 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002239 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2240 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2241 return;
2242 }
Damien George59fba2d2015-06-25 14:42:13 +00002243 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002244 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002245 n_keyword += 1;
2246 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002247 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2248 n_positional++;
2249 }
2250 } else {
2251 goto normal_argument;
2252 }
2253 } else {
2254 normal_argument:
2255 if (n_keyword > 0) {
2256 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2257 return;
2258 }
2259 compile_node(comp, args[i]);
2260 n_positional++;
2261 }
Damien429d7192013-10-04 19:53:11 +01002262 }
2263
Delio Brignolie6978a42015-09-17 12:07:06 +02002264 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002265 // if we had one but not the other then we load "null" as a place holder
2266 if (star_flags != 0) {
2267 if (star_args_node == NULL) {
2268 EMIT(load_null);
2269 } else {
2270 compile_node(comp, star_args_node->nodes[0]);
2271 }
2272 if (dblstar_args_node == NULL) {
2273 EMIT(load_null);
2274 } else {
2275 compile_node(comp, dblstar_args_node->nodes[0]);
2276 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002277 }
2278
Damien George2c0842b2014-04-27 16:46:51 +01002279 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002280 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002281 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002282 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002283 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002284 }
Damien429d7192013-10-04 19:53:11 +01002285}
2286
pohmelie81ebba72016-01-27 23:23:11 +03002287STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002288 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002289 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002290 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 +01002291 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002292 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2293 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002294 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002295 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002296 i += 1;
2297 } else {
2298 compile_node(comp, pns->nodes[i]);
2299 }
Damien George35e2a4e2014-02-05 00:51:47 +00002300 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002301 }
2302}
2303
Damien George969a6b32014-12-10 22:07:04 +00002304STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002305 // a list of strings
Damien63321742013-12-10 17:41:49 +00002306
2307 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002308 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002309 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002310 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002311 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002312 int pn_kind;
2313 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2314 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2315 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2316 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2317 } else {
2318 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2319 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002320 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2321 pn_kind = MP_PARSE_NODE_STRING;
2322 } else {
2323 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2324 pn_kind = MP_PARSE_NODE_BYTES;
2325 }
Damien George831137b2015-12-17 13:13:18 +00002326 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002327 }
Damien63321742013-12-10 17:41:49 +00002328 if (i == 0) {
2329 string_kind = pn_kind;
2330 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002331 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002332 return;
2333 }
Damien429d7192013-10-04 19:53:11 +01002334 }
Damien63321742013-12-10 17:41:49 +00002335
Damien George65ef6b72015-01-14 21:17:27 +00002336 // if we are not in the last pass, just load a dummy object
2337 if (comp->pass != MP_PASS_EMIT) {
2338 EMIT_ARG(load_const_obj, mp_const_none);
2339 return;
2340 }
2341
Damien63321742013-12-10 17:41:49 +00002342 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002343 vstr_t vstr;
2344 vstr_init_len(&vstr, n_bytes);
2345 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002346 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002347 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002348 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002349 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2350 memcpy(s_dest, s, s_len);
2351 s_dest += s_len;
2352 } else {
2353 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002354 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2355 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002356 }
Damien63321742013-12-10 17:41:49 +00002357 }
Damien George65ef6b72015-01-14 21:17:27 +00002358
2359 // load the object
Damien George05005f62015-01-21 22:48:37 +00002360 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 +01002361}
2362
2363// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002364STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002365 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2366 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2367 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002368
Damien George36db6bc2014-05-07 17:24:22 +01002369 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002370 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002371 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 +01002372 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002373 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002374 }
2375
2376 // get the scope for this comprehension
2377 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2378
2379 // compile the comprehension
2380 close_over_variables_etc(comp, this_scope, 0, 0);
2381
2382 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
Damien George4d2bab12017-02-10 15:39:33 +11002383 if (kind == SCOPE_GEN_EXPR) {
2384 EMIT_ARG(get_iter, false);
2385 }
Damien George922ddd62014-04-09 12:43:17 +01002386 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002387}
2388
Damien George969a6b32014-12-10 22:07:04 +00002389STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002390 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002391 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002392 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002393 } else {
2394 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002395 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2396 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2397 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2398 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2399 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002400 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002401 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002402 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002403 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002404 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002405 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002407 // generator expression
2408 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2409 } else {
2410 // tuple with 2 items
2411 goto tuple_with_2_items;
2412 }
2413 } else {
2414 // tuple with 2 items
2415 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002416 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002417 }
Damien429d7192013-10-04 19:53:11 +01002418 }
2419}
2420
Damien George969a6b32014-12-10 22:07:04 +00002421STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002422 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002423 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002424 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002425 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2426 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2427 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2428 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2429 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002430 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002431 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002432 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002433 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002434 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002435 // list of many items
2436 compile_node(comp, pns2->nodes[0]);
2437 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002438 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002439 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002440 // list comprehension
2441 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2442 } else {
2443 // list with 2 items
2444 goto list_with_2_items;
2445 }
2446 } else {
2447 // list with 2 items
2448 list_with_2_items:
2449 compile_node(comp, pns2->nodes[0]);
2450 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002451 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002452 }
2453 } else {
2454 // list with 1 item
2455 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002456 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002457 }
2458}
2459
Damien George969a6b32014-12-10 22:07:04 +00002460STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002461 mp_parse_node_t pn = pns->nodes[0];
2462 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002463 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002464 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002465 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2466 pns = (mp_parse_node_struct_t*)pn;
2467 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002468 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002469 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002470 compile_node(comp, pn);
2471 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002472 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2473 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2474 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2475 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002476 // dict/set with multiple elements
2477
2478 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002479 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002480 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002481
2482 // first element sets whether it's a dict or set
2483 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002484 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002485 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002486 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002487 compile_node(comp, pns->nodes[0]);
2488 EMIT(store_map);
2489 is_dict = true;
2490 } else {
2491 // a set
2492 compile_node(comp, pns->nodes[0]); // 1st value of set
2493 is_dict = false;
2494 }
2495
2496 // process rest of elements
2497 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002498 mp_parse_node_t pn_i = nodes[i];
2499 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2500 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002501 if (is_dict) {
2502 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002503 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002504 return;
2505 }
2506 EMIT(store_map);
2507 } else {
2508 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002509 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002510 return;
2511 }
2512 }
2513 }
2514
Damien Georgee37dcaa2014-12-27 17:07:16 +00002515 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002516 // if it's a set, build it
2517 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002518 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002519 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002520 #endif
Damien George0bb97132015-02-27 14:25:47 +00002521 } else {
2522 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002523 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002524 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002525 // a dictionary comprehension
2526 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2527 } else {
2528 // a set comprehension
2529 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2530 }
Damien429d7192013-10-04 19:53:11 +01002531 }
2532 } else {
2533 // set with one element
2534 goto set_with_one_element;
2535 }
2536 } else {
2537 // set with one element
2538 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002539 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002540 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002542 #else
2543 assert(0);
2544 #endif
Damien429d7192013-10-04 19:53:11 +01002545 }
2546}
2547
Damien George969a6b32014-12-10 22:07:04 +00002548STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002549 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002550}
2551
Damien George969a6b32014-12-10 22:07:04 +00002552STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002553 // object who's index we want is on top of stack
2554 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002555 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002556}
2557
Damien George969a6b32014-12-10 22:07:04 +00002558STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002559 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002560 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002561}
2562
Damien George83204f32014-12-27 17:20:41 +00002563#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002564STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002565 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2566 mp_parse_node_t pn = pns->nodes[0];
2567 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002568 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2570 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002571 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2572 pns = (mp_parse_node_struct_t*)pn;
2573 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002575 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002576 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002577 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002578 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002579 } else {
2580 // [?::x]
2581 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002582 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002583 }
Damiend99b0522013-12-21 18:17:45 +00002584 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002585 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002586 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2587 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2588 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2589 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002590 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002591 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002592 } else {
2593 // [?:x:x]
2594 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002595 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002596 }
2597 } else {
2598 // [?:x]
2599 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002600 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002601 }
2602 } else {
2603 // [?:x]
2604 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002606 }
2607}
2608
Damien George969a6b32014-12-10 22:07:04 +00002609STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002610 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002611 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2612 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002613}
2614
Damien George969a6b32014-12-10 22:07:04 +00002615STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002616 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002617 compile_subscript_3_helper(comp, pns);
2618}
Damien George83204f32014-12-27 17:20:41 +00002619#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002620
Damien George969a6b32014-12-10 22:07:04 +00002621STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002622 // if this is called then we are compiling a dict key:value pair
2623 compile_node(comp, pns->nodes[1]); // value
2624 compile_node(comp, pns->nodes[0]); // key
2625}
2626
Damien George969a6b32014-12-10 22:07:04 +00002627STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002628 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002629 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002630 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002631}
2632
Damien George969a6b32014-12-10 22:07:04 +00002633STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002634 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002635 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002636 return;
2637 }
Damiend99b0522013-12-21 18:17:45 +00002638 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002639 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002640 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002641 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2642 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002643 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002644 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002645 } else {
2646 compile_node(comp, pns->nodes[0]);
2647 EMIT(yield_value);
2648 }
2649}
2650
pohmelie81ebba72016-01-27 23:23:11 +03002651#if MICROPY_PY_ASYNC_AWAIT
2652STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2653 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2654 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2655 return;
2656 }
2657 compile_atom_expr_normal(comp, pns);
2658 compile_yield_from(comp);
2659}
2660#endif
2661
Damien George65ef6b72015-01-14 21:17:27 +00002662STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2663 // only create and load the actual str object on the last pass
2664 if (comp->pass != MP_PASS_EMIT) {
2665 EMIT_ARG(load_const_obj, mp_const_none);
2666 } else {
Damien George831137b2015-12-17 13:13:18 +00002667 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 +00002668 }
2669}
2670
2671STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2672 // only create and load the actual bytes object on the last pass
2673 if (comp->pass != MP_PASS_EMIT) {
2674 EMIT_ARG(load_const_obj, mp_const_none);
2675 } else {
Damien George831137b2015-12-17 13:13:18 +00002676 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002677 }
2678}
2679
Damien George7d414a12015-02-08 01:57:40 +00002680STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002681 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2682 // nodes are 32-bit pointers, but need to extract 64-bit object
2683 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2684 #else
Damien George7d414a12015-02-08 01:57:40 +00002685 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002686 #endif
Damien George7d414a12015-02-08 01:57:40 +00002687}
2688
Damiend99b0522013-12-21 18:17:45 +00002689typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002690STATIC const compile_function_t compile_function[] = {
Damien George71019ae2017-02-15 10:58:05 +11002691// only define rules with a compile function
Damien429d7192013-10-04 19:53:11 +01002692#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002693#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George71019ae2017-02-15 10:58:05 +11002694#define DEF_RULE_NC(rule, kind, ...)
Damien George51dfcb42015-01-01 20:27:54 +00002695#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002696#undef c
2697#undef DEF_RULE
Damien George71019ae2017-02-15 10:58:05 +11002698#undef DEF_RULE_NC
Damien George65ef6b72015-01-14 21:17:27 +00002699 compile_string,
2700 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002701 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002702};
2703
Damien George6be0b0a2014-08-15 14:30:52 +01002704STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002705 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002706 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002707 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002708 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002709 #if MICROPY_DYNAMIC_COMPILER
2710 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2711 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2712 // integer fits in target runtime's small-int
2713 EMIT_ARG(load_const_small_int, arg);
2714 } else {
2715 // integer doesn't fit, so create a multi-precision int object
2716 // (but only create the actual object on the last pass)
2717 if (comp->pass != MP_PASS_EMIT) {
2718 EMIT_ARG(load_const_obj, mp_const_none);
2719 } else {
2720 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2721 }
2722 }
2723 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002724 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002725 #endif
Damiend99b0522013-12-21 18:17:45 +00002726 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002727 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002728 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002729 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002730 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2731 case MP_PARSE_NODE_BYTES:
2732 // only create and load the actual bytes object on the last pass
2733 if (comp->pass != MP_PASS_EMIT) {
2734 EMIT_ARG(load_const_obj, mp_const_none);
2735 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002736 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002737 const byte *data = qstr_data(arg, &len);
2738 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2739 }
2740 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002741 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002742 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002743 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002744 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002745 // do nothing
2746 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002747 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002748 }
2749 break;
Damien429d7192013-10-04 19:53:11 +01002750 }
2751 } else {
Damiend99b0522013-12-21 18:17:45 +00002752 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002753 EMIT_ARG(set_source_line, pns->source_line);
Damien George71019ae2017-02-15 10:58:05 +11002754 assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object);
Damien George65ef6b72015-01-14 21:17:27 +00002755 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002756 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002757 }
2758}
2759
Damien George2ac4af62014-08-15 16:45:41 +01002760STATIC 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 +00002761 // check that **kw is last
2762 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2763 compile_syntax_error(comp, pn, "invalid syntax");
2764 return;
2765 }
2766
Damien George2827d622014-04-27 15:50:52 +01002767 qstr param_name = MP_QSTR_NULL;
2768 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002769 if (MP_PARSE_NODE_IS_ID(pn)) {
2770 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002771 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002772 // comes after a star, so counts as a keyword-only parameter
2773 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002774 } else {
Damien George2827d622014-04-27 15:50:52 +01002775 // comes before a star, so counts as a positional parameter
2776 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002777 }
Damienb14de212013-10-06 00:28:28 +01002778 } else {
Damiend99b0522013-12-21 18:17:45 +00002779 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2780 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2781 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2782 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002783 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002784 // comes after a star, so counts as a keyword-only parameter
2785 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002786 } else {
Damien George2827d622014-04-27 15:50:52 +01002787 // comes before a star, so counts as a positional parameter
2788 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002789 }
Damiend99b0522013-12-21 18:17:45 +00002790 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002791 if (comp->have_star) {
2792 // more than one star
2793 compile_syntax_error(comp, pn, "invalid syntax");
2794 return;
2795 }
Damien George8b19db02014-04-11 23:25:34 +01002796 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002797 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002798 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002799 // bare star
2800 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002801 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002802 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002803 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002804 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002805 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002806 } else {
2807 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002808 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002809 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002810 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2811 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002812 }
Damien George0bb97132015-02-27 14:25:47 +00002813 } else {
2814 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002815 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002816 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002817 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002818 }
Damien429d7192013-10-04 19:53:11 +01002819 }
2820
Damien George2827d622014-04-27 15:50:52 +01002821 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002822 bool added;
2823 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2824 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002825 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002826 return;
2827 }
Damien429d7192013-10-04 19:53:11 +01002828 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002829 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002830 }
2831}
2832
Damien George2827d622014-04-27 15:50:52 +01002833STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002834 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002835}
2836
Damien George2827d622014-04-27 15:50:52 +01002837STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002838 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2839}
2840
Damien Georgeea5b59b2015-09-04 16:44:14 +01002841#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002842STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2843 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2844 // no annotation
2845 return;
2846 }
2847
2848 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2849 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2850 // named parameter with possible annotation
2851 // fallthrough
2852 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2853 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2854 // named star with possible annotation
2855 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2856 // fallthrough
2857 } else {
2858 // no annotation
2859 return;
2860 }
Damien Georgee49153f2016-10-11 12:29:54 +11002861 } else {
2862 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002863 // double star with possible annotation
2864 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002865 }
2866
2867 mp_parse_node_t pn_annotation = pns->nodes[1];
2868
2869 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002870 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2871 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2872 assert(id_info != NULL);
2873
Damien Georgeea5b59b2015-09-04 16:44:14 +01002874 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2875 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2876 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2877 } else {
2878 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002879 }
Damien George2ac4af62014-08-15 16:45:41 +01002880 }
Damien429d7192013-10-04 19:53:11 +01002881}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002882#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002883
Damien Georgea8312432015-12-18 01:37:55 +00002884STATIC 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) {
2885 uint l_top = comp_next_label(comp);
2886 uint l_end = comp_next_label(comp);
2887 EMIT_ARG(label_assign, l_top);
2888 EMIT_ARG(for_iter, l_end);
2889 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2890 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2891
Damien429d7192013-10-04 19:53:11 +01002892 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002893 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002894 // no more nested if/for; compile inner expression
2895 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002896 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002897 EMIT(yield_value);
2898 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002899 } else {
Damien George088740e2017-01-17 15:27:37 +11002900 EMIT_ARG(store_comp, comp->scope_cur->kind, 4 * for_depth + 5);
Damien429d7192013-10-04 19:53:11 +01002901 }
Damiend99b0522013-12-21 18:17:45 +00002902 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002903 // if condition
Damiend99b0522013-12-21 18:17:45 +00002904 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002905 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2906 pn_iter = pns_comp_if->nodes[1];
2907 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002908 } else {
2909 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002910 // for loop
Damiend99b0522013-12-21 18:17:45 +00002911 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002912 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6e769da2017-01-17 14:32:50 +11002913 EMIT_ARG(get_iter, true);
Damien Georgea8312432015-12-18 01:37:55 +00002914 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002915 }
Damien Georgea8312432015-12-18 01:37:55 +00002916
2917 EMIT_ARG(jump, l_top);
2918 EMIT_ARG(label_assign, l_end);
Damien George30b42dd2017-01-17 15:30:18 +11002919 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002920}
2921
Damien George1463c1f2014-04-25 23:52:57 +01002922STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002923#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002924 // see http://www.python.org/dev/peps/pep-0257/
2925
2926 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002927 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002928 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002929 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002930 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002931 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2932 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002933 for (int i = 0; i < num_nodes; i++) {
2934 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002935 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 +00002936 // not a newline, so this is the first statement; finish search
2937 break;
2938 }
2939 }
2940 // 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 +00002941 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002942 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002943 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002944 } else {
2945 return;
2946 }
2947
2948 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002949 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002950 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002951 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2952 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2953 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2954 // compile the doc string
2955 compile_node(comp, pns->nodes[0]);
2956 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002957 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002958 }
2959 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002960#else
2961 (void)comp;
2962 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002963#endif
Damien429d7192013-10-04 19:53:11 +01002964}
2965
Damien George1463c1f2014-04-25 23:52:57 +01002966STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002967 comp->pass = pass;
2968 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002969 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002970 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002971
Damien George36db6bc2014-05-07 17:24:22 +01002972 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002973 // reset maximum stack sizes in scope
2974 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002975 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002976 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002977 }
2978
Damien429d7192013-10-04 19:53:11 +01002979 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002980 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2981 assert(scope->kind == SCOPE_MODULE);
2982 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2983 compile_node(comp, pns->nodes[0]); // compile the expression
2984 EMIT(return_value);
2985 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002986 if (!comp->is_repl) {
2987 check_for_doc_string(comp, scope->pn);
2988 }
Damien429d7192013-10-04 19:53:11 +01002989 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002990 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002991 EMIT(return_value);
2992 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002993 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2994 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2995 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002996
2997 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002998 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002999 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003000 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003001 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01003002 }
3003 #if MICROPY_EMIT_NATIVE
3004 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01003005 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01003006 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01003007
Damien George2ac4af62014-08-15 16:45:41 +01003008 // argument annotations
3009 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3010
3011 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003012 mp_parse_node_t pn_annotation = pns->nodes[2];
3013 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01003014 // nodes[2] can be null or a test-expr
3015 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3016 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3017 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3018 } else {
3019 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003020 }
3021 }
Damien George2ac4af62014-08-15 16:45:41 +01003022 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01003023 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01003024
3025 compile_node(comp, pns->nodes[3]); // 3 is function body
3026 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003027 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003028 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003029 EMIT(return_value);
3030 }
3031 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003032 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3033 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3034 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003035
3036 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003037 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003038 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003039 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003040 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3041 }
3042
3043 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003044
3045 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3046 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3047 EMIT(pop_top);
3048 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3049 }
Damien429d7192013-10-04 19:53:11 +01003050 EMIT(return_value);
3051 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3052 // a bit of a hack at the moment
3053
Damiend99b0522013-12-21 18:17:45 +00003054 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3055 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3056 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3057 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3058 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003059
Damien George708c0732014-04-27 19:23:46 +01003060 // We need a unique name for the comprehension argument (the iterator).
3061 // CPython uses .0, but we should be able to use anything that won't
3062 // clash with a user defined variable. Best to use an existing qstr,
3063 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003064 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003065 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003066 bool added;
3067 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3068 assert(added);
3069 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003070 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003071 }
3072
3073 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003074 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003075 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003076 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003077 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003078 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003079 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003080 #endif
Damien429d7192013-10-04 19:53:11 +01003081 }
3082
Damien George088740e2017-01-17 15:27:37 +11003083 // There are 4 slots on the stack for the iterator, and the first one is
3084 // NULL to indicate that the second one points to the iterator object.
Damien George4d2bab12017-02-10 15:39:33 +11003085 if (scope->kind == SCOPE_GEN_EXPR) {
3086 EMIT(load_null);
3087 compile_load_id(comp, qstr_arg);
3088 EMIT(load_null);
3089 EMIT(load_null);
3090 } else {
3091 compile_load_id(comp, qstr_arg);
3092 EMIT_ARG(get_iter, true);
3093 }
Damien George6e769da2017-01-17 14:32:50 +11003094
Damien Georgea8312432015-12-18 01:37:55 +00003095 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003096
3097 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003098 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003099 }
3100 EMIT(return_value);
3101 } else {
3102 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003103 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3104 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3105 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003106
Damien George36db6bc2014-05-07 17:24:22 +01003107 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003108 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003109 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003110 assert(added);
3111 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003112 }
3113
Damien George542bd6b2015-03-26 14:42:40 +00003114 compile_load_id(comp, MP_QSTR___name__);
3115 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003116 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003117 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003118
3119 check_for_doc_string(comp, pns->nodes[2]);
3120 compile_node(comp, pns->nodes[2]); // 2 is class body
3121
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003122 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003123 assert(id != NULL);
3124 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003125 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003126 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003127 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003128 }
3129 EMIT(return_value);
3130 }
3131
Damien415eb6f2013-10-05 12:19:06 +01003132 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003133
3134 // make sure we match all the exception levels
3135 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003136}
3137
Damien Georgead297a12016-12-09 13:17:49 +11003138#if MICROPY_EMIT_INLINE_ASM
Damien George36db6bc2014-05-07 17:24:22 +01003139// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003140STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003141 comp->pass = pass;
3142 comp->scope_cur = scope;
3143 comp->next_label = 1;
3144
3145 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003146 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003147 return;
3148 }
3149
Damien George36db6bc2014-05-07 17:24:22 +01003150 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgee920bab2016-12-09 21:23:17 +11003151 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003152 }
3153
Damien826005c2013-10-05 23:17:28 +01003154 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003155 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3156 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3157 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003158
Damiend99b0522013-12-21 18:17:45 +00003159 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003160
Damiena2f2f7d2013-10-06 00:14:13 +01003161 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003162 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003163 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003164 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003165 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003166 if (comp->compile_error != MP_OBJ_NULL) {
3167 goto inline_asm_error;
3168 }
Damiena2f2f7d2013-10-06 00:14:13 +01003169 }
3170
Damien George8f54c082016-01-15 15:20:43 +00003171 // pns->nodes[2] is function return annotation
3172 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3173 mp_parse_node_t pn_annotation = pns->nodes[2];
3174 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3175 // nodes[2] can be null or a test-expr
3176 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3177 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3178 switch (ret_type) {
3179 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3180 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3181 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3182 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3183 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3184 }
3185 } else {
3186 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3187 }
3188 }
Damien826005c2013-10-05 23:17:28 +01003189
Damiend99b0522013-12-21 18:17:45 +00003190 mp_parse_node_t pn_body = pns->nodes[3]; // body
3191 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003192 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003193
Damien826005c2013-10-05 23:17:28 +01003194 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003195 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3196 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003197 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3198 // no instructions
3199 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003200 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003201 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003202 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003203 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003204 return;
3205 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003206
3207 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003208 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003209 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3210 goto not_an_instruction;
3211 }
Damiend99b0522013-12-21 18:17:45 +00003212 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003213 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003214 goto not_an_instruction;
3215 }
3216 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3217 goto not_an_instruction;
3218 }
3219 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3220 goto not_an_instruction;
3221 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003222
3223 // parse node looks like an instruction
3224 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003225 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3226 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3227 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003228 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003229
3230 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003231 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003232 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003233 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003234 return;
3235 }
Damien George6f355fd2014-04-10 14:11:31 +01003236 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003237 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003238 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3239 compile_syntax_error(comp, nodes[i], "label redefined");
3240 return;
3241 }
Damien826005c2013-10-05 23:17:28 +01003242 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003243 } else if (op == MP_QSTR_align) {
3244 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003245 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003246 return;
3247 }
Damien George36db6bc2014-05-07 17:24:22 +01003248 if (pass > MP_PASS_SCOPE) {
Damien Georgedd53b122016-12-09 20:54:54 +11003249 mp_asm_base_align((mp_asm_base_t*)comp->emit_inline_asm,
3250 MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003251 }
3252 } else if (op == MP_QSTR_data) {
3253 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003254 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003255 return;
3256 }
Damien George36db6bc2014-05-07 17:24:22 +01003257 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003258 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003259 for (uint j = 1; j < n_args; j++) {
3260 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003261 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003262 return;
3263 }
Damien Georgedd53b122016-12-09 20:54:54 +11003264 mp_asm_base_data((mp_asm_base_t*)comp->emit_inline_asm,
3265 bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003266 }
3267 }
Damien826005c2013-10-05 23:17:28 +01003268 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003269 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003270 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003271 }
3272 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003273
3274 if (comp->compile_error != MP_OBJ_NULL) {
3275 pns = pns2; // this is the parse node that had the error
3276 goto inline_asm_error;
3277 }
Damien826005c2013-10-05 23:17:28 +01003278 }
3279
Damien George36db6bc2014-05-07 17:24:22 +01003280 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003281 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien Georgee920bab2016-12-09 21:23:17 +11003282
3283 if (comp->pass == MP_PASS_EMIT) {
3284 void *f = mp_asm_base_get_code((mp_asm_base_t*)comp->emit_inline_asm);
3285 mp_emit_glue_assign_native(comp->scope_cur->raw_code, MP_CODE_NATIVE_ASM,
3286 f, mp_asm_base_get_code_size((mp_asm_base_t*)comp->emit_inline_asm),
3287 NULL, comp->scope_cur->num_pos_args, 0, type_sig);
3288 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003289 }
3290
3291 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003292 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003293 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003294 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003295 }
Damien429d7192013-10-04 19:53:11 +01003296}
Damien George094d4502014-04-02 17:31:27 +01003297#endif
Damien429d7192013-10-04 19:53:11 +01003298
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003299STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003300 // in Micro Python we put the *x parameter after all other parameters (except **y)
3301 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3302 id_info_t *id_param = NULL;
3303 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3304 id_info_t *id = &scope->id_info[i];
3305 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3306 if (id_param != NULL) {
3307 // swap star param with last param
3308 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3309 }
3310 break;
3311 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3312 id_param = id;
3313 }
3314 }
3315 }
Damien George2827d622014-04-27 15:50:52 +01003316
Damien429d7192013-10-04 19:53:11 +01003317 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003318 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003319 scope->num_locals = 0;
3320 for (int i = 0; i < scope->id_info_len; i++) {
3321 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003322 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003323 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3324 continue;
3325 }
Damien George3dea8c92016-09-30 12:34:05 +10003326 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003327 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3328 }
Damien George2827d622014-04-27 15:50:52 +01003329 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003330 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003331 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003332 }
3333 }
3334
Damien George65dc9602015-08-14 12:24:11 +01003335 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003336 for (int i = 0; i < scope->id_info_len; i++) {
3337 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003338 // in Micro Python the cells come right after the fast locals
3339 // parameters are not counted here, since they remain at the start
3340 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003341 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003342 id->local_num = scope->num_locals;
3343 scope->num_locals += 1;
3344 }
Damien9ecbcff2013-12-11 00:41:43 +00003345 }
Damien9ecbcff2013-12-11 00:41:43 +00003346
Damien George65dc9602015-08-14 12:24:11 +01003347 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003348 // make sure they are in the order of the parent scope
3349 if (scope->parent != NULL) {
3350 int num_free = 0;
3351 for (int i = 0; i < scope->parent->id_info_len; i++) {
3352 id_info_t *id = &scope->parent->id_info[i];
3353 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3354 for (int j = 0; j < scope->id_info_len; j++) {
3355 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003356 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003357 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003358 // in Micro Python the frees come first, before the params
3359 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003360 num_free += 1;
3361 }
3362 }
3363 }
Damien429d7192013-10-04 19:53:11 +01003364 }
Damien George6baf76e2013-12-30 22:32:17 +00003365 // in Micro Python shift all other locals after the free locals
3366 if (num_free > 0) {
3367 for (int i = 0; i < scope->id_info_len; i++) {
3368 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003369 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003370 id->local_num += num_free;
3371 }
3372 }
Damien George2827d622014-04-27 15:50:52 +01003373 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 +00003374 scope->num_locals += num_free;
3375 }
Damien429d7192013-10-04 19:53:11 +01003376 }
Damien429d7192013-10-04 19:53:11 +01003377}
3378
Damien Georged4dba882015-11-13 13:38:28 +00003379#if !MICROPY_PERSISTENT_CODE_SAVE
3380STATIC
3381#endif
3382mp_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 +01003383 // put compiler state on the stack, it's relatively small
3384 compiler_t comp_state = {0};
3385 compiler_t *comp = &comp_state;
3386
Damien Georgecbd2f742014-01-19 11:48:48 +00003387 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003388 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003389
Damien George62a3a282015-03-01 12:04:05 +00003390 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003391 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003392
Damien Georgea210c772015-03-26 15:49:53 +00003393 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003394 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003395
Damien826005c2013-10-05 23:17:28 +01003396 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003397 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003398 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003399 comp->emit_method_table = &emit_bc_method_table;
3400 #endif
Damien826005c2013-10-05 23:17:28 +01003401 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003402 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003403 if (false) {
Damien Georgead297a12016-12-09 13:17:49 +11003404 #if MICROPY_EMIT_INLINE_ASM
3405 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
Damien George36db6bc2014-05-07 17:24:22 +01003406 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damien Georgead297a12016-12-09 13:17:49 +11003407 #endif
Damien826005c2013-10-05 23:17:28 +01003408 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003409 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003410 }
3411
3412 // update maximim number of labels needed
3413 if (comp->next_label > max_num_labels) {
3414 max_num_labels = comp->next_label;
3415 }
Damien429d7192013-10-04 19:53:11 +01003416 }
3417
Damien826005c2013-10-05 23:17:28 +01003418 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003419 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 +00003420 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003421 }
3422
Damien Georgea210c772015-03-26 15:49:53 +00003423 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003424 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003425
Damien826005c2013-10-05 23:17:28 +01003426 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003427#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003428 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003429#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003430 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003431 if (false) {
3432 // dummy
3433
Damien Georgead297a12016-12-09 13:17:49 +11003434 #if MICROPY_EMIT_INLINE_ASM
3435 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
3436 // inline assembly
3437 if (comp->emit_inline_asm == NULL) {
3438 comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003439 }
3440 comp->emit = NULL;
Damien Georgead297a12016-12-09 13:17:49 +11003441 comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table);
Damien George36db6bc2014-05-07 17:24:22 +01003442 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgede9cd002016-12-19 17:42:25 +11003443 #if MICROPY_EMIT_INLINE_XTENSA
3444 // Xtensa requires an extra pass to compute size of l32r const table
3445 // TODO this can be improved by calculating it during SCOPE pass
3446 // but that requires some other structural changes to the asm emitters
3447 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
3448 #endif
Damien Georgea91ac202014-10-05 19:01:34 +01003449 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003450 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003451 }
Damien Georgead297a12016-12-09 13:17:49 +11003452 #endif
Damienc025ebb2013-10-12 14:30:21 +01003453
Damien826005c2013-10-05 23:17:28 +01003454 } else {
Damienc025ebb2013-10-12 14:30:21 +01003455
3456 // choose the emit type
3457
Damien826005c2013-10-05 23:17:28 +01003458 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003459
3460#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003461 case MP_EMIT_OPT_NATIVE_PYTHON:
3462 case MP_EMIT_OPT_VIPER:
Damiendc833822013-10-06 01:01:01 +01003463 if (emit_native == NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003464 emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003465 }
Damien George080a78b2016-12-07 11:17:17 +11003466 comp->emit_method_table = &NATIVE_EMITTER(method_table);
Damienc025ebb2013-10-12 14:30:21 +01003467 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003468 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003469 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003470#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003471
Damien826005c2013-10-05 23:17:28 +01003472 default:
Damien826005c2013-10-05 23:17:28 +01003473 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003474 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003475 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003476 #endif
Damien826005c2013-10-05 23:17:28 +01003477 break;
3478 }
Damienc025ebb2013-10-12 14:30:21 +01003479
Damien George1e1779e2015-01-14 00:20:28 +00003480 // need a pass to compute stack size
3481 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3482
Damien George36db6bc2014-05-07 17:24:22 +01003483 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003484 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003485 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3486 }
3487
3488 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003489 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003490 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003491 }
Damien6cdd3af2013-10-05 18:08:26 +01003492 }
Damien429d7192013-10-04 19:53:11 +01003493 }
3494
Damien Georgecfc4c332015-07-29 22:16:01 +00003495 if (comp->compile_error != MP_OBJ_NULL) {
3496 // if there is no line number for the error then use the line
3497 // number for the start of this scope
3498 compile_error_set_line(comp, comp->scope_cur->pn);
3499 // add a traceback to the exception using relevant source info
3500 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3501 comp->compile_error_line, comp->scope_cur->simple_name);
3502 }
3503
Damien George41d02b62014-01-24 22:42:28 +00003504 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003505
Damien Georgea210c772015-03-26 15:49:53 +00003506 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003507#if MICROPY_EMIT_NATIVE
3508 if (emit_native != NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003509 NATIVE_EMITTER(free)(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003510 }
3511#endif
Damien Georgead297a12016-12-09 13:17:49 +11003512 #if MICROPY_EMIT_INLINE_ASM
3513 if (comp->emit_inline_asm != NULL) {
3514 ASM_EMITTER(free)(comp->emit_inline_asm);
Damien George41d02b62014-01-24 22:42:28 +00003515 }
Damien Georgead297a12016-12-09 13:17:49 +11003516 #endif
Damien George41d02b62014-01-24 22:42:28 +00003517
Damien George52b5d762014-09-23 15:31:56 +00003518 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003519 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003520
Damien George41d02b62014-01-24 22:42:28 +00003521 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003522 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003523 for (scope_t *s = module_scope; s;) {
3524 scope_t *next = s->next;
3525 scope_free(s);
3526 s = next;
3527 }
Damien5ac1b2e2013-10-18 19:58:12 +01003528
Damien George9d5e5c02015-09-24 13:15:57 +01003529 if (comp->compile_error != MP_OBJ_NULL) {
3530 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003531 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003532 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003533 }
Damien429d7192013-10-04 19:53:11 +01003534}
Damien Georged4dba882015-11-13 13:38:28 +00003535
3536mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3537 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3538 // return function that executes the outer module
3539 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3540}
Damien Georgedd5353a2015-12-18 12:35:44 +00003541
3542#endif // MICROPY_ENABLE_COMPILER