blob: 4fde278d0fda345b04bfa6efda77d6771c18e938 [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 George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000049 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000050 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010051} pn_kind_t;
52
Damien George65dc9602015-08-14 12:24:11 +010053#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000054
55#if NEED_METHOD_TABLE
56
57// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000058#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
59#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000060#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
61#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010062
Damien George41125902015-03-26 16:44:14 +000063#else
64
65// if we only have the bytecode emitter enabled then we can do a direct call to the functions
66#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
67#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
68#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
69#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
70
71#endif
72
Damien George080a78b2016-12-07 11:17:17 +110073#if MICROPY_EMIT_NATIVE
74// define a macro to access external native emitter
75#if MICROPY_EMIT_X64
76#define NATIVE_EMITTER(f) emit_native_x64_##f
77#elif MICROPY_EMIT_X86
78#define NATIVE_EMITTER(f) emit_native_x86_##f
79#elif MICROPY_EMIT_THUMB
80#define NATIVE_EMITTER(f) emit_native_thumb_##f
81#elif MICROPY_EMIT_ARM
82#define NATIVE_EMITTER(f) emit_native_arm_##f
Damien George8e5aced2016-12-09 16:39:39 +110083#elif MICROPY_EMIT_XTENSA
84#define NATIVE_EMITTER(f) emit_native_xtensa_##f
Damien George080a78b2016-12-07 11:17:17 +110085#else
86#error "unknown native emitter"
87#endif
88#endif
89
Damien Georgead297a12016-12-09 13:17:49 +110090#if MICROPY_EMIT_INLINE_ASM
91// define macros for inline assembler
92#if MICROPY_EMIT_INLINE_THUMB
93#define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
94#define ASM_EMITTER(f) emit_inline_thumb_##f
Damien Georgef76b1bf2016-12-09 17:03:33 +110095#elif MICROPY_EMIT_INLINE_XTENSA
96#define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa
97#define ASM_EMITTER(f) emit_inline_xtensa_##f
Damien Georgead297a12016-12-09 13:17:49 +110098#else
99#error "unknown asm emitter"
100#endif
101#endif
102
Damien George0496de22015-10-03 17:07:54 +0100103#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
104#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
105
Damien Georgea91ac202014-10-05 19:01:34 +0100106// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +0100107typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +0000108 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +0100109
Damien George78035b92014-04-09 12:27:39 +0100110 uint8_t is_repl;
111 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +0100112 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +0100113 uint8_t have_star;
114
115 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +0000116 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +0000117 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +0100118
Damien George6f355fd2014-04-10 14:11:31 +0100119 uint next_label;
Damienb05d7072013-10-05 13:37:10 +0100120
Damien George69b89d22014-04-11 13:38:30 +0000121 uint16_t num_dict_params;
122 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +0100123
Damien Georgea91ac202014-10-05 19:01:34 +0100124 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
125 uint16_t continue_label;
126 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +0000127 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +0100128
Damien429d7192013-10-04 19:53:11 +0100129 scope_t *scope_head;
130 scope_t *scope_cur;
131
Damien6cdd3af2013-10-05 18:08:26 +0100132 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000133 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100134 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000135 #endif
Damien826005c2013-10-05 23:17:28 +0100136
Damien Georgead297a12016-12-09 13:17:49 +1100137 #if MICROPY_EMIT_INLINE_ASM
Damien826005c2013-10-05 23:17:28 +0100138 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
139 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 +0000140 #endif
Damien429d7192013-10-04 19:53:11 +0100141} compiler_t;
142
Damien Georgecfc4c332015-07-29 22:16:01 +0000143STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
144 // if the line of the error is unknown then try to update it from the pn
145 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000146 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100147 }
Damien George84d59c22015-07-27 22:20:00 +0100148}
149
150STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000151 // only register the error if there has been no other error
152 if (comp->compile_error == MP_OBJ_NULL) {
153 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
154 compile_error_set_line(comp, pn);
155 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000156}
157
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200158STATIC 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 +0100159STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000160STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100161
Damien George6f355fd2014-04-10 14:11:31 +0100162STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100163 return comp->next_label++;
164}
165
Damien George8dcc0c72014-03-27 10:55:21 +0000166STATIC void compile_increase_except_level(compiler_t *comp) {
167 comp->cur_except_level += 1;
168 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
169 comp->scope_cur->exc_stack_size = comp->cur_except_level;
170 }
171}
172
173STATIC void compile_decrease_except_level(compiler_t *comp) {
174 assert(comp->cur_except_level > 0);
175 comp->cur_except_level -= 1;
176}
177
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200178STATIC 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 +0100179 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100180 scope->parent = comp->scope_cur;
181 scope->next = NULL;
182 if (comp->scope_head == NULL) {
183 comp->scope_head = scope;
184 } else {
185 scope_t *s = comp->scope_head;
186 while (s->next != NULL) {
187 s = s->next;
188 }
189 s->next = scope;
190 }
191 return scope;
192}
193
Damien George91bc32d2015-04-09 15:31:53 +0000194typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
195
196STATIC 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 +0000197 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000198 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
199 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100200 for (int i = 0; i < num_nodes; i++) {
201 f(comp, pns->nodes[i]);
202 }
Damiend99b0522013-12-21 18:17:45 +0000203 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100204 f(comp, pn);
205 }
206}
207
Damien George969a6b32014-12-10 22:07:04 +0000208STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000209 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100210 for (int i = 0; i < num_nodes; i++) {
211 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000212 if (comp->compile_error != MP_OBJ_NULL) {
213 // add line info for the error in case it didn't have a line number
214 compile_error_set_line(comp, pns->nodes[i]);
215 return;
216 }
Damien429d7192013-10-04 19:53:11 +0100217 }
218}
219
Damien George542bd6b2015-03-26 14:42:40 +0000220STATIC void compile_load_id(compiler_t *comp, qstr qst) {
221 if (comp->pass == MP_PASS_SCOPE) {
222 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
223 } else {
Damien George41125902015-03-26 16:44:14 +0000224 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000225 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000226 #else
227 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
228 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000229 }
230}
231
232STATIC void compile_store_id(compiler_t *comp, qstr qst) {
233 if (comp->pass == MP_PASS_SCOPE) {
234 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
235 } else {
Damien George41125902015-03-26 16:44:14 +0000236 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000237 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000238 #else
239 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
240 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000241 }
242}
243
244STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
245 if (comp->pass == MP_PASS_SCOPE) {
246 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
247 } else {
Damien George41125902015-03-26 16:44:14 +0000248 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000249 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000250 #else
251 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
252 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000253 }
254}
255
Damien George2c0842b2014-04-27 16:46:51 +0100256STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100257 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000258 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100259 compile_node(comp, pn);
260 total += 1;
261 }
262 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000263 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100264 for (int i = 0; i < n; i++) {
265 compile_node(comp, pns_list->nodes[i]);
266 }
267 total += n;
268 }
Damien Georgeb9791222014-01-23 00:34:21 +0000269 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100270}
Damien429d7192013-10-04 19:53:11 +0100271
Damien George969a6b32014-12-10 22:07:04 +0000272STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100273 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000274 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100275}
276
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200277STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100278 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100279 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000280 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100281 }
282 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100283 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100284 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000285 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100286 }
287 return;
Damiend99b0522013-12-21 18:17:45 +0000288 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
289 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
290 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
291 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100292 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000293 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100294 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100295 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000296 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100297 }
Damien George0b2fd912015-02-28 14:37:54 +0000298 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000299 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100300 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000301 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100302 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000303 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100304 }
305 }
306 return;
Damiend99b0522013-12-21 18:17:45 +0000307 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100308 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000309 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100310 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000311 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100312 }
Damiend99b0522013-12-21 18:17:45 +0000313 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100314 c_if_cond(comp, pns->nodes[0], !jump_if, label);
315 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100316 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
317 // cond is something in parenthesis
318 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
319 // empty tuple, acts as false for the condition
320 if (jump_if == false) {
321 EMIT_ARG(jump, label);
322 }
Damien George93b37262016-01-07 13:07:52 +0000323 } else {
324 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100325 // non-empty tuple, acts as true for the condition
326 if (jump_if == true) {
327 EMIT_ARG(jump, label);
328 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100329 }
330 return;
Damien3a205172013-10-12 15:01:56 +0100331 }
332 }
333
334 // nothing special, fall back to default compiling for node and jump
335 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000336 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100337}
338
339typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100340STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100341
pohmelie81ebba72016-01-27 23:23:11 +0300342STATIC 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 +0100343 if (assign_kind != ASSIGN_AUG_STORE) {
344 compile_node(comp, pns->nodes[0]);
345 }
346
Damiend99b0522013-12-21 18:17:45 +0000347 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
348 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300349 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000350 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100351 if (assign_kind != ASSIGN_AUG_STORE) {
352 for (int i = 0; i < n - 1; i++) {
353 compile_node(comp, pns1->nodes[i]);
354 }
355 }
Damiend99b0522013-12-21 18:17:45 +0000356 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
357 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100358 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000359 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100360 if (assign_kind == ASSIGN_AUG_STORE) {
361 EMIT(rot_three);
362 EMIT(store_subscr);
363 } else {
364 compile_node(comp, pns1->nodes[0]);
365 if (assign_kind == ASSIGN_AUG_LOAD) {
366 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100367 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100368 } else {
369 EMIT(store_subscr);
370 }
371 }
Damiend99b0522013-12-21 18:17:45 +0000372 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
373 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100374 if (assign_kind == ASSIGN_AUG_LOAD) {
375 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000376 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100377 } else {
378 if (assign_kind == ASSIGN_AUG_STORE) {
379 EMIT(rot_two);
380 }
Damien Georgeb9791222014-01-23 00:34:21 +0000381 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100382 }
383 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100384 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100385 }
386 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100387 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100388 }
389
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100390 return;
391
392cannot_assign:
393 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100394}
395
Damien George0288cf02014-04-11 11:53:00 +0000396// 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 +0100397STATIC 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 +0000398 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
399
400 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000401 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000402 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
403 EMIT_ARG(unpack_ex, 0, num_tail);
404 have_star_index = 0;
405 }
Damien George963a5a32015-01-16 17:47:07 +0000406 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000407 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000408 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000409 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
410 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100411 } else {
Damien George9d181f62014-04-27 16:55:27 +0100412 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100413 return;
414 }
415 }
416 }
Damien George963a5a32015-01-16 17:47:07 +0000417 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000418 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100419 }
Damien George0288cf02014-04-11 11:53:00 +0000420 if (num_head != 0) {
421 if (0 == have_star_index) {
422 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100423 } else {
Damien George0288cf02014-04-11 11:53:00 +0000424 c_assign(comp, node_head, ASSIGN_STORE);
425 }
426 }
Damien George963a5a32015-01-16 17:47:07 +0000427 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000428 if (num_head + i == have_star_index) {
429 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
430 } else {
431 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100432 }
433 }
434}
435
436// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100437STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000438 assert(!MP_PARSE_NODE_IS_NULL(pn));
439 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000440 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000441 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100442 switch (assign_kind) {
443 case ASSIGN_STORE:
444 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000445 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100446 break;
447 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000448 default:
Damien George542bd6b2015-03-26 14:42:40 +0000449 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100450 break;
451 }
452 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100453 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100454 return;
455 }
456 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000457 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000458 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
459 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300460 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100461 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300462 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100463 break;
464
465 case PN_testlist_star_expr:
466 case PN_exprlist:
467 // lhs is a tuple
468 if (assign_kind != ASSIGN_STORE) {
469 goto bad_aug;
470 }
Damien George0288cf02014-04-11 11:53:00 +0000471 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100472 break;
473
474 case PN_atom_paren:
475 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000476 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100477 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100478 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000479 } else {
480 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000481 if (assign_kind != ASSIGN_STORE) {
482 goto bad_aug;
483 }
Damiend99b0522013-12-21 18:17:45 +0000484 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100485 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100486 }
487 break;
488
489 case PN_atom_bracket:
490 // lhs is something in brackets
491 if (assign_kind != ASSIGN_STORE) {
492 goto bad_aug;
493 }
Damiend99b0522013-12-21 18:17:45 +0000494 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100495 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000496 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
498 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100499 goto testlist_comp;
500 } else {
501 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000502 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100503 }
504 break;
505
506 default:
Damien George9d181f62014-04-27 16:55:27 +0100507 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100508 }
509 return;
510
511 testlist_comp:
512 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000513 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
514 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
515 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100516 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000517 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000518 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000519 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100520 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000521 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
522 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000523 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien George9d181f62014-04-27 16:55:27 +0100524 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100525 } else {
526 // sequence with 2 items
527 goto sequence_with_2_items;
528 }
529 } else {
530 // sequence with 2 items
531 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000532 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100533 }
534 return;
535 }
536 return;
537
Damien George9d181f62014-04-27 16:55:27 +0100538 cannot_assign:
539 compile_syntax_error(comp, pn, "can't assign to expression");
540 return;
541
Damien429d7192013-10-04 19:53:11 +0100542 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100543 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100544}
545
Damien George65dc9602015-08-14 12:24:11 +0100546// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100547// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
548// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
549// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100550STATIC 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 +0100551 assert(n_pos_defaults >= 0);
552 assert(n_kw_defaults >= 0);
553
Damien George3a3db4d2015-10-22 23:45:37 +0100554 // set flags
555 if (n_kw_defaults > 0) {
556 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
557 }
558 this_scope->num_def_pos_args = n_pos_defaults;
559
Damien429d7192013-10-04 19:53:11 +0100560 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000561 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100562 int nfree = 0;
563 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000564 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
565 id_info_t *id = &comp->scope_cur->id_info[i];
566 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
567 for (int j = 0; j < this_scope->id_info_len; j++) {
568 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100569 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000570 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000571 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000572 nfree += 1;
573 }
574 }
Damien429d7192013-10-04 19:53:11 +0100575 }
576 }
577 }
Damien429d7192013-10-04 19:53:11 +0100578
579 // make the function/closure
580 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100581 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100582 } else {
Damien George3558f622014-04-20 17:50:40 +0100583 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100584 }
585}
586
Damien George2c838942015-11-17 14:00:14 +0000587STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
588 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
589 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100590 comp->have_star = true;
591 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000592 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
593 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100594 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100595 } else {
596 // named star
Damien429d7192013-10-04 19:53:11 +0100597 }
Damien George8b19db02014-04-11 23:25:34 +0100598 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000599
Damien George2c838942015-11-17 14:00:14 +0000600 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
601 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100602 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000603 // TODO do we need to do anything with this?
604
605 } else {
606 mp_parse_node_t pn_id;
607 mp_parse_node_t pn_colon;
608 mp_parse_node_t pn_equal;
609 if (MP_PARSE_NODE_IS_ID(pn)) {
610 // this parameter is just an id
611
612 pn_id = pn;
613 pn_colon = MP_PARSE_NODE_NULL;
614 pn_equal = MP_PARSE_NODE_NULL;
615
Damien George2c838942015-11-17 14:00:14 +0000616 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000617 // this parameter has a colon and/or equal specifier
618
619 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
620 pn_id = pns->nodes[0];
621 pn_colon = pns->nodes[1];
622 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000623
624 } else {
625 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
626 // this parameter has an equal specifier
627
628 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
629 pn_id = pns->nodes[0];
630 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000631 }
632
633 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
634 // this parameter does not have a default value
635
636 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100637 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100638 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000639 return;
640 }
641
642 } else {
643 // this parameter has a default value
644 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
645
Damien George8b19db02014-04-11 23:25:34 +0100646 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000647 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000648 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
649 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100650 // in Micro Python we put the default positional parameters into a tuple using the bytecode
651 // we need to do this here before we start building the map for the default keywords
652 if (comp->num_default_params > 0) {
653 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100654 } else {
655 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100656 }
Damien George69b89d22014-04-11 13:38:30 +0000657 // first default dict param, so make the map
658 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000659 }
Damien Georgef0778a72014-06-07 22:01:00 +0100660
661 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000662 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000663 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000664 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000665 } else {
Damien George69b89d22014-04-11 13:38:30 +0000666 comp->num_default_params += 1;
667 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000668 }
669 }
670
671 // TODO pn_colon not implemented
672 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100673 }
674}
675
Damien George2c838942015-11-17 14:00:14 +0000676STATIC 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 +0000677 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
678 // expression for default arguments, which may contain a lambda. The lambda will
679 // call here in a nested way, so we must save and restore the relevant state.
680 bool orig_have_star = comp->have_star;
681 uint16_t orig_num_dict_params = comp->num_dict_params;
682 uint16_t orig_num_default_params = comp->num_default_params;
683
Damien George2c838942015-11-17 14:00:14 +0000684 // compile default parameters
685 comp->have_star = false;
686 comp->num_dict_params = 0;
687 comp->num_default_params = 0;
688 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
689
690 if (comp->compile_error != MP_OBJ_NULL) {
691 return;
692 }
693
694 // in Micro Python we put the default positional parameters into a tuple using the bytecode
695 // the default keywords args may have already made the tuple; if not, do it now
696 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
697 EMIT_ARG(build_tuple, comp->num_default_params);
698 EMIT(load_null); // sentinel indicating empty default keyword args
699 }
700
701 // make the function
702 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000703
704 // restore state
705 comp->have_star = orig_have_star;
706 comp->num_dict_params = orig_num_dict_params;
707 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000708}
709
Damien429d7192013-10-04 19:53:11 +0100710// leaves function object on stack
711// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000712STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100713 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100714 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000715 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100716 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000717 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100718 }
719
Damien429d7192013-10-04 19:53:11 +0100720 // get the scope for this function
721 scope_t *fscope = (scope_t*)pns->nodes[4];
722
Damien George2c838942015-11-17 14:00:14 +0000723 // compile the function definition
724 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100725
Damien429d7192013-10-04 19:53:11 +0100726 // return its name (the 'f' in "def f(...):")
727 return fscope->simple_name;
728}
729
730// leaves class object on stack
731// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000732STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100733 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100734 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000735 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100736 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000737 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100738 }
739
740 EMIT(load_build_class);
741
742 // scope for this class
743 scope_t *cscope = (scope_t*)pns->nodes[3];
744
745 // compile the class
746 close_over_variables_etc(comp, cscope, 0, 0);
747
748 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000749 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100750
751 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100752 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
753 mp_parse_node_t parents = pns->nodes[1];
754 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
755 parents = MP_PARSE_NODE_NULL;
756 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000757 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100758 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100759
760 // return its name (the 'C' in class C(...):")
761 return cscope->simple_name;
762}
763
Damien6cdd3af2013-10-05 18:08:26 +0100764// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200765STATIC 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 +0000766 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100767 return false;
768 }
769
770 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100771 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100772 return true;
773 }
774
Damiend99b0522013-12-21 18:17:45 +0000775 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100776 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100777 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100778#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000779 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100780 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000781 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100782 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100783#endif
Damien Georgead297a12016-12-09 13:17:49 +1100784 #if MICROPY_EMIT_INLINE_ASM
785 } else if (attr == ASM_DECORATOR_QSTR) {
786 *emit_options = MP_EMIT_OPT_ASM;
787 #endif
Damien6cdd3af2013-10-05 18:08:26 +0100788 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100789 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100790 }
791
792 return true;
793}
794
Damien George969a6b32014-12-10 22:07:04 +0000795STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100796 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000797 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000798 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100799
Damien6cdd3af2013-10-05 18:08:26 +0100800 // inherit emit options for this function/class definition
801 uint emit_options = comp->scope_cur->emit_options;
802
803 // compile each decorator
804 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100805 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000806 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
807 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100808
809 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000810 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000811 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100812
813 // check for built-in decorators
814 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
815 // this was a built-in
816 num_built_in_decorators += 1;
817
818 } else {
819 // not a built-in, compile normally
820
821 // compile the decorator function
822 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000823 for (int j = 1; j < name_len; j++) {
824 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
825 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100826 }
827
828 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000829 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100830 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000831 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100832 compile_node(comp, pns_decorator->nodes[1]);
833 }
Damien429d7192013-10-04 19:53:11 +0100834 }
835 }
836
pohmelie81ebba72016-01-27 23:23:11 +0300837 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000838 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100839 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000840 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100841 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300842 #if MICROPY_PY_ASYNC_AWAIT
843 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
844 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
845 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
846 body_name = compile_funcdef_helper(comp, pns0, emit_options);
847 scope_t *fscope = (scope_t*)pns0->nodes[4];
848 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
849 #endif
Damien429d7192013-10-04 19:53:11 +0100850 } else {
Damien George0bb97132015-02-27 14:25:47 +0000851 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
852 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100853 }
854
855 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100856 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100857 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100858 }
859
860 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000861 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100862}
863
Damien George969a6b32014-12-10 22:07:04 +0000864STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100865 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100866 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000867 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100868}
869
Damien George6be0b0a2014-08-15 14:30:52 +0100870STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000871 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000872 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300873 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000874 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100875
pohmelie81ebba72016-01-27 23:23:11 +0300876 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100877
Damiend99b0522013-12-21 18:17:45 +0000878 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
879 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300880 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000881 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100882 for (int i = 0; i < n - 1; i++) {
883 compile_node(comp, pns1->nodes[i]);
884 }
Damiend99b0522013-12-21 18:17:45 +0000885 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
886 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100887 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000888 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100889 compile_node(comp, pns1->nodes[0]);
890 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000891 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
892 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000893 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100894 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100895 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100896 }
897 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100898 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100899 }
900
Damiend99b0522013-12-21 18:17:45 +0000901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
902 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000903 if (MP_PARSE_NODE_IS_NULL(pn)) {
904 goto cannot_delete;
905 } else {
906 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000907 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100908 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
909
Damiend99b0522013-12-21 18:17:45 +0000910 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
911 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
912 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100913 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000914 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100915 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000916 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100917 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000918 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100919 c_del_stmt(comp, pns->nodes[0]);
920 for (int i = 0; i < n; i++) {
921 c_del_stmt(comp, pns1->nodes[i]);
922 }
Damien George216a7112016-09-30 14:48:06 +1000923 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100924 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100925 } else {
926 // sequence with 2 items
927 goto sequence_with_2_items;
928 }
929 } else {
930 // sequence with 2 items
931 sequence_with_2_items:
932 c_del_stmt(comp, pns->nodes[0]);
933 c_del_stmt(comp, pns->nodes[1]);
934 }
Damien429d7192013-10-04 19:53:11 +0100935 }
936 } else {
Damien George93b37262016-01-07 13:07:52 +0000937 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100938 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100939 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100940
941 return;
942
943cannot_delete:
944 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100945}
946
Damien George969a6b32014-12-10 22:07:04 +0000947STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100948 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
949}
950
Damien George969a6b32014-12-10 22:07:04 +0000951STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100952 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100953 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100954 }
Damien George090c9232014-10-17 14:08:49 +0000955 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000956 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100957}
958
Damien George969a6b32014-12-10 22:07:04 +0000959STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100960 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100961 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100962 }
Damien George090c9232014-10-17 14:08:49 +0000963 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000964 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100965}
966
Damien George969a6b32014-12-10 22:07:04 +0000967STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100968 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100969 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100970 return;
971 }
Damiend99b0522013-12-21 18:17:45 +0000972 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100973 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000974 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000975 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100976 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000977 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
978 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 +0100979
Damien George6f355fd2014-04-10 14:11:31 +0100980 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100981 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
982 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
983 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000984 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100985 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
986 } else {
987 compile_node(comp, pns->nodes[0]);
988 }
989 EMIT(return_value);
990}
991
Damien George969a6b32014-12-10 22:07:04 +0000992STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100993 compile_node(comp, pns->nodes[0]);
994 EMIT(pop_top);
995}
996
Damien George969a6b32014-12-10 22:07:04 +0000997STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000998 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100999 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001000 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001001 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001002 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001003 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001004 compile_node(comp, pns->nodes[0]);
1005 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001006 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001007 } else {
1008 // raise x
1009 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001010 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001011 }
1012}
1013
Damien George635543c2014-04-10 12:56:52 +01001014// q_base holds the base of the name
1015// eg a -> q_base=a
1016// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001017STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001018 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001019 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1020 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001021 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001022 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001023 pn = pns->nodes[0];
1024 is_as = true;
1025 }
Damien George635543c2014-04-10 12:56:52 +01001026 if (MP_PARSE_NODE_IS_NULL(pn)) {
1027 // empty name (eg, from . import x)
1028 *q_base = MP_QSTR_;
1029 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1030 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001031 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001032 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001033 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001034 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001035 }
Damien George635543c2014-04-10 12:56:52 +01001036 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001037 } else {
1038 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001040 {
Damien429d7192013-10-04 19:53:11 +01001041 // a name of the form a.b.c
1042 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001043 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001044 }
Damiend99b0522013-12-21 18:17:45 +00001045 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001046 int len = n - 1;
1047 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001048 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001049 }
Damien George55baff42014-01-21 21:40:13 +00001050 byte *q_ptr;
1051 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001052 for (int i = 0; i < n; i++) {
1053 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001054 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001055 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001056 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001057 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001058 memcpy(str_dest, str_src, str_src_len);
1059 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001060 }
Damien George635543c2014-04-10 12:56:52 +01001061 qstr q_full = qstr_build_end(q_ptr);
1062 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001063 if (is_as) {
1064 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001065 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001066 }
1067 }
Damien429d7192013-10-04 19:53:11 +01001068 }
Damien429d7192013-10-04 19:53:11 +01001069 }
1070}
1071
Damien George6be0b0a2014-08-15 14:30:52 +01001072STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001073 EMIT_ARG(load_const_small_int, 0); // level 0 import
1074 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1075 qstr q_base;
1076 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001077 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001078}
1079
Damien George969a6b32014-12-10 22:07:04 +00001080STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001081 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1082}
1083
Damien George969a6b32014-12-10 22:07:04 +00001084STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001085 mp_parse_node_t pn_import_source = pns->nodes[0];
1086
1087 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1088 uint import_level = 0;
1089 do {
1090 mp_parse_node_t pn_rel;
1091 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)) {
1092 // This covers relative imports with dots only like "from .. import"
1093 pn_rel = pn_import_source;
1094 pn_import_source = MP_PARSE_NODE_NULL;
1095 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1096 // This covers relative imports starting with dot(s) like "from .foo import"
1097 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1098 pn_rel = pns_2b->nodes[0];
1099 pn_import_source = pns_2b->nodes[1];
1100 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1101 } else {
1102 // Not a relative import
1103 break;
1104 }
1105
1106 // get the list of . and/or ...'s
1107 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001108 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001109
1110 // count the total number of .'s
1111 for (int i = 0; i < n; i++) {
1112 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1113 import_level++;
1114 } else {
1115 // should be an MP_TOKEN_ELLIPSIS
1116 import_level += 3;
1117 }
1118 }
1119 } while (0);
1120
Damiend99b0522013-12-21 18:17:45 +00001121 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001122 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001123
1124 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001125 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001126 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001127
1128 // do the import
Damien George635543c2014-04-10 12:56:52 +01001129 qstr dummy_q;
1130 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001131 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001132
Damien429d7192013-10-04 19:53:11 +01001133 } else {
Damien George635543c2014-04-10 12:56:52 +01001134 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001135
1136 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001137 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001138 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001139 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001140 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1141 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1142 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001143 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001144 }
Damien Georgeb9791222014-01-23 00:34:21 +00001145 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001146
1147 // do the import
Damien George635543c2014-04-10 12:56:52 +01001148 qstr dummy_q;
1149 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001150 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001151 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1152 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1153 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001154 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001155 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001156 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001157 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001158 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001159 }
1160 }
1161 EMIT(pop_top);
1162 }
1163}
1164
Damien George584ba672014-12-21 17:26:45 +00001165STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1166 bool added;
1167 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001168 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1169 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001170 return;
1171 }
1172 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1173
1174 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1175 id_info = scope_find_global(comp->scope_cur, qst);
1176 if (id_info != NULL) {
1177 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1178 }
1179}
1180
Damien George969a6b32014-12-10 22:07:04 +00001181STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001182 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001183 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001184 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001185 for (int i = 0; i < n; i++) {
1186 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001187 }
1188 }
1189}
1190
Damien George584ba672014-12-21 17:26:45 +00001191STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1192 bool added;
1193 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001194 if (added) {
1195 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1196 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1197 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1198 }
1199 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001200 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001201 }
Damien George584ba672014-12-21 17:26:45 +00001202}
1203
Damien George969a6b32014-12-10 22:07:04 +00001204STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001205 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001206 if (comp->scope_cur->kind == SCOPE_MODULE) {
1207 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1208 return;
1209 }
1210 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001211 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001212 for (int i = 0; i < n; i++) {
1213 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001214 }
1215 }
1216}
1217
Damien George969a6b32014-12-10 22:07:04 +00001218STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001219 // with optimisations enabled we don't compile assertions
1220 if (MP_STATE_VM(mp_optimise_value) != 0) {
1221 return;
1222 }
1223
Damien George6f355fd2014-04-10 14:11:31 +01001224 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001225 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001226 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001227 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001228 // assertion message
1229 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001230 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001231 }
Damien Georgeb9791222014-01-23 00:34:21 +00001232 EMIT_ARG(raise_varargs, 1);
1233 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001234}
1235
Damien George969a6b32014-12-10 22:07:04 +00001236STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001237 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001238
Damien George65dc9602015-08-14 12:24:11 +01001239 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001240 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001241 uint l_fail = comp_next_label(comp);
1242 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001243
Damien George391db862014-10-17 17:57:33 +00001244 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001245
Damien George65dc9602015-08-14 12:24:11 +01001246 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001247 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001248 goto done;
1249 }
1250
1251 if (
Damien George65dc9602015-08-14 12:24:11 +01001252 // optimisation: don't jump over non-existent elif/else blocks
1253 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001254 // optimisation: don't jump if last instruction was return
1255 && !EMIT(last_emit_was_return_value)
1256 ) {
1257 // jump over elif/else blocks
1258 EMIT_ARG(jump, l_end);
1259 }
1260
1261 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001262 }
1263
Damien George235f9b32014-10-17 17:30:16 +00001264 // compile elif blocks (if any)
1265 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001266 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 +00001267 for (int i = 0; i < n_elif; i++) {
1268 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1269 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001270
Damien George65dc9602015-08-14 12:24:11 +01001271 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001272 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001273 uint l_fail = comp_next_label(comp);
1274 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001275
Damien George391db862014-10-17 17:57:33 +00001276 compile_node(comp, pns_elif->nodes[1]); // elif block
1277
Damien George65dc9602015-08-14 12:24:11 +01001278 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001279 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001280 goto done;
1281 }
1282
1283 // optimisation: don't jump if last instruction was return
1284 if (!EMIT(last_emit_was_return_value)) {
1285 EMIT_ARG(jump, l_end);
1286 }
1287 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001288 }
1289 }
1290
1291 // compile else block
1292 compile_node(comp, pns->nodes[3]); // can be null
1293
Damien George391db862014-10-17 17:57:33 +00001294done:
Damien Georgeb9791222014-01-23 00:34:21 +00001295 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001296}
1297
Damien Georgecbddb272014-02-01 20:08:18 +00001298#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001299 uint16_t old_break_label = comp->break_label; \
1300 uint16_t old_continue_label = comp->continue_label; \
1301 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001302 uint break_label = comp_next_label(comp); \
1303 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001304 comp->break_label = break_label; \
1305 comp->continue_label = continue_label; \
1306 comp->break_continue_except_level = comp->cur_except_level;
1307
1308#define END_BREAK_CONTINUE_BLOCK \
1309 comp->break_label = old_break_label; \
1310 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001311 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001312
Damien George969a6b32014-12-10 22:07:04 +00001313STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001314 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001315
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001316 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 +00001317 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001318 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 +00001319 EMIT_ARG(jump, continue_label);
1320 }
1321 EMIT_ARG(label_assign, top_label);
1322 compile_node(comp, pns->nodes[1]); // body
1323 EMIT_ARG(label_assign, continue_label);
1324 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1325 }
Damience89a212013-10-15 22:25:17 +01001326
1327 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001328 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001329
1330 compile_node(comp, pns->nodes[2]); // else
1331
Damien Georgeb9791222014-01-23 00:34:21 +00001332 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001333}
1334
Damien Georgee181c0d2014-12-12 17:19:56 +00001335// This function compiles an optimised for-loop of the form:
1336// for <var> in range(<start>, <end>, <step>):
1337// <body>
1338// else:
1339// <else>
1340// <var> must be an identifier and <step> must be a small-int.
1341//
1342// Semantics of for-loop require:
1343// - final failing value should not be stored in the loop variable
1344// - if the loop never runs, the loop variable should never be assigned
1345// - assignments to <var>, <end> or <step> in the body do not alter the loop
1346// (<step> is a constant for us, so no need to worry about it changing)
1347//
1348// If <end> is a small-int, then the stack during the for-loop contains just
1349// the current value of <var>. Otherwise, the stack contains <end> then the
1350// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001351STATIC 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 +00001352 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001353
Damien George6f355fd2014-04-10 14:11:31 +01001354 uint top_label = comp_next_label(comp);
1355 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001356
Damien Georgee181c0d2014-12-12 17:19:56 +00001357 // put the end value on the stack if it's not a small-int constant
1358 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1359 if (end_on_stack) {
1360 compile_node(comp, pn_end);
1361 }
1362
1363 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001364 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001365
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(jump, entry_label);
1367 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001368
Damien Georgec33ce602014-12-11 17:35:23 +00001369 // duplicate next value and store it to var
1370 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001371 c_assign(comp, pn_var, ASSIGN_STORE);
1372
Damienf3822fc2013-11-09 20:12:03 +00001373 // compile body
1374 compile_node(comp, pn_body);
1375
Damien Georgeb9791222014-01-23 00:34:21 +00001376 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001377
Damien Georgee181c0d2014-12-12 17:19:56 +00001378 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001379 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001380 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001381
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001383
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001384 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001385 if (end_on_stack) {
1386 EMIT(dup_top_two);
1387 EMIT(rot_two);
1388 } else {
1389 EMIT(dup_top);
1390 compile_node(comp, pn_end);
1391 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001392 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1393 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001394 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001395 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001396 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001397 }
Damien George63f38322015-02-28 15:04:06 +00001398 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001399
1400 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001401 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001402
1403 compile_node(comp, pn_else);
1404
Damien Georgeb9791222014-01-23 00:34:21 +00001405 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001406
1407 // discard final value of var that failed the loop condition
1408 EMIT(pop_top);
1409
1410 // discard <end> value if it's on the stack
1411 if (end_on_stack) {
1412 EMIT(pop_top);
1413 }
Damienf72fd0e2013-11-06 20:20:49 +00001414}
1415
Damien George969a6b32014-12-10 22:07:04 +00001416STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001417 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1418 // this is actually slower, but uses no heap memory
1419 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001420 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 +00001421 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001422 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001423 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001424 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001425 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1426 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001427 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001428 mp_parse_node_t pn_range_start;
1429 mp_parse_node_t pn_range_end;
1430 mp_parse_node_t pn_range_step;
1431 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001432 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001433 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001434 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001435 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001436 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001437 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001438 } else if (n_args == 2) {
1439 pn_range_start = args[0];
1440 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001441 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001442 } else {
1443 pn_range_start = args[0];
1444 pn_range_end = args[1];
1445 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001446 // We need to know sign of step. This is possible only if it's constant
1447 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1448 optimize = false;
1449 }
Damienf72fd0e2013-11-06 20:20:49 +00001450 }
Damien George33ac0fd2015-12-08 21:05:14 +00001451 // arguments must be able to be compiled as standard expressions
1452 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1453 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1454 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1455 optimize = false;
1456 }
1457 }
1458 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1459 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1460 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1461 optimize = false;
1462 }
1463 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001464 }
1465 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001466 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1467 return;
1468 }
1469 }
1470 }
Damienf72fd0e2013-11-06 20:20:49 +00001471
Damien Georgecbddb272014-02-01 20:08:18 +00001472 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001473 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001474
Damien George6f355fd2014-04-10 14:11:31 +01001475 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001476
Damien429d7192013-10-04 19:53:11 +01001477 compile_node(comp, pns->nodes[1]); // iterator
Damien Georgef4df3aa2016-01-09 23:59:52 +00001478 EMIT_ARG(get_iter, true);
Damien Georgecbddb272014-02-01 20:08:18 +00001479 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001480 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001481 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1482 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001483 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001484 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001485 }
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(label_assign, pop_label);
Damien Georgef4df3aa2016-01-09 23:59:52 +00001487 EMIT_ARG(for_iter_end, true);
Damien429d7192013-10-04 19:53:11 +01001488
1489 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001490 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001491
Damien429d7192013-10-04 19:53:11 +01001492 compile_node(comp, pns->nodes[3]); // else (not tested)
1493
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001495}
1496
Damien George6be0b0a2014-08-15 14:30:52 +01001497STATIC 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 +01001498 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001499 uint l1 = comp_next_label(comp);
1500 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001501
Damien Georgeb9791222014-01-23 00:34:21 +00001502 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001503 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001504
Damien429d7192013-10-04 19:53:11 +01001505 compile_node(comp, pn_body); // body
1506 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001507 EMIT_ARG(jump, success_label); // jump over exception handler
1508
1509 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001510 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001511
Damien Georgef0406852016-09-27 12:37:21 +10001512 // at this point the top of the stack contains the exception instance that was raised
1513
Damien George6f355fd2014-04-10 14:11:31 +01001514 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001515
1516 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001517 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1518 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001519
1520 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001521 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001522
Damiend99b0522013-12-21 18:17:45 +00001523 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001524 // this is a catch all exception handler
1525 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001526 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001527 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001528 return;
1529 }
1530 } else {
1531 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001532 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1533 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1534 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1535 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001536 // handler binds the exception to a local
1537 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001538 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001539 }
1540 }
1541 EMIT(dup_top);
1542 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001543 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001544 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001545 }
1546
Damien Georgef0406852016-09-27 12:37:21 +10001547 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001548 if (qstr_exception_local == 0) {
1549 EMIT(pop_top);
1550 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001551 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001552 }
1553
Damien George6f355fd2014-04-10 14:11:31 +01001554 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001555 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001556 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001557 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001558 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001559 }
1560 compile_node(comp, pns_except->nodes[1]);
1561 if (qstr_exception_local != 0) {
1562 EMIT(pop_block);
1563 }
1564 EMIT(pop_except);
1565 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1567 EMIT_ARG(label_assign, l3);
1568 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001569 compile_store_id(comp, qstr_exception_local);
1570 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001571
Damien George8dcc0c72014-03-27 10:55:21 +00001572 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001573 EMIT(end_finally);
1574 }
Damien Georgeb9791222014-01-23 00:34:21 +00001575 EMIT_ARG(jump, l2);
1576 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001577 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001578 }
1579
Damien George8dcc0c72014-03-27 10:55:21 +00001580 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001581 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001582 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001583
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001585 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001587}
1588
Damien George6be0b0a2014-08-15 14:30:52 +01001589STATIC 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 +01001590 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001591
Damien Georgeb9791222014-01-23 00:34:21 +00001592 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001593 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001594
Damien429d7192013-10-04 19:53:11 +01001595 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001596 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001597 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001598 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001599 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001600 } else {
1601 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1602 }
1603 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001604 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1605 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001606 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001607
Damien George8dcc0c72014-03-27 10:55:21 +00001608 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001609 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001610}
1611
Damien George969a6b32014-12-10 22:07:04 +00001612STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001613 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1614 {
Damiend99b0522013-12-21 18:17:45 +00001615 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1616 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001617 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001618 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1619 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001620 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001621 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001622 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001623 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001624 // no finally
1625 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1626 } else {
1627 // have finally
Damiend99b0522013-12-21 18:17:45 +00001628 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 +01001629 }
1630 } else {
1631 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001632 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001633 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001634 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001635 }
Damien429d7192013-10-04 19:53:11 +01001636 }
1637}
1638
Damien George6be0b0a2014-08-15 14:30:52 +01001639STATIC 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 +01001640 if (n == 0) {
1641 // no more pre-bits, compile the body of the with
1642 compile_node(comp, body);
1643 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001644 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001645 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1646 // we need to allocate an extra label for the native emitter
1647 // it will use l_end+1 as an auxiliary label
1648 comp_next_label(comp);
1649 }
Damiend99b0522013-12-21 18:17:45 +00001650 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001651 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001652 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001653 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001655 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1656 } else {
1657 // this pre-bit is just an expression
1658 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001659 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001660 EMIT(pop_top);
1661 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001662 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001663 // compile additional pre-bits and the body
1664 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1665 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001666 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001667 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001668 EMIT(end_finally);
1669 }
1670}
1671
Damien George969a6b32014-12-10 22:07:04 +00001672STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001673 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001674 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001675 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001676 assert(n > 0);
1677
1678 // compile in a nested fashion
1679 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1680}
1681
pohmelie81ebba72016-01-27 23:23:11 +03001682STATIC void compile_yield_from(compiler_t *comp) {
Damien Georgef4df3aa2016-01-09 23:59:52 +00001683 EMIT_ARG(get_iter, false);
pohmelie81ebba72016-01-27 23:23:11 +03001684 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1685 EMIT(yield_from);
1686}
1687
1688#if MICROPY_PY_ASYNC_AWAIT
1689STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1690 EMIT_ARG(load_method, method);
1691 EMIT_ARG(call_method, 0, 0, 0);
1692 compile_yield_from(comp);
1693}
1694
1695STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1696 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1697
1698 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1699 uint while_else_label = comp_next_label(comp);
1700 uint try_exception_label = comp_next_label(comp);
1701 uint try_else_label = comp_next_label(comp);
1702 uint try_finally_label = comp_next_label(comp);
1703
1704 compile_node(comp, pns->nodes[1]); // iterator
1705 compile_await_object_method(comp, MP_QSTR___aiter__);
1706 compile_store_id(comp, context);
1707
1708 START_BREAK_CONTINUE_BLOCK
1709
1710 EMIT_ARG(label_assign, continue_label);
1711
1712 EMIT_ARG(setup_except, try_exception_label);
1713 compile_increase_except_level(comp);
1714
1715 compile_load_id(comp, context);
1716 compile_await_object_method(comp, MP_QSTR___anext__);
1717 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1718 EMIT(pop_block);
1719 EMIT_ARG(jump, try_else_label);
1720
1721 EMIT_ARG(label_assign, try_exception_label);
1722 EMIT(start_except_handler);
1723 EMIT(dup_top);
1724 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1725 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1726 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001727 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001728 EMIT(pop_except);
1729 EMIT_ARG(jump, while_else_label);
1730
1731 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001732 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001733 compile_decrease_except_level(comp);
1734 EMIT(end_finally);
1735 EMIT(end_except_handler);
1736
1737 EMIT_ARG(label_assign, try_else_label);
1738 compile_node(comp, pns->nodes[2]); // body
1739
1740 EMIT_ARG(jump, continue_label);
1741 // break/continue apply to outer loop (if any) in the else block
1742 END_BREAK_CONTINUE_BLOCK
1743
1744 EMIT_ARG(label_assign, while_else_label);
1745 compile_node(comp, pns->nodes[3]); // else
1746
1747 EMIT_ARG(label_assign, break_label);
1748}
1749
1750STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1751 if (n == 0) {
1752 // no more pre-bits, compile the body of the with
1753 compile_node(comp, body);
1754 } else {
1755 uint try_exception_label = comp_next_label(comp);
1756 uint no_reraise_label = comp_next_label(comp);
1757 uint try_else_label = comp_next_label(comp);
1758 uint end_label = comp_next_label(comp);
1759 qstr context;
1760
1761 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1762 // this pre-bit is of the form "a as b"
1763 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1764 compile_node(comp, pns->nodes[0]);
1765 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1766 compile_store_id(comp, context);
1767 compile_load_id(comp, context);
1768 compile_await_object_method(comp, MP_QSTR___aenter__);
1769 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1770 } else {
1771 // this pre-bit is just an expression
1772 compile_node(comp, nodes[0]);
1773 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1774 compile_store_id(comp, context);
1775 compile_load_id(comp, context);
1776 compile_await_object_method(comp, MP_QSTR___aenter__);
1777 EMIT(pop_top);
1778 }
1779
1780 compile_load_id(comp, context);
1781 EMIT_ARG(load_method, MP_QSTR___aexit__);
1782
1783 EMIT_ARG(setup_except, try_exception_label);
1784 compile_increase_except_level(comp);
1785 // compile additional pre-bits and the body
1786 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1787 // finish this with block
1788 EMIT(pop_block);
1789 EMIT_ARG(jump, try_else_label); // jump over exception handler
1790
1791 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1792 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001793
1794 // at this point the stack contains: ..., __aexit__, self, exc
1795 EMIT(dup_top);
1796 #if MICROPY_CPYTHON_COMPAT
1797 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1798 #else
1799 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001800 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001801 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1802 #endif
1803 EMIT(rot_two);
1804 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1805 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001806 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001807
pohmelie81ebba72016-01-27 23:23:11 +03001808 compile_yield_from(comp);
1809 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1810 EMIT_ARG(raise_varargs, 0);
1811
1812 EMIT_ARG(label_assign, no_reraise_label);
1813 EMIT(pop_except);
1814 EMIT_ARG(jump, end_label);
1815
Damien Georgeb32c01b2016-09-28 11:52:13 +10001816 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001817 compile_decrease_except_level(comp);
1818 EMIT(end_finally);
1819 EMIT(end_except_handler);
1820
1821 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1822 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1823 EMIT(dup_top);
1824 EMIT(dup_top);
1825 EMIT_ARG(call_method, 3, 0, 0);
1826 compile_yield_from(comp);
1827 EMIT(pop_top);
1828
1829 EMIT_ARG(label_assign, end_label);
1830
1831 }
1832}
1833
1834STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1835 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1836 mp_parse_node_t *nodes;
1837 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1838 assert(n > 0);
1839
1840 // compile in a nested fashion
1841 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1842}
1843
1844STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1845 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1846 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1847 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1848 // async def
1849 compile_funcdef(comp, pns0);
1850 scope_t *fscope = (scope_t*)pns0->nodes[4];
1851 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1852 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1853 // async for
1854 compile_async_for_stmt(comp, pns0);
1855 } else {
1856 // async with
1857 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1858 compile_async_with_stmt(comp, pns0);
1859 }
1860}
1861#endif
1862
Damien George969a6b32014-12-10 22:07:04 +00001863STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001864 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001865 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1866 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001867 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001868 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001869 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001870 EMIT(pop_top);
1871
Damien429d7192013-10-04 19:53:11 +01001872 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001873 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001874 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001875 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001876 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1877 || 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 George2bf7c092014-04-09 15:26:46 +01002189 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002190 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002191 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002192 EMIT_ARG(call_function, 2, 0, 0);
2193 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002194 }
2195 }
Damien George01039b52014-12-21 17:44:27 +00002196 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002197 return;
2198 }
Damien George35e2a4e2014-02-05 00:51:47 +00002199
Damien George2c0842b2014-04-27 16:46:51 +01002200 // get the list of arguments
2201 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002202 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002203
Damien George2c0842b2014-04-27 16:46:51 +01002204 // compile the arguments
2205 // Rather than calling compile_node on the list, we go through the list of args
2206 // explicitly here so that we can count the number of arguments and give sensible
2207 // error messages.
2208 int n_positional = n_positional_extra;
2209 uint n_keyword = 0;
2210 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002211 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002212 for (int i = 0; i < n_args; i++) {
2213 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2214 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2215 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2216 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2217 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2218 return;
2219 }
2220 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002221 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002222 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2223 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2224 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2225 return;
2226 }
2227 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002228 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002229 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002230 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002231 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2232 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2233 return;
2234 }
Damien George59fba2d2015-06-25 14:42:13 +00002235 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002236 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002237 n_keyword += 1;
2238 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002239 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2240 n_positional++;
2241 }
2242 } else {
2243 goto normal_argument;
2244 }
2245 } else {
2246 normal_argument:
2247 if (n_keyword > 0) {
2248 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2249 return;
2250 }
2251 compile_node(comp, args[i]);
2252 n_positional++;
2253 }
Damien429d7192013-10-04 19:53:11 +01002254 }
2255
Delio Brignolie6978a42015-09-17 12:07:06 +02002256 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002257 // if we had one but not the other then we load "null" as a place holder
2258 if (star_flags != 0) {
2259 if (star_args_node == NULL) {
2260 EMIT(load_null);
2261 } else {
2262 compile_node(comp, star_args_node->nodes[0]);
2263 }
2264 if (dblstar_args_node == NULL) {
2265 EMIT(load_null);
2266 } else {
2267 compile_node(comp, dblstar_args_node->nodes[0]);
2268 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002269 }
2270
Damien George2c0842b2014-04-27 16:46:51 +01002271 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002272 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002273 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002274 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002275 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002276 }
Damien429d7192013-10-04 19:53:11 +01002277}
2278
pohmelie81ebba72016-01-27 23:23:11 +03002279STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002280 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002281 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002282 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 +01002283 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002284 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2285 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002286 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002287 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002288 i += 1;
2289 } else {
2290 compile_node(comp, pns->nodes[i]);
2291 }
Damien George35e2a4e2014-02-05 00:51:47 +00002292 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002293 }
2294}
2295
Damien George969a6b32014-12-10 22:07:04 +00002296STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002297 // a list of strings
Damien63321742013-12-10 17:41:49 +00002298
2299 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002300 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002301 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002302 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002303 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002304 int pn_kind;
2305 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2306 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2307 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2308 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2309 } else {
2310 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2311 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002312 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2313 pn_kind = MP_PARSE_NODE_STRING;
2314 } else {
2315 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2316 pn_kind = MP_PARSE_NODE_BYTES;
2317 }
Damien George831137b2015-12-17 13:13:18 +00002318 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002319 }
Damien63321742013-12-10 17:41:49 +00002320 if (i == 0) {
2321 string_kind = pn_kind;
2322 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002323 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002324 return;
2325 }
Damien429d7192013-10-04 19:53:11 +01002326 }
Damien63321742013-12-10 17:41:49 +00002327
Damien George65ef6b72015-01-14 21:17:27 +00002328 // if we are not in the last pass, just load a dummy object
2329 if (comp->pass != MP_PASS_EMIT) {
2330 EMIT_ARG(load_const_obj, mp_const_none);
2331 return;
2332 }
2333
Damien63321742013-12-10 17:41:49 +00002334 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002335 vstr_t vstr;
2336 vstr_init_len(&vstr, n_bytes);
2337 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002338 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002339 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002340 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002341 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2342 memcpy(s_dest, s, s_len);
2343 s_dest += s_len;
2344 } else {
2345 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002346 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2347 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002348 }
Damien63321742013-12-10 17:41:49 +00002349 }
Damien George65ef6b72015-01-14 21:17:27 +00002350
2351 // load the object
Damien George05005f62015-01-21 22:48:37 +00002352 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002353}
2354
2355// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002356STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002357 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2358 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2359 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002360
Damien George36db6bc2014-05-07 17:24:22 +01002361 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002362 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002363 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 +01002364 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002365 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002366 }
2367
2368 // get the scope for this comprehension
2369 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2370
2371 // compile the comprehension
2372 close_over_variables_etc(comp, this_scope, 0, 0);
2373
2374 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
Damien Georgef4df3aa2016-01-09 23:59:52 +00002375 EMIT_ARG(get_iter, false);
Damien George922ddd62014-04-09 12:43:17 +01002376 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002377}
2378
Damien George969a6b32014-12-10 22:07:04 +00002379STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002380 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002381 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002382 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002383 } else {
2384 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002385 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2386 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2387 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2388 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2389 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002390 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002391 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002392 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002393 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002394 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002395 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002396 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002397 // generator expression
2398 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2399 } else {
2400 // tuple with 2 items
2401 goto tuple_with_2_items;
2402 }
2403 } else {
2404 // tuple with 2 items
2405 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002406 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002407 }
Damien429d7192013-10-04 19:53:11 +01002408 }
2409}
2410
Damien George969a6b32014-12-10 22:07:04 +00002411STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002412 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002413 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002414 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002415 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2416 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2417 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2418 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2419 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002420 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002421 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002422 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002423 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002424 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002425 // list of many items
2426 compile_node(comp, pns2->nodes[0]);
2427 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002428 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002429 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002430 // list comprehension
2431 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2432 } else {
2433 // list with 2 items
2434 goto list_with_2_items;
2435 }
2436 } else {
2437 // list with 2 items
2438 list_with_2_items:
2439 compile_node(comp, pns2->nodes[0]);
2440 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002441 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002442 }
2443 } else {
2444 // list with 1 item
2445 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002446 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002447 }
2448}
2449
Damien George969a6b32014-12-10 22:07:04 +00002450STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002451 mp_parse_node_t pn = pns->nodes[0];
2452 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002453 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002454 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002455 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2456 pns = (mp_parse_node_struct_t*)pn;
2457 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002458 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002459 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002460 compile_node(comp, pn);
2461 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2463 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2464 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2465 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002466 // dict/set with multiple elements
2467
2468 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002469 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002470 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002471
2472 // first element sets whether it's a dict or set
2473 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002474 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002475 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002476 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002477 compile_node(comp, pns->nodes[0]);
2478 EMIT(store_map);
2479 is_dict = true;
2480 } else {
2481 // a set
2482 compile_node(comp, pns->nodes[0]); // 1st value of set
2483 is_dict = false;
2484 }
2485
2486 // process rest of elements
2487 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002488 mp_parse_node_t pn_i = nodes[i];
2489 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2490 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002491 if (is_dict) {
2492 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002493 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002494 return;
2495 }
2496 EMIT(store_map);
2497 } else {
2498 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002499 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002500 return;
2501 }
2502 }
2503 }
2504
Damien Georgee37dcaa2014-12-27 17:07:16 +00002505 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002506 // if it's a set, build it
2507 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002508 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002509 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002510 #endif
Damien George0bb97132015-02-27 14:25:47 +00002511 } else {
2512 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002513 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002514 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002515 // a dictionary comprehension
2516 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2517 } else {
2518 // a set comprehension
2519 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2520 }
Damien429d7192013-10-04 19:53:11 +01002521 }
2522 } else {
2523 // set with one element
2524 goto set_with_one_element;
2525 }
2526 } else {
2527 // set with one element
2528 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002529 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002530 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002531 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002532 #else
2533 assert(0);
2534 #endif
Damien429d7192013-10-04 19:53:11 +01002535 }
2536}
2537
Damien George969a6b32014-12-10 22:07:04 +00002538STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002539 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002540}
2541
Damien George969a6b32014-12-10 22:07:04 +00002542STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002543 // object who's index we want is on top of stack
2544 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002545 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002546}
2547
Damien George969a6b32014-12-10 22:07:04 +00002548STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002549 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002551}
2552
Damien George83204f32014-12-27 17:20:41 +00002553#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002554STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002555 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2556 mp_parse_node_t pn = pns->nodes[0];
2557 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002558 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002559 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2560 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002561 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2562 pns = (mp_parse_node_struct_t*)pn;
2563 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002565 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002566 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002567 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002568 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002569 } else {
2570 // [?::x]
2571 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002572 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002573 }
Damiend99b0522013-12-21 18:17:45 +00002574 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002575 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002576 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2577 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2578 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2579 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002580 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002581 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002582 } else {
2583 // [?:x:x]
2584 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002585 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002586 }
2587 } else {
2588 // [?:x]
2589 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002591 }
2592 } else {
2593 // [?:x]
2594 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002595 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002596 }
2597}
2598
Damien George969a6b32014-12-10 22:07:04 +00002599STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002600 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002601 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2602 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002603}
2604
Damien George969a6b32014-12-10 22:07:04 +00002605STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002606 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002607 compile_subscript_3_helper(comp, pns);
2608}
Damien George83204f32014-12-27 17:20:41 +00002609#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002610
Damien George969a6b32014-12-10 22:07:04 +00002611STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002612 // if this is called then we are compiling a dict key:value pair
2613 compile_node(comp, pns->nodes[1]); // value
2614 compile_node(comp, pns->nodes[0]); // key
2615}
2616
Damien George969a6b32014-12-10 22:07:04 +00002617STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002618 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002619 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002620 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002621}
2622
Damien George969a6b32014-12-10 22:07:04 +00002623STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002624 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002625 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002626 return;
2627 }
Damiend99b0522013-12-21 18:17:45 +00002628 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002629 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002630 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002631 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2632 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002633 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002634 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002635 } else {
2636 compile_node(comp, pns->nodes[0]);
2637 EMIT(yield_value);
2638 }
2639}
2640
pohmelie81ebba72016-01-27 23:23:11 +03002641#if MICROPY_PY_ASYNC_AWAIT
2642STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2643 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2644 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2645 return;
2646 }
2647 compile_atom_expr_normal(comp, pns);
2648 compile_yield_from(comp);
2649}
2650#endif
2651
Damien George65ef6b72015-01-14 21:17:27 +00002652STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2653 // only create and load the actual str object on the last pass
2654 if (comp->pass != MP_PASS_EMIT) {
2655 EMIT_ARG(load_const_obj, mp_const_none);
2656 } else {
Damien George831137b2015-12-17 13:13:18 +00002657 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], pns->nodes[1], false));
Damien George65ef6b72015-01-14 21:17:27 +00002658 }
2659}
2660
2661STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2662 // only create and load the actual bytes object on the last pass
2663 if (comp->pass != MP_PASS_EMIT) {
2664 EMIT_ARG(load_const_obj, mp_const_none);
2665 } else {
Damien George831137b2015-12-17 13:13:18 +00002666 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002667 }
2668}
2669
Damien George7d414a12015-02-08 01:57:40 +00002670STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002671 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2672 // nodes are 32-bit pointers, but need to extract 64-bit object
2673 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2674 #else
Damien George7d414a12015-02-08 01:57:40 +00002675 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002676 #endif
Damien George7d414a12015-02-08 01:57:40 +00002677}
2678
Damiend99b0522013-12-21 18:17:45 +00002679typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002680STATIC const compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002681#define nc NULL
2682#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002683#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002684#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002685#undef nc
2686#undef c
2687#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002688 NULL,
2689 compile_string,
2690 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002691 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002692};
2693
Damien George6be0b0a2014-08-15 14:30:52 +01002694STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002695 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002696 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002697 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002698 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002699 #if MICROPY_DYNAMIC_COMPILER
2700 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2701 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2702 // integer fits in target runtime's small-int
2703 EMIT_ARG(load_const_small_int, arg);
2704 } else {
2705 // integer doesn't fit, so create a multi-precision int object
2706 // (but only create the actual object on the last pass)
2707 if (comp->pass != MP_PASS_EMIT) {
2708 EMIT_ARG(load_const_obj, mp_const_none);
2709 } else {
2710 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2711 }
2712 }
2713 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002714 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002715 #endif
Damiend99b0522013-12-21 18:17:45 +00002716 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002717 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002718 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002719 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002720 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2721 case MP_PARSE_NODE_BYTES:
2722 // only create and load the actual bytes object on the last pass
2723 if (comp->pass != MP_PASS_EMIT) {
2724 EMIT_ARG(load_const_obj, mp_const_none);
2725 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002726 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002727 const byte *data = qstr_data(arg, &len);
2728 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2729 }
2730 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002731 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002732 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002733 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002734 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002735 // do nothing
2736 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002737 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002738 }
2739 break;
Damien429d7192013-10-04 19:53:11 +01002740 }
2741 } else {
Damiend99b0522013-12-21 18:17:45 +00002742 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002743 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002744 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002745 assert(f != NULL);
2746 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002747 }
2748}
2749
Damien George2ac4af62014-08-15 16:45:41 +01002750STATIC 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 +00002751 // check that **kw is last
2752 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2753 compile_syntax_error(comp, pn, "invalid syntax");
2754 return;
2755 }
2756
Damien George2827d622014-04-27 15:50:52 +01002757 qstr param_name = MP_QSTR_NULL;
2758 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002759 if (MP_PARSE_NODE_IS_ID(pn)) {
2760 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002761 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002762 // comes after a star, so counts as a keyword-only parameter
2763 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002764 } else {
Damien George2827d622014-04-27 15:50:52 +01002765 // comes before a star, so counts as a positional parameter
2766 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002767 }
Damienb14de212013-10-06 00:28:28 +01002768 } else {
Damiend99b0522013-12-21 18:17:45 +00002769 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2770 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2771 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2772 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002773 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002774 // comes after a star, so counts as a keyword-only parameter
2775 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002776 } else {
Damien George2827d622014-04-27 15:50:52 +01002777 // comes before a star, so counts as a positional parameter
2778 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002779 }
Damiend99b0522013-12-21 18:17:45 +00002780 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002781 if (comp->have_star) {
2782 // more than one star
2783 compile_syntax_error(comp, pn, "invalid syntax");
2784 return;
2785 }
Damien George8b19db02014-04-11 23:25:34 +01002786 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002787 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002788 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002789 // bare star
2790 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002791 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002792 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002793 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002794 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002795 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002796 } else {
2797 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002798 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002799 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002800 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2801 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002802 }
Damien George0bb97132015-02-27 14:25:47 +00002803 } else {
2804 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002805 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002806 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002807 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002808 }
Damien429d7192013-10-04 19:53:11 +01002809 }
2810
Damien George2827d622014-04-27 15:50:52 +01002811 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002812 bool added;
2813 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2814 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002815 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002816 return;
2817 }
Damien429d7192013-10-04 19:53:11 +01002818 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002819 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002820 }
2821}
2822
Damien George2827d622014-04-27 15:50:52 +01002823STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002824 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002825}
2826
Damien George2827d622014-04-27 15:50:52 +01002827STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002828 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2829}
2830
Damien Georgeea5b59b2015-09-04 16:44:14 +01002831#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002832STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2833 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2834 // no annotation
2835 return;
2836 }
2837
2838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2839 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2840 // named parameter with possible annotation
2841 // fallthrough
2842 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2843 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2844 // named star with possible annotation
2845 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2846 // fallthrough
2847 } else {
2848 // no annotation
2849 return;
2850 }
Damien Georgee49153f2016-10-11 12:29:54 +11002851 } else {
2852 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002853 // double star with possible annotation
2854 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002855 }
2856
2857 mp_parse_node_t pn_annotation = pns->nodes[1];
2858
2859 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002860 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2861 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2862 assert(id_info != NULL);
2863
Damien Georgeea5b59b2015-09-04 16:44:14 +01002864 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2865 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2866 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2867 } else {
2868 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002869 }
Damien George2ac4af62014-08-15 16:45:41 +01002870 }
Damien429d7192013-10-04 19:53:11 +01002871}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002872#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002873
Damien Georgea8312432015-12-18 01:37:55 +00002874STATIC 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) {
2875 uint l_top = comp_next_label(comp);
2876 uint l_end = comp_next_label(comp);
2877 EMIT_ARG(label_assign, l_top);
2878 EMIT_ARG(for_iter, l_end);
2879 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2880 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2881
Damien429d7192013-10-04 19:53:11 +01002882 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002883 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002884 // no more nested if/for; compile inner expression
2885 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002886 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002887 EMIT(yield_value);
2888 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002889 } else {
Damien George6e769da2017-01-17 14:32:50 +11002890 EMIT_ARG(store_comp, comp->scope_cur->kind, 5 * for_depth + 6);
Damien429d7192013-10-04 19:53:11 +01002891 }
Damiend99b0522013-12-21 18:17:45 +00002892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002893 // if condition
Damiend99b0522013-12-21 18:17:45 +00002894 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002895 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2896 pn_iter = pns_comp_if->nodes[1];
2897 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002898 } else {
2899 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002900 // for loop
Damiend99b0522013-12-21 18:17:45 +00002901 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002902 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6e769da2017-01-17 14:32:50 +11002903 EMIT_ARG(get_iter, true);
Damien Georgea8312432015-12-18 01:37:55 +00002904 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002905 }
Damien Georgea8312432015-12-18 01:37:55 +00002906
2907 EMIT_ARG(jump, l_top);
2908 EMIT_ARG(label_assign, l_end);
Damien George6e769da2017-01-17 14:32:50 +11002909 EMIT_ARG(for_iter_end, true);
Damien429d7192013-10-04 19:53:11 +01002910}
2911
Damien George1463c1f2014-04-25 23:52:57 +01002912STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002913#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002914 // see http://www.python.org/dev/peps/pep-0257/
2915
2916 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002917 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002918 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002919 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002920 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002921 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2922 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002923 for (int i = 0; i < num_nodes; i++) {
2924 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002925 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 +00002926 // not a newline, so this is the first statement; finish search
2927 break;
2928 }
2929 }
2930 // 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 +00002931 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002932 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002933 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002934 } else {
2935 return;
2936 }
2937
2938 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002939 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002940 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002941 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2942 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2943 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2944 // compile the doc string
2945 compile_node(comp, pns->nodes[0]);
2946 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002947 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002948 }
2949 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002950#else
2951 (void)comp;
2952 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002953#endif
Damien429d7192013-10-04 19:53:11 +01002954}
2955
Damien George1463c1f2014-04-25 23:52:57 +01002956STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002957 comp->pass = pass;
2958 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002959 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002960 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002961
Damien George36db6bc2014-05-07 17:24:22 +01002962 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002963 // reset maximum stack sizes in scope
2964 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002965 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002966 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002967 }
2968
Damien429d7192013-10-04 19:53:11 +01002969 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002970 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2971 assert(scope->kind == SCOPE_MODULE);
2972 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2973 compile_node(comp, pns->nodes[0]); // compile the expression
2974 EMIT(return_value);
2975 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002976 if (!comp->is_repl) {
2977 check_for_doc_string(comp, scope->pn);
2978 }
Damien429d7192013-10-04 19:53:11 +01002979 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002980 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002981 EMIT(return_value);
2982 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002983 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2984 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2985 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002986
2987 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002988 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002989 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002990 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002991 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002992 }
2993 #if MICROPY_EMIT_NATIVE
2994 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002995 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002996 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002997
Damien George2ac4af62014-08-15 16:45:41 +01002998 // argument annotations
2999 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3000
3001 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003002 mp_parse_node_t pn_annotation = pns->nodes[2];
3003 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01003004 // nodes[2] can be null or a test-expr
3005 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3006 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3007 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3008 } else {
3009 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003010 }
3011 }
Damien George2ac4af62014-08-15 16:45:41 +01003012 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01003013 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01003014
3015 compile_node(comp, pns->nodes[3]); // 3 is function body
3016 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003017 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003018 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003019 EMIT(return_value);
3020 }
3021 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003022 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3023 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3024 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003025
3026 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003027 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003028 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003029 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003030 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3031 }
3032
3033 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003034
3035 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3036 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3037 EMIT(pop_top);
3038 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3039 }
Damien429d7192013-10-04 19:53:11 +01003040 EMIT(return_value);
3041 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3042 // a bit of a hack at the moment
3043
Damiend99b0522013-12-21 18:17:45 +00003044 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3045 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3046 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3047 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3048 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003049
Damien George708c0732014-04-27 19:23:46 +01003050 // We need a unique name for the comprehension argument (the iterator).
3051 // CPython uses .0, but we should be able to use anything that won't
3052 // clash with a user defined variable. Best to use an existing qstr,
3053 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003054 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003055 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003056 bool added;
3057 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3058 assert(added);
3059 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003060 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003061 }
3062
3063 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003064 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003065 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003066 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003067 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003068 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003069 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003070 #endif
Damien429d7192013-10-04 19:53:11 +01003071 }
3072
Damien George6e769da2017-01-17 14:32:50 +11003073 // dummy 4 objects
3074 EMIT(load_null);
3075 EMIT(load_null);
3076 EMIT(load_null);
3077 EMIT(load_null);
3078
Damien George542bd6b2015-03-26 14:42:40 +00003079 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00003080 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003081
3082 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003083 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003084 }
3085 EMIT(return_value);
3086 } else {
3087 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003088 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3089 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3090 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003091
Damien George36db6bc2014-05-07 17:24:22 +01003092 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003093 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003094 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003095 assert(added);
3096 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003097 }
3098
Damien George542bd6b2015-03-26 14:42:40 +00003099 compile_load_id(comp, MP_QSTR___name__);
3100 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003101 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003102 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003103
3104 check_for_doc_string(comp, pns->nodes[2]);
3105 compile_node(comp, pns->nodes[2]); // 2 is class body
3106
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003107 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003108 assert(id != NULL);
3109 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003110 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003111 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003112 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003113 }
3114 EMIT(return_value);
3115 }
3116
Damien415eb6f2013-10-05 12:19:06 +01003117 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003118
3119 // make sure we match all the exception levels
3120 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003121}
3122
Damien Georgead297a12016-12-09 13:17:49 +11003123#if MICROPY_EMIT_INLINE_ASM
Damien George36db6bc2014-05-07 17:24:22 +01003124// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003125STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003126 comp->pass = pass;
3127 comp->scope_cur = scope;
3128 comp->next_label = 1;
3129
3130 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003131 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003132 return;
3133 }
3134
Damien George36db6bc2014-05-07 17:24:22 +01003135 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgee920bab2016-12-09 21:23:17 +11003136 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003137 }
3138
Damien826005c2013-10-05 23:17:28 +01003139 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003140 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3141 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3142 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003143
Damiend99b0522013-12-21 18:17:45 +00003144 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003145
Damiena2f2f7d2013-10-06 00:14:13 +01003146 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003147 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003148 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003149 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003150 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003151 if (comp->compile_error != MP_OBJ_NULL) {
3152 goto inline_asm_error;
3153 }
Damiena2f2f7d2013-10-06 00:14:13 +01003154 }
3155
Damien George8f54c082016-01-15 15:20:43 +00003156 // pns->nodes[2] is function return annotation
3157 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3158 mp_parse_node_t pn_annotation = pns->nodes[2];
3159 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3160 // nodes[2] can be null or a test-expr
3161 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3162 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3163 switch (ret_type) {
3164 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3165 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3166 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3167 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3168 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3169 }
3170 } else {
3171 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3172 }
3173 }
Damien826005c2013-10-05 23:17:28 +01003174
Damiend99b0522013-12-21 18:17:45 +00003175 mp_parse_node_t pn_body = pns->nodes[3]; // body
3176 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003177 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003178
Damien826005c2013-10-05 23:17:28 +01003179 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003180 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3181 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003182 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3183 // no instructions
3184 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003185 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003186 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003187 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003188 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003189 return;
3190 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003191
3192 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003193 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003194 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3195 goto not_an_instruction;
3196 }
Damiend99b0522013-12-21 18:17:45 +00003197 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003198 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003199 goto not_an_instruction;
3200 }
3201 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3202 goto not_an_instruction;
3203 }
3204 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3205 goto not_an_instruction;
3206 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003207
3208 // parse node looks like an instruction
3209 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003210 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3211 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3212 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003213 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003214
3215 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003216 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003217 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003218 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003219 return;
3220 }
Damien George6f355fd2014-04-10 14:11:31 +01003221 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003222 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003223 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3224 compile_syntax_error(comp, nodes[i], "label redefined");
3225 return;
3226 }
Damien826005c2013-10-05 23:17:28 +01003227 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003228 } else if (op == MP_QSTR_align) {
3229 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003230 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003231 return;
3232 }
Damien George36db6bc2014-05-07 17:24:22 +01003233 if (pass > MP_PASS_SCOPE) {
Damien Georgedd53b122016-12-09 20:54:54 +11003234 mp_asm_base_align((mp_asm_base_t*)comp->emit_inline_asm,
3235 MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003236 }
3237 } else if (op == MP_QSTR_data) {
3238 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003239 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003240 return;
3241 }
Damien George36db6bc2014-05-07 17:24:22 +01003242 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003243 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003244 for (uint j = 1; j < n_args; j++) {
3245 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003246 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003247 return;
3248 }
Damien Georgedd53b122016-12-09 20:54:54 +11003249 mp_asm_base_data((mp_asm_base_t*)comp->emit_inline_asm,
3250 bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003251 }
3252 }
Damien826005c2013-10-05 23:17:28 +01003253 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003254 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003255 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003256 }
3257 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003258
3259 if (comp->compile_error != MP_OBJ_NULL) {
3260 pns = pns2; // this is the parse node that had the error
3261 goto inline_asm_error;
3262 }
Damien826005c2013-10-05 23:17:28 +01003263 }
3264
Damien George36db6bc2014-05-07 17:24:22 +01003265 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003266 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien Georgee920bab2016-12-09 21:23:17 +11003267
3268 if (comp->pass == MP_PASS_EMIT) {
3269 void *f = mp_asm_base_get_code((mp_asm_base_t*)comp->emit_inline_asm);
3270 mp_emit_glue_assign_native(comp->scope_cur->raw_code, MP_CODE_NATIVE_ASM,
3271 f, mp_asm_base_get_code_size((mp_asm_base_t*)comp->emit_inline_asm),
3272 NULL, comp->scope_cur->num_pos_args, 0, type_sig);
3273 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003274 }
3275
3276 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003277 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003278 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003279 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003280 }
Damien429d7192013-10-04 19:53:11 +01003281}
Damien George094d4502014-04-02 17:31:27 +01003282#endif
Damien429d7192013-10-04 19:53:11 +01003283
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003284STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003285 // in Micro Python we put the *x parameter after all other parameters (except **y)
3286 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3287 id_info_t *id_param = NULL;
3288 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3289 id_info_t *id = &scope->id_info[i];
3290 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3291 if (id_param != NULL) {
3292 // swap star param with last param
3293 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3294 }
3295 break;
3296 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3297 id_param = id;
3298 }
3299 }
3300 }
Damien George2827d622014-04-27 15:50:52 +01003301
Damien429d7192013-10-04 19:53:11 +01003302 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003303 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003304 scope->num_locals = 0;
3305 for (int i = 0; i < scope->id_info_len; i++) {
3306 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003307 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003308 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3309 continue;
3310 }
Damien George3dea8c92016-09-30 12:34:05 +10003311 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003312 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3313 }
Damien George2827d622014-04-27 15:50:52 +01003314 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003315 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003316 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003317 }
3318 }
3319
Damien George65dc9602015-08-14 12:24:11 +01003320 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003321 for (int i = 0; i < scope->id_info_len; i++) {
3322 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003323 // in Micro Python the cells come right after the fast locals
3324 // parameters are not counted here, since they remain at the start
3325 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003326 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003327 id->local_num = scope->num_locals;
3328 scope->num_locals += 1;
3329 }
Damien9ecbcff2013-12-11 00:41:43 +00003330 }
Damien9ecbcff2013-12-11 00:41:43 +00003331
Damien George65dc9602015-08-14 12:24:11 +01003332 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003333 // make sure they are in the order of the parent scope
3334 if (scope->parent != NULL) {
3335 int num_free = 0;
3336 for (int i = 0; i < scope->parent->id_info_len; i++) {
3337 id_info_t *id = &scope->parent->id_info[i];
3338 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3339 for (int j = 0; j < scope->id_info_len; j++) {
3340 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003341 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003342 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003343 // in Micro Python the frees come first, before the params
3344 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003345 num_free += 1;
3346 }
3347 }
3348 }
Damien429d7192013-10-04 19:53:11 +01003349 }
Damien George6baf76e2013-12-30 22:32:17 +00003350 // in Micro Python shift all other locals after the free locals
3351 if (num_free > 0) {
3352 for (int i = 0; i < scope->id_info_len; i++) {
3353 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003354 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003355 id->local_num += num_free;
3356 }
3357 }
Damien George2827d622014-04-27 15:50:52 +01003358 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 +00003359 scope->num_locals += num_free;
3360 }
Damien429d7192013-10-04 19:53:11 +01003361 }
Damien429d7192013-10-04 19:53:11 +01003362}
3363
Damien Georged4dba882015-11-13 13:38:28 +00003364#if !MICROPY_PERSISTENT_CODE_SAVE
3365STATIC
3366#endif
3367mp_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 +01003368 // put compiler state on the stack, it's relatively small
3369 compiler_t comp_state = {0};
3370 compiler_t *comp = &comp_state;
3371
Damien Georgecbd2f742014-01-19 11:48:48 +00003372 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003373 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003374
Damien George62a3a282015-03-01 12:04:05 +00003375 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003376 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003377
Damien Georgea210c772015-03-26 15:49:53 +00003378 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003379 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003380
Damien826005c2013-10-05 23:17:28 +01003381 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003382 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003383 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003384 comp->emit_method_table = &emit_bc_method_table;
3385 #endif
Damien826005c2013-10-05 23:17:28 +01003386 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003387 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003388 if (false) {
Damien Georgead297a12016-12-09 13:17:49 +11003389 #if MICROPY_EMIT_INLINE_ASM
3390 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
Damien George36db6bc2014-05-07 17:24:22 +01003391 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damien Georgead297a12016-12-09 13:17:49 +11003392 #endif
Damien826005c2013-10-05 23:17:28 +01003393 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003394 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003395 }
3396
3397 // update maximim number of labels needed
3398 if (comp->next_label > max_num_labels) {
3399 max_num_labels = comp->next_label;
3400 }
Damien429d7192013-10-04 19:53:11 +01003401 }
3402
Damien826005c2013-10-05 23:17:28 +01003403 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003404 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 +00003405 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003406 }
3407
Damien Georgea210c772015-03-26 15:49:53 +00003408 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003409 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003410
Damien826005c2013-10-05 23:17:28 +01003411 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003412#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003413 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003414#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003415 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003416 if (false) {
3417 // dummy
3418
Damien Georgead297a12016-12-09 13:17:49 +11003419 #if MICROPY_EMIT_INLINE_ASM
3420 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
3421 // inline assembly
3422 if (comp->emit_inline_asm == NULL) {
3423 comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003424 }
3425 comp->emit = NULL;
Damien Georgead297a12016-12-09 13:17:49 +11003426 comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table);
Damien George36db6bc2014-05-07 17:24:22 +01003427 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgede9cd002016-12-19 17:42:25 +11003428 #if MICROPY_EMIT_INLINE_XTENSA
3429 // Xtensa requires an extra pass to compute size of l32r const table
3430 // TODO this can be improved by calculating it during SCOPE pass
3431 // but that requires some other structural changes to the asm emitters
3432 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
3433 #endif
Damien Georgea91ac202014-10-05 19:01:34 +01003434 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003435 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003436 }
Damien Georgead297a12016-12-09 13:17:49 +11003437 #endif
Damienc025ebb2013-10-12 14:30:21 +01003438
Damien826005c2013-10-05 23:17:28 +01003439 } else {
Damienc025ebb2013-10-12 14:30:21 +01003440
3441 // choose the emit type
3442
Damien826005c2013-10-05 23:17:28 +01003443 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003444
3445#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003446 case MP_EMIT_OPT_NATIVE_PYTHON:
3447 case MP_EMIT_OPT_VIPER:
Damiendc833822013-10-06 01:01:01 +01003448 if (emit_native == NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003449 emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003450 }
Damien George080a78b2016-12-07 11:17:17 +11003451 comp->emit_method_table = &NATIVE_EMITTER(method_table);
Damienc025ebb2013-10-12 14:30:21 +01003452 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003453 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003454 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003455#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003456
Damien826005c2013-10-05 23:17:28 +01003457 default:
Damien826005c2013-10-05 23:17:28 +01003458 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003459 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003460 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003461 #endif
Damien826005c2013-10-05 23:17:28 +01003462 break;
3463 }
Damienc025ebb2013-10-12 14:30:21 +01003464
Damien George1e1779e2015-01-14 00:20:28 +00003465 // need a pass to compute stack size
3466 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3467
Damien George36db6bc2014-05-07 17:24:22 +01003468 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003469 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003470 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3471 }
3472
3473 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003474 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003475 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003476 }
Damien6cdd3af2013-10-05 18:08:26 +01003477 }
Damien429d7192013-10-04 19:53:11 +01003478 }
3479
Damien Georgecfc4c332015-07-29 22:16:01 +00003480 if (comp->compile_error != MP_OBJ_NULL) {
3481 // if there is no line number for the error then use the line
3482 // number for the start of this scope
3483 compile_error_set_line(comp, comp->scope_cur->pn);
3484 // add a traceback to the exception using relevant source info
3485 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3486 comp->compile_error_line, comp->scope_cur->simple_name);
3487 }
3488
Damien George41d02b62014-01-24 22:42:28 +00003489 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003490
Damien Georgea210c772015-03-26 15:49:53 +00003491 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003492#if MICROPY_EMIT_NATIVE
3493 if (emit_native != NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003494 NATIVE_EMITTER(free)(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003495 }
3496#endif
Damien Georgead297a12016-12-09 13:17:49 +11003497 #if MICROPY_EMIT_INLINE_ASM
3498 if (comp->emit_inline_asm != NULL) {
3499 ASM_EMITTER(free)(comp->emit_inline_asm);
Damien George41d02b62014-01-24 22:42:28 +00003500 }
Damien Georgead297a12016-12-09 13:17:49 +11003501 #endif
Damien George41d02b62014-01-24 22:42:28 +00003502
Damien George52b5d762014-09-23 15:31:56 +00003503 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003504 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003505
Damien George41d02b62014-01-24 22:42:28 +00003506 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003507 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003508 for (scope_t *s = module_scope; s;) {
3509 scope_t *next = s->next;
3510 scope_free(s);
3511 s = next;
3512 }
Damien5ac1b2e2013-10-18 19:58:12 +01003513
Damien George9d5e5c02015-09-24 13:15:57 +01003514 if (comp->compile_error != MP_OBJ_NULL) {
3515 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003516 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003517 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003518 }
Damien429d7192013-10-04 19:53:11 +01003519}
Damien Georged4dba882015-11-13 13:38:28 +00003520
3521mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3522 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3523 // return function that executes the outer module
3524 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3525}
Damien Georgedd5353a2015-12-18 12:35:44 +00003526
3527#endif // MICROPY_ENABLE_COMPILER