blob: b114f8962cfd11b345f653fd5180d814a75dbf75 [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 George7d414a12015-02-08 01:57:40 +000050 PN_const_object, // special node for a constant, generic Python object
Damien George71019ae2017-02-15 10:58:05 +110051// define rules without a compile function
52#define DEF_RULE(rule, comp, kind, ...)
53#define DEF_RULE_NC(rule, kind, ...) PN_##rule,
54#include "py/grammar.h"
55#undef DEF_RULE
56#undef DEF_RULE_NC
Damien429d7192013-10-04 19:53:11 +010057} pn_kind_t;
58
Damien George65dc9602015-08-14 12:24:11 +010059#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000060
61#if NEED_METHOD_TABLE
62
63// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000064#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
65#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000066#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
67#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010068
Damien George41125902015-03-26 16:44:14 +000069#else
70
71// if we only have the bytecode emitter enabled then we can do a direct call to the functions
72#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
73#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
74#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
75#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
76
77#endif
78
Damien George080a78b2016-12-07 11:17:17 +110079#if MICROPY_EMIT_NATIVE
80// define a macro to access external native emitter
81#if MICROPY_EMIT_X64
82#define NATIVE_EMITTER(f) emit_native_x64_##f
83#elif MICROPY_EMIT_X86
84#define NATIVE_EMITTER(f) emit_native_x86_##f
85#elif MICROPY_EMIT_THUMB
86#define NATIVE_EMITTER(f) emit_native_thumb_##f
87#elif MICROPY_EMIT_ARM
88#define NATIVE_EMITTER(f) emit_native_arm_##f
Damien George8e5aced2016-12-09 16:39:39 +110089#elif MICROPY_EMIT_XTENSA
90#define NATIVE_EMITTER(f) emit_native_xtensa_##f
Damien George080a78b2016-12-07 11:17:17 +110091#else
92#error "unknown native emitter"
93#endif
94#endif
95
Damien Georgead297a12016-12-09 13:17:49 +110096#if MICROPY_EMIT_INLINE_ASM
97// define macros for inline assembler
98#if MICROPY_EMIT_INLINE_THUMB
99#define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
100#define ASM_EMITTER(f) emit_inline_thumb_##f
Damien Georgef76b1bf2016-12-09 17:03:33 +1100101#elif MICROPY_EMIT_INLINE_XTENSA
102#define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa
103#define ASM_EMITTER(f) emit_inline_xtensa_##f
Damien Georgead297a12016-12-09 13:17:49 +1100104#else
105#error "unknown asm emitter"
106#endif
107#endif
108
Damien George0496de22015-10-03 17:07:54 +0100109#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
110#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
111
Damien Georgea91ac202014-10-05 19:01:34 +0100112// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +0100113typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +0000114 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +0100115
Damien George78035b92014-04-09 12:27:39 +0100116 uint8_t is_repl;
117 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +0100118 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +0100119 uint8_t have_star;
120
121 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +0000122 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +0000123 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +0100124
Damien George6f355fd2014-04-10 14:11:31 +0100125 uint next_label;
Damienb05d7072013-10-05 13:37:10 +0100126
Damien George69b89d22014-04-11 13:38:30 +0000127 uint16_t num_dict_params;
128 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +0100129
Damien Georgea91ac202014-10-05 19:01:34 +0100130 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
131 uint16_t continue_label;
132 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +0000133 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +0100134
Damien429d7192013-10-04 19:53:11 +0100135 scope_t *scope_head;
136 scope_t *scope_cur;
137
Damien6cdd3af2013-10-05 18:08:26 +0100138 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000139 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100140 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000141 #endif
Damien826005c2013-10-05 23:17:28 +0100142
Damien Georgead297a12016-12-09 13:17:49 +1100143 #if MICROPY_EMIT_INLINE_ASM
Damien826005c2013-10-05 23:17:28 +0100144 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
145 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 +0000146 #endif
Damien429d7192013-10-04 19:53:11 +0100147} compiler_t;
148
Damien Georgecfc4c332015-07-29 22:16:01 +0000149STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
150 // if the line of the error is unknown then try to update it from the pn
151 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000152 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100153 }
Damien George84d59c22015-07-27 22:20:00 +0100154}
155
156STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000157 // only register the error if there has been no other error
158 if (comp->compile_error == MP_OBJ_NULL) {
159 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
160 compile_error_set_line(comp, pn);
161 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000162}
163
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200164STATIC 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 +0100165STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000166STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100167
Damien George6f355fd2014-04-10 14:11:31 +0100168STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100169 return comp->next_label++;
170}
171
Damien George8dcc0c72014-03-27 10:55:21 +0000172STATIC void compile_increase_except_level(compiler_t *comp) {
173 comp->cur_except_level += 1;
174 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
175 comp->scope_cur->exc_stack_size = comp->cur_except_level;
176 }
177}
178
179STATIC void compile_decrease_except_level(compiler_t *comp) {
180 assert(comp->cur_except_level > 0);
181 comp->cur_except_level -= 1;
182}
183
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200184STATIC 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 +0100185 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100186 scope->parent = comp->scope_cur;
187 scope->next = NULL;
188 if (comp->scope_head == NULL) {
189 comp->scope_head = scope;
190 } else {
191 scope_t *s = comp->scope_head;
192 while (s->next != NULL) {
193 s = s->next;
194 }
195 s->next = scope;
196 }
197 return scope;
198}
199
Damien George91bc32d2015-04-09 15:31:53 +0000200typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
201
202STATIC 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 +0000203 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000204 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
205 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100206 for (int i = 0; i < num_nodes; i++) {
207 f(comp, pns->nodes[i]);
208 }
Damiend99b0522013-12-21 18:17:45 +0000209 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100210 f(comp, pn);
211 }
212}
213
Damien George969a6b32014-12-10 22:07:04 +0000214STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000215 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100216 for (int i = 0; i < num_nodes; i++) {
217 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000218 if (comp->compile_error != MP_OBJ_NULL) {
219 // add line info for the error in case it didn't have a line number
220 compile_error_set_line(comp, pns->nodes[i]);
221 return;
222 }
Damien429d7192013-10-04 19:53:11 +0100223 }
224}
225
Damien George542bd6b2015-03-26 14:42:40 +0000226STATIC void compile_load_id(compiler_t *comp, qstr qst) {
227 if (comp->pass == MP_PASS_SCOPE) {
228 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
229 } else {
Damien George41125902015-03-26 16:44:14 +0000230 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000231 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000232 #else
233 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
234 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000235 }
236}
237
238STATIC void compile_store_id(compiler_t *comp, qstr qst) {
239 if (comp->pass == MP_PASS_SCOPE) {
240 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
241 } else {
Damien George41125902015-03-26 16:44:14 +0000242 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000243 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000244 #else
245 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
246 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000247 }
248}
249
250STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
251 if (comp->pass == MP_PASS_SCOPE) {
252 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
253 } else {
Damien George41125902015-03-26 16:44:14 +0000254 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000255 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000256 #else
257 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
258 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000259 }
260}
261
Damien George2c0842b2014-04-27 16:46:51 +0100262STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100263 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000264 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100265 compile_node(comp, pn);
266 total += 1;
267 }
268 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000269 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100270 for (int i = 0; i < n; i++) {
271 compile_node(comp, pns_list->nodes[i]);
272 }
273 total += n;
274 }
Damien Georgeb9791222014-01-23 00:34:21 +0000275 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100276}
Damien429d7192013-10-04 19:53:11 +0100277
Damien George969a6b32014-12-10 22:07:04 +0000278STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100279 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000280 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100281}
282
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200283STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100284 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100285 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000286 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100287 }
288 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100289 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100290 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000291 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100292 }
293 return;
Damiend99b0522013-12-21 18:17:45 +0000294 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
295 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
296 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
297 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100298 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000299 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100300 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100301 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000302 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100303 }
Damien George0b2fd912015-02-28 14:37:54 +0000304 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000305 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100306 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000307 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100308 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000309 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100310 }
311 }
312 return;
Damiend99b0522013-12-21 18:17:45 +0000313 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100314 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000315 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100316 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000317 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100318 }
Damiend99b0522013-12-21 18:17:45 +0000319 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100320 c_if_cond(comp, pns->nodes[0], !jump_if, label);
321 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100322 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
323 // cond is something in parenthesis
324 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
325 // empty tuple, acts as false for the condition
326 if (jump_if == false) {
327 EMIT_ARG(jump, label);
328 }
Damien George93b37262016-01-07 13:07:52 +0000329 } else {
330 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100331 // non-empty tuple, acts as true for the condition
332 if (jump_if == true) {
333 EMIT_ARG(jump, label);
334 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100335 }
336 return;
Damien3a205172013-10-12 15:01:56 +0100337 }
338 }
339
340 // nothing special, fall back to default compiling for node and jump
341 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000342 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100343}
344
345typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100346STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100347
pohmelie81ebba72016-01-27 23:23:11 +0300348STATIC 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 +0100349 if (assign_kind != ASSIGN_AUG_STORE) {
350 compile_node(comp, pns->nodes[0]);
351 }
352
Damiend99b0522013-12-21 18:17:45 +0000353 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
354 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300355 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000356 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100357 if (assign_kind != ASSIGN_AUG_STORE) {
358 for (int i = 0; i < n - 1; i++) {
359 compile_node(comp, pns1->nodes[i]);
360 }
361 }
Damiend99b0522013-12-21 18:17:45 +0000362 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
363 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100364 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000365 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100366 if (assign_kind == ASSIGN_AUG_STORE) {
367 EMIT(rot_three);
368 EMIT(store_subscr);
369 } else {
370 compile_node(comp, pns1->nodes[0]);
371 if (assign_kind == ASSIGN_AUG_LOAD) {
372 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100373 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100374 } else {
375 EMIT(store_subscr);
376 }
377 }
Damiend99b0522013-12-21 18:17:45 +0000378 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
379 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100380 if (assign_kind == ASSIGN_AUG_LOAD) {
381 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000382 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100383 } else {
384 if (assign_kind == ASSIGN_AUG_STORE) {
385 EMIT(rot_two);
386 }
Damien Georgeb9791222014-01-23 00:34:21 +0000387 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100388 }
389 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100390 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100391 }
392 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100393 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100394 }
395
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100396 return;
397
398cannot_assign:
399 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100400}
401
Damien George0288cf02014-04-11 11:53:00 +0000402// 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 +0100403STATIC 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 +0000404 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
405
406 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000407 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000408 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
409 EMIT_ARG(unpack_ex, 0, num_tail);
410 have_star_index = 0;
411 }
Damien George963a5a32015-01-16 17:47:07 +0000412 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000413 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000414 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000415 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
416 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100417 } else {
Damien George9d181f62014-04-27 16:55:27 +0100418 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100419 return;
420 }
421 }
422 }
Damien George963a5a32015-01-16 17:47:07 +0000423 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000424 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100425 }
Damien George0288cf02014-04-11 11:53:00 +0000426 if (num_head != 0) {
427 if (0 == have_star_index) {
428 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100429 } else {
Damien George0288cf02014-04-11 11:53:00 +0000430 c_assign(comp, node_head, ASSIGN_STORE);
431 }
432 }
Damien George963a5a32015-01-16 17:47:07 +0000433 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000434 if (num_head + i == have_star_index) {
435 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
436 } else {
437 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100438 }
439 }
440}
441
442// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100443STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000444 assert(!MP_PARSE_NODE_IS_NULL(pn));
445 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000446 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000447 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100448 switch (assign_kind) {
449 case ASSIGN_STORE:
450 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000451 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100452 break;
453 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000454 default:
Damien George542bd6b2015-03-26 14:42:40 +0000455 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100456 break;
457 }
458 } else {
Damien Georgef55a0592017-03-29 12:28:33 +1100459 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100460 }
461 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000462 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000463 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
464 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300465 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100466 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300467 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100468 break;
469
470 case PN_testlist_star_expr:
471 case PN_exprlist:
472 // lhs is a tuple
473 if (assign_kind != ASSIGN_STORE) {
Damien Georgef55a0592017-03-29 12:28:33 +1100474 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100475 }
Damien George0288cf02014-04-11 11:53:00 +0000476 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100477 break;
478
479 case PN_atom_paren:
480 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100482 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100483 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000484 } else {
485 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000486 if (assign_kind != ASSIGN_STORE) {
Damien Georgef55a0592017-03-29 12:28:33 +1100487 goto cannot_assign;
Damien George44f65c02015-03-25 23:06:48 +0000488 }
Damiend99b0522013-12-21 18:17:45 +0000489 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100490 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100491 }
492 break;
493
494 case PN_atom_bracket:
495 // lhs is something in brackets
496 if (assign_kind != ASSIGN_STORE) {
Damien Georgef55a0592017-03-29 12:28:33 +1100497 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100498 }
Damiend99b0522013-12-21 18:17:45 +0000499 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100500 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000501 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000502 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
503 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100504 goto testlist_comp;
505 } else {
506 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000507 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100508 }
509 break;
510
511 default:
Damien George9d181f62014-04-27 16:55:27 +0100512 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100513 }
514 return;
515
516 testlist_comp:
517 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000518 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
519 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
520 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100521 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000522 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000523 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000524 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100525 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000526 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
527 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000528 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien George9d181f62014-04-27 16:55:27 +0100529 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100530 } else {
531 // sequence with 2 items
532 goto sequence_with_2_items;
533 }
534 } else {
535 // sequence with 2 items
536 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000537 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100538 }
539 return;
540 }
541 return;
542
Damien George9d181f62014-04-27 16:55:27 +0100543 cannot_assign:
544 compile_syntax_error(comp, pn, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100545}
546
Damien George65dc9602015-08-14 12:24:11 +0100547// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100548// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
549// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
550// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100551STATIC 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 +0100552 assert(n_pos_defaults >= 0);
553 assert(n_kw_defaults >= 0);
554
Damien George3a3db4d2015-10-22 23:45:37 +0100555 // set flags
556 if (n_kw_defaults > 0) {
557 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
558 }
559 this_scope->num_def_pos_args = n_pos_defaults;
560
Damien429d7192013-10-04 19:53:11 +0100561 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000562 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100563 int nfree = 0;
564 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000565 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
566 id_info_t *id = &comp->scope_cur->id_info[i];
567 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
568 for (int j = 0; j < this_scope->id_info_len; j++) {
569 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100570 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000571 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000572 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000573 nfree += 1;
574 }
575 }
Damien429d7192013-10-04 19:53:11 +0100576 }
577 }
578 }
Damien429d7192013-10-04 19:53:11 +0100579
580 // make the function/closure
581 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100582 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100583 } else {
Damien George3558f622014-04-20 17:50:40 +0100584 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100585 }
586}
587
Damien George2c838942015-11-17 14:00:14 +0000588STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
589 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
590 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100591 comp->have_star = true;
592 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000593 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
594 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100595 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100596 } else {
597 // named star
Damien429d7192013-10-04 19:53:11 +0100598 }
Damien George8b19db02014-04-11 23:25:34 +0100599 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000600
Damien George2c838942015-11-17 14:00:14 +0000601 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
602 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100603 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000604 // TODO do we need to do anything with this?
605
606 } else {
607 mp_parse_node_t pn_id;
608 mp_parse_node_t pn_colon;
609 mp_parse_node_t pn_equal;
610 if (MP_PARSE_NODE_IS_ID(pn)) {
611 // this parameter is just an id
612
613 pn_id = pn;
614 pn_colon = MP_PARSE_NODE_NULL;
615 pn_equal = MP_PARSE_NODE_NULL;
616
Damien George2c838942015-11-17 14:00:14 +0000617 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000618 // this parameter has a colon and/or equal specifier
619
620 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
621 pn_id = pns->nodes[0];
622 pn_colon = pns->nodes[1];
623 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000624
625 } else {
626 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
627 // this parameter has an equal specifier
628
629 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
630 pn_id = pns->nodes[0];
631 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000632 }
633
634 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
635 // this parameter does not have a default value
636
637 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100638 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100639 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000640 return;
641 }
642
643 } else {
644 // this parameter has a default value
645 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
646
Damien George8b19db02014-04-11 23:25:34 +0100647 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000648 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000649 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
650 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100651 // in Micro Python we put the default positional parameters into a tuple using the bytecode
652 // we need to do this here before we start building the map for the default keywords
653 if (comp->num_default_params > 0) {
654 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100655 } else {
656 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100657 }
Damien George69b89d22014-04-11 13:38:30 +0000658 // first default dict param, so make the map
659 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000660 }
Damien Georgef0778a72014-06-07 22:01:00 +0100661
662 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000663 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000664 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000665 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000666 } else {
Damien George69b89d22014-04-11 13:38:30 +0000667 comp->num_default_params += 1;
668 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000669 }
670 }
671
672 // TODO pn_colon not implemented
673 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100674 }
675}
676
Damien George2c838942015-11-17 14:00:14 +0000677STATIC 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 +0000678 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
679 // expression for default arguments, which may contain a lambda. The lambda will
680 // call here in a nested way, so we must save and restore the relevant state.
681 bool orig_have_star = comp->have_star;
682 uint16_t orig_num_dict_params = comp->num_dict_params;
683 uint16_t orig_num_default_params = comp->num_default_params;
684
Damien George2c838942015-11-17 14:00:14 +0000685 // compile default parameters
686 comp->have_star = false;
687 comp->num_dict_params = 0;
688 comp->num_default_params = 0;
689 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
690
691 if (comp->compile_error != MP_OBJ_NULL) {
692 return;
693 }
694
695 // in Micro Python we put the default positional parameters into a tuple using the bytecode
696 // the default keywords args may have already made the tuple; if not, do it now
697 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
698 EMIT_ARG(build_tuple, comp->num_default_params);
699 EMIT(load_null); // sentinel indicating empty default keyword args
700 }
701
702 // make the function
703 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000704
705 // restore state
706 comp->have_star = orig_have_star;
707 comp->num_dict_params = orig_num_dict_params;
708 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000709}
710
Damien429d7192013-10-04 19:53:11 +0100711// leaves function object on stack
712// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000713STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100714 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100715 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000716 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100717 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000718 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100719 }
720
Damien429d7192013-10-04 19:53:11 +0100721 // get the scope for this function
722 scope_t *fscope = (scope_t*)pns->nodes[4];
723
Damien George2c838942015-11-17 14:00:14 +0000724 // compile the function definition
725 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100726
Damien429d7192013-10-04 19:53:11 +0100727 // return its name (the 'f' in "def f(...):")
728 return fscope->simple_name;
729}
730
731// leaves class object on stack
732// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000733STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100734 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100735 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000736 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100737 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000738 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100739 }
740
741 EMIT(load_build_class);
742
743 // scope for this class
744 scope_t *cscope = (scope_t*)pns->nodes[3];
745
746 // compile the class
747 close_over_variables_etc(comp, cscope, 0, 0);
748
749 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000750 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100751
752 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100753 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
754 mp_parse_node_t parents = pns->nodes[1];
755 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
756 parents = MP_PARSE_NODE_NULL;
757 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000758 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100759 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100760
761 // return its name (the 'C' in class C(...):")
762 return cscope->simple_name;
763}
764
Damien6cdd3af2013-10-05 18:08:26 +0100765// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200766STATIC 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 +0000767 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100768 return false;
769 }
770
771 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100772 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100773 return true;
774 }
775
Damiend99b0522013-12-21 18:17:45 +0000776 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100777 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100778 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100779#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000780 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100781 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000782 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100783 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100784#endif
Damien Georgead297a12016-12-09 13:17:49 +1100785 #if MICROPY_EMIT_INLINE_ASM
786 } else if (attr == ASM_DECORATOR_QSTR) {
787 *emit_options = MP_EMIT_OPT_ASM;
788 #endif
Damien6cdd3af2013-10-05 18:08:26 +0100789 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100790 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100791 }
792
793 return true;
794}
795
Damien George969a6b32014-12-10 22:07:04 +0000796STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100797 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000798 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000799 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100800
Damien6cdd3af2013-10-05 18:08:26 +0100801 // inherit emit options for this function/class definition
802 uint emit_options = comp->scope_cur->emit_options;
803
804 // compile each decorator
805 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100806 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000807 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
808 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100809
810 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000811 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000812 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100813
814 // check for built-in decorators
815 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
816 // this was a built-in
817 num_built_in_decorators += 1;
818
819 } else {
820 // not a built-in, compile normally
821
822 // compile the decorator function
823 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000824 for (int j = 1; j < name_len; j++) {
825 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
826 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100827 }
828
829 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000830 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100831 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000832 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100833 compile_node(comp, pns_decorator->nodes[1]);
834 }
Damien429d7192013-10-04 19:53:11 +0100835 }
836 }
837
pohmelie81ebba72016-01-27 23:23:11 +0300838 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000839 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100840 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000841 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100842 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300843 #if MICROPY_PY_ASYNC_AWAIT
844 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
845 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
846 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
847 body_name = compile_funcdef_helper(comp, pns0, emit_options);
848 scope_t *fscope = (scope_t*)pns0->nodes[4];
849 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
850 #endif
Damien429d7192013-10-04 19:53:11 +0100851 } else {
Damien George0bb97132015-02-27 14:25:47 +0000852 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
853 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100854 }
855
856 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100857 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100858 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100859 }
860
861 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000862 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100863}
864
Damien George969a6b32014-12-10 22:07:04 +0000865STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100866 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100867 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000868 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100869}
870
Damien George6be0b0a2014-08-15 14:30:52 +0100871STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000872 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000873 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300874 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000875 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100876
pohmelie81ebba72016-01-27 23:23:11 +0300877 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100878
Damiend99b0522013-12-21 18:17:45 +0000879 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
880 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300881 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000882 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100883 for (int i = 0; i < n - 1; i++) {
884 compile_node(comp, pns1->nodes[i]);
885 }
Damiend99b0522013-12-21 18:17:45 +0000886 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
887 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100888 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000889 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100890 compile_node(comp, pns1->nodes[0]);
891 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000892 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
893 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000894 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100895 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100896 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100897 }
898 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100899 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100900 }
901
Damiend99b0522013-12-21 18:17:45 +0000902 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
903 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000904 if (MP_PARSE_NODE_IS_NULL(pn)) {
905 goto cannot_delete;
906 } else {
907 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000908 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100909 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
910
Damiend99b0522013-12-21 18:17:45 +0000911 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
912 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
913 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100914 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000915 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100916 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000917 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100918 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000919 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100920 c_del_stmt(comp, pns->nodes[0]);
921 for (int i = 0; i < n; i++) {
922 c_del_stmt(comp, pns1->nodes[i]);
923 }
Damien George216a7112016-09-30 14:48:06 +1000924 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100925 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100926 } else {
927 // sequence with 2 items
928 goto sequence_with_2_items;
929 }
930 } else {
931 // sequence with 2 items
932 sequence_with_2_items:
933 c_del_stmt(comp, pns->nodes[0]);
934 c_del_stmt(comp, pns->nodes[1]);
935 }
Damien429d7192013-10-04 19:53:11 +0100936 }
937 } else {
Damien George93b37262016-01-07 13:07:52 +0000938 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100939 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100940 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100941
942 return;
943
944cannot_delete:
945 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100946}
947
Damien George969a6b32014-12-10 22:07:04 +0000948STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100949 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
950}
951
Damien George969a6b32014-12-10 22:07:04 +0000952STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100953 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100954 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100955 }
Damien George090c9232014-10-17 14:08:49 +0000956 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000957 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100958}
959
Damien George969a6b32014-12-10 22:07:04 +0000960STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100961 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100962 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100963 }
Damien George090c9232014-10-17 14:08:49 +0000964 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000965 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100966}
967
Damien George969a6b32014-12-10 22:07:04 +0000968STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100969 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100970 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100971 return;
972 }
Damiend99b0522013-12-21 18:17:45 +0000973 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100974 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000975 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000976 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100977 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000978 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
979 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 +0100980
Damien George6f355fd2014-04-10 14:11:31 +0100981 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100982 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
983 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
984 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000985 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100986 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
987 } else {
988 compile_node(comp, pns->nodes[0]);
989 }
990 EMIT(return_value);
991}
992
Damien George969a6b32014-12-10 22:07:04 +0000993STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100994 compile_node(comp, pns->nodes[0]);
995 EMIT(pop_top);
996}
997
Damien George969a6b32014-12-10 22:07:04 +0000998STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000999 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001000 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001001 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001002 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001003 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001004 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001005 compile_node(comp, pns->nodes[0]);
1006 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001007 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001008 } else {
1009 // raise x
1010 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001011 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001012 }
1013}
1014
Damien George635543c2014-04-10 12:56:52 +01001015// q_base holds the base of the name
1016// eg a -> q_base=a
1017// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001018STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001019 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001020 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1021 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001022 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001023 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001024 pn = pns->nodes[0];
1025 is_as = true;
1026 }
Damien George635543c2014-04-10 12:56:52 +01001027 if (MP_PARSE_NODE_IS_NULL(pn)) {
1028 // empty name (eg, from . import x)
1029 *q_base = MP_QSTR_;
1030 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1031 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001032 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001033 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001034 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001035 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001036 }
Damien George635543c2014-04-10 12:56:52 +01001037 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001038 } else {
1039 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001040 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001041 {
Damien429d7192013-10-04 19:53:11 +01001042 // a name of the form a.b.c
1043 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001044 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001045 }
Damiend99b0522013-12-21 18:17:45 +00001046 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001047 int len = n - 1;
1048 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001049 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001050 }
Damien George55baff42014-01-21 21:40:13 +00001051 byte *q_ptr;
1052 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001053 for (int i = 0; i < n; i++) {
1054 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001055 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001056 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001057 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001058 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001059 memcpy(str_dest, str_src, str_src_len);
1060 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001061 }
Damien George635543c2014-04-10 12:56:52 +01001062 qstr q_full = qstr_build_end(q_ptr);
1063 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001064 if (is_as) {
1065 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001066 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001067 }
1068 }
Damien429d7192013-10-04 19:53:11 +01001069 }
Damien429d7192013-10-04 19:53:11 +01001070 }
1071}
1072
Damien George6be0b0a2014-08-15 14:30:52 +01001073STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001074 EMIT_ARG(load_const_small_int, 0); // level 0 import
1075 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1076 qstr q_base;
1077 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001078 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001079}
1080
Damien George969a6b32014-12-10 22:07:04 +00001081STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001082 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1083}
1084
Damien George969a6b32014-12-10 22:07:04 +00001085STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001086 mp_parse_node_t pn_import_source = pns->nodes[0];
1087
1088 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1089 uint import_level = 0;
1090 do {
1091 mp_parse_node_t pn_rel;
1092 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)) {
1093 // This covers relative imports with dots only like "from .. import"
1094 pn_rel = pn_import_source;
1095 pn_import_source = MP_PARSE_NODE_NULL;
1096 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1097 // This covers relative imports starting with dot(s) like "from .foo import"
1098 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1099 pn_rel = pns_2b->nodes[0];
1100 pn_import_source = pns_2b->nodes[1];
1101 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1102 } else {
1103 // Not a relative import
1104 break;
1105 }
1106
1107 // get the list of . and/or ...'s
1108 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001109 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001110
1111 // count the total number of .'s
1112 for (int i = 0; i < n; i++) {
1113 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1114 import_level++;
1115 } else {
1116 // should be an MP_TOKEN_ELLIPSIS
1117 import_level += 3;
1118 }
1119 }
1120 } while (0);
1121
Damiend99b0522013-12-21 18:17:45 +00001122 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001123 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001124
1125 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001126 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001127 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001128
1129 // do the import
Damien George635543c2014-04-10 12:56:52 +01001130 qstr dummy_q;
1131 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001132 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001133
Damien429d7192013-10-04 19:53:11 +01001134 } else {
Damien George635543c2014-04-10 12:56:52 +01001135 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001136
1137 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001138 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001139 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001140 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001141 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1142 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1143 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001144 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001145 }
Damien Georgeb9791222014-01-23 00:34:21 +00001146 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001147
1148 // do the import
Damien George635543c2014-04-10 12:56:52 +01001149 qstr dummy_q;
1150 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001151 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001152 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1153 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1154 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001155 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001156 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001157 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001158 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001159 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001160 }
1161 }
1162 EMIT(pop_top);
1163 }
1164}
1165
Damien George584ba672014-12-21 17:26:45 +00001166STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1167 bool added;
1168 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001169 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1170 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001171 return;
1172 }
1173 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1174
1175 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1176 id_info = scope_find_global(comp->scope_cur, qst);
1177 if (id_info != NULL) {
1178 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1179 }
1180}
1181
Damien George969a6b32014-12-10 22:07:04 +00001182STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001183 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001184 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001185 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001186 for (int i = 0; i < n; i++) {
1187 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001188 }
1189 }
1190}
1191
Damien George584ba672014-12-21 17:26:45 +00001192STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1193 bool added;
1194 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001195 if (added) {
1196 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1197 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1198 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1199 }
1200 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001201 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001202 }
Damien George584ba672014-12-21 17:26:45 +00001203}
1204
Damien George969a6b32014-12-10 22:07:04 +00001205STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001206 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001207 if (comp->scope_cur->kind == SCOPE_MODULE) {
1208 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1209 return;
1210 }
1211 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001212 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001213 for (int i = 0; i < n; i++) {
1214 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001215 }
1216 }
1217}
1218
Damien George969a6b32014-12-10 22:07:04 +00001219STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001220 // with optimisations enabled we don't compile assertions
1221 if (MP_STATE_VM(mp_optimise_value) != 0) {
1222 return;
1223 }
1224
Damien George6f355fd2014-04-10 14:11:31 +01001225 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001226 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001227 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001228 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001229 // assertion message
1230 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001231 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001232 }
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(raise_varargs, 1);
1234 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001235}
1236
Damien George969a6b32014-12-10 22:07:04 +00001237STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001238 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001239
Damien George65dc9602015-08-14 12:24:11 +01001240 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001241 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001242 uint l_fail = comp_next_label(comp);
1243 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001244
Damien George391db862014-10-17 17:57:33 +00001245 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001246
Damien George65dc9602015-08-14 12:24:11 +01001247 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001248 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001249 goto done;
1250 }
1251
1252 if (
Damien George65dc9602015-08-14 12:24:11 +01001253 // optimisation: don't jump over non-existent elif/else blocks
1254 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001255 // optimisation: don't jump if last instruction was return
1256 && !EMIT(last_emit_was_return_value)
1257 ) {
1258 // jump over elif/else blocks
1259 EMIT_ARG(jump, l_end);
1260 }
1261
1262 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001263 }
1264
Damien George235f9b32014-10-17 17:30:16 +00001265 // compile elif blocks (if any)
1266 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001267 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 +00001268 for (int i = 0; i < n_elif; i++) {
1269 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1270 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001271
Damien George65dc9602015-08-14 12:24:11 +01001272 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001273 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001274 uint l_fail = comp_next_label(comp);
1275 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001276
Damien George391db862014-10-17 17:57:33 +00001277 compile_node(comp, pns_elif->nodes[1]); // elif block
1278
Damien George65dc9602015-08-14 12:24:11 +01001279 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001280 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001281 goto done;
1282 }
1283
1284 // optimisation: don't jump if last instruction was return
1285 if (!EMIT(last_emit_was_return_value)) {
1286 EMIT_ARG(jump, l_end);
1287 }
1288 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001289 }
1290 }
1291
1292 // compile else block
1293 compile_node(comp, pns->nodes[3]); // can be null
1294
Damien George391db862014-10-17 17:57:33 +00001295done:
Damien Georgeb9791222014-01-23 00:34:21 +00001296 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001297}
1298
Damien Georgecbddb272014-02-01 20:08:18 +00001299#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001300 uint16_t old_break_label = comp->break_label; \
1301 uint16_t old_continue_label = comp->continue_label; \
1302 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001303 uint break_label = comp_next_label(comp); \
1304 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001305 comp->break_label = break_label; \
1306 comp->continue_label = continue_label; \
1307 comp->break_continue_except_level = comp->cur_except_level;
1308
1309#define END_BREAK_CONTINUE_BLOCK \
1310 comp->break_label = old_break_label; \
1311 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001312 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001313
Damien George969a6b32014-12-10 22:07:04 +00001314STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001315 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001316
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001317 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 +00001318 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001319 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 +00001320 EMIT_ARG(jump, continue_label);
1321 }
1322 EMIT_ARG(label_assign, top_label);
1323 compile_node(comp, pns->nodes[1]); // body
1324 EMIT_ARG(label_assign, continue_label);
1325 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1326 }
Damience89a212013-10-15 22:25:17 +01001327
1328 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001329 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001330
1331 compile_node(comp, pns->nodes[2]); // else
1332
Damien Georgeb9791222014-01-23 00:34:21 +00001333 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001334}
1335
Damien Georgee181c0d2014-12-12 17:19:56 +00001336// This function compiles an optimised for-loop of the form:
1337// for <var> in range(<start>, <end>, <step>):
1338// <body>
1339// else:
1340// <else>
1341// <var> must be an identifier and <step> must be a small-int.
1342//
1343// Semantics of for-loop require:
1344// - final failing value should not be stored in the loop variable
1345// - if the loop never runs, the loop variable should never be assigned
1346// - assignments to <var>, <end> or <step> in the body do not alter the loop
1347// (<step> is a constant for us, so no need to worry about it changing)
1348//
1349// If <end> is a small-int, then the stack during the for-loop contains just
1350// the current value of <var>. Otherwise, the stack contains <end> then the
1351// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001352STATIC 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 +00001353 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001354
Damien George6f355fd2014-04-10 14:11:31 +01001355 uint top_label = comp_next_label(comp);
1356 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001357
Damien Georgee181c0d2014-12-12 17:19:56 +00001358 // put the end value on the stack if it's not a small-int constant
1359 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1360 if (end_on_stack) {
1361 compile_node(comp, pn_end);
1362 }
1363
1364 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001365 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001366
Damien Georgeb9791222014-01-23 00:34:21 +00001367 EMIT_ARG(jump, entry_label);
1368 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001369
Damien Georgec33ce602014-12-11 17:35:23 +00001370 // duplicate next value and store it to var
1371 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001372 c_assign(comp, pn_var, ASSIGN_STORE);
1373
Damienf3822fc2013-11-09 20:12:03 +00001374 // compile body
1375 compile_node(comp, pn_body);
1376
Damien Georgeb9791222014-01-23 00:34:21 +00001377 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001378
Damien Georgee181c0d2014-12-12 17:19:56 +00001379 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001380 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001381 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001382
Damien Georgeb9791222014-01-23 00:34:21 +00001383 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001384
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001385 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001386 if (end_on_stack) {
1387 EMIT(dup_top_two);
1388 EMIT(rot_two);
1389 } else {
1390 EMIT(dup_top);
1391 compile_node(comp, pn_end);
1392 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001393 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1394 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001395 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001396 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001397 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001398 }
Damien George63f38322015-02-28 15:04:06 +00001399 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001400
1401 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001402 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001403
1404 compile_node(comp, pn_else);
1405
Damien Georgeb9791222014-01-23 00:34:21 +00001406 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001407
1408 // discard final value of var that failed the loop condition
1409 EMIT(pop_top);
1410
1411 // discard <end> value if it's on the stack
1412 if (end_on_stack) {
1413 EMIT(pop_top);
1414 }
Damienf72fd0e2013-11-06 20:20:49 +00001415}
1416
Damien George969a6b32014-12-10 22:07:04 +00001417STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001418 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1419 // this is actually slower, but uses no heap memory
1420 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001421 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 +00001422 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001423 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001424 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgefa03bbf2017-04-22 14:13:37 +10001425 && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pns_it->nodes[1]) == PN_trailer_paren) {
Damiend99b0522013-12-21 18:17:45 +00001426 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1427 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001428 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001429 mp_parse_node_t pn_range_start;
1430 mp_parse_node_t pn_range_end;
1431 mp_parse_node_t pn_range_step;
1432 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001433 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001434 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001435 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001436 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001437 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001438 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001439 } else if (n_args == 2) {
1440 pn_range_start = args[0];
1441 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001442 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001443 } else {
1444 pn_range_start = args[0];
1445 pn_range_end = args[1];
1446 pn_range_step = args[2];
Damien Georgede9b5362017-04-05 10:50:26 +10001447 // the step must be a non-zero constant integer to do the optimisation
1448 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)
1449 || MP_PARSE_NODE_LEAF_SMALL_INT(pn_range_step) == 0) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001450 optimize = false;
1451 }
Damienf72fd0e2013-11-06 20:20:49 +00001452 }
Damien George33ac0fd2015-12-08 21:05:14 +00001453 // arguments must be able to be compiled as standard expressions
1454 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1455 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1456 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1457 optimize = false;
1458 }
1459 }
1460 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1461 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1462 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1463 optimize = false;
1464 }
1465 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001466 }
1467 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001468 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1469 return;
1470 }
1471 }
1472 }
Damienf72fd0e2013-11-06 20:20:49 +00001473
Damien Georgecbddb272014-02-01 20:08:18 +00001474 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001475 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001476
Damien George6f355fd2014-04-10 14:11:31 +01001477 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001478
Damien429d7192013-10-04 19:53:11 +01001479 compile_node(comp, pns->nodes[1]); // iterator
Damien Georgef4df3aa2016-01-09 23:59:52 +00001480 EMIT_ARG(get_iter, true);
Damien Georgecbddb272014-02-01 20:08:18 +00001481 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001482 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001483 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1484 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001485 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001486 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001487 }
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(label_assign, pop_label);
Damien George30b42dd2017-01-17 15:30:18 +11001489 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01001490
1491 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001492 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001493
Damien429d7192013-10-04 19:53:11 +01001494 compile_node(comp, pns->nodes[3]); // else (not tested)
1495
Damien Georgeb9791222014-01-23 00:34:21 +00001496 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001497}
1498
Damien George6be0b0a2014-08-15 14:30:52 +01001499STATIC 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 +01001500 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001501 uint l1 = comp_next_label(comp);
1502 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001503
Damien Georgeb9791222014-01-23 00:34:21 +00001504 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001505 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001506
Damien429d7192013-10-04 19:53:11 +01001507 compile_node(comp, pn_body); // body
1508 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001509 EMIT_ARG(jump, success_label); // jump over exception handler
1510
1511 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001512 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001513
Damien Georgef0406852016-09-27 12:37:21 +10001514 // at this point the top of the stack contains the exception instance that was raised
1515
Damien George6f355fd2014-04-10 14:11:31 +01001516 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001517
1518 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001519 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1520 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001521
1522 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001523 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001524
Damiend99b0522013-12-21 18:17:45 +00001525 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001526 // this is a catch all exception handler
1527 if (i + 1 != n_except) {
Damien George18c059f2017-03-29 12:36:46 +11001528 compile_syntax_error(comp, pn_excepts[i], "default 'except' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001529 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001530 return;
1531 }
1532 } else {
1533 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001534 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1535 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1536 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1537 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001538 // handler binds the exception to a local
1539 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001540 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001541 }
1542 }
1543 EMIT(dup_top);
1544 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001545 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001546 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001547 }
1548
Damien Georgef0406852016-09-27 12:37:21 +10001549 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001550 if (qstr_exception_local == 0) {
1551 EMIT(pop_top);
1552 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001553 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001554 }
1555
Damien George6f355fd2014-04-10 14:11:31 +01001556 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001557 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001558 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001559 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001560 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001561 }
1562 compile_node(comp, pns_except->nodes[1]);
1563 if (qstr_exception_local != 0) {
1564 EMIT(pop_block);
1565 }
1566 EMIT(pop_except);
1567 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1569 EMIT_ARG(label_assign, l3);
1570 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001571 compile_store_id(comp, qstr_exception_local);
1572 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001573
Damien George8dcc0c72014-03-27 10:55:21 +00001574 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001575 EMIT(end_finally);
1576 }
Damien Georgeb9791222014-01-23 00:34:21 +00001577 EMIT_ARG(jump, l2);
1578 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001579 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001580 }
1581
Damien George8dcc0c72014-03-27 10:55:21 +00001582 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001583 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001584 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001585
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001587 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001588 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001589}
1590
Damien George6be0b0a2014-08-15 14:30:52 +01001591STATIC 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 +01001592 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001593
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001595 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001596
Damien429d7192013-10-04 19:53:11 +01001597 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001598 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001599 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001600 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001601 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001602 } else {
1603 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1604 }
1605 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1607 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001608 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001609
Damien George8dcc0c72014-03-27 10:55:21 +00001610 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001611 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001612}
1613
Damien George969a6b32014-12-10 22:07:04 +00001614STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001615 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1616 {
Damiend99b0522013-12-21 18:17:45 +00001617 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1618 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001619 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001620 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1621 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001622 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001623 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001624 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001625 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001626 // no finally
1627 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1628 } else {
1629 // have finally
Damiend99b0522013-12-21 18:17:45 +00001630 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 +01001631 }
1632 } else {
1633 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001634 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001635 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001636 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001637 }
Damien429d7192013-10-04 19:53:11 +01001638 }
1639}
1640
Damien George6be0b0a2014-08-15 14:30:52 +01001641STATIC 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 +01001642 if (n == 0) {
1643 // no more pre-bits, compile the body of the with
1644 compile_node(comp, body);
1645 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001646 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001647 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1648 // we need to allocate an extra label for the native emitter
1649 // it will use l_end+1 as an auxiliary label
1650 comp_next_label(comp);
1651 }
Damiend99b0522013-12-21 18:17:45 +00001652 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001653 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001654 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001655 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001657 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1658 } else {
1659 // this pre-bit is just an expression
1660 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001661 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001662 EMIT(pop_top);
1663 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001664 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001665 // compile additional pre-bits and the body
1666 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1667 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001668 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001669 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001670 EMIT(end_finally);
1671 }
1672}
1673
Damien George969a6b32014-12-10 22:07:04 +00001674STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001675 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001676 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001677 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001678 assert(n > 0);
1679
1680 // compile in a nested fashion
1681 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1682}
1683
pohmelie81ebba72016-01-27 23:23:11 +03001684STATIC void compile_yield_from(compiler_t *comp) {
Damien Georgef4df3aa2016-01-09 23:59:52 +00001685 EMIT_ARG(get_iter, false);
pohmelie81ebba72016-01-27 23:23:11 +03001686 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1687 EMIT(yield_from);
1688}
1689
1690#if MICROPY_PY_ASYNC_AWAIT
1691STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1692 EMIT_ARG(load_method, method);
1693 EMIT_ARG(call_method, 0, 0, 0);
1694 compile_yield_from(comp);
1695}
1696
1697STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1698 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1699
1700 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1701 uint while_else_label = comp_next_label(comp);
1702 uint try_exception_label = comp_next_label(comp);
1703 uint try_else_label = comp_next_label(comp);
1704 uint try_finally_label = comp_next_label(comp);
1705
1706 compile_node(comp, pns->nodes[1]); // iterator
1707 compile_await_object_method(comp, MP_QSTR___aiter__);
1708 compile_store_id(comp, context);
1709
1710 START_BREAK_CONTINUE_BLOCK
1711
1712 EMIT_ARG(label_assign, continue_label);
1713
1714 EMIT_ARG(setup_except, try_exception_label);
1715 compile_increase_except_level(comp);
1716
1717 compile_load_id(comp, context);
1718 compile_await_object_method(comp, MP_QSTR___anext__);
1719 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1720 EMIT(pop_block);
1721 EMIT_ARG(jump, try_else_label);
1722
1723 EMIT_ARG(label_assign, try_exception_label);
1724 EMIT(start_except_handler);
1725 EMIT(dup_top);
1726 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1727 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1728 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001729 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001730 EMIT(pop_except);
1731 EMIT_ARG(jump, while_else_label);
1732
1733 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001734 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001735 compile_decrease_except_level(comp);
1736 EMIT(end_finally);
1737 EMIT(end_except_handler);
1738
1739 EMIT_ARG(label_assign, try_else_label);
1740 compile_node(comp, pns->nodes[2]); // body
1741
1742 EMIT_ARG(jump, continue_label);
1743 // break/continue apply to outer loop (if any) in the else block
1744 END_BREAK_CONTINUE_BLOCK
1745
1746 EMIT_ARG(label_assign, while_else_label);
1747 compile_node(comp, pns->nodes[3]); // else
1748
1749 EMIT_ARG(label_assign, break_label);
1750}
1751
1752STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1753 if (n == 0) {
1754 // no more pre-bits, compile the body of the with
1755 compile_node(comp, body);
1756 } else {
1757 uint try_exception_label = comp_next_label(comp);
1758 uint no_reraise_label = comp_next_label(comp);
1759 uint try_else_label = comp_next_label(comp);
1760 uint end_label = comp_next_label(comp);
1761 qstr context;
1762
1763 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1764 // this pre-bit is of the form "a as b"
1765 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1766 compile_node(comp, pns->nodes[0]);
1767 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1768 compile_store_id(comp, context);
1769 compile_load_id(comp, context);
1770 compile_await_object_method(comp, MP_QSTR___aenter__);
1771 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1772 } else {
1773 // this pre-bit is just an expression
1774 compile_node(comp, nodes[0]);
1775 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1776 compile_store_id(comp, context);
1777 compile_load_id(comp, context);
1778 compile_await_object_method(comp, MP_QSTR___aenter__);
1779 EMIT(pop_top);
1780 }
1781
1782 compile_load_id(comp, context);
1783 EMIT_ARG(load_method, MP_QSTR___aexit__);
1784
1785 EMIT_ARG(setup_except, try_exception_label);
1786 compile_increase_except_level(comp);
1787 // compile additional pre-bits and the body
1788 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1789 // finish this with block
1790 EMIT(pop_block);
1791 EMIT_ARG(jump, try_else_label); // jump over exception handler
1792
1793 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1794 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001795
1796 // at this point the stack contains: ..., __aexit__, self, exc
1797 EMIT(dup_top);
1798 #if MICROPY_CPYTHON_COMPAT
1799 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1800 #else
1801 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001802 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001803 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1804 #endif
1805 EMIT(rot_two);
1806 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1807 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001808 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001809
pohmelie81ebba72016-01-27 23:23:11 +03001810 compile_yield_from(comp);
1811 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1812 EMIT_ARG(raise_varargs, 0);
1813
1814 EMIT_ARG(label_assign, no_reraise_label);
1815 EMIT(pop_except);
1816 EMIT_ARG(jump, end_label);
1817
Damien Georgeb32c01b2016-09-28 11:52:13 +10001818 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001819 compile_decrease_except_level(comp);
1820 EMIT(end_finally);
1821 EMIT(end_except_handler);
1822
1823 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1824 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1825 EMIT(dup_top);
1826 EMIT(dup_top);
1827 EMIT_ARG(call_method, 3, 0, 0);
1828 compile_yield_from(comp);
1829 EMIT(pop_top);
1830
1831 EMIT_ARG(label_assign, end_label);
1832
1833 }
1834}
1835
1836STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1837 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1838 mp_parse_node_t *nodes;
1839 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1840 assert(n > 0);
1841
1842 // compile in a nested fashion
1843 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1844}
1845
1846STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1847 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1848 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1849 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1850 // async def
1851 compile_funcdef(comp, pns0);
1852 scope_t *fscope = (scope_t*)pns0->nodes[4];
1853 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1854 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1855 // async for
1856 compile_async_for_stmt(comp, pns0);
1857 } else {
1858 // async with
1859 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1860 compile_async_with_stmt(comp, pns0);
1861 }
1862}
1863#endif
1864
Damien George969a6b32014-12-10 22:07:04 +00001865STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001866 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001867 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1868 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001869 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001870 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001871 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001872 EMIT(pop_top);
1873
Damien429d7192013-10-04 19:53:11 +01001874 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001875 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001876 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George7d414a12015-02-08 01:57:40 +00001877 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001878 // do nothing with a lonely constant
1879 } else {
1880 compile_node(comp, pns->nodes[0]); // just an expression
1881 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1882 }
Damien429d7192013-10-04 19:53:11 +01001883 }
Damien Georgeb948de32015-10-08 14:26:01 +01001884 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001885 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1886 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001887 if (kind == PN_expr_stmt_augassign) {
1888 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1889 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001890 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001891 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001892 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001893 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1894 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1895 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1896 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1897 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1898 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1899 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1900 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1901 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1902 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1903 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001904 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001905 }
Damien George7e5fb242014-02-01 22:18:47 +00001906 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001907 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1908 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001909 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001910 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001911 // following CPython, we store left-most first
1912 if (rhs > 0) {
1913 EMIT(dup_top);
1914 }
1915 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1916 for (int i = 0; i < rhs; i++) {
1917 if (i + 1 < rhs) {
1918 EMIT(dup_top);
1919 }
Damien Georgeb948de32015-10-08 14:26:01 +01001920 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001921 }
Damien George0bb97132015-02-27 14:25:47 +00001922 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001923 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001924 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001925 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001926 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001927 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001928 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001929 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001930 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001931 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001932 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1933 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1934 // can't optimise when it's a star expression on the lhs
1935 goto no_optimisation;
1936 }
Damien429d7192013-10-04 19:53:11 +01001937 compile_node(comp, pns10->nodes[0]); // rhs
1938 compile_node(comp, pns10->nodes[1]); // rhs
1939 EMIT(rot_two);
1940 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1941 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001942 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001943 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001944 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001945 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001946 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001947 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001948 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001949 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001950 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1951 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1952 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1953 // can't optimise when it's a star expression on the lhs
1954 goto no_optimisation;
1955 }
Damien429d7192013-10-04 19:53:11 +01001956 compile_node(comp, pns10->nodes[0]); // rhs
1957 compile_node(comp, pns10->nodes[1]); // rhs
1958 compile_node(comp, pns10->nodes[2]); // rhs
1959 EMIT(rot_three);
1960 EMIT(rot_two);
1961 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1962 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1963 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1964 } else {
Damien George495d7812014-04-08 17:51:47 +01001965 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001966 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001967 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1968 }
Damien429d7192013-10-04 19:53:11 +01001969 }
Damien Georgeb948de32015-10-08 14:26:01 +01001970 } else {
1971 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001972 }
1973}
1974
Damien George6be0b0a2014-08-15 14:30:52 +01001975STATIC 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 +00001976 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001977 compile_node(comp, pns->nodes[0]);
1978 for (int i = 1; i < num_nodes; i += 1) {
1979 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982}
1983
Damien George969a6b32014-12-10 22:07:04 +00001984STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001985 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1986 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001987
Damien George6f355fd2014-04-10 14:11:31 +01001988 uint l_fail = comp_next_label(comp);
1989 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001990 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1991 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(jump, l_end);
1993 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001994 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001995 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001996 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001997}
1998
Damien George969a6b32014-12-10 22:07:04 +00001999STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01002000 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002001 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002002 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 +01002003 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002004 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002005 }
2006
2007 // get the scope for this lambda
2008 scope_t *this_scope = (scope_t*)pns->nodes[2];
2009
Damien George2c838942015-11-17 14:00:14 +00002010 // compile the lambda definition
2011 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01002012}
2013
Damien George7711afb2015-02-28 15:10:18 +00002014STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002015 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002016 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002017 for (int i = 0; i < n; i += 1) {
2018 compile_node(comp, pns->nodes[i]);
2019 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002020 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002021 }
2022 }
Damien Georgeb9791222014-01-23 00:34:21 +00002023 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002024}
2025
Damien George7711afb2015-02-28 15:10:18 +00002026STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2027 compile_or_and_test(comp, pns, true);
2028}
2029
Damien George969a6b32014-12-10 22:07:04 +00002030STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002031 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002032}
2033
Damien George969a6b32014-12-10 22:07:04 +00002034STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002035 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002036 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002037}
2038
Damien George969a6b32014-12-10 22:07:04 +00002039STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002040 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002041 compile_node(comp, pns->nodes[0]);
2042 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002043 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002044 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002045 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002046 }
2047 for (int i = 1; i + 1 < num_nodes; i += 2) {
2048 compile_node(comp, pns->nodes[i + 1]);
2049 if (i + 2 < num_nodes) {
2050 EMIT(dup_top);
2051 EMIT(rot_three);
2052 }
Damien George7e5fb242014-02-01 22:18:47 +00002053 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002054 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002055 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002056 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2057 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2058 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2059 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2060 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2061 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002062 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002063 }
2064 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002065 } else {
2066 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002067 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2068 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002069 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002070 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002071 } else {
2072 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002073 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002074 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002075 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002076 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002077 }
Damien429d7192013-10-04 19:53:11 +01002078 }
Damien429d7192013-10-04 19:53:11 +01002079 }
2080 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002081 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002082 }
2083 }
2084 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002085 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(jump, l_end);
2087 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002088 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002089 EMIT(rot_two);
2090 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002091 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002092 }
2093}
2094
Damien George969a6b32014-12-10 22:07:04 +00002095STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002096 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002097}
2098
Damien George969a6b32014-12-10 22:07:04 +00002099STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002101}
2102
Damien George969a6b32014-12-10 22:07:04 +00002103STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002104 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002105}
2106
Damien George969a6b32014-12-10 22:07:04 +00002107STATIC void compile_and_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_AND);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damien George969a6b32014-12-10 22:07:04 +00002111STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002112 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002113 compile_node(comp, pns->nodes[0]);
2114 for (int i = 1; i + 1 < num_nodes; i += 2) {
2115 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002116 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002118 } else {
Damien George0bb97132015-02-27 14:25:47 +00002119 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2120 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002121 }
2122 }
2123}
2124
Damien George969a6b32014-12-10 22:07:04 +00002125STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002126 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002127 compile_node(comp, pns->nodes[0]);
2128 for (int i = 1; i + 1 < num_nodes; i += 2) {
2129 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002130 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002132 } else {
Damien George0bb97132015-02-27 14:25:47 +00002133 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2134 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002135 }
2136 }
2137}
2138
Damien George969a6b32014-12-10 22:07:04 +00002139STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002140 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002141 compile_node(comp, pns->nodes[0]);
2142 for (int i = 1; i + 1 < num_nodes; i += 2) {
2143 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002144 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002145 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002146 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002147 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002148 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002150 } else {
Damien George0bb97132015-02-27 14:25:47 +00002151 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2152 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002153 }
2154 }
2155}
2156
Damien George969a6b32014-12-10 22:07:04 +00002157STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002158 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002160 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002162 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002163 } else {
Damien George0bb97132015-02-27 14:25:47 +00002164 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2165 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002166 }
2167}
2168
pohmelie81ebba72016-01-27 23:23:11 +03002169STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002170 // this is to handle special super() call
2171 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2172
2173 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002174}
Damien George3acaa282016-03-16 13:04:51 +00002175
pohmelie81ebba72016-01-27 23:23:11 +03002176STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2177 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2178 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002179}
2180
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002181STATIC 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 +01002182 // function to call is on top of stack
2183
Damien George35e2a4e2014-02-05 00:51:47 +00002184 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002185 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 +00002186 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002187 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002188 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George40c12722017-03-27 11:27:08 +11002189 id_info_t *id = &comp->scope_cur->id_info[i];
2190 if (id->flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002191 // first argument found; load it and call super
Damien George40c12722017-03-27 11:27:08 +11002192 compile_load_id(comp, id->qst);
Damien George01039b52014-12-21 17:44:27 +00002193 EMIT_ARG(call_function, 2, 0, 0);
2194 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002195 }
2196 }
Damien George01039b52014-12-21 17:44:27 +00002197 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002198 return;
2199 }
Damien George35e2a4e2014-02-05 00:51:47 +00002200
Damien George2c0842b2014-04-27 16:46:51 +01002201 // get the list of arguments
2202 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002203 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002204
Damien George2c0842b2014-04-27 16:46:51 +01002205 // compile the arguments
2206 // Rather than calling compile_node on the list, we go through the list of args
2207 // explicitly here so that we can count the number of arguments and give sensible
2208 // error messages.
2209 int n_positional = n_positional_extra;
2210 uint n_keyword = 0;
2211 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002212 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002213 for (int i = 0; i < n_args; i++) {
2214 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2215 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2216 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2217 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2218 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2219 return;
2220 }
2221 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002222 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002223 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2224 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
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_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002229 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002230 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002231 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002232 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2233 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2234 return;
2235 }
Damien George59fba2d2015-06-25 14:42:13 +00002236 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002237 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002238 n_keyword += 1;
2239 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002240 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2241 n_positional++;
2242 }
2243 } else {
2244 goto normal_argument;
2245 }
2246 } else {
2247 normal_argument:
2248 if (n_keyword > 0) {
2249 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2250 return;
2251 }
2252 compile_node(comp, args[i]);
2253 n_positional++;
2254 }
Damien429d7192013-10-04 19:53:11 +01002255 }
2256
Delio Brignolie6978a42015-09-17 12:07:06 +02002257 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002258 // if we had one but not the other then we load "null" as a place holder
2259 if (star_flags != 0) {
2260 if (star_args_node == NULL) {
2261 EMIT(load_null);
2262 } else {
2263 compile_node(comp, star_args_node->nodes[0]);
2264 }
2265 if (dblstar_args_node == NULL) {
2266 EMIT(load_null);
2267 } else {
2268 compile_node(comp, dblstar_args_node->nodes[0]);
2269 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002270 }
2271
Damien George2c0842b2014-04-27 16:46:51 +01002272 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002273 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002274 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002275 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002276 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002277 }
Damien429d7192013-10-04 19:53:11 +01002278}
2279
pohmelie81ebba72016-01-27 23:23:11 +03002280STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002282 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002283 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 +01002284 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002285 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2286 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002287 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002288 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002289 i += 1;
2290 } else {
2291 compile_node(comp, pns->nodes[i]);
2292 }
Damien George35e2a4e2014-02-05 00:51:47 +00002293 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002294 }
2295}
2296
Damien429d7192013-10-04 19:53:11 +01002297// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002298STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002299 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2300 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2301 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002302
Damien George36db6bc2014-05-07 17:24:22 +01002303 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002304 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002305 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 +01002306 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002307 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002308 }
2309
2310 // get the scope for this comprehension
2311 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2312
2313 // compile the comprehension
2314 close_over_variables_etc(comp, this_scope, 0, 0);
2315
2316 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
Damien George4d2bab12017-02-10 15:39:33 +11002317 if (kind == SCOPE_GEN_EXPR) {
2318 EMIT_ARG(get_iter, false);
2319 }
Damien George922ddd62014-04-09 12:43:17 +01002320 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002321}
2322
Damien George969a6b32014-12-10 22:07:04 +00002323STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002324 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002325 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002326 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002327 } else {
2328 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002329 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2330 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2331 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2332 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2333 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002334 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002335 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002336 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002337 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002338 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002339 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002341 // generator expression
2342 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2343 } else {
2344 // tuple with 2 items
2345 goto tuple_with_2_items;
2346 }
2347 } else {
2348 // tuple with 2 items
2349 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002350 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002351 }
Damien429d7192013-10-04 19:53:11 +01002352 }
2353}
2354
Damien George969a6b32014-12-10 22:07:04 +00002355STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002356 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002357 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002358 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002359 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2360 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2361 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2362 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2363 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002364 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002365 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002366 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002367 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002368 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002369 // list of many items
2370 compile_node(comp, pns2->nodes[0]);
2371 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002372 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002373 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002374 // list comprehension
2375 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2376 } else {
2377 // list with 2 items
2378 goto list_with_2_items;
2379 }
2380 } else {
2381 // list with 2 items
2382 list_with_2_items:
2383 compile_node(comp, pns2->nodes[0]);
2384 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002385 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002386 }
2387 } else {
2388 // list with 1 item
2389 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002391 }
2392}
2393
Damien George969a6b32014-12-10 22:07:04 +00002394STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002395 mp_parse_node_t pn = pns->nodes[0];
2396 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002397 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002398 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002399 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2400 pns = (mp_parse_node_struct_t*)pn;
2401 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002402 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002403 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002404 compile_node(comp, pn);
2405 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2407 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2408 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2409 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002410 // dict/set with multiple elements
2411
2412 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002413 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002414 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002415
2416 // first element sets whether it's a dict or set
2417 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002418 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002419 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002420 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002421 compile_node(comp, pns->nodes[0]);
2422 EMIT(store_map);
2423 is_dict = true;
2424 } else {
2425 // a set
2426 compile_node(comp, pns->nodes[0]); // 1st value of set
2427 is_dict = false;
2428 }
2429
2430 // process rest of elements
2431 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002432 mp_parse_node_t pn_i = nodes[i];
2433 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2434 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002435 if (is_dict) {
2436 if (!is_key_value) {
Damien Georgef9b0e642017-03-29 12:44:27 +11002437 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
2438 compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax");
2439 } else {
2440 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dict");
2441 }
Damien429d7192013-10-04 19:53:11 +01002442 return;
2443 }
2444 EMIT(store_map);
2445 } else {
2446 if (is_key_value) {
Damien Georgef9b0e642017-03-29 12:44:27 +11002447 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
2448 compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax");
2449 } else {
2450 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
2451 }
Damien429d7192013-10-04 19:53:11 +01002452 return;
2453 }
2454 }
2455 }
2456
Damien Georgee37dcaa2014-12-27 17:07:16 +00002457 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002458 // if it's a set, build it
2459 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002461 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002462 #endif
Damien George0bb97132015-02-27 14:25:47 +00002463 } else {
2464 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002465 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002466 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002467 // a dictionary comprehension
2468 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2469 } else {
2470 // a set comprehension
2471 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2472 }
Damien429d7192013-10-04 19:53:11 +01002473 }
2474 } else {
2475 // set with one element
2476 goto set_with_one_element;
2477 }
2478 } else {
2479 // set with one element
2480 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002481 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002482 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002483 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002484 #else
2485 assert(0);
2486 #endif
Damien429d7192013-10-04 19:53:11 +01002487 }
2488}
2489
Damien George969a6b32014-12-10 22:07:04 +00002490STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002491 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002492}
2493
Damien George969a6b32014-12-10 22:07:04 +00002494STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002495 // object who's index we want is on top of stack
2496 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002497 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002498}
2499
Damien George969a6b32014-12-10 22:07:04 +00002500STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002501 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002502 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002503}
2504
Damien George83204f32014-12-27 17:20:41 +00002505#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002506STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002507 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2508 mp_parse_node_t pn = pns->nodes[0];
2509 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002510 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002511 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2512 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002513 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2514 pns = (mp_parse_node_struct_t*)pn;
2515 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002517 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002518 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002519 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002520 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002521 } else {
2522 // [?::x]
2523 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002524 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002525 }
Damiend99b0522013-12-21 18:17:45 +00002526 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002527 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002528 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2529 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2530 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2531 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002532 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002534 } else {
2535 // [?:x:x]
2536 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002538 }
2539 } else {
2540 // [?:x]
2541 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002543 }
2544 } else {
2545 // [?:x]
2546 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002547 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002548 }
2549}
2550
Damien George969a6b32014-12-10 22:07:04 +00002551STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002552 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002553 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2554 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002555}
2556
Damien George969a6b32014-12-10 22:07:04 +00002557STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002558 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002559 compile_subscript_3_helper(comp, pns);
2560}
Damien George83204f32014-12-27 17:20:41 +00002561#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002562
Damien George969a6b32014-12-10 22:07:04 +00002563STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002564 // if this is called then we are compiling a dict key:value pair
2565 compile_node(comp, pns->nodes[1]); // value
2566 compile_node(comp, pns->nodes[0]); // key
2567}
2568
Damien George969a6b32014-12-10 22:07:04 +00002569STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002570 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002571 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002572 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002573}
2574
Damien George969a6b32014-12-10 22:07:04 +00002575STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002576 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002577 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002578 return;
2579 }
Damiend99b0522013-12-21 18:17:45 +00002580 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002581 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002582 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002583 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2584 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002585 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002586 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002587 } else {
2588 compile_node(comp, pns->nodes[0]);
2589 EMIT(yield_value);
2590 }
2591}
2592
pohmelie81ebba72016-01-27 23:23:11 +03002593#if MICROPY_PY_ASYNC_AWAIT
2594STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2595 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2596 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2597 return;
2598 }
2599 compile_atom_expr_normal(comp, pns);
2600 compile_yield_from(comp);
2601}
2602#endif
2603
Damien George52552552017-02-24 13:43:43 +11002604STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) {
2605 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2606 // nodes are 32-bit pointers, but need to extract 64-bit object
2607 return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
2608 #else
2609 return (mp_obj_t)pns->nodes[0];
2610 #endif
Damien George65ef6b72015-01-14 21:17:27 +00002611}
2612
Damien George7d414a12015-02-08 01:57:40 +00002613STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George52552552017-02-24 13:43:43 +11002614 EMIT_ARG(load_const_obj, get_const_object(pns));
Damien George7d414a12015-02-08 01:57:40 +00002615}
2616
Damiend99b0522013-12-21 18:17:45 +00002617typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002618STATIC const compile_function_t compile_function[] = {
Damien George71019ae2017-02-15 10:58:05 +11002619// only define rules with a compile function
Damien429d7192013-10-04 19:53:11 +01002620#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002621#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George71019ae2017-02-15 10:58:05 +11002622#define DEF_RULE_NC(rule, kind, ...)
Damien George51dfcb42015-01-01 20:27:54 +00002623#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002624#undef c
2625#undef DEF_RULE
Damien George71019ae2017-02-15 10:58:05 +11002626#undef DEF_RULE_NC
Damien George7d414a12015-02-08 01:57:40 +00002627 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002628};
2629
Damien George6be0b0a2014-08-15 14:30:52 +01002630STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002631 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002632 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002633 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002634 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002635 #if MICROPY_DYNAMIC_COMPILER
2636 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2637 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2638 // integer fits in target runtime's small-int
2639 EMIT_ARG(load_const_small_int, arg);
2640 } else {
2641 // integer doesn't fit, so create a multi-precision int object
2642 // (but only create the actual object on the last pass)
2643 if (comp->pass != MP_PASS_EMIT) {
2644 EMIT_ARG(load_const_obj, mp_const_none);
2645 } else {
2646 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2647 }
2648 }
2649 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002650 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002651 #endif
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002653 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002654 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002655 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002656 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2657 case MP_PARSE_NODE_BYTES:
2658 // only create and load the actual bytes object on the last pass
2659 if (comp->pass != MP_PASS_EMIT) {
2660 EMIT_ARG(load_const_obj, mp_const_none);
2661 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002662 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002663 const byte *data = qstr_data(arg, &len);
2664 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2665 }
2666 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002667 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002668 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002669 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002670 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002671 // do nothing
2672 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002674 }
2675 break;
Damien429d7192013-10-04 19:53:11 +01002676 }
2677 } else {
Damiend99b0522013-12-21 18:17:45 +00002678 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002679 EMIT_ARG(set_source_line, pns->source_line);
Damien George71019ae2017-02-15 10:58:05 +11002680 assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object);
Damien George65ef6b72015-01-14 21:17:27 +00002681 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002682 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002683 }
2684}
2685
Damien George2ac4af62014-08-15 16:45:41 +01002686STATIC 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 +00002687 // check that **kw is last
2688 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2689 compile_syntax_error(comp, pn, "invalid syntax");
2690 return;
2691 }
2692
Damien George2827d622014-04-27 15:50:52 +01002693 qstr param_name = MP_QSTR_NULL;
2694 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002695 if (MP_PARSE_NODE_IS_ID(pn)) {
2696 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002697 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002698 // comes after a star, so counts as a keyword-only parameter
2699 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002700 } else {
Damien George2827d622014-04-27 15:50:52 +01002701 // comes before a star, so counts as a positional parameter
2702 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002703 }
Damienb14de212013-10-06 00:28:28 +01002704 } else {
Damiend99b0522013-12-21 18:17:45 +00002705 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2706 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2707 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2708 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002709 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002710 // comes after a star, so counts as a keyword-only parameter
2711 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002712 } else {
Damien George2827d622014-04-27 15:50:52 +01002713 // comes before a star, so counts as a positional parameter
2714 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002715 }
Damiend99b0522013-12-21 18:17:45 +00002716 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002717 if (comp->have_star) {
2718 // more than one star
2719 compile_syntax_error(comp, pn, "invalid syntax");
2720 return;
2721 }
Damien George8b19db02014-04-11 23:25:34 +01002722 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002723 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002724 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002725 // bare star
2726 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002727 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002728 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002729 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002730 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002731 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002732 } else {
2733 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002734 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002735 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002736 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2737 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002738 }
Damien George0bb97132015-02-27 14:25:47 +00002739 } else {
2740 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002741 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002742 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002743 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002744 }
Damien429d7192013-10-04 19:53:11 +01002745 }
2746
Damien George2827d622014-04-27 15:50:52 +01002747 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002748 bool added;
2749 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2750 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002751 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002752 return;
2753 }
Damien429d7192013-10-04 19:53:11 +01002754 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002755 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002756 }
2757}
2758
Damien George2827d622014-04-27 15:50:52 +01002759STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002760 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002761}
2762
Damien George2827d622014-04-27 15:50:52 +01002763STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002764 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2765}
2766
Damien Georgeea5b59b2015-09-04 16:44:14 +01002767#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002768STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2769 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2770 // no annotation
2771 return;
2772 }
2773
2774 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2775 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2776 // named parameter with possible annotation
2777 // fallthrough
2778 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2779 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2780 // named star with possible annotation
2781 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2782 // fallthrough
2783 } else {
2784 // no annotation
2785 return;
2786 }
Damien Georgee49153f2016-10-11 12:29:54 +11002787 } else {
2788 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002789 // double star with possible annotation
2790 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002791 }
2792
2793 mp_parse_node_t pn_annotation = pns->nodes[1];
2794
2795 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002796 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2797 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2798 assert(id_info != NULL);
2799
Damien Georgeea5b59b2015-09-04 16:44:14 +01002800 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2801 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2802 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2803 } else {
2804 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002805 }
Damien George2ac4af62014-08-15 16:45:41 +01002806 }
Damien429d7192013-10-04 19:53:11 +01002807}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002808#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002809
Damien Georgea8312432015-12-18 01:37:55 +00002810STATIC 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) {
2811 uint l_top = comp_next_label(comp);
2812 uint l_end = comp_next_label(comp);
2813 EMIT_ARG(label_assign, l_top);
2814 EMIT_ARG(for_iter, l_end);
2815 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2816 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2817
Damien429d7192013-10-04 19:53:11 +01002818 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002819 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002820 // no more nested if/for; compile inner expression
2821 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002822 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002823 EMIT(yield_value);
2824 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002825 } else {
Damien George088740e2017-01-17 15:27:37 +11002826 EMIT_ARG(store_comp, comp->scope_cur->kind, 4 * for_depth + 5);
Damien429d7192013-10-04 19:53:11 +01002827 }
Damiend99b0522013-12-21 18:17:45 +00002828 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002829 // if condition
Damiend99b0522013-12-21 18:17:45 +00002830 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002831 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2832 pn_iter = pns_comp_if->nodes[1];
2833 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002834 } else {
2835 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002836 // for loop
Damiend99b0522013-12-21 18:17:45 +00002837 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002838 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6e769da2017-01-17 14:32:50 +11002839 EMIT_ARG(get_iter, true);
Damien Georgea8312432015-12-18 01:37:55 +00002840 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002841 }
Damien Georgea8312432015-12-18 01:37:55 +00002842
2843 EMIT_ARG(jump, l_top);
2844 EMIT_ARG(label_assign, l_end);
Damien George30b42dd2017-01-17 15:30:18 +11002845 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002846}
2847
Damien George1463c1f2014-04-25 23:52:57 +01002848STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002849#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002850 // see http://www.python.org/dev/peps/pep-0257/
2851
2852 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002853 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002854 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002855 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002856 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002857 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2858 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002859 for (int i = 0; i < num_nodes; i++) {
2860 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002861 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 +00002862 // not a newline, so this is the first statement; finish search
2863 break;
2864 }
2865 }
2866 // 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 +00002867 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002868 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002869 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002870 } else {
2871 return;
2872 }
2873
2874 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002875 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002876 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002877 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2878 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
Damien George52552552017-02-24 13:43:43 +11002879 || (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)
2880 && MP_OBJ_IS_STR(get_const_object((mp_parse_node_struct_t*)pns->nodes[0])))) {
Damien George5042bce2014-05-25 22:06:06 +01002881 // compile the doc string
2882 compile_node(comp, pns->nodes[0]);
2883 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002884 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002885 }
2886 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002887#else
2888 (void)comp;
2889 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002890#endif
Damien429d7192013-10-04 19:53:11 +01002891}
2892
Damien George1463c1f2014-04-25 23:52:57 +01002893STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002894 comp->pass = pass;
2895 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002896 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002898
Damien George36db6bc2014-05-07 17:24:22 +01002899 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002900 // reset maximum stack sizes in scope
2901 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002902 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002903 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002904 }
2905
Damien429d7192013-10-04 19:53:11 +01002906 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002907 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2908 assert(scope->kind == SCOPE_MODULE);
2909 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2910 compile_node(comp, pns->nodes[0]); // compile the expression
2911 EMIT(return_value);
2912 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002913 if (!comp->is_repl) {
2914 check_for_doc_string(comp, scope->pn);
2915 }
Damien429d7192013-10-04 19:53:11 +01002916 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002917 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002918 EMIT(return_value);
2919 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002920 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2921 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2922 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002923
2924 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002925 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002926 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002927 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002928 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002929 }
2930 #if MICROPY_EMIT_NATIVE
2931 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002932 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002933 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002934
Damien George2ac4af62014-08-15 16:45:41 +01002935 // argument annotations
2936 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2937
2938 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002939 mp_parse_node_t pn_annotation = pns->nodes[2];
2940 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002941 // nodes[2] can be null or a test-expr
2942 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2943 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2944 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2945 } else {
2946 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002947 }
2948 }
Damien George2ac4af62014-08-15 16:45:41 +01002949 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002950 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002951
2952 compile_node(comp, pns->nodes[3]); // 3 is function body
2953 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002954 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002955 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002956 EMIT(return_value);
2957 }
2958 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002959 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2960 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2961 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002962
2963 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002964 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002965 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002966 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002967 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2968 }
2969
2970 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002971
2972 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2973 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2974 EMIT(pop_top);
2975 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2976 }
Damien429d7192013-10-04 19:53:11 +01002977 EMIT(return_value);
2978 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2979 // a bit of a hack at the moment
2980
Damiend99b0522013-12-21 18:17:45 +00002981 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2982 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2983 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2984 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2985 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002986
Damien George708c0732014-04-27 19:23:46 +01002987 // We need a unique name for the comprehension argument (the iterator).
2988 // CPython uses .0, but we should be able to use anything that won't
2989 // clash with a user defined variable. Best to use an existing qstr,
2990 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01002991 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01002992 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002993 bool added;
2994 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2995 assert(added);
2996 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002997 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01002998 }
2999
3000 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003001 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003002 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003003 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003004 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003005 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003006 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003007 #endif
Damien429d7192013-10-04 19:53:11 +01003008 }
3009
Damien George088740e2017-01-17 15:27:37 +11003010 // There are 4 slots on the stack for the iterator, and the first one is
3011 // NULL to indicate that the second one points to the iterator object.
Damien George4d2bab12017-02-10 15:39:33 +11003012 if (scope->kind == SCOPE_GEN_EXPR) {
Damien George60656ea2017-03-23 16:36:08 +11003013 // TODO static assert that MP_OBJ_ITER_BUF_NSLOTS == 4
Damien George4d2bab12017-02-10 15:39:33 +11003014 EMIT(load_null);
3015 compile_load_id(comp, qstr_arg);
3016 EMIT(load_null);
3017 EMIT(load_null);
3018 } else {
3019 compile_load_id(comp, qstr_arg);
3020 EMIT_ARG(get_iter, true);
3021 }
Damien George6e769da2017-01-17 14:32:50 +11003022
Damien Georgea8312432015-12-18 01:37:55 +00003023 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003024
3025 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003026 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003027 }
3028 EMIT(return_value);
3029 } else {
3030 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003031 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3032 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3033 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003034
Damien George36db6bc2014-05-07 17:24:22 +01003035 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003036 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003037 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003038 assert(added);
3039 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003040 }
3041
Damien George542bd6b2015-03-26 14:42:40 +00003042 compile_load_id(comp, MP_QSTR___name__);
3043 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003044 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003045 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003046
3047 check_for_doc_string(comp, pns->nodes[2]);
3048 compile_node(comp, pns->nodes[2]); // 2 is class body
3049
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003050 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003051 assert(id != NULL);
3052 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003054 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003055 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003056 }
3057 EMIT(return_value);
3058 }
3059
Damien415eb6f2013-10-05 12:19:06 +01003060 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003061
3062 // make sure we match all the exception levels
3063 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003064}
3065
Damien Georgead297a12016-12-09 13:17:49 +11003066#if MICROPY_EMIT_INLINE_ASM
Damien George36db6bc2014-05-07 17:24:22 +01003067// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003068STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003069 comp->pass = pass;
3070 comp->scope_cur = scope;
3071 comp->next_label = 1;
3072
3073 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003074 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003075 return;
3076 }
3077
Damien George36db6bc2014-05-07 17:24:22 +01003078 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgee920bab2016-12-09 21:23:17 +11003079 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003080 }
3081
Damien826005c2013-10-05 23:17:28 +01003082 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003083 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3084 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3085 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003086
Damiend99b0522013-12-21 18:17:45 +00003087 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003088
Damiena2f2f7d2013-10-06 00:14:13 +01003089 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003090 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003091 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003092 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003093 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003094 if (comp->compile_error != MP_OBJ_NULL) {
3095 goto inline_asm_error;
3096 }
Damiena2f2f7d2013-10-06 00:14:13 +01003097 }
3098
Damien George8f54c082016-01-15 15:20:43 +00003099 // pns->nodes[2] is function return annotation
3100 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3101 mp_parse_node_t pn_annotation = pns->nodes[2];
3102 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3103 // nodes[2] can be null or a test-expr
3104 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3105 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3106 switch (ret_type) {
3107 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3108 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3109 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3110 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3111 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3112 }
3113 } else {
3114 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3115 }
3116 }
Damien826005c2013-10-05 23:17:28 +01003117
Damiend99b0522013-12-21 18:17:45 +00003118 mp_parse_node_t pn_body = pns->nodes[3]; // body
3119 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003120 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003121
Damien826005c2013-10-05 23:17:28 +01003122 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003123 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3124 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003125 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3126 // no instructions
3127 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003128 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003129 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003130 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003131 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003132 return;
3133 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003134
3135 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003136 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003137 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3138 goto not_an_instruction;
3139 }
Damiend99b0522013-12-21 18:17:45 +00003140 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003141 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003142 goto not_an_instruction;
3143 }
3144 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3145 goto not_an_instruction;
3146 }
3147 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3148 goto not_an_instruction;
3149 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003150
3151 // parse node looks like an instruction
3152 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003153 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3154 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3155 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003156 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003157
3158 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003159 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003160 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003161 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003162 return;
3163 }
Damien George6f355fd2014-04-10 14:11:31 +01003164 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003165 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003166 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3167 compile_syntax_error(comp, nodes[i], "label redefined");
3168 return;
3169 }
Damien826005c2013-10-05 23:17:28 +01003170 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003171 } else if (op == MP_QSTR_align) {
3172 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003173 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003174 return;
3175 }
Damien George36db6bc2014-05-07 17:24:22 +01003176 if (pass > MP_PASS_SCOPE) {
Damien Georgedd53b122016-12-09 20:54:54 +11003177 mp_asm_base_align((mp_asm_base_t*)comp->emit_inline_asm,
3178 MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003179 }
3180 } else if (op == MP_QSTR_data) {
3181 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003182 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003183 return;
3184 }
Damien George36db6bc2014-05-07 17:24:22 +01003185 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003186 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003187 for (uint j = 1; j < n_args; j++) {
3188 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003189 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003190 return;
3191 }
Damien Georgedd53b122016-12-09 20:54:54 +11003192 mp_asm_base_data((mp_asm_base_t*)comp->emit_inline_asm,
3193 bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003194 }
3195 }
Damien826005c2013-10-05 23:17:28 +01003196 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003197 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003198 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003199 }
3200 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003201
3202 if (comp->compile_error != MP_OBJ_NULL) {
3203 pns = pns2; // this is the parse node that had the error
3204 goto inline_asm_error;
3205 }
Damien826005c2013-10-05 23:17:28 +01003206 }
3207
Damien George36db6bc2014-05-07 17:24:22 +01003208 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003209 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien Georgee920bab2016-12-09 21:23:17 +11003210
3211 if (comp->pass == MP_PASS_EMIT) {
3212 void *f = mp_asm_base_get_code((mp_asm_base_t*)comp->emit_inline_asm);
3213 mp_emit_glue_assign_native(comp->scope_cur->raw_code, MP_CODE_NATIVE_ASM,
3214 f, mp_asm_base_get_code_size((mp_asm_base_t*)comp->emit_inline_asm),
3215 NULL, comp->scope_cur->num_pos_args, 0, type_sig);
3216 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003217 }
3218
3219 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003220 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003221 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003222 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003223 }
Damien429d7192013-10-04 19:53:11 +01003224}
Damien George094d4502014-04-02 17:31:27 +01003225#endif
Damien429d7192013-10-04 19:53:11 +01003226
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003227STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003228 // in Micro Python we put the *x parameter after all other parameters (except **y)
3229 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3230 id_info_t *id_param = NULL;
3231 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3232 id_info_t *id = &scope->id_info[i];
3233 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3234 if (id_param != NULL) {
3235 // swap star param with last param
3236 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3237 }
3238 break;
3239 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3240 id_param = id;
3241 }
3242 }
3243 }
Damien George2827d622014-04-27 15:50:52 +01003244
Damien429d7192013-10-04 19:53:11 +01003245 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003246 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003247 scope->num_locals = 0;
3248 for (int i = 0; i < scope->id_info_len; i++) {
3249 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003250 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003251 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3252 continue;
3253 }
Damien George3dea8c92016-09-30 12:34:05 +10003254 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003255 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3256 }
Damien George2827d622014-04-27 15:50:52 +01003257 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003258 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003259 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003260 }
3261 }
3262
Damien George65dc9602015-08-14 12:24:11 +01003263 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003264 for (int i = 0; i < scope->id_info_len; i++) {
3265 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003266 // in Micro Python the cells come right after the fast locals
3267 // parameters are not counted here, since they remain at the start
3268 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003269 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003270 id->local_num = scope->num_locals;
3271 scope->num_locals += 1;
3272 }
Damien9ecbcff2013-12-11 00:41:43 +00003273 }
Damien9ecbcff2013-12-11 00:41:43 +00003274
Damien George65dc9602015-08-14 12:24:11 +01003275 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003276 // make sure they are in the order of the parent scope
3277 if (scope->parent != NULL) {
3278 int num_free = 0;
3279 for (int i = 0; i < scope->parent->id_info_len; i++) {
3280 id_info_t *id = &scope->parent->id_info[i];
3281 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3282 for (int j = 0; j < scope->id_info_len; j++) {
3283 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003284 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003285 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003286 // in Micro Python the frees come first, before the params
3287 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003288 num_free += 1;
3289 }
3290 }
3291 }
Damien429d7192013-10-04 19:53:11 +01003292 }
Damien George6baf76e2013-12-30 22:32:17 +00003293 // in Micro Python shift all other locals after the free locals
3294 if (num_free > 0) {
3295 for (int i = 0; i < scope->id_info_len; i++) {
3296 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003297 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003298 id->local_num += num_free;
3299 }
3300 }
Damien George2827d622014-04-27 15:50:52 +01003301 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 +00003302 scope->num_locals += num_free;
3303 }
Damien429d7192013-10-04 19:53:11 +01003304 }
Damien429d7192013-10-04 19:53:11 +01003305}
3306
Damien Georged4dba882015-11-13 13:38:28 +00003307#if !MICROPY_PERSISTENT_CODE_SAVE
3308STATIC
3309#endif
3310mp_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 +01003311 // put compiler state on the stack, it's relatively small
3312 compiler_t comp_state = {0};
3313 compiler_t *comp = &comp_state;
3314
Damien Georgecbd2f742014-01-19 11:48:48 +00003315 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003316 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003317
Damien George62a3a282015-03-01 12:04:05 +00003318 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003319 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003320
Damien Georgea210c772015-03-26 15:49:53 +00003321 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003322 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003323
Damien826005c2013-10-05 23:17:28 +01003324 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003325 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003326 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003327 comp->emit_method_table = &emit_bc_method_table;
3328 #endif
Damien826005c2013-10-05 23:17:28 +01003329 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003330 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003331 if (false) {
Damien Georgead297a12016-12-09 13:17:49 +11003332 #if MICROPY_EMIT_INLINE_ASM
3333 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
Damien George36db6bc2014-05-07 17:24:22 +01003334 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damien Georgead297a12016-12-09 13:17:49 +11003335 #endif
Damien826005c2013-10-05 23:17:28 +01003336 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003337 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003338 }
3339
3340 // update maximim number of labels needed
3341 if (comp->next_label > max_num_labels) {
3342 max_num_labels = comp->next_label;
3343 }
Damien429d7192013-10-04 19:53:11 +01003344 }
3345
Damien826005c2013-10-05 23:17:28 +01003346 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003347 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 +00003348 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003349 }
3350
Damien Georgea210c772015-03-26 15:49:53 +00003351 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003352 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003353
Damien826005c2013-10-05 23:17:28 +01003354 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003355#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003356 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003357#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003358 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003359 if (false) {
3360 // dummy
3361
Damien Georgead297a12016-12-09 13:17:49 +11003362 #if MICROPY_EMIT_INLINE_ASM
3363 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
3364 // inline assembly
3365 if (comp->emit_inline_asm == NULL) {
3366 comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003367 }
3368 comp->emit = NULL;
Damien Georgead297a12016-12-09 13:17:49 +11003369 comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table);
Damien George36db6bc2014-05-07 17:24:22 +01003370 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgede9cd002016-12-19 17:42:25 +11003371 #if MICROPY_EMIT_INLINE_XTENSA
3372 // Xtensa requires an extra pass to compute size of l32r const table
3373 // TODO this can be improved by calculating it during SCOPE pass
3374 // but that requires some other structural changes to the asm emitters
3375 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
3376 #endif
Damien Georgea91ac202014-10-05 19:01:34 +01003377 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003378 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003379 }
Damien Georgead297a12016-12-09 13:17:49 +11003380 #endif
Damienc025ebb2013-10-12 14:30:21 +01003381
Damien826005c2013-10-05 23:17:28 +01003382 } else {
Damienc025ebb2013-10-12 14:30:21 +01003383
3384 // choose the emit type
3385
Damien826005c2013-10-05 23:17:28 +01003386 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003387
3388#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003389 case MP_EMIT_OPT_NATIVE_PYTHON:
3390 case MP_EMIT_OPT_VIPER:
Damiendc833822013-10-06 01:01:01 +01003391 if (emit_native == NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003392 emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003393 }
Damien George080a78b2016-12-07 11:17:17 +11003394 comp->emit_method_table = &NATIVE_EMITTER(method_table);
Damienc025ebb2013-10-12 14:30:21 +01003395 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003396 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003397 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003398#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003399
Damien826005c2013-10-05 23:17:28 +01003400 default:
Damien826005c2013-10-05 23:17:28 +01003401 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003402 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003403 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003404 #endif
Damien826005c2013-10-05 23:17:28 +01003405 break;
3406 }
Damienc025ebb2013-10-12 14:30:21 +01003407
Damien George1e1779e2015-01-14 00:20:28 +00003408 // need a pass to compute stack size
3409 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3410
Damien George36db6bc2014-05-07 17:24:22 +01003411 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003412 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003413 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3414 }
3415
3416 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003417 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003418 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003419 }
Damien6cdd3af2013-10-05 18:08:26 +01003420 }
Damien429d7192013-10-04 19:53:11 +01003421 }
3422
Damien Georgecfc4c332015-07-29 22:16:01 +00003423 if (comp->compile_error != MP_OBJ_NULL) {
3424 // if there is no line number for the error then use the line
3425 // number for the start of this scope
3426 compile_error_set_line(comp, comp->scope_cur->pn);
3427 // add a traceback to the exception using relevant source info
3428 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3429 comp->compile_error_line, comp->scope_cur->simple_name);
3430 }
3431
Damien George41d02b62014-01-24 22:42:28 +00003432 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003433
Damien Georgea210c772015-03-26 15:49:53 +00003434 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003435#if MICROPY_EMIT_NATIVE
3436 if (emit_native != NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003437 NATIVE_EMITTER(free)(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003438 }
3439#endif
Damien Georgead297a12016-12-09 13:17:49 +11003440 #if MICROPY_EMIT_INLINE_ASM
3441 if (comp->emit_inline_asm != NULL) {
3442 ASM_EMITTER(free)(comp->emit_inline_asm);
Damien George41d02b62014-01-24 22:42:28 +00003443 }
Damien Georgead297a12016-12-09 13:17:49 +11003444 #endif
Damien George41d02b62014-01-24 22:42:28 +00003445
Damien George52b5d762014-09-23 15:31:56 +00003446 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003447 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003448
Damien George41d02b62014-01-24 22:42:28 +00003449 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003450 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003451 for (scope_t *s = module_scope; s;) {
3452 scope_t *next = s->next;
3453 scope_free(s);
3454 s = next;
3455 }
Damien5ac1b2e2013-10-18 19:58:12 +01003456
Damien George9d5e5c02015-09-24 13:15:57 +01003457 if (comp->compile_error != MP_OBJ_NULL) {
3458 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003459 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003460 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003461 }
Damien429d7192013-10-04 19:53:11 +01003462}
Damien Georged4dba882015-11-13 13:38:28 +00003463
3464mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3465 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3466 // return function that executes the outer module
3467 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3468}
Damien Georgedd5353a2015-12-18 12:35:44 +00003469
3470#endif // MICROPY_ENABLE_COMPILER