blob: bc8d8d16d768a0ae7ea266bcd21d987162a0967f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Damien George9d5e5c02015-09-24 13:15:57 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010037
Damien Georgedd5353a2015-12-18 12:35:44 +000038#if MICROPY_ENABLE_COMPILER
39
Damien429d7192013-10-04 19:53:11 +010040// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George65dc9602015-08-14 12:24:11 +010052#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000053
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010061
Damien George41125902015-03-26 16:44:14 +000062#else
63
64// if we only have the bytecode emitter enabled then we can do a direct call to the functions
65#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
66#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
67#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
68#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
69
70#endif
71
Damien George0496de22015-10-03 17:07:54 +010072#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
73#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
74
Damien Georgea91ac202014-10-05 19:01:34 +010075// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010076typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000077 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010078
Damien George78035b92014-04-09 12:27:39 +010079 uint8_t is_repl;
80 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010081 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010082 uint8_t have_star;
83
84 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +000085 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +000086 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +010087
Damien George6f355fd2014-04-10 14:11:31 +010088 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010089
Damien George69b89d22014-04-11 13:38:30 +000090 uint16_t num_dict_params;
91 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010092
Damien Georgea91ac202014-10-05 19:01:34 +010093 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
94 uint16_t continue_label;
95 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000096 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010097
Damien429d7192013-10-04 19:53:11 +010098 scope_t *scope_head;
99 scope_t *scope_cur;
100
Damien6cdd3af2013-10-05 18:08:26 +0100101 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000102 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100103 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000104 #endif
Damien826005c2013-10-05 23:17:28 +0100105
Damien Georgea77ffe62015-03-14 12:59:31 +0000106 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100107 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
108 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000109 #endif
Damien429d7192013-10-04 19:53:11 +0100110} compiler_t;
111
Damien Georgecfc4c332015-07-29 22:16:01 +0000112STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
113 // if the line of the error is unknown then try to update it from the pn
114 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000115 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100116 }
Damien George84d59c22015-07-27 22:20:00 +0100117}
118
119STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000120 // only register the error if there has been no other error
121 if (comp->compile_error == MP_OBJ_NULL) {
122 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
123 compile_error_set_line(comp, pn);
124 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000125}
126
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200127STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100128STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000129STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100130
Damien George6f355fd2014-04-10 14:11:31 +0100131STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100132 return comp->next_label++;
133}
134
Damien George8dcc0c72014-03-27 10:55:21 +0000135STATIC void compile_increase_except_level(compiler_t *comp) {
136 comp->cur_except_level += 1;
137 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
138 comp->scope_cur->exc_stack_size = comp->cur_except_level;
139 }
140}
141
142STATIC void compile_decrease_except_level(compiler_t *comp) {
143 assert(comp->cur_except_level > 0);
144 comp->cur_except_level -= 1;
145}
146
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200147STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100148 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100149 scope->parent = comp->scope_cur;
150 scope->next = NULL;
151 if (comp->scope_head == NULL) {
152 comp->scope_head = scope;
153 } else {
154 scope_t *s = comp->scope_head;
155 while (s->next != NULL) {
156 s = s->next;
157 }
158 s->next = scope;
159 }
160 return scope;
161}
162
Damien George91bc32d2015-04-09 15:31:53 +0000163typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
164
165STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) {
Damien George963a5a32015-01-16 17:47:07 +0000166 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000167 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
168 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100169 for (int i = 0; i < num_nodes; i++) {
170 f(comp, pns->nodes[i]);
171 }
Damiend99b0522013-12-21 18:17:45 +0000172 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100173 f(comp, pn);
174 }
175}
176
Damien George969a6b32014-12-10 22:07:04 +0000177STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000178 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100179 for (int i = 0; i < num_nodes; i++) {
180 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000181 if (comp->compile_error != MP_OBJ_NULL) {
182 // add line info for the error in case it didn't have a line number
183 compile_error_set_line(comp, pns->nodes[i]);
184 return;
185 }
Damien429d7192013-10-04 19:53:11 +0100186 }
187}
188
Damien George542bd6b2015-03-26 14:42:40 +0000189STATIC void compile_load_id(compiler_t *comp, qstr qst) {
190 if (comp->pass == MP_PASS_SCOPE) {
191 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
192 } else {
Damien George41125902015-03-26 16:44:14 +0000193 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000194 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000195 #else
196 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
197 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000198 }
199}
200
201STATIC void compile_store_id(compiler_t *comp, qstr qst) {
202 if (comp->pass == MP_PASS_SCOPE) {
203 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
204 } else {
Damien George41125902015-03-26 16:44:14 +0000205 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000206 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000207 #else
208 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
209 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000210 }
211}
212
213STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
214 if (comp->pass == MP_PASS_SCOPE) {
215 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
216 } else {
Damien George41125902015-03-26 16:44:14 +0000217 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000218 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000219 #else
220 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
221 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000222 }
223}
224
Damien George2c0842b2014-04-27 16:46:51 +0100225STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100226 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000227 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100228 compile_node(comp, pn);
229 total += 1;
230 }
231 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000232 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100233 for (int i = 0; i < n; i++) {
234 compile_node(comp, pns_list->nodes[i]);
235 }
236 total += n;
237 }
Damien Georgeb9791222014-01-23 00:34:21 +0000238 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100239}
Damien429d7192013-10-04 19:53:11 +0100240
Damien George969a6b32014-12-10 22:07:04 +0000241STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100242 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000243 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100244}
245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100247 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100248 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000249 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100250 }
251 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100252 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100253 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000254 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100255 }
256 return;
Damiend99b0522013-12-21 18:17:45 +0000257 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
258 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
259 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
260 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100261 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000262 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100263 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100264 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000265 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100266 }
Damien George0b2fd912015-02-28 14:37:54 +0000267 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000268 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100269 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000270 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100271 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000272 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100273 }
274 }
275 return;
Damiend99b0522013-12-21 18:17:45 +0000276 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100277 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000278 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100279 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000280 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100281 }
Damiend99b0522013-12-21 18:17:45 +0000282 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100283 c_if_cond(comp, pns->nodes[0], !jump_if, label);
284 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100285 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
286 // cond is something in parenthesis
287 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
288 // empty tuple, acts as false for the condition
289 if (jump_if == false) {
290 EMIT_ARG(jump, label);
291 }
Damien George93b37262016-01-07 13:07:52 +0000292 } else {
293 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100294 // non-empty tuple, acts as true for the condition
295 if (jump_if == true) {
296 EMIT_ARG(jump, label);
297 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100298 }
299 return;
Damien3a205172013-10-12 15:01:56 +0100300 }
301 }
302
303 // nothing special, fall back to default compiling for node and jump
304 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000305 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100306}
307
308typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100309STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100310
pohmelie81ebba72016-01-27 23:23:11 +0300311STATIC 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 +0100312 if (assign_kind != ASSIGN_AUG_STORE) {
313 compile_node(comp, pns->nodes[0]);
314 }
315
Damiend99b0522013-12-21 18:17:45 +0000316 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
317 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300318 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000319 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100320 if (assign_kind != ASSIGN_AUG_STORE) {
321 for (int i = 0; i < n - 1; i++) {
322 compile_node(comp, pns1->nodes[i]);
323 }
324 }
Damiend99b0522013-12-21 18:17:45 +0000325 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
326 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100327 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000328 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100329 if (assign_kind == ASSIGN_AUG_STORE) {
330 EMIT(rot_three);
331 EMIT(store_subscr);
332 } else {
333 compile_node(comp, pns1->nodes[0]);
334 if (assign_kind == ASSIGN_AUG_LOAD) {
335 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100336 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100337 } else {
338 EMIT(store_subscr);
339 }
340 }
Damiend99b0522013-12-21 18:17:45 +0000341 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
342 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100343 if (assign_kind == ASSIGN_AUG_LOAD) {
344 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000345 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100346 } else {
347 if (assign_kind == ASSIGN_AUG_STORE) {
348 EMIT(rot_two);
349 }
Damien Georgeb9791222014-01-23 00:34:21 +0000350 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100351 }
352 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100353 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100354 }
355 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100356 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100357 }
358
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100359 return;
360
361cannot_assign:
362 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100363}
364
Damien George0288cf02014-04-11 11:53:00 +0000365// 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 +0100366STATIC 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 +0000367 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
368
369 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000370 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000371 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
372 EMIT_ARG(unpack_ex, 0, num_tail);
373 have_star_index = 0;
374 }
Damien George963a5a32015-01-16 17:47:07 +0000375 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000376 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000377 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000378 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
379 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100380 } else {
Damien George9d181f62014-04-27 16:55:27 +0100381 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100382 return;
383 }
384 }
385 }
Damien George963a5a32015-01-16 17:47:07 +0000386 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000387 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100388 }
Damien George0288cf02014-04-11 11:53:00 +0000389 if (num_head != 0) {
390 if (0 == have_star_index) {
391 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100392 } else {
Damien George0288cf02014-04-11 11:53:00 +0000393 c_assign(comp, node_head, ASSIGN_STORE);
394 }
395 }
Damien George963a5a32015-01-16 17:47:07 +0000396 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000397 if (num_head + i == have_star_index) {
398 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
399 } else {
400 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100401 }
402 }
403}
404
405// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100406STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000407 assert(!MP_PARSE_NODE_IS_NULL(pn));
408 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000409 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000410 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100411 switch (assign_kind) {
412 case ASSIGN_STORE:
413 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000414 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100415 break;
416 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000417 default:
Damien George542bd6b2015-03-26 14:42:40 +0000418 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100419 break;
420 }
421 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100422 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100423 return;
424 }
425 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000426 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000427 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
428 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300429 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100430 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300431 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100432 break;
433
434 case PN_testlist_star_expr:
435 case PN_exprlist:
436 // lhs is a tuple
437 if (assign_kind != ASSIGN_STORE) {
438 goto bad_aug;
439 }
Damien George0288cf02014-04-11 11:53:00 +0000440 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100441 break;
442
443 case PN_atom_paren:
444 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000445 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100446 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100447 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000448 } else {
449 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000450 if (assign_kind != ASSIGN_STORE) {
451 goto bad_aug;
452 }
Damiend99b0522013-12-21 18:17:45 +0000453 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100454 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100455 }
456 break;
457
458 case PN_atom_bracket:
459 // lhs is something in brackets
460 if (assign_kind != ASSIGN_STORE) {
461 goto bad_aug;
462 }
Damiend99b0522013-12-21 18:17:45 +0000463 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100464 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000465 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000466 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
467 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100468 goto testlist_comp;
469 } else {
470 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000471 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100472 }
473 break;
474
475 default:
Damien George9d181f62014-04-27 16:55:27 +0100476 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100477 }
478 return;
479
480 testlist_comp:
481 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000482 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
483 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
484 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100485 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000486 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000487 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000488 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100489 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000490 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
491 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000492 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien George9d181f62014-04-27 16:55:27 +0100493 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100494 } else {
495 // sequence with 2 items
496 goto sequence_with_2_items;
497 }
498 } else {
499 // sequence with 2 items
500 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000501 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100502 }
503 return;
504 }
505 return;
506
Damien George9d181f62014-04-27 16:55:27 +0100507 cannot_assign:
508 compile_syntax_error(comp, pn, "can't assign to expression");
509 return;
510
Damien429d7192013-10-04 19:53:11 +0100511 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100512 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100513}
514
Damien George65dc9602015-08-14 12:24:11 +0100515// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100516// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
517// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
518// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100519STATIC 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 +0100520 assert(n_pos_defaults >= 0);
521 assert(n_kw_defaults >= 0);
522
Damien George3a3db4d2015-10-22 23:45:37 +0100523 // set flags
524 if (n_kw_defaults > 0) {
525 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
526 }
527 this_scope->num_def_pos_args = n_pos_defaults;
528
Damien429d7192013-10-04 19:53:11 +0100529 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000530 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100531 int nfree = 0;
532 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000533 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
534 id_info_t *id = &comp->scope_cur->id_info[i];
535 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
536 for (int j = 0; j < this_scope->id_info_len; j++) {
537 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100538 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000539 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000540 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000541 nfree += 1;
542 }
543 }
Damien429d7192013-10-04 19:53:11 +0100544 }
545 }
546 }
Damien429d7192013-10-04 19:53:11 +0100547
548 // make the function/closure
549 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100550 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100551 } else {
Damien George3558f622014-04-20 17:50:40 +0100552 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100553 }
554}
555
Damien George2c838942015-11-17 14:00:14 +0000556STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
557 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
558 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100559 comp->have_star = true;
560 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000561 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
562 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100563 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100564 } else {
565 // named star
Damien429d7192013-10-04 19:53:11 +0100566 }
Damien George8b19db02014-04-11 23:25:34 +0100567 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000568
Damien George2c838942015-11-17 14:00:14 +0000569 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
570 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100571 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000572 // TODO do we need to do anything with this?
573
574 } else {
575 mp_parse_node_t pn_id;
576 mp_parse_node_t pn_colon;
577 mp_parse_node_t pn_equal;
578 if (MP_PARSE_NODE_IS_ID(pn)) {
579 // this parameter is just an id
580
581 pn_id = pn;
582 pn_colon = MP_PARSE_NODE_NULL;
583 pn_equal = MP_PARSE_NODE_NULL;
584
Damien George2c838942015-11-17 14:00:14 +0000585 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000586 // this parameter has a colon and/or equal specifier
587
588 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
589 pn_id = pns->nodes[0];
590 pn_colon = pns->nodes[1];
591 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000592
593 } else {
594 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
595 // this parameter has an equal specifier
596
597 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
598 pn_id = pns->nodes[0];
599 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000600 }
601
602 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
603 // this parameter does not have a default value
604
605 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100606 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100607 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000608 return;
609 }
610
611 } else {
612 // this parameter has a default value
613 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
614
Damien George8b19db02014-04-11 23:25:34 +0100615 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000616 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000617 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
618 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100619 // in Micro Python we put the default positional parameters into a tuple using the bytecode
620 // we need to do this here before we start building the map for the default keywords
621 if (comp->num_default_params > 0) {
622 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100623 } else {
624 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100625 }
Damien George69b89d22014-04-11 13:38:30 +0000626 // first default dict param, so make the map
627 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000628 }
Damien Georgef0778a72014-06-07 22:01:00 +0100629
630 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000631 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000632 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000633 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000634 } else {
Damien George69b89d22014-04-11 13:38:30 +0000635 comp->num_default_params += 1;
636 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000637 }
638 }
639
640 // TODO pn_colon not implemented
641 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100642 }
643}
644
Damien George2c838942015-11-17 14:00:14 +0000645STATIC 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 +0000646 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
647 // expression for default arguments, which may contain a lambda. The lambda will
648 // call here in a nested way, so we must save and restore the relevant state.
649 bool orig_have_star = comp->have_star;
650 uint16_t orig_num_dict_params = comp->num_dict_params;
651 uint16_t orig_num_default_params = comp->num_default_params;
652
Damien George2c838942015-11-17 14:00:14 +0000653 // compile default parameters
654 comp->have_star = false;
655 comp->num_dict_params = 0;
656 comp->num_default_params = 0;
657 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
658
659 if (comp->compile_error != MP_OBJ_NULL) {
660 return;
661 }
662
663 // in Micro Python we put the default positional parameters into a tuple using the bytecode
664 // the default keywords args may have already made the tuple; if not, do it now
665 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
666 EMIT_ARG(build_tuple, comp->num_default_params);
667 EMIT(load_null); // sentinel indicating empty default keyword args
668 }
669
670 // make the function
671 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000672
673 // restore state
674 comp->have_star = orig_have_star;
675 comp->num_dict_params = orig_num_dict_params;
676 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000677}
678
Damien429d7192013-10-04 19:53:11 +0100679// leaves function object on stack
680// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000681STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100682 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100683 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000684 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100685 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000686 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100687 }
688
Damien429d7192013-10-04 19:53:11 +0100689 // get the scope for this function
690 scope_t *fscope = (scope_t*)pns->nodes[4];
691
Damien George2c838942015-11-17 14:00:14 +0000692 // compile the function definition
693 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100694
Damien429d7192013-10-04 19:53:11 +0100695 // return its name (the 'f' in "def f(...):")
696 return fscope->simple_name;
697}
698
699// leaves class object on stack
700// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000701STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100702 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100703 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000704 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100705 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000706 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100707 }
708
709 EMIT(load_build_class);
710
711 // scope for this class
712 scope_t *cscope = (scope_t*)pns->nodes[3];
713
714 // compile the class
715 close_over_variables_etc(comp, cscope, 0, 0);
716
717 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000718 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100719
720 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100721 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
722 mp_parse_node_t parents = pns->nodes[1];
723 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
724 parents = MP_PARSE_NODE_NULL;
725 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000726 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100727 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100728
729 // return its name (the 'C' in class C(...):")
730 return cscope->simple_name;
731}
732
Damien6cdd3af2013-10-05 18:08:26 +0100733// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200734STATIC 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 +0000735 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100736 return false;
737 }
738
739 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100740 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100741 return true;
742 }
743
Damiend99b0522013-12-21 18:17:45 +0000744 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100745 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100746 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100747#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000748 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100749 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000750 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100751 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100752#endif
Damien3ef4abb2013-10-12 16:53:13 +0100753#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000754 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100755 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100756#endif
Damien6cdd3af2013-10-05 18:08:26 +0100757 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100758 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100759 }
760
761 return true;
762}
763
Damien George969a6b32014-12-10 22:07:04 +0000764STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100765 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000766 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000767 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100768
Damien6cdd3af2013-10-05 18:08:26 +0100769 // inherit emit options for this function/class definition
770 uint emit_options = comp->scope_cur->emit_options;
771
772 // compile each decorator
773 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100774 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000775 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
776 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100777
778 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000779 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000780 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100781
782 // check for built-in decorators
783 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
784 // this was a built-in
785 num_built_in_decorators += 1;
786
787 } else {
788 // not a built-in, compile normally
789
790 // compile the decorator function
791 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000792 for (int j = 1; j < name_len; j++) {
793 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
794 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100795 }
796
797 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000798 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100799 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000800 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100801 compile_node(comp, pns_decorator->nodes[1]);
802 }
Damien429d7192013-10-04 19:53:11 +0100803 }
804 }
805
pohmelie81ebba72016-01-27 23:23:11 +0300806 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000807 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100808 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000809 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100810 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300811 #if MICROPY_PY_ASYNC_AWAIT
812 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
813 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
814 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
815 body_name = compile_funcdef_helper(comp, pns0, emit_options);
816 scope_t *fscope = (scope_t*)pns0->nodes[4];
817 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
818 #endif
Damien429d7192013-10-04 19:53:11 +0100819 } else {
Damien George0bb97132015-02-27 14:25:47 +0000820 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
821 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100822 }
823
824 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100825 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100826 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100827 }
828
829 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000830 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100831}
832
Damien George969a6b32014-12-10 22:07:04 +0000833STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100834 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100835 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000836 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100837}
838
Damien George6be0b0a2014-08-15 14:30:52 +0100839STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000840 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000841 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300842 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000843 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100844
pohmelie81ebba72016-01-27 23:23:11 +0300845 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100846
Damiend99b0522013-12-21 18:17:45 +0000847 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
848 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300849 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000850 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100851 for (int i = 0; i < n - 1; i++) {
852 compile_node(comp, pns1->nodes[i]);
853 }
Damiend99b0522013-12-21 18:17:45 +0000854 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
855 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100856 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000857 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100858 compile_node(comp, pns1->nodes[0]);
859 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000860 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
861 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000862 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100863 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100864 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100865 }
866 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100867 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100868 }
869
Damiend99b0522013-12-21 18:17:45 +0000870 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
871 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000872 if (MP_PARSE_NODE_IS_NULL(pn)) {
873 goto cannot_delete;
874 } else {
875 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000876 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100877 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
878
Damiend99b0522013-12-21 18:17:45 +0000879 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
880 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
881 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100882 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000883 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100884 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000885 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100886 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000887 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100888 c_del_stmt(comp, pns->nodes[0]);
889 for (int i = 0; i < n; i++) {
890 c_del_stmt(comp, pns1->nodes[i]);
891 }
Damien George216a7112016-09-30 14:48:06 +1000892 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100893 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100894 } else {
895 // sequence with 2 items
896 goto sequence_with_2_items;
897 }
898 } else {
899 // sequence with 2 items
900 sequence_with_2_items:
901 c_del_stmt(comp, pns->nodes[0]);
902 c_del_stmt(comp, pns->nodes[1]);
903 }
Damien429d7192013-10-04 19:53:11 +0100904 }
905 } else {
Damien George93b37262016-01-07 13:07:52 +0000906 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100907 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100908 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100909
910 return;
911
912cannot_delete:
913 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100914}
915
Damien George969a6b32014-12-10 22:07:04 +0000916STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100917 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
918}
919
Damien George969a6b32014-12-10 22:07:04 +0000920STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100921 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100922 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100923 }
Damien George090c9232014-10-17 14:08:49 +0000924 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000925 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100926}
927
Damien George969a6b32014-12-10 22:07:04 +0000928STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100929 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100930 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100931 }
Damien George090c9232014-10-17 14:08:49 +0000932 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000933 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100934}
935
Damien George969a6b32014-12-10 22:07:04 +0000936STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100937 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100938 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100939 return;
940 }
Damiend99b0522013-12-21 18:17:45 +0000941 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100942 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000943 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000944 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100945 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000946 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
947 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 +0100948
Damien George6f355fd2014-04-10 14:11:31 +0100949 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100950 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
951 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
952 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000953 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100954 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
955 } else {
956 compile_node(comp, pns->nodes[0]);
957 }
958 EMIT(return_value);
959}
960
Damien George969a6b32014-12-10 22:07:04 +0000961STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100962 compile_node(comp, pns->nodes[0]);
963 EMIT(pop_top);
964}
965
Damien George969a6b32014-12-10 22:07:04 +0000966STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000967 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100968 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000969 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +0000970 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +0100971 // raise x from y
Damiend99b0522013-12-21 18:17:45 +0000972 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100973 compile_node(comp, pns->nodes[0]);
974 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000975 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +0100976 } else {
977 // raise x
978 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +0000979 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +0100980 }
981}
982
Damien George635543c2014-04-10 12:56:52 +0100983// q_base holds the base of the name
984// eg a -> q_base=a
985// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +0100986STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +0100987 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +0000988 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
989 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100990 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +0100991 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +0100992 pn = pns->nodes[0];
993 is_as = true;
994 }
Damien George635543c2014-04-10 12:56:52 +0100995 if (MP_PARSE_NODE_IS_NULL(pn)) {
996 // empty name (eg, from . import x)
997 *q_base = MP_QSTR_;
998 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
999 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001000 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001001 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001002 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001003 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001004 }
Damien George635543c2014-04-10 12:56:52 +01001005 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001006 } else {
1007 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001008 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001009 {
Damien429d7192013-10-04 19:53:11 +01001010 // a name of the form a.b.c
1011 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001012 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001013 }
Damiend99b0522013-12-21 18:17:45 +00001014 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001015 int len = n - 1;
1016 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001017 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001018 }
Damien George55baff42014-01-21 21:40:13 +00001019 byte *q_ptr;
1020 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001021 for (int i = 0; i < n; i++) {
1022 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001023 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001024 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001025 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001026 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001027 memcpy(str_dest, str_src, str_src_len);
1028 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001029 }
Damien George635543c2014-04-10 12:56:52 +01001030 qstr q_full = qstr_build_end(q_ptr);
1031 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001032 if (is_as) {
1033 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001034 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001035 }
1036 }
Damien429d7192013-10-04 19:53:11 +01001037 }
Damien429d7192013-10-04 19:53:11 +01001038 }
1039}
1040
Damien George6be0b0a2014-08-15 14:30:52 +01001041STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001042 EMIT_ARG(load_const_small_int, 0); // level 0 import
1043 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1044 qstr q_base;
1045 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001046 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001047}
1048
Damien George969a6b32014-12-10 22:07:04 +00001049STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001050 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1051}
1052
Damien George969a6b32014-12-10 22:07:04 +00001053STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001054 mp_parse_node_t pn_import_source = pns->nodes[0];
1055
1056 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1057 uint import_level = 0;
1058 do {
1059 mp_parse_node_t pn_rel;
1060 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)) {
1061 // This covers relative imports with dots only like "from .. import"
1062 pn_rel = pn_import_source;
1063 pn_import_source = MP_PARSE_NODE_NULL;
1064 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1065 // This covers relative imports starting with dot(s) like "from .foo import"
1066 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1067 pn_rel = pns_2b->nodes[0];
1068 pn_import_source = pns_2b->nodes[1];
1069 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1070 } else {
1071 // Not a relative import
1072 break;
1073 }
1074
1075 // get the list of . and/or ...'s
1076 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001077 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001078
1079 // count the total number of .'s
1080 for (int i = 0; i < n; i++) {
1081 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1082 import_level++;
1083 } else {
1084 // should be an MP_TOKEN_ELLIPSIS
1085 import_level += 3;
1086 }
1087 }
1088 } while (0);
1089
Damiend99b0522013-12-21 18:17:45 +00001090 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001091 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001092
1093 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001094 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001095 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001096
1097 // do the import
Damien George635543c2014-04-10 12:56:52 +01001098 qstr dummy_q;
1099 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001100 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001101
Damien429d7192013-10-04 19:53:11 +01001102 } else {
Damien George635543c2014-04-10 12:56:52 +01001103 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001104
1105 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001106 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001107 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001108 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001109 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1110 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1111 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001112 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001113 }
Damien Georgeb9791222014-01-23 00:34:21 +00001114 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001115
1116 // do the import
Damien George635543c2014-04-10 12:56:52 +01001117 qstr dummy_q;
1118 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001119 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001120 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1121 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1122 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001123 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001124 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001125 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001126 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001127 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001128 }
1129 }
1130 EMIT(pop_top);
1131 }
1132}
1133
Damien George584ba672014-12-21 17:26:45 +00001134STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1135 bool added;
1136 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001137 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1138 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001139 return;
1140 }
1141 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1142
1143 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1144 id_info = scope_find_global(comp->scope_cur, qst);
1145 if (id_info != NULL) {
1146 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1147 }
1148}
1149
Damien George969a6b32014-12-10 22:07:04 +00001150STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001151 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001152 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001153 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001154 for (int i = 0; i < n; i++) {
1155 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001156 }
1157 }
1158}
1159
Damien George584ba672014-12-21 17:26:45 +00001160STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1161 bool added;
1162 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001163 if (added) {
1164 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1165 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1166 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1167 }
1168 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001169 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001170 }
Damien George584ba672014-12-21 17:26:45 +00001171}
1172
Damien George969a6b32014-12-10 22:07:04 +00001173STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001174 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001175 if (comp->scope_cur->kind == SCOPE_MODULE) {
1176 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1177 return;
1178 }
1179 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001180 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001181 for (int i = 0; i < n; i++) {
1182 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001183 }
1184 }
1185}
1186
Damien George969a6b32014-12-10 22:07:04 +00001187STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001188 // with optimisations enabled we don't compile assertions
1189 if (MP_STATE_VM(mp_optimise_value) != 0) {
1190 return;
1191 }
1192
Damien George6f355fd2014-04-10 14:11:31 +01001193 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001194 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001195 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001196 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001197 // assertion message
1198 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001199 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001200 }
Damien Georgeb9791222014-01-23 00:34:21 +00001201 EMIT_ARG(raise_varargs, 1);
1202 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001203}
1204
Damien George969a6b32014-12-10 22:07:04 +00001205STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001206 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001207
Damien George65dc9602015-08-14 12:24:11 +01001208 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001209 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001210 uint l_fail = comp_next_label(comp);
1211 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001212
Damien George391db862014-10-17 17:57:33 +00001213 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001214
Damien George65dc9602015-08-14 12:24:11 +01001215 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001216 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001217 goto done;
1218 }
1219
1220 if (
Damien George65dc9602015-08-14 12:24:11 +01001221 // optimisation: don't jump over non-existent elif/else blocks
1222 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001223 // optimisation: don't jump if last instruction was return
1224 && !EMIT(last_emit_was_return_value)
1225 ) {
1226 // jump over elif/else blocks
1227 EMIT_ARG(jump, l_end);
1228 }
1229
1230 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001231 }
1232
Damien George235f9b32014-10-17 17:30:16 +00001233 // compile elif blocks (if any)
1234 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001235 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 +00001236 for (int i = 0; i < n_elif; i++) {
1237 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1238 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001239
Damien George65dc9602015-08-14 12:24:11 +01001240 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001241 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001242 uint l_fail = comp_next_label(comp);
1243 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001244
Damien George391db862014-10-17 17:57:33 +00001245 compile_node(comp, pns_elif->nodes[1]); // elif block
1246
Damien George65dc9602015-08-14 12:24:11 +01001247 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001248 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001249 goto done;
1250 }
1251
1252 // optimisation: don't jump if last instruction was return
1253 if (!EMIT(last_emit_was_return_value)) {
1254 EMIT_ARG(jump, l_end);
1255 }
1256 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001257 }
1258 }
1259
1260 // compile else block
1261 compile_node(comp, pns->nodes[3]); // can be null
1262
Damien George391db862014-10-17 17:57:33 +00001263done:
Damien Georgeb9791222014-01-23 00:34:21 +00001264 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001265}
1266
Damien Georgecbddb272014-02-01 20:08:18 +00001267#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001268 uint16_t old_break_label = comp->break_label; \
1269 uint16_t old_continue_label = comp->continue_label; \
1270 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001271 uint break_label = comp_next_label(comp); \
1272 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001273 comp->break_label = break_label; \
1274 comp->continue_label = continue_label; \
1275 comp->break_continue_except_level = comp->cur_except_level;
1276
1277#define END_BREAK_CONTINUE_BLOCK \
1278 comp->break_label = old_break_label; \
1279 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001280 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001281
Damien George969a6b32014-12-10 22:07:04 +00001282STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001283 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001284
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001285 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 +00001286 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001287 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 +00001288 EMIT_ARG(jump, continue_label);
1289 }
1290 EMIT_ARG(label_assign, top_label);
1291 compile_node(comp, pns->nodes[1]); // body
1292 EMIT_ARG(label_assign, continue_label);
1293 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1294 }
Damience89a212013-10-15 22:25:17 +01001295
1296 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001297 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001298
1299 compile_node(comp, pns->nodes[2]); // else
1300
Damien Georgeb9791222014-01-23 00:34:21 +00001301 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001302}
1303
Damien Georgee181c0d2014-12-12 17:19:56 +00001304// This function compiles an optimised for-loop of the form:
1305// for <var> in range(<start>, <end>, <step>):
1306// <body>
1307// else:
1308// <else>
1309// <var> must be an identifier and <step> must be a small-int.
1310//
1311// Semantics of for-loop require:
1312// - final failing value should not be stored in the loop variable
1313// - if the loop never runs, the loop variable should never be assigned
1314// - assignments to <var>, <end> or <step> in the body do not alter the loop
1315// (<step> is a constant for us, so no need to worry about it changing)
1316//
1317// If <end> is a small-int, then the stack during the for-loop contains just
1318// the current value of <var>. Otherwise, the stack contains <end> then the
1319// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001320STATIC 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 +00001321 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001322
Damien George6f355fd2014-04-10 14:11:31 +01001323 uint top_label = comp_next_label(comp);
1324 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001325
Damien Georgee181c0d2014-12-12 17:19:56 +00001326 // put the end value on the stack if it's not a small-int constant
1327 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1328 if (end_on_stack) {
1329 compile_node(comp, pn_end);
1330 }
1331
1332 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001333 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001334
Damien Georgeb9791222014-01-23 00:34:21 +00001335 EMIT_ARG(jump, entry_label);
1336 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001337
Damien Georgec33ce602014-12-11 17:35:23 +00001338 // duplicate next value and store it to var
1339 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001340 c_assign(comp, pn_var, ASSIGN_STORE);
1341
Damienf3822fc2013-11-09 20:12:03 +00001342 // compile body
1343 compile_node(comp, pn_body);
1344
Damien Georgeb9791222014-01-23 00:34:21 +00001345 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001346
Damien Georgee181c0d2014-12-12 17:19:56 +00001347 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001348 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001349 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001350
Damien Georgeb9791222014-01-23 00:34:21 +00001351 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001352
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001353 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001354 if (end_on_stack) {
1355 EMIT(dup_top_two);
1356 EMIT(rot_two);
1357 } else {
1358 EMIT(dup_top);
1359 compile_node(comp, pn_end);
1360 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001361 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1362 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001363 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001364 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001365 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001366 }
Damien George63f38322015-02-28 15:04:06 +00001367 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001368
1369 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001370 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001371
1372 compile_node(comp, pn_else);
1373
Damien Georgeb9791222014-01-23 00:34:21 +00001374 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001375
1376 // discard final value of var that failed the loop condition
1377 EMIT(pop_top);
1378
1379 // discard <end> value if it's on the stack
1380 if (end_on_stack) {
1381 EMIT(pop_top);
1382 }
Damienf72fd0e2013-11-06 20:20:49 +00001383}
1384
Damien George969a6b32014-12-10 22:07:04 +00001385STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001386 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1387 // this is actually slower, but uses no heap memory
1388 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001389 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 +00001390 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001391 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001392 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001393 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001394 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1395 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001396 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001397 mp_parse_node_t pn_range_start;
1398 mp_parse_node_t pn_range_end;
1399 mp_parse_node_t pn_range_step;
1400 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001401 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001402 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001403 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001404 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001405 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001406 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001407 } else if (n_args == 2) {
1408 pn_range_start = args[0];
1409 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001410 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001411 } else {
1412 pn_range_start = args[0];
1413 pn_range_end = args[1];
1414 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001415 // We need to know sign of step. This is possible only if it's constant
1416 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1417 optimize = false;
1418 }
Damienf72fd0e2013-11-06 20:20:49 +00001419 }
Damien George33ac0fd2015-12-08 21:05:14 +00001420 // arguments must be able to be compiled as standard expressions
1421 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1422 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1423 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1424 optimize = false;
1425 }
1426 }
1427 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1428 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1429 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1430 optimize = false;
1431 }
1432 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001433 }
1434 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001435 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1436 return;
1437 }
1438 }
1439 }
Damienf72fd0e2013-11-06 20:20:49 +00001440
Damien Georgecbddb272014-02-01 20:08:18 +00001441 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001442 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001443
Damien George6f355fd2014-04-10 14:11:31 +01001444 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001445
Damien429d7192013-10-04 19:53:11 +01001446 compile_node(comp, pns->nodes[1]); // iterator
1447 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001448 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001449 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001450 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1451 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001452 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001453 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001454 }
Damien Georgeb9791222014-01-23 00:34:21 +00001455 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001456 EMIT(for_iter_end);
1457
1458 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001459 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001460
Damien429d7192013-10-04 19:53:11 +01001461 compile_node(comp, pns->nodes[3]); // else (not tested)
1462
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001464}
1465
Damien George6be0b0a2014-08-15 14:30:52 +01001466STATIC 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 +01001467 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001468 uint l1 = comp_next_label(comp);
1469 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001470
Damien Georgeb9791222014-01-23 00:34:21 +00001471 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001472 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001473
Damien429d7192013-10-04 19:53:11 +01001474 compile_node(comp, pn_body); // body
1475 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001476 EMIT_ARG(jump, success_label); // jump over exception handler
1477
1478 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001479 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001480
Damien Georgef0406852016-09-27 12:37:21 +10001481 // at this point the top of the stack contains the exception instance that was raised
1482
Damien George6f355fd2014-04-10 14:11:31 +01001483 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001484
1485 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001486 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1487 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001488
1489 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001490 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001491
Damiend99b0522013-12-21 18:17:45 +00001492 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001493 // this is a catch all exception handler
1494 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001495 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001496 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001497 return;
1498 }
1499 } else {
1500 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001501 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1502 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1503 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1504 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001505 // handler binds the exception to a local
1506 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001507 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001508 }
1509 }
1510 EMIT(dup_top);
1511 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001512 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001513 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001514 }
1515
Damien Georgef0406852016-09-27 12:37:21 +10001516 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001517 if (qstr_exception_local == 0) {
1518 EMIT(pop_top);
1519 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001520 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001521 }
1522
Damien George6f355fd2014-04-10 14:11:31 +01001523 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001524 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001525 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001526 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001527 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001528 }
1529 compile_node(comp, pns_except->nodes[1]);
1530 if (qstr_exception_local != 0) {
1531 EMIT(pop_block);
1532 }
1533 EMIT(pop_except);
1534 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001535 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1536 EMIT_ARG(label_assign, l3);
1537 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001538 compile_store_id(comp, qstr_exception_local);
1539 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001540
Damien George8dcc0c72014-03-27 10:55:21 +00001541 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001542 EMIT(end_finally);
1543 }
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(jump, l2);
1545 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001546 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001547 }
1548
Damien George8dcc0c72014-03-27 10:55:21 +00001549 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001550 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001551 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001552
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001554 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001555 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001556}
1557
Damien George6be0b0a2014-08-15 14:30:52 +01001558STATIC 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 +01001559 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001560
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001562 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001563
Damien429d7192013-10-04 19:53:11 +01001564 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001565 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001566 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001567 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001568 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001569 } else {
1570 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1571 }
1572 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1574 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001575 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001576
Damien George8dcc0c72014-03-27 10:55:21 +00001577 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001578 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001579}
1580
Damien George969a6b32014-12-10 22:07:04 +00001581STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001582 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1583 {
Damiend99b0522013-12-21 18:17:45 +00001584 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1585 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001586 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001587 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1588 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001589 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001590 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001591 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001592 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001593 // no finally
1594 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1595 } else {
1596 // have finally
Damiend99b0522013-12-21 18:17:45 +00001597 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 +01001598 }
1599 } else {
1600 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001601 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001602 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001603 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001604 }
Damien429d7192013-10-04 19:53:11 +01001605 }
1606}
1607
Damien George6be0b0a2014-08-15 14:30:52 +01001608STATIC 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 +01001609 if (n == 0) {
1610 // no more pre-bits, compile the body of the with
1611 compile_node(comp, body);
1612 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001613 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001614 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1615 // we need to allocate an extra label for the native emitter
1616 // it will use l_end+1 as an auxiliary label
1617 comp_next_label(comp);
1618 }
Damiend99b0522013-12-21 18:17:45 +00001619 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001620 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001621 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001622 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001623 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001624 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1625 } else {
1626 // this pre-bit is just an expression
1627 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001628 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001629 EMIT(pop_top);
1630 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001631 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001632 // compile additional pre-bits and the body
1633 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1634 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001635 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001636 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001637 EMIT(end_finally);
1638 }
1639}
1640
Damien George969a6b32014-12-10 22:07:04 +00001641STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001642 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001643 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001644 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001645 assert(n > 0);
1646
1647 // compile in a nested fashion
1648 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1649}
1650
pohmelie81ebba72016-01-27 23:23:11 +03001651STATIC void compile_yield_from(compiler_t *comp) {
1652 EMIT(get_iter);
1653 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1654 EMIT(yield_from);
1655}
1656
1657#if MICROPY_PY_ASYNC_AWAIT
1658STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1659 EMIT_ARG(load_method, method);
1660 EMIT_ARG(call_method, 0, 0, 0);
1661 compile_yield_from(comp);
1662}
1663
1664STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1665 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1666
1667 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1668 uint while_else_label = comp_next_label(comp);
1669 uint try_exception_label = comp_next_label(comp);
1670 uint try_else_label = comp_next_label(comp);
1671 uint try_finally_label = comp_next_label(comp);
1672
1673 compile_node(comp, pns->nodes[1]); // iterator
1674 compile_await_object_method(comp, MP_QSTR___aiter__);
1675 compile_store_id(comp, context);
1676
1677 START_BREAK_CONTINUE_BLOCK
1678
1679 EMIT_ARG(label_assign, continue_label);
1680
1681 EMIT_ARG(setup_except, try_exception_label);
1682 compile_increase_except_level(comp);
1683
1684 compile_load_id(comp, context);
1685 compile_await_object_method(comp, MP_QSTR___anext__);
1686 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1687 EMIT(pop_block);
1688 EMIT_ARG(jump, try_else_label);
1689
1690 EMIT_ARG(label_assign, try_exception_label);
1691 EMIT(start_except_handler);
1692 EMIT(dup_top);
1693 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1694 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1695 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001696 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001697 EMIT(pop_except);
1698 EMIT_ARG(jump, while_else_label);
1699
1700 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001701 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001702 compile_decrease_except_level(comp);
1703 EMIT(end_finally);
1704 EMIT(end_except_handler);
1705
1706 EMIT_ARG(label_assign, try_else_label);
1707 compile_node(comp, pns->nodes[2]); // body
1708
1709 EMIT_ARG(jump, continue_label);
1710 // break/continue apply to outer loop (if any) in the else block
1711 END_BREAK_CONTINUE_BLOCK
1712
1713 EMIT_ARG(label_assign, while_else_label);
1714 compile_node(comp, pns->nodes[3]); // else
1715
1716 EMIT_ARG(label_assign, break_label);
1717}
1718
1719STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1720 if (n == 0) {
1721 // no more pre-bits, compile the body of the with
1722 compile_node(comp, body);
1723 } else {
1724 uint try_exception_label = comp_next_label(comp);
1725 uint no_reraise_label = comp_next_label(comp);
1726 uint try_else_label = comp_next_label(comp);
1727 uint end_label = comp_next_label(comp);
1728 qstr context;
1729
1730 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1731 // this pre-bit is of the form "a as b"
1732 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1733 compile_node(comp, pns->nodes[0]);
1734 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1735 compile_store_id(comp, context);
1736 compile_load_id(comp, context);
1737 compile_await_object_method(comp, MP_QSTR___aenter__);
1738 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1739 } else {
1740 // this pre-bit is just an expression
1741 compile_node(comp, nodes[0]);
1742 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1743 compile_store_id(comp, context);
1744 compile_load_id(comp, context);
1745 compile_await_object_method(comp, MP_QSTR___aenter__);
1746 EMIT(pop_top);
1747 }
1748
1749 compile_load_id(comp, context);
1750 EMIT_ARG(load_method, MP_QSTR___aexit__);
1751
1752 EMIT_ARG(setup_except, try_exception_label);
1753 compile_increase_except_level(comp);
1754 // compile additional pre-bits and the body
1755 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1756 // finish this with block
1757 EMIT(pop_block);
1758 EMIT_ARG(jump, try_else_label); // jump over exception handler
1759
1760 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1761 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001762
1763 // at this point the stack contains: ..., __aexit__, self, exc
1764 EMIT(dup_top);
1765 #if MICROPY_CPYTHON_COMPAT
1766 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1767 #else
1768 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001769 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001770 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1771 #endif
1772 EMIT(rot_two);
1773 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1774 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001775 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001776
pohmelie81ebba72016-01-27 23:23:11 +03001777 compile_yield_from(comp);
1778 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1779 EMIT_ARG(raise_varargs, 0);
1780
1781 EMIT_ARG(label_assign, no_reraise_label);
1782 EMIT(pop_except);
1783 EMIT_ARG(jump, end_label);
1784
Damien Georgeb32c01b2016-09-28 11:52:13 +10001785 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001786 compile_decrease_except_level(comp);
1787 EMIT(end_finally);
1788 EMIT(end_except_handler);
1789
1790 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1791 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1792 EMIT(dup_top);
1793 EMIT(dup_top);
1794 EMIT_ARG(call_method, 3, 0, 0);
1795 compile_yield_from(comp);
1796 EMIT(pop_top);
1797
1798 EMIT_ARG(label_assign, end_label);
1799
1800 }
1801}
1802
1803STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1804 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1805 mp_parse_node_t *nodes;
1806 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1807 assert(n > 0);
1808
1809 // compile in a nested fashion
1810 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1811}
1812
1813STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1814 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1815 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1816 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1817 // async def
1818 compile_funcdef(comp, pns0);
1819 scope_t *fscope = (scope_t*)pns0->nodes[4];
1820 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1821 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1822 // async for
1823 compile_async_for_stmt(comp, pns0);
1824 } else {
1825 // async with
1826 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1827 compile_async_with_stmt(comp, pns0);
1828 }
1829}
1830#endif
1831
Damien George969a6b32014-12-10 22:07:04 +00001832STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001833 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001834 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1835 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001836 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001837 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001838 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001839 EMIT(pop_top);
1840
Damien429d7192013-10-04 19:53:11 +01001841 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001842 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001843 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001844 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001845 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1846 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001847 // do nothing with a lonely constant
1848 } else {
1849 compile_node(comp, pns->nodes[0]); // just an expression
1850 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1851 }
Damien429d7192013-10-04 19:53:11 +01001852 }
Damien Georgeb948de32015-10-08 14:26:01 +01001853 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001854 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1855 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001856 if (kind == PN_expr_stmt_augassign) {
1857 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1858 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001859 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001860 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001861 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001862 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1863 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1864 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1865 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1866 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1867 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1868 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1869 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1870 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1871 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1872 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001873 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001874 }
Damien George7e5fb242014-02-01 22:18:47 +00001875 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001876 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1877 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001878 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001879 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001880 // following CPython, we store left-most first
1881 if (rhs > 0) {
1882 EMIT(dup_top);
1883 }
1884 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1885 for (int i = 0; i < rhs; i++) {
1886 if (i + 1 < rhs) {
1887 EMIT(dup_top);
1888 }
Damien Georgeb948de32015-10-08 14:26:01 +01001889 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001890 }
Damien George0bb97132015-02-27 14:25:47 +00001891 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001892 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001893 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001894 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001895 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001896 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001897 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001898 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001899 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001900 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001901 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1902 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1903 // can't optimise when it's a star expression on the lhs
1904 goto no_optimisation;
1905 }
Damien429d7192013-10-04 19:53:11 +01001906 compile_node(comp, pns10->nodes[0]); // rhs
1907 compile_node(comp, pns10->nodes[1]); // rhs
1908 EMIT(rot_two);
1909 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1910 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001911 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001912 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001913 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001914 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001915 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001916 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001917 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001918 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001919 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1920 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1921 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1922 // can't optimise when it's a star expression on the lhs
1923 goto no_optimisation;
1924 }
Damien429d7192013-10-04 19:53:11 +01001925 compile_node(comp, pns10->nodes[0]); // rhs
1926 compile_node(comp, pns10->nodes[1]); // rhs
1927 compile_node(comp, pns10->nodes[2]); // rhs
1928 EMIT(rot_three);
1929 EMIT(rot_two);
1930 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1931 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1932 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1933 } else {
Damien George495d7812014-04-08 17:51:47 +01001934 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001935 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001936 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1937 }
Damien429d7192013-10-04 19:53:11 +01001938 }
Damien Georgeb948de32015-10-08 14:26:01 +01001939 } else {
1940 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001941 }
1942}
1943
Damien George6be0b0a2014-08-15 14:30:52 +01001944STATIC 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 +00001945 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001946 compile_node(comp, pns->nodes[0]);
1947 for (int i = 1; i < num_nodes; i += 1) {
1948 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001950 }
1951}
1952
Damien George969a6b32014-12-10 22:07:04 +00001953STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001954 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1955 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001956
Damien George6f355fd2014-04-10 14:11:31 +01001957 uint l_fail = comp_next_label(comp);
1958 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001959 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1960 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001961 EMIT_ARG(jump, l_end);
1962 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001963 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001964 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001966}
1967
Damien George969a6b32014-12-10 22:07:04 +00001968STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001969 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001970 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001971 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 +01001972 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001973 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001974 }
1975
1976 // get the scope for this lambda
1977 scope_t *this_scope = (scope_t*)pns->nodes[2];
1978
Damien George2c838942015-11-17 14:00:14 +00001979 // compile the lambda definition
1980 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01001981}
1982
Damien George7711afb2015-02-28 15:10:18 +00001983STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01001984 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001985 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001986 for (int i = 0; i < n; i += 1) {
1987 compile_node(comp, pns->nodes[i]);
1988 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00001989 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01001990 }
1991 }
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001993}
1994
Damien George7711afb2015-02-28 15:10:18 +00001995STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
1996 compile_or_and_test(comp, pns, true);
1997}
1998
Damien George969a6b32014-12-10 22:07:04 +00001999STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002000 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002001}
2002
Damien George969a6b32014-12-10 22:07:04 +00002003STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002004 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002005 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002006}
2007
Damien George969a6b32014-12-10 22:07:04 +00002008STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002009 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002010 compile_node(comp, pns->nodes[0]);
2011 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002012 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002013 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002014 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002015 }
2016 for (int i = 1; i + 1 < num_nodes; i += 2) {
2017 compile_node(comp, pns->nodes[i + 1]);
2018 if (i + 2 < num_nodes) {
2019 EMIT(dup_top);
2020 EMIT(rot_three);
2021 }
Damien George7e5fb242014-02-01 22:18:47 +00002022 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002023 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002024 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002025 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2026 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2027 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2028 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2029 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2030 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002031 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002032 }
2033 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002034 } else {
2035 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002036 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2037 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002038 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002039 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002040 } else {
2041 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002042 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002043 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002044 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002045 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002046 }
Damien429d7192013-10-04 19:53:11 +01002047 }
Damien429d7192013-10-04 19:53:11 +01002048 }
2049 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002050 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002051 }
2052 }
2053 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002054 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002055 EMIT_ARG(jump, l_end);
2056 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002057 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002058 EMIT(rot_two);
2059 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002060 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002061 }
2062}
2063
Damien George969a6b32014-12-10 22:07:04 +00002064STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002065 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002066}
2067
Damien George969a6b32014-12-10 22:07:04 +00002068STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002069 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002070}
2071
Damien George969a6b32014-12-10 22:07:04 +00002072STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002073 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002074}
2075
Damien George969a6b32014-12-10 22:07:04 +00002076STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002077 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002078}
2079
Damien George969a6b32014-12-10 22:07:04 +00002080STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002081 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002082 compile_node(comp, pns->nodes[0]);
2083 for (int i = 1; i + 1 < num_nodes; i += 2) {
2084 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002085 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002086 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002087 } else {
Damien George0bb97132015-02-27 14:25:47 +00002088 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2089 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 }
2092}
2093
Damien George969a6b32014-12-10 22:07:04 +00002094STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002095 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002096 compile_node(comp, pns->nodes[0]);
2097 for (int i = 1; i + 1 < num_nodes; i += 2) {
2098 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002101 } else {
Damien George0bb97132015-02-27 14:25:47 +00002102 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2103 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002104 }
2105 }
2106}
2107
Damien George969a6b32014-12-10 22:07:04 +00002108STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002109 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002110 compile_node(comp, pns->nodes[0]);
2111 for (int i = 1; i + 1 < num_nodes; i += 2) {
2112 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002113 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002114 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002115 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002116 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002117 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002118 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002119 } else {
Damien George0bb97132015-02-27 14:25:47 +00002120 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2121 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002122 }
2123 }
2124}
2125
Damien George969a6b32014-12-10 22:07:04 +00002126STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002127 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002128 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002129 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
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[0], MP_TOKEN_OP_TILDE)); // should be
2134 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002135 }
2136}
2137
pohmelie81ebba72016-01-27 23:23:11 +03002138STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002139 // this is to handle special super() call
2140 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2141
2142 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002143}
Damien George3acaa282016-03-16 13:04:51 +00002144
pohmelie81ebba72016-01-27 23:23:11 +03002145STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2146 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2147 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002148}
2149
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002150STATIC 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 +01002151 // function to call is on top of stack
2152
Damien George35e2a4e2014-02-05 00:51:47 +00002153 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002154 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 +00002155 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002156 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002157 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002158 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002159 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002160 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002161 EMIT_ARG(call_function, 2, 0, 0);
2162 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002163 }
2164 }
Damien George01039b52014-12-21 17:44:27 +00002165 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002166 return;
2167 }
Damien George35e2a4e2014-02-05 00:51:47 +00002168
Damien George2c0842b2014-04-27 16:46:51 +01002169 // get the list of arguments
2170 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002171 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002172
Damien George2c0842b2014-04-27 16:46:51 +01002173 // compile the arguments
2174 // Rather than calling compile_node on the list, we go through the list of args
2175 // explicitly here so that we can count the number of arguments and give sensible
2176 // error messages.
2177 int n_positional = n_positional_extra;
2178 uint n_keyword = 0;
2179 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002180 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002181 for (int i = 0; i < n_args; i++) {
2182 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2183 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2184 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2185 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2186 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2187 return;
2188 }
2189 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002190 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002191 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2192 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2193 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2194 return;
2195 }
2196 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002197 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002198 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002199 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002200 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2201 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2202 return;
2203 }
Damien George59fba2d2015-06-25 14:42:13 +00002204 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002205 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002206 n_keyword += 1;
2207 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002208 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2209 n_positional++;
2210 }
2211 } else {
2212 goto normal_argument;
2213 }
2214 } else {
2215 normal_argument:
2216 if (n_keyword > 0) {
2217 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2218 return;
2219 }
2220 compile_node(comp, args[i]);
2221 n_positional++;
2222 }
Damien429d7192013-10-04 19:53:11 +01002223 }
2224
Delio Brignolie6978a42015-09-17 12:07:06 +02002225 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002226 // if we had one but not the other then we load "null" as a place holder
2227 if (star_flags != 0) {
2228 if (star_args_node == NULL) {
2229 EMIT(load_null);
2230 } else {
2231 compile_node(comp, star_args_node->nodes[0]);
2232 }
2233 if (dblstar_args_node == NULL) {
2234 EMIT(load_null);
2235 } else {
2236 compile_node(comp, dblstar_args_node->nodes[0]);
2237 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002238 }
2239
Damien George2c0842b2014-04-27 16:46:51 +01002240 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002241 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002242 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002243 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002244 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002245 }
Damien429d7192013-10-04 19:53:11 +01002246}
2247
pohmelie81ebba72016-01-27 23:23:11 +03002248STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002249 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002250 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002251 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 +01002252 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002253 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2254 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002255 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002256 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002257 i += 1;
2258 } else {
2259 compile_node(comp, pns->nodes[i]);
2260 }
Damien George35e2a4e2014-02-05 00:51:47 +00002261 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002262 }
2263}
2264
Damien George969a6b32014-12-10 22:07:04 +00002265STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002266 // a list of strings
Damien63321742013-12-10 17:41:49 +00002267
2268 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002269 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002270 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002271 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002272 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002273 int pn_kind;
2274 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2275 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2276 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2277 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2278 } else {
2279 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2280 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002281 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2282 pn_kind = MP_PARSE_NODE_STRING;
2283 } else {
2284 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2285 pn_kind = MP_PARSE_NODE_BYTES;
2286 }
Damien George831137b2015-12-17 13:13:18 +00002287 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002288 }
Damien63321742013-12-10 17:41:49 +00002289 if (i == 0) {
2290 string_kind = pn_kind;
2291 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002292 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002293 return;
2294 }
Damien429d7192013-10-04 19:53:11 +01002295 }
Damien63321742013-12-10 17:41:49 +00002296
Damien George65ef6b72015-01-14 21:17:27 +00002297 // if we are not in the last pass, just load a dummy object
2298 if (comp->pass != MP_PASS_EMIT) {
2299 EMIT_ARG(load_const_obj, mp_const_none);
2300 return;
2301 }
2302
Damien63321742013-12-10 17:41:49 +00002303 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002304 vstr_t vstr;
2305 vstr_init_len(&vstr, n_bytes);
2306 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002307 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002308 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002309 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002310 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2311 memcpy(s_dest, s, s_len);
2312 s_dest += s_len;
2313 } else {
2314 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002315 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2316 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002317 }
Damien63321742013-12-10 17:41:49 +00002318 }
Damien George65ef6b72015-01-14 21:17:27 +00002319
2320 // load the object
Damien George05005f62015-01-21 22:48:37 +00002321 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 +01002322}
2323
2324// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002325STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002326 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2327 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2328 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002329
Damien George36db6bc2014-05-07 17:24:22 +01002330 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002331 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002332 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 +01002333 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002334 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002335 }
2336
2337 // get the scope for this comprehension
2338 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2339
2340 // compile the comprehension
2341 close_over_variables_etc(comp, this_scope, 0, 0);
2342
2343 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2344 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002345 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damien George969a6b32014-12-10 22:07:04 +00002348STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002349 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002350 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002351 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002352 } else {
2353 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002354 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2355 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2356 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2357 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2358 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002359 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002360 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002361 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002362 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002363 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002364 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002365 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002366 // generator expression
2367 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2368 } else {
2369 // tuple with 2 items
2370 goto tuple_with_2_items;
2371 }
2372 } else {
2373 // tuple with 2 items
2374 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002375 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002376 }
Damien429d7192013-10-04 19:53:11 +01002377 }
2378}
2379
Damien George969a6b32014-12-10 22:07:04 +00002380STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002381 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002382 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002383 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002384 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2385 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2386 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2387 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2388 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002389 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002390 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002391 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002392 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002393 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002394 // list of many items
2395 compile_node(comp, pns2->nodes[0]);
2396 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002397 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002398 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002399 // list comprehension
2400 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2401 } else {
2402 // list with 2 items
2403 goto list_with_2_items;
2404 }
2405 } else {
2406 // list with 2 items
2407 list_with_2_items:
2408 compile_node(comp, pns2->nodes[0]);
2409 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002410 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002411 }
2412 } else {
2413 // list with 1 item
2414 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002415 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002416 }
2417}
2418
Damien George969a6b32014-12-10 22:07:04 +00002419STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002420 mp_parse_node_t pn = pns->nodes[0];
2421 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002422 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002423 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002424 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2425 pns = (mp_parse_node_struct_t*)pn;
2426 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002427 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002428 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pn);
2430 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002431 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2432 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2433 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2434 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002435 // dict/set with multiple elements
2436
2437 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002438 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002439 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002440
2441 // first element sets whether it's a dict or set
2442 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002443 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002444 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002445 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002446 compile_node(comp, pns->nodes[0]);
2447 EMIT(store_map);
2448 is_dict = true;
2449 } else {
2450 // a set
2451 compile_node(comp, pns->nodes[0]); // 1st value of set
2452 is_dict = false;
2453 }
2454
2455 // process rest of elements
2456 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002457 mp_parse_node_t pn_i = nodes[i];
2458 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2459 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002460 if (is_dict) {
2461 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002462 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002463 return;
2464 }
2465 EMIT(store_map);
2466 } else {
2467 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002468 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002469 return;
2470 }
2471 }
2472 }
2473
Damien Georgee37dcaa2014-12-27 17:07:16 +00002474 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002475 // if it's a set, build it
2476 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002478 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002479 #endif
Damien George0bb97132015-02-27 14:25:47 +00002480 } else {
2481 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002482 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002483 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002484 // a dictionary comprehension
2485 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2486 } else {
2487 // a set comprehension
2488 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2489 }
Damien429d7192013-10-04 19:53:11 +01002490 }
2491 } else {
2492 // set with one element
2493 goto set_with_one_element;
2494 }
2495 } else {
2496 // set with one element
2497 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002498 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002499 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002500 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002501 #else
2502 assert(0);
2503 #endif
Damien429d7192013-10-04 19:53:11 +01002504 }
2505}
2506
Damien George969a6b32014-12-10 22:07:04 +00002507STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002508 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002509}
2510
Damien George969a6b32014-12-10 22:07:04 +00002511STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002512 // object who's index we want is on top of stack
2513 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002514 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002515}
2516
Damien George969a6b32014-12-10 22:07:04 +00002517STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002518 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002519 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002520}
2521
Damien George83204f32014-12-27 17:20:41 +00002522#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002523STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002524 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2525 mp_parse_node_t pn = pns->nodes[0];
2526 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002527 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2529 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002530 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2531 pns = (mp_parse_node_struct_t*)pn;
2532 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002534 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002535 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002536 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002538 } else {
2539 // [?::x]
2540 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002542 }
Damiend99b0522013-12-21 18:17:45 +00002543 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002544 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002545 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2546 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2547 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2548 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002549 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002551 } else {
2552 // [?:x:x]
2553 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002555 }
2556 } else {
2557 // [?:x]
2558 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002559 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002560 }
2561 } else {
2562 // [?:x]
2563 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002565 }
2566}
2567
Damien George969a6b32014-12-10 22:07:04 +00002568STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002569 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002570 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2571 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002572}
2573
Damien George969a6b32014-12-10 22:07:04 +00002574STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002576 compile_subscript_3_helper(comp, pns);
2577}
Damien George83204f32014-12-27 17:20:41 +00002578#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002579
Damien George969a6b32014-12-10 22:07:04 +00002580STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002581 // if this is called then we are compiling a dict key:value pair
2582 compile_node(comp, pns->nodes[1]); // value
2583 compile_node(comp, pns->nodes[0]); // key
2584}
2585
Damien George969a6b32014-12-10 22:07:04 +00002586STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002587 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002588 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002589 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002590}
2591
Damien George969a6b32014-12-10 22:07:04 +00002592STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002593 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002594 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002595 return;
2596 }
Damiend99b0522013-12-21 18:17:45 +00002597 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002598 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002599 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002600 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2601 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002602 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002603 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002604 } else {
2605 compile_node(comp, pns->nodes[0]);
2606 EMIT(yield_value);
2607 }
2608}
2609
pohmelie81ebba72016-01-27 23:23:11 +03002610#if MICROPY_PY_ASYNC_AWAIT
2611STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2612 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2613 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2614 return;
2615 }
2616 compile_atom_expr_normal(comp, pns);
2617 compile_yield_from(comp);
2618}
2619#endif
2620
Damien George65ef6b72015-01-14 21:17:27 +00002621STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2622 // only create and load the actual str object on the last pass
2623 if (comp->pass != MP_PASS_EMIT) {
2624 EMIT_ARG(load_const_obj, mp_const_none);
2625 } else {
Damien George831137b2015-12-17 13:13:18 +00002626 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 +00002627 }
2628}
2629
2630STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2631 // only create and load the actual bytes object on the last pass
2632 if (comp->pass != MP_PASS_EMIT) {
2633 EMIT_ARG(load_const_obj, mp_const_none);
2634 } else {
Damien George831137b2015-12-17 13:13:18 +00002635 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002636 }
2637}
2638
Damien George7d414a12015-02-08 01:57:40 +00002639STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002640 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2641 // nodes are 32-bit pointers, but need to extract 64-bit object
2642 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2643 #else
Damien George7d414a12015-02-08 01:57:40 +00002644 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002645 #endif
Damien George7d414a12015-02-08 01:57:40 +00002646}
2647
Damiend99b0522013-12-21 18:17:45 +00002648typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002649STATIC const compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002650#define nc NULL
2651#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002652#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002653#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002654#undef nc
2655#undef c
2656#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002657 NULL,
2658 compile_string,
2659 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002660 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002661};
2662
Damien George6be0b0a2014-08-15 14:30:52 +01002663STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002664 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002665 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002666 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002667 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002668 #if MICROPY_DYNAMIC_COMPILER
2669 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2670 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2671 // integer fits in target runtime's small-int
2672 EMIT_ARG(load_const_small_int, arg);
2673 } else {
2674 // integer doesn't fit, so create a multi-precision int object
2675 // (but only create the actual object on the last pass)
2676 if (comp->pass != MP_PASS_EMIT) {
2677 EMIT_ARG(load_const_obj, mp_const_none);
2678 } else {
2679 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2680 }
2681 }
2682 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002683 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002684 #endif
Damiend99b0522013-12-21 18:17:45 +00002685 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002686 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002687 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002688 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002689 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2690 case MP_PARSE_NODE_BYTES:
2691 // only create and load the actual bytes object on the last pass
2692 if (comp->pass != MP_PASS_EMIT) {
2693 EMIT_ARG(load_const_obj, mp_const_none);
2694 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002695 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002696 const byte *data = qstr_data(arg, &len);
2697 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2698 }
2699 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002700 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002701 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002702 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002703 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002704 // do nothing
2705 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002706 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002707 }
2708 break;
Damien429d7192013-10-04 19:53:11 +01002709 }
2710 } else {
Damiend99b0522013-12-21 18:17:45 +00002711 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002712 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002713 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002714 assert(f != NULL);
2715 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002716 }
2717}
2718
Damien George2ac4af62014-08-15 16:45:41 +01002719STATIC 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 +00002720 // check that **kw is last
2721 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2722 compile_syntax_error(comp, pn, "invalid syntax");
2723 return;
2724 }
2725
Damien George2827d622014-04-27 15:50:52 +01002726 qstr param_name = MP_QSTR_NULL;
2727 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002728 if (MP_PARSE_NODE_IS_ID(pn)) {
2729 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002730 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002731 // comes after a star, so counts as a keyword-only parameter
2732 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002733 } else {
Damien George2827d622014-04-27 15:50:52 +01002734 // comes before a star, so counts as a positional parameter
2735 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002736 }
Damienb14de212013-10-06 00:28:28 +01002737 } else {
Damiend99b0522013-12-21 18:17:45 +00002738 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2739 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2740 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2741 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002742 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002743 // comes after a star, so counts as a keyword-only parameter
2744 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002745 } else {
Damien George2827d622014-04-27 15:50:52 +01002746 // comes before a star, so counts as a positional parameter
2747 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002748 }
Damiend99b0522013-12-21 18:17:45 +00002749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002750 if (comp->have_star) {
2751 // more than one star
2752 compile_syntax_error(comp, pn, "invalid syntax");
2753 return;
2754 }
Damien George8b19db02014-04-11 23:25:34 +01002755 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002756 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002757 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002758 // bare star
2759 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002760 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002761 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002762 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002763 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002764 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002765 } else {
2766 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002767 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002768 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002769 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2770 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002771 }
Damien George0bb97132015-02-27 14:25:47 +00002772 } else {
2773 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002774 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002775 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002776 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002777 }
Damien429d7192013-10-04 19:53:11 +01002778 }
2779
Damien George2827d622014-04-27 15:50:52 +01002780 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002781 bool added;
2782 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2783 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002784 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002785 return;
2786 }
Damien429d7192013-10-04 19:53:11 +01002787 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002788 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002789 }
2790}
2791
Damien George2827d622014-04-27 15:50:52 +01002792STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002793 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002794}
2795
Damien George2827d622014-04-27 15:50:52 +01002796STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002797 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2798}
2799
Damien Georgeea5b59b2015-09-04 16:44:14 +01002800#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002801STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2802 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2803 // no annotation
2804 return;
2805 }
2806
2807 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2808 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2809 // named parameter with possible annotation
2810 // fallthrough
2811 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2812 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2813 // named star with possible annotation
2814 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2815 // fallthrough
2816 } else {
2817 // no annotation
2818 return;
2819 }
Damien Georgee49153f2016-10-11 12:29:54 +11002820 } else {
2821 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002822 // double star with possible annotation
2823 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002824 }
2825
2826 mp_parse_node_t pn_annotation = pns->nodes[1];
2827
2828 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002829 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2830 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2831 assert(id_info != NULL);
2832
Damien Georgeea5b59b2015-09-04 16:44:14 +01002833 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2834 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2835 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2836 } else {
2837 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002838 }
Damien George2ac4af62014-08-15 16:45:41 +01002839 }
Damien429d7192013-10-04 19:53:11 +01002840}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002841#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002842
Damien Georgea8312432015-12-18 01:37:55 +00002843STATIC 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) {
2844 uint l_top = comp_next_label(comp);
2845 uint l_end = comp_next_label(comp);
2846 EMIT_ARG(label_assign, l_top);
2847 EMIT_ARG(for_iter, l_end);
2848 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2849 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2850
Damien429d7192013-10-04 19:53:11 +01002851 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002852 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002853 // no more nested if/for; compile inner expression
2854 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002855 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002856 EMIT(yield_value);
2857 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002858 } else {
2859 EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002860 }
Damiend99b0522013-12-21 18:17:45 +00002861 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002862 // if condition
Damiend99b0522013-12-21 18:17:45 +00002863 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002864 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2865 pn_iter = pns_comp_if->nodes[1];
2866 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002867 } else {
2868 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002869 // for loop
Damiend99b0522013-12-21 18:17:45 +00002870 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002871 compile_node(comp, pns_comp_for2->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002872 EMIT(get_iter);
Damien Georgea8312432015-12-18 01:37:55 +00002873 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002874 }
Damien Georgea8312432015-12-18 01:37:55 +00002875
2876 EMIT_ARG(jump, l_top);
2877 EMIT_ARG(label_assign, l_end);
2878 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002879}
2880
Damien George1463c1f2014-04-25 23:52:57 +01002881STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002882#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002883 // see http://www.python.org/dev/peps/pep-0257/
2884
2885 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002886 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002887 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002888 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002889 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002890 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2891 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002892 for (int i = 0; i < num_nodes; i++) {
2893 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002894 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 +00002895 // not a newline, so this is the first statement; finish search
2896 break;
2897 }
2898 }
2899 // 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 +00002900 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002901 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002902 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002903 } else {
2904 return;
2905 }
2906
2907 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002908 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002909 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002910 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2911 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2912 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2913 // compile the doc string
2914 compile_node(comp, pns->nodes[0]);
2915 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002916 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002917 }
2918 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002919#else
2920 (void)comp;
2921 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002922#endif
Damien429d7192013-10-04 19:53:11 +01002923}
2924
Damien George1463c1f2014-04-25 23:52:57 +01002925STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002926 comp->pass = pass;
2927 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002928 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002929 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002930
Damien George36db6bc2014-05-07 17:24:22 +01002931 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002932 // reset maximum stack sizes in scope
2933 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002934 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002935 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002936 }
2937
Damien429d7192013-10-04 19:53:11 +01002938 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002939 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2940 assert(scope->kind == SCOPE_MODULE);
2941 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2942 compile_node(comp, pns->nodes[0]); // compile the expression
2943 EMIT(return_value);
2944 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002945 if (!comp->is_repl) {
2946 check_for_doc_string(comp, scope->pn);
2947 }
Damien429d7192013-10-04 19:53:11 +01002948 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002950 EMIT(return_value);
2951 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002952 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2953 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2954 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002955
2956 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002957 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002958 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002959 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002960 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002961 }
2962 #if MICROPY_EMIT_NATIVE
2963 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002964 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002965 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002966
Damien George2ac4af62014-08-15 16:45:41 +01002967 // argument annotations
2968 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2969
2970 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002971 mp_parse_node_t pn_annotation = pns->nodes[2];
2972 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002973 // nodes[2] can be null or a test-expr
2974 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2975 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2976 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2977 } else {
2978 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002979 }
2980 }
Damien George2ac4af62014-08-15 16:45:41 +01002981 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002982 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002983
2984 compile_node(comp, pns->nodes[3]); // 3 is function body
2985 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002986 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002987 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002988 EMIT(return_value);
2989 }
2990 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002991 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2992 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2993 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002994
2995 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002996 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002997 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002998 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002999 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3000 }
3001
3002 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003003
3004 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3005 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3006 EMIT(pop_top);
3007 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3008 }
Damien429d7192013-10-04 19:53:11 +01003009 EMIT(return_value);
3010 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3011 // a bit of a hack at the moment
3012
Damiend99b0522013-12-21 18:17:45 +00003013 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3014 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3015 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3016 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3017 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003018
Damien George708c0732014-04-27 19:23:46 +01003019 // We need a unique name for the comprehension argument (the iterator).
3020 // CPython uses .0, but we should be able to use anything that won't
3021 // clash with a user defined variable. Best to use an existing qstr,
3022 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003023 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003024 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003025 bool added;
3026 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3027 assert(added);
3028 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003029 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003030 }
3031
3032 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003033 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003034 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003035 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003036 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003037 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003038 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003039 #endif
Damien429d7192013-10-04 19:53:11 +01003040 }
3041
Damien George542bd6b2015-03-26 14:42:40 +00003042 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00003043 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003044
3045 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003046 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003047 }
3048 EMIT(return_value);
3049 } else {
3050 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003051 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3052 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3053 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003054
Damien George36db6bc2014-05-07 17:24:22 +01003055 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003056 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003057 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003058 assert(added);
3059 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003060 }
3061
Damien George542bd6b2015-03-26 14:42:40 +00003062 compile_load_id(comp, MP_QSTR___name__);
3063 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003064 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003065 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003066
3067 check_for_doc_string(comp, pns->nodes[2]);
3068 compile_node(comp, pns->nodes[2]); // 2 is class body
3069
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003070 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003071 assert(id != NULL);
3072 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003073 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003074 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003075 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003076 }
3077 EMIT(return_value);
3078 }
3079
Damien415eb6f2013-10-05 12:19:06 +01003080 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003081
3082 // make sure we match all the exception levels
3083 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003084}
3085
Damien George094d4502014-04-02 17:31:27 +01003086#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003087// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003088STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003089 comp->pass = pass;
3090 comp->scope_cur = scope;
3091 comp->next_label = 1;
3092
3093 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003094 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003095 return;
3096 }
3097
Damien George36db6bc2014-05-07 17:24:22 +01003098 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003099 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003100 }
3101
Damien826005c2013-10-05 23:17:28 +01003102 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003103 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3104 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3105 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003106
Damiend99b0522013-12-21 18:17:45 +00003107 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003108
Damiena2f2f7d2013-10-06 00:14:13 +01003109 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003110 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003111 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003112 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003113 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003114 if (comp->compile_error != MP_OBJ_NULL) {
3115 goto inline_asm_error;
3116 }
Damiena2f2f7d2013-10-06 00:14:13 +01003117 }
3118
Damien George8f54c082016-01-15 15:20:43 +00003119 // pns->nodes[2] is function return annotation
3120 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3121 mp_parse_node_t pn_annotation = pns->nodes[2];
3122 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3123 // nodes[2] can be null or a test-expr
3124 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3125 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3126 switch (ret_type) {
3127 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3128 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3129 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3130 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3131 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3132 }
3133 } else {
3134 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3135 }
3136 }
Damien826005c2013-10-05 23:17:28 +01003137
Damiend99b0522013-12-21 18:17:45 +00003138 mp_parse_node_t pn_body = pns->nodes[3]; // body
3139 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003140 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003141
Damien826005c2013-10-05 23:17:28 +01003142 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003143 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3144 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003145 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3146 // no instructions
3147 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003148 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003149 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003150 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003151 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003152 return;
3153 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003154
3155 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003156 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003157 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3158 goto not_an_instruction;
3159 }
Damiend99b0522013-12-21 18:17:45 +00003160 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003161 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003162 goto not_an_instruction;
3163 }
3164 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3165 goto not_an_instruction;
3166 }
3167 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3168 goto not_an_instruction;
3169 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003170
3171 // parse node looks like an instruction
3172 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003173 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3174 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3175 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003176 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003177
3178 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003179 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003180 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003181 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003182 return;
3183 }
Damien George6f355fd2014-04-10 14:11:31 +01003184 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003185 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003186 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3187 compile_syntax_error(comp, nodes[i], "label redefined");
3188 return;
3189 }
Damien826005c2013-10-05 23:17:28 +01003190 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003191 } else if (op == MP_QSTR_align) {
3192 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003193 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003194 return;
3195 }
Damien George36db6bc2014-05-07 17:24:22 +01003196 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003197 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3198 }
3199 } else if (op == MP_QSTR_data) {
3200 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003201 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003202 return;
3203 }
Damien George36db6bc2014-05-07 17:24:22 +01003204 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003205 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003206 for (uint j = 1; j < n_args; j++) {
3207 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003208 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003209 return;
3210 }
Damien George50912e72015-01-20 11:55:10 +00003211 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003212 }
3213 }
Damien826005c2013-10-05 23:17:28 +01003214 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003215 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003216 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003217 }
3218 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003219
3220 if (comp->compile_error != MP_OBJ_NULL) {
3221 pns = pns2; // this is the parse node that had the error
3222 goto inline_asm_error;
3223 }
Damien826005c2013-10-05 23:17:28 +01003224 }
3225
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003227 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien George8dfbd2d2015-02-13 01:00:51 +00003228 }
3229
3230 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003231 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003232 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003233 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003234 }
Damien429d7192013-10-04 19:53:11 +01003235}
Damien George094d4502014-04-02 17:31:27 +01003236#endif
Damien429d7192013-10-04 19:53:11 +01003237
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003238STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003239 // in Micro Python we put the *x parameter after all other parameters (except **y)
3240 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3241 id_info_t *id_param = NULL;
3242 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3243 id_info_t *id = &scope->id_info[i];
3244 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3245 if (id_param != NULL) {
3246 // swap star param with last param
3247 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3248 }
3249 break;
3250 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3251 id_param = id;
3252 }
3253 }
3254 }
Damien George2827d622014-04-27 15:50:52 +01003255
Damien429d7192013-10-04 19:53:11 +01003256 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003257 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003258 scope->num_locals = 0;
3259 for (int i = 0; i < scope->id_info_len; i++) {
3260 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003261 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003262 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3263 continue;
3264 }
Damien George3dea8c92016-09-30 12:34:05 +10003265 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003266 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3267 }
Damien George2827d622014-04-27 15:50:52 +01003268 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003269 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003270 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003271 }
3272 }
3273
Damien George65dc9602015-08-14 12:24:11 +01003274 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003275 for (int i = 0; i < scope->id_info_len; i++) {
3276 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003277 // in Micro Python the cells come right after the fast locals
3278 // parameters are not counted here, since they remain at the start
3279 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003280 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003281 id->local_num = scope->num_locals;
3282 scope->num_locals += 1;
3283 }
Damien9ecbcff2013-12-11 00:41:43 +00003284 }
Damien9ecbcff2013-12-11 00:41:43 +00003285
Damien George65dc9602015-08-14 12:24:11 +01003286 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003287 // make sure they are in the order of the parent scope
3288 if (scope->parent != NULL) {
3289 int num_free = 0;
3290 for (int i = 0; i < scope->parent->id_info_len; i++) {
3291 id_info_t *id = &scope->parent->id_info[i];
3292 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3293 for (int j = 0; j < scope->id_info_len; j++) {
3294 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003295 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003296 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003297 // in Micro Python the frees come first, before the params
3298 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003299 num_free += 1;
3300 }
3301 }
3302 }
Damien429d7192013-10-04 19:53:11 +01003303 }
Damien George6baf76e2013-12-30 22:32:17 +00003304 // in Micro Python shift all other locals after the free locals
3305 if (num_free > 0) {
3306 for (int i = 0; i < scope->id_info_len; i++) {
3307 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003308 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003309 id->local_num += num_free;
3310 }
3311 }
Damien George2827d622014-04-27 15:50:52 +01003312 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 +00003313 scope->num_locals += num_free;
3314 }
Damien429d7192013-10-04 19:53:11 +01003315 }
Damien429d7192013-10-04 19:53:11 +01003316}
3317
Damien Georged4dba882015-11-13 13:38:28 +00003318#if !MICROPY_PERSISTENT_CODE_SAVE
3319STATIC
3320#endif
3321mp_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 +01003322 // put compiler state on the stack, it's relatively small
3323 compiler_t comp_state = {0};
3324 compiler_t *comp = &comp_state;
3325
Damien Georgecbd2f742014-01-19 11:48:48 +00003326 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003327 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003328
Damien George62a3a282015-03-01 12:04:05 +00003329 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003330 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003331
Damien Georgea210c772015-03-26 15:49:53 +00003332 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003333 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003334
Damien826005c2013-10-05 23:17:28 +01003335 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003336 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003337 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003338 comp->emit_method_table = &emit_bc_method_table;
3339 #endif
Damien826005c2013-10-05 23:17:28 +01003340 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003341 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003342 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003343#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003344 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003345 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003346#endif
Damien826005c2013-10-05 23:17:28 +01003347 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003348 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003349 }
3350
3351 // update maximim number of labels needed
3352 if (comp->next_label > max_num_labels) {
3353 max_num_labels = comp->next_label;
3354 }
Damien429d7192013-10-04 19:53:11 +01003355 }
3356
Damien826005c2013-10-05 23:17:28 +01003357 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003358 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003359 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003360 }
3361
Damien Georgea210c772015-03-26 15:49:53 +00003362 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003363 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003364
Damien826005c2013-10-05 23:17:28 +01003365 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003366#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003367 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003368#endif
Damien3ef4abb2013-10-12 16:53:13 +01003369#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003370 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003371#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003372 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003373 if (false) {
3374 // dummy
3375
Damien3ef4abb2013-10-12 16:53:13 +01003376#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003377 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003378 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003379 if (emit_inline_thumb == NULL) {
3380 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3381 }
3382 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003383 comp->emit_inline_asm = emit_inline_thumb;
3384 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003385 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003386 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003387 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003388 }
Damienc025ebb2013-10-12 14:30:21 +01003389#endif
3390
Damien826005c2013-10-05 23:17:28 +01003391 } else {
Damienc025ebb2013-10-12 14:30:21 +01003392
3393 // choose the emit type
3394
Damien826005c2013-10-05 23:17:28 +01003395 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003396
3397#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003398 case MP_EMIT_OPT_NATIVE_PYTHON:
3399 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003400#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003401 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003402 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003403 }
Damien13ed3a62013-10-08 09:05:10 +01003404 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003405#elif MICROPY_EMIT_X86
3406 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003407 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003408 }
3409 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003410#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003411 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003412 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003413 }
3414 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003415#elif MICROPY_EMIT_ARM
3416 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003417 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003418 }
3419 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003420#endif
3421 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003422 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003423 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003424#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003425
Damien826005c2013-10-05 23:17:28 +01003426 default:
Damien826005c2013-10-05 23:17:28 +01003427 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003428 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003429 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003430 #endif
Damien826005c2013-10-05 23:17:28 +01003431 break;
3432 }
Damienc025ebb2013-10-12 14:30:21 +01003433
Damien George1e1779e2015-01-14 00:20:28 +00003434 // need a pass to compute stack size
3435 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3436
Damien George36db6bc2014-05-07 17:24:22 +01003437 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003438 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003439 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3440 }
3441
3442 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003443 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003444 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003445 }
Damien6cdd3af2013-10-05 18:08:26 +01003446 }
Damien429d7192013-10-04 19:53:11 +01003447 }
3448
Damien Georgecfc4c332015-07-29 22:16:01 +00003449 if (comp->compile_error != MP_OBJ_NULL) {
3450 // if there is no line number for the error then use the line
3451 // number for the start of this scope
3452 compile_error_set_line(comp, comp->scope_cur->pn);
3453 // add a traceback to the exception using relevant source info
3454 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3455 comp->compile_error_line, comp->scope_cur->simple_name);
3456 }
3457
Damien George41d02b62014-01-24 22:42:28 +00003458 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003459
Damien Georgea210c772015-03-26 15:49:53 +00003460 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003461#if MICROPY_EMIT_NATIVE
3462 if (emit_native != NULL) {
3463#if MICROPY_EMIT_X64
3464 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003465#elif MICROPY_EMIT_X86
3466 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003467#elif MICROPY_EMIT_THUMB
3468 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003469#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003470 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003471#endif
3472 }
3473#endif
3474#if MICROPY_EMIT_INLINE_THUMB
3475 if (emit_inline_thumb != NULL) {
3476 emit_inline_thumb_free(emit_inline_thumb);
3477 }
3478#endif
Damien George41d02b62014-01-24 22:42:28 +00003479
Damien George52b5d762014-09-23 15:31:56 +00003480 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003481 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003482
Damien George41d02b62014-01-24 22:42:28 +00003483 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003484 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003485 for (scope_t *s = module_scope; s;) {
3486 scope_t *next = s->next;
3487 scope_free(s);
3488 s = next;
3489 }
Damien5ac1b2e2013-10-18 19:58:12 +01003490
Damien George9d5e5c02015-09-24 13:15:57 +01003491 if (comp->compile_error != MP_OBJ_NULL) {
3492 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003493 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003494 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003495 }
Damien429d7192013-10-04 19:53:11 +01003496}
Damien Georged4dba882015-11-13 13:38:28 +00003497
3498mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3499 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3500 // return function that executes the outer module
3501 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3502}
Damien Georgedd5353a2015-12-18 12:35:44 +00003503
3504#endif // MICROPY_ENABLE_COMPILER