blob: 10c3e2f4835319e246a380a6a77a57047079e01a [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
38// TODO need to mangle __attr names
39
40typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000041#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000042#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010043#undef DEF_RULE
44 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010045 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000046 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000047 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010048} pn_kind_t;
49
Damien George65dc9602015-08-14 12:24:11 +010050#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000051
52#if NEED_METHOD_TABLE
53
54// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000055#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
56#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000057#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
58#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010059
Damien George41125902015-03-26 16:44:14 +000060#else
61
62// if we only have the bytecode emitter enabled then we can do a direct call to the functions
63#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
64#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
65#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
66#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
67
68#endif
69
Damien George0496de22015-10-03 17:07:54 +010070#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
71#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
72
Damien Georgea91ac202014-10-05 19:01:34 +010073// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010074typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000075 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010076
Damien George78035b92014-04-09 12:27:39 +010077 uint8_t is_repl;
78 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010079 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010080 uint8_t have_star;
81
82 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +000083 mp_obj_t compile_error; // set to an exception object if there's an error
84 mp_uint_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +010085
Damien George6f355fd2014-04-10 14:11:31 +010086 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010087
Damien George69b89d22014-04-11 13:38:30 +000088 uint16_t num_dict_params;
89 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010090
Damien Georgea91ac202014-10-05 19:01:34 +010091 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
92 uint16_t continue_label;
93 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000094 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010095
Damien429d7192013-10-04 19:53:11 +010096 scope_t *scope_head;
97 scope_t *scope_cur;
98
Damien6cdd3af2013-10-05 18:08:26 +010099 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000100 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100101 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000102 #endif
Damien826005c2013-10-05 23:17:28 +0100103
Damien Georgea77ffe62015-03-14 12:59:31 +0000104 #if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +0100105 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
106 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 +0000107 #endif
Damien429d7192013-10-04 19:53:11 +0100108} compiler_t;
109
Damien Georgecfc4c332015-07-29 22:16:01 +0000110STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
111 // if the line of the error is unknown then try to update it from the pn
112 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
113 comp->compile_error_line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100114 }
Damien George84d59c22015-07-27 22:20:00 +0100115}
116
117STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000118 // only register the error if there has been no other error
119 if (comp->compile_error == MP_OBJ_NULL) {
120 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
121 compile_error_set_line(comp, pn);
122 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000123}
124
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200125STATIC 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 +0100126STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000127STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100128
Damien George6f355fd2014-04-10 14:11:31 +0100129STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100130 return comp->next_label++;
131}
132
Damien George8dcc0c72014-03-27 10:55:21 +0000133STATIC void compile_increase_except_level(compiler_t *comp) {
134 comp->cur_except_level += 1;
135 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
136 comp->scope_cur->exc_stack_size = comp->cur_except_level;
137 }
138}
139
140STATIC void compile_decrease_except_level(compiler_t *comp) {
141 assert(comp->cur_except_level > 0);
142 comp->cur_except_level -= 1;
143}
144
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200145STATIC 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 +0100146 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100147 scope->parent = comp->scope_cur;
148 scope->next = NULL;
149 if (comp->scope_head == NULL) {
150 comp->scope_head = scope;
151 } else {
152 scope_t *s = comp->scope_head;
153 while (s->next != NULL) {
154 s = s->next;
155 }
156 s->next = scope;
157 }
158 return scope;
159}
160
Damien George91bc32d2015-04-09 15:31:53 +0000161typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
162
163STATIC 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 +0000164 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000165 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
166 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100167 for (int i = 0; i < num_nodes; i++) {
168 f(comp, pns->nodes[i]);
169 }
Damiend99b0522013-12-21 18:17:45 +0000170 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100171 f(comp, pn);
172 }
173}
174
Damien George969a6b32014-12-10 22:07:04 +0000175STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000176 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100177 for (int i = 0; i < num_nodes; i++) {
178 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000179 if (comp->compile_error != MP_OBJ_NULL) {
180 // add line info for the error in case it didn't have a line number
181 compile_error_set_line(comp, pns->nodes[i]);
182 return;
183 }
Damien429d7192013-10-04 19:53:11 +0100184 }
185}
186
Damien George542bd6b2015-03-26 14:42:40 +0000187STATIC void compile_load_id(compiler_t *comp, qstr qst) {
188 if (comp->pass == MP_PASS_SCOPE) {
189 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
190 } else {
Damien George41125902015-03-26 16:44:14 +0000191 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000192 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000193 #else
194 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
195 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000196 }
197}
198
199STATIC void compile_store_id(compiler_t *comp, qstr qst) {
200 if (comp->pass == MP_PASS_SCOPE) {
201 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
202 } else {
Damien George41125902015-03-26 16:44:14 +0000203 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000204 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000205 #else
206 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
207 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000208 }
209}
210
211STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
212 if (comp->pass == MP_PASS_SCOPE) {
213 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
214 } else {
Damien George41125902015-03-26 16:44:14 +0000215 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000216 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000217 #else
218 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
219 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000220 }
221}
222
Damien George2c0842b2014-04-27 16:46:51 +0100223STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100224 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000225 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100226 compile_node(comp, pn);
227 total += 1;
228 }
229 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000230 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100231 for (int i = 0; i < n; i++) {
232 compile_node(comp, pns_list->nodes[i]);
233 }
234 total += n;
235 }
Damien Georgeb9791222014-01-23 00:34:21 +0000236 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100237}
Damien429d7192013-10-04 19:53:11 +0100238
Damien George969a6b32014-12-10 22:07:04 +0000239STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100240 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000241 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100242}
243
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200244STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000245 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
246 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100247}
248
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200249STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000250 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
251 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100252}
253
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200254STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3a205172013-10-12 15:01:56 +0100255 if (node_is_const_false(pn)) {
256 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000257 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100258 }
259 return;
260 } else if (node_is_const_true(pn)) {
261 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000262 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100263 }
264 return;
Damiend99b0522013-12-21 18:17:45 +0000265 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
266 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
267 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
268 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100269 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000270 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100271 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100272 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000273 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100274 }
Damien George0b2fd912015-02-28 14:37:54 +0000275 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000276 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100277 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000278 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100279 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000280 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100281 }
282 }
283 return;
Damiend99b0522013-12-21 18:17:45 +0000284 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100285 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000286 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100287 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000288 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100289 }
Damiend99b0522013-12-21 18:17:45 +0000290 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100291 c_if_cond(comp, pns->nodes[0], !jump_if, label);
292 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100293 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
294 // cond is something in parenthesis
295 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
296 // empty tuple, acts as false for the condition
297 if (jump_if == false) {
298 EMIT_ARG(jump, label);
299 }
300 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
301 // non-empty tuple, acts as true for the condition
302 if (jump_if == true) {
303 EMIT_ARG(jump, label);
304 }
305 } else {
306 // parenthesis around 1 item, is just that item
307 c_if_cond(comp, pns->nodes[0], jump_if, label);
308 }
309 return;
Damien3a205172013-10-12 15:01:56 +0100310 }
311 }
312
313 // nothing special, fall back to default compiling for node and jump
314 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000315 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100316}
317
318typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100319STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100320
Damien George6be0b0a2014-08-15 14:30:52 +0100321STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100322 if (assign_kind != ASSIGN_AUG_STORE) {
323 compile_node(comp, pns->nodes[0]);
324 }
325
Damiend99b0522013-12-21 18:17:45 +0000326 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
327 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
328 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
329 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100330 if (assign_kind != ASSIGN_AUG_STORE) {
331 for (int i = 0; i < n - 1; i++) {
332 compile_node(comp, pns1->nodes[i]);
333 }
334 }
Damiend99b0522013-12-21 18:17:45 +0000335 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
336 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100337 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000338 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100339 if (assign_kind == ASSIGN_AUG_STORE) {
340 EMIT(rot_three);
341 EMIT(store_subscr);
342 } else {
343 compile_node(comp, pns1->nodes[0]);
344 if (assign_kind == ASSIGN_AUG_LOAD) {
345 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100346 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100347 } else {
348 EMIT(store_subscr);
349 }
350 }
Damiend99b0522013-12-21 18:17:45 +0000351 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
352 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100353 if (assign_kind == ASSIGN_AUG_LOAD) {
354 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000355 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100356 } else {
357 if (assign_kind == ASSIGN_AUG_STORE) {
358 EMIT(rot_two);
359 }
Damien Georgeb9791222014-01-23 00:34:21 +0000360 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100361 }
362 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100363 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100364 }
365 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100366 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100367 }
368
Damiend99b0522013-12-21 18:17:45 +0000369 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100370 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100371 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100372
373 return;
374
375cannot_assign:
376 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100377}
378
Damien George0288cf02014-04-11 11:53:00 +0000379// 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 +0100380STATIC 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 +0000381 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
382
383 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000384 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000385 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
386 EMIT_ARG(unpack_ex, 0, num_tail);
387 have_star_index = 0;
388 }
Damien George963a5a32015-01-16 17:47:07 +0000389 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000390 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000391 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000392 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
393 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100394 } else {
Damien George9d181f62014-04-27 16:55:27 +0100395 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100396 return;
397 }
398 }
399 }
Damien George963a5a32015-01-16 17:47:07 +0000400 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000401 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100402 }
Damien George0288cf02014-04-11 11:53:00 +0000403 if (num_head != 0) {
404 if (0 == have_star_index) {
405 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damien George0288cf02014-04-11 11:53:00 +0000407 c_assign(comp, node_head, ASSIGN_STORE);
408 }
409 }
Damien George963a5a32015-01-16 17:47:07 +0000410 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000411 if (num_head + i == have_star_index) {
412 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
413 } else {
414 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100415 }
416 }
417}
418
419// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100420STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100421 tail_recursion:
Damien Georgeaedf5832015-03-25 22:06:47 +0000422 assert(!MP_PARSE_NODE_IS_NULL(pn));
423 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000424 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000425 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100426 switch (assign_kind) {
427 case ASSIGN_STORE:
428 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000429 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100430 break;
431 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000432 default:
Damien George542bd6b2015-03-26 14:42:40 +0000433 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100434 break;
435 }
436 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100437 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100438 return;
439 }
440 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000441 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000442 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
443 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100444 case PN_power:
445 // lhs is an index or attribute
446 c_assign_power(comp, pns, assign_kind);
447 break;
448
449 case PN_testlist_star_expr:
450 case PN_exprlist:
451 // lhs is a tuple
452 if (assign_kind != ASSIGN_STORE) {
453 goto bad_aug;
454 }
Damien George0288cf02014-04-11 11:53:00 +0000455 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100456 break;
457
458 case PN_atom_paren:
459 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000460 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100461 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100462 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000463 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
Damien George44f65c02015-03-25 23:06:48 +0000464 if (assign_kind != ASSIGN_STORE) {
465 goto bad_aug;
466 }
Damiend99b0522013-12-21 18:17:45 +0000467 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100468 goto testlist_comp;
469 } else {
470 // parenthesis around 1 item, is just that item
471 pn = pns->nodes[0];
472 goto tail_recursion;
473 }
474 break;
475
476 case PN_atom_bracket:
477 // lhs is something in brackets
478 if (assign_kind != ASSIGN_STORE) {
479 goto bad_aug;
480 }
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100482 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000483 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000484 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
485 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100486 goto testlist_comp;
487 } else {
488 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000489 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100490 }
491 break;
492
493 default:
Damien George9d181f62014-04-27 16:55:27 +0100494 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
497
498 testlist_comp:
499 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000500 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
501 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
502 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100503 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000504 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000505 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000506 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100507 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000508 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
509 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000510 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100511 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100512 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100513 } else {
514 // sequence with 2 items
515 goto sequence_with_2_items;
516 }
517 } else {
518 // sequence with 2 items
519 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000520 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100521 }
522 return;
523 }
524 return;
525
Damien George9d181f62014-04-27 16:55:27 +0100526 cannot_assign:
527 compile_syntax_error(comp, pn, "can't assign to expression");
528 return;
529
Damien429d7192013-10-04 19:53:11 +0100530 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100531 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100532}
533
Damien George65dc9602015-08-14 12:24:11 +0100534// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100535// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
536// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
537// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100538STATIC 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 +0100539 assert(n_pos_defaults >= 0);
540 assert(n_kw_defaults >= 0);
541
Damien George3a3db4d2015-10-22 23:45:37 +0100542 // set flags
543 if (n_kw_defaults > 0) {
544 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
545 }
546 this_scope->num_def_pos_args = n_pos_defaults;
547
Damien429d7192013-10-04 19:53:11 +0100548 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000549 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100550 int nfree = 0;
551 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000552 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
553 id_info_t *id = &comp->scope_cur->id_info[i];
554 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
555 for (int j = 0; j < this_scope->id_info_len; j++) {
556 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100557 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000558 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000559 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000560 nfree += 1;
561 }
562 }
Damien429d7192013-10-04 19:53:11 +0100563 }
564 }
565 }
Damien429d7192013-10-04 19:53:11 +0100566
567 // make the function/closure
568 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100569 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100570 } else {
Damien George3558f622014-04-20 17:50:40 +0100571 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100572 }
573}
574
Damien George2c838942015-11-17 14:00:14 +0000575STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
576 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
577 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100578 comp->have_star = true;
579 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000580 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
581 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100582 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100583 } else {
584 // named star
Damien429d7192013-10-04 19:53:11 +0100585 }
Damien George8b19db02014-04-11 23:25:34 +0100586 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000587
Damien George2c838942015-11-17 14:00:14 +0000588 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
589 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100590 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000591 // TODO do we need to do anything with this?
592
593 } else {
594 mp_parse_node_t pn_id;
595 mp_parse_node_t pn_colon;
596 mp_parse_node_t pn_equal;
597 if (MP_PARSE_NODE_IS_ID(pn)) {
598 // this parameter is just an id
599
600 pn_id = pn;
601 pn_colon = MP_PARSE_NODE_NULL;
602 pn_equal = MP_PARSE_NODE_NULL;
603
Damien George2c838942015-11-17 14:00:14 +0000604 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000605 // this parameter has a colon and/or equal specifier
606
607 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
608 pn_id = pns->nodes[0];
609 pn_colon = pns->nodes[1];
610 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000611
612 } else {
613 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
614 // this parameter has an equal specifier
615
616 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
617 pn_id = pns->nodes[0];
618 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000619 }
620
621 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
622 // this parameter does not have a default value
623
624 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100625 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100626 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000627 return;
628 }
629
630 } else {
631 // this parameter has a default value
632 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
633
Damien George8b19db02014-04-11 23:25:34 +0100634 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000635 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000636 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
637 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100638 // in Micro Python we put the default positional parameters into a tuple using the bytecode
639 // we need to do this here before we start building the map for the default keywords
640 if (comp->num_default_params > 0) {
641 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100642 } else {
643 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100644 }
Damien George69b89d22014-04-11 13:38:30 +0000645 // first default dict param, so make the map
646 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000647 }
Damien Georgef0778a72014-06-07 22:01:00 +0100648
649 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000650 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000651 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000652 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000653 } else {
Damien George69b89d22014-04-11 13:38:30 +0000654 comp->num_default_params += 1;
655 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000656 }
657 }
658
659 // TODO pn_colon not implemented
660 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100661 }
662}
663
Damien George2c838942015-11-17 14:00:14 +0000664STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) {
665 // compile default parameters
666 comp->have_star = false;
667 comp->num_dict_params = 0;
668 comp->num_default_params = 0;
669 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
670
671 if (comp->compile_error != MP_OBJ_NULL) {
672 return;
673 }
674
675 // in Micro Python we put the default positional parameters into a tuple using the bytecode
676 // the default keywords args may have already made the tuple; if not, do it now
677 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
678 EMIT_ARG(build_tuple, comp->num_default_params);
679 EMIT(load_null); // sentinel indicating empty default keyword args
680 }
681
682 // make the function
683 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
684}
685
Damien429d7192013-10-04 19:53:11 +0100686// leaves function object on stack
687// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000688STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100689 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100690 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000691 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100692 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000693 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100694 }
695
Damien429d7192013-10-04 19:53:11 +0100696 // get the scope for this function
697 scope_t *fscope = (scope_t*)pns->nodes[4];
698
Damien George2c838942015-11-17 14:00:14 +0000699 // compile the function definition
700 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100701
Damien429d7192013-10-04 19:53:11 +0100702 // return its name (the 'f' in "def f(...):")
703 return fscope->simple_name;
704}
705
706// leaves class object on stack
707// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000708STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100709 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100710 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000711 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100712 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000713 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100714 }
715
716 EMIT(load_build_class);
717
718 // scope for this class
719 scope_t *cscope = (scope_t*)pns->nodes[3];
720
721 // compile the class
722 close_over_variables_etc(comp, cscope, 0, 0);
723
724 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000725 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100726
727 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100728 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
729 mp_parse_node_t parents = pns->nodes[1];
730 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
731 parents = MP_PARSE_NODE_NULL;
732 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000733 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100734 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100735
736 // return its name (the 'C' in class C(...):")
737 return cscope->simple_name;
738}
739
Damien6cdd3af2013-10-05 18:08:26 +0100740// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200741STATIC 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 +0000742 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100743 return false;
744 }
745
746 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100747 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100748 return true;
749 }
750
Damiend99b0522013-12-21 18:17:45 +0000751 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100752 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100753 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100754#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000755 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100756 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000757 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100758 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100759#endif
Damien3ef4abb2013-10-12 16:53:13 +0100760#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000761 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +0100762 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100763#endif
Damien6cdd3af2013-10-05 18:08:26 +0100764 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100765 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100766 }
767
768 return true;
769}
770
Damien George969a6b32014-12-10 22:07:04 +0000771STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100772 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000773 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000774 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100775
Damien6cdd3af2013-10-05 18:08:26 +0100776 // inherit emit options for this function/class definition
777 uint emit_options = comp->scope_cur->emit_options;
778
779 // compile each decorator
780 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100781 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000782 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
783 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100784
785 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000786 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000787 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100788
789 // check for built-in decorators
790 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
791 // this was a built-in
792 num_built_in_decorators += 1;
793
794 } else {
795 // not a built-in, compile normally
796
797 // compile the decorator function
798 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000799 for (int j = 1; j < name_len; j++) {
800 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
801 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100802 }
803
804 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000805 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100806 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000807 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100808 compile_node(comp, pns_decorator->nodes[1]);
809 }
Damien429d7192013-10-04 19:53:11 +0100810 }
811 }
812
813 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000814 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100815 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000816 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100817 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100818 } else {
Damien George0bb97132015-02-27 14:25:47 +0000819 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
820 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100821 }
822
823 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100824 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100825 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100826 }
827
828 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000829 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100830}
831
Damien George969a6b32014-12-10 22:07:04 +0000832STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100833 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100834 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000835 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100836}
837
Damien George6be0b0a2014-08-15 14:30:52 +0100838STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000839 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000840 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +0000841 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100843
844 compile_node(comp, pns->nodes[0]); // base of the power node
845
Damiend99b0522013-12-21 18:17:45 +0000846 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
847 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
848 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
849 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100850 for (int i = 0; i < n - 1; i++) {
851 compile_node(comp, pns1->nodes[i]);
852 }
Damiend99b0522013-12-21 18:17:45 +0000853 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
854 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100855 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000856 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100857 compile_node(comp, pns1->nodes[0]);
858 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000859 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
860 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000861 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100862 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100863 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100864 }
865 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100866 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100867 }
868
Damiend99b0522013-12-21 18:17:45 +0000869 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100870 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100871 }
Damiend99b0522013-12-21 18:17:45 +0000872 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
873 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
874 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
875 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100876 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
877
Damiend99b0522013-12-21 18:17:45 +0000878 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
879 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
880 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100881 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000882 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100883 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000884 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100885 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000886 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100887 c_del_stmt(comp, pns->nodes[0]);
888 for (int i = 0; i < n; i++) {
889 c_del_stmt(comp, pns1->nodes[i]);
890 }
Damiend99b0522013-12-21 18:17:45 +0000891 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000892 // TODO not implemented; can't del comprehension? can we get here?
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 }
904 } else {
905 // tuple with 1 element
906 c_del_stmt(comp, pn);
907 }
908 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100909 // TODO is there anything else to implement?
910 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100911 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100912
913 return;
914
915cannot_delete:
916 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100917}
918
Damien George969a6b32014-12-10 22:07:04 +0000919STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100920 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
921}
922
Damien George969a6b32014-12-10 22:07:04 +0000923STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100924 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100925 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100926 }
Damien George090c9232014-10-17 14:08:49 +0000927 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000928 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100929}
930
Damien George969a6b32014-12-10 22:07:04 +0000931STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100932 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100933 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100934 }
Damien George090c9232014-10-17 14:08:49 +0000935 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000936 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100937}
938
Damien George969a6b32014-12-10 22:07:04 +0000939STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100940 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100941 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100942 return;
943 }
Damiend99b0522013-12-21 18:17:45 +0000944 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100945 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000946 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000947 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100948 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000949 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
950 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 +0100951
Damien George6f355fd2014-04-10 14:11:31 +0100952 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100953 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
954 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
955 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000956 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100957 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
958 } else {
959 compile_node(comp, pns->nodes[0]);
960 }
961 EMIT(return_value);
962}
963
Damien George969a6b32014-12-10 22:07:04 +0000964STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100965 compile_node(comp, pns->nodes[0]);
966 EMIT(pop_top);
967}
968
Damien George969a6b32014-12-10 22:07:04 +0000969STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000970 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100971 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000972 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +0000973 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +0100974 // raise x from y
Damiend99b0522013-12-21 18:17:45 +0000975 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100976 compile_node(comp, pns->nodes[0]);
977 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000978 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +0100979 } else {
980 // raise x
981 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +0000982 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +0100983 }
984}
985
Damien George635543c2014-04-10 12:56:52 +0100986// q_base holds the base of the name
987// eg a -> q_base=a
988// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +0100989STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +0100990 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +0000991 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
992 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100993 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +0100994 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +0100995 pn = pns->nodes[0];
996 is_as = true;
997 }
Damien George635543c2014-04-10 12:56:52 +0100998 if (MP_PARSE_NODE_IS_NULL(pn)) {
999 // empty name (eg, from . import x)
1000 *q_base = MP_QSTR_;
1001 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1002 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001003 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001004 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001005 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001006 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001007 }
Damien George635543c2014-04-10 12:56:52 +01001008 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001009 } else {
1010 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001011 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001012 {
Damien429d7192013-10-04 19:53:11 +01001013 // a name of the form a.b.c
1014 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001015 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001016 }
Damiend99b0522013-12-21 18:17:45 +00001017 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001018 int len = n - 1;
1019 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001020 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001021 }
Damien George55baff42014-01-21 21:40:13 +00001022 byte *q_ptr;
1023 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001024 for (int i = 0; i < n; i++) {
1025 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001026 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001027 }
Damien George39dc1452014-10-03 19:52:22 +01001028 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001029 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001030 memcpy(str_dest, str_src, str_src_len);
1031 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001032 }
Damien George635543c2014-04-10 12:56:52 +01001033 qstr q_full = qstr_build_end(q_ptr);
1034 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001035 if (is_as) {
1036 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001037 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001038 }
1039 }
Damien429d7192013-10-04 19:53:11 +01001040 }
Damien429d7192013-10-04 19:53:11 +01001041 }
1042}
1043
Damien George6be0b0a2014-08-15 14:30:52 +01001044STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001045 EMIT_ARG(load_const_small_int, 0); // level 0 import
1046 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1047 qstr q_base;
1048 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001049 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001050}
1051
Damien George969a6b32014-12-10 22:07:04 +00001052STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001053 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1054}
1055
Damien George969a6b32014-12-10 22:07:04 +00001056STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001057 mp_parse_node_t pn_import_source = pns->nodes[0];
1058
1059 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1060 uint import_level = 0;
1061 do {
1062 mp_parse_node_t pn_rel;
1063 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)) {
1064 // This covers relative imports with dots only like "from .. import"
1065 pn_rel = pn_import_source;
1066 pn_import_source = MP_PARSE_NODE_NULL;
1067 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1068 // This covers relative imports starting with dot(s) like "from .foo import"
1069 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1070 pn_rel = pns_2b->nodes[0];
1071 pn_import_source = pns_2b->nodes[1];
1072 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1073 } else {
1074 // Not a relative import
1075 break;
1076 }
1077
1078 // get the list of . and/or ...'s
1079 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001080 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001081
1082 // count the total number of .'s
1083 for (int i = 0; i < n; i++) {
1084 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1085 import_level++;
1086 } else {
1087 // should be an MP_TOKEN_ELLIPSIS
1088 import_level += 3;
1089 }
1090 }
1091 } while (0);
1092
Damiend99b0522013-12-21 18:17:45 +00001093 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001094 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001095
1096 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001097 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001098 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001099
1100 // do the import
Damien George635543c2014-04-10 12:56:52 +01001101 qstr dummy_q;
1102 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001103 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001104
Damien429d7192013-10-04 19:53:11 +01001105 } else {
Damien George635543c2014-04-10 12:56:52 +01001106 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001107
1108 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001109 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001110 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001111 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001112 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1113 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1114 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001115 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001116 }
Damien Georgeb9791222014-01-23 00:34:21 +00001117 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001118
1119 // do the import
Damien George635543c2014-04-10 12:56:52 +01001120 qstr dummy_q;
1121 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001122 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001123 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1124 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1125 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001126 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001127 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001128 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001129 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001130 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001131 }
1132 }
1133 EMIT(pop_top);
1134 }
1135}
1136
Damien George584ba672014-12-21 17:26:45 +00001137STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1138 bool added;
1139 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001140 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1141 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001142 return;
1143 }
1144 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1145
1146 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1147 id_info = scope_find_global(comp->scope_cur, qst);
1148 if (id_info != NULL) {
1149 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1150 }
1151}
1152
Damien George969a6b32014-12-10 22:07:04 +00001153STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001154 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001155 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001156 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001157 for (int i = 0; i < n; i++) {
1158 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001159 }
1160 }
1161}
1162
Damien George584ba672014-12-21 17:26:45 +00001163STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1164 bool added;
1165 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001166 if (!added && id_info->kind != ID_INFO_KIND_FREE) {
1167 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001168 return;
1169 }
1170 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1171 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1172 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1173 return;
1174 }
1175 id_info->kind = ID_INFO_KIND_FREE;
1176 scope_close_over_in_parents(comp->scope_cur, qst);
1177}
1178
Damien George969a6b32014-12-10 22:07:04 +00001179STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001180 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001181 if (comp->scope_cur->kind == SCOPE_MODULE) {
1182 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1183 return;
1184 }
1185 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001186 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001187 for (int i = 0; i < n; i++) {
1188 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001189 }
1190 }
1191}
1192
Damien George969a6b32014-12-10 22:07:04 +00001193STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001194 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001195 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001196 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001197 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001198 // assertion message
1199 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001200 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001201 }
Damien Georgeb9791222014-01-23 00:34:21 +00001202 EMIT_ARG(raise_varargs, 1);
1203 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001204}
1205
Damien George969a6b32014-12-10 22:07:04 +00001206STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001207 // TODO proper and/or short circuiting
1208
Damien George6f355fd2014-04-10 14:11:31 +01001209 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001210
Damien George65dc9602015-08-14 12:24:11 +01001211 // optimisation: don't emit anything when "if False"
1212 if (!node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001213 uint l_fail = comp_next_label(comp);
1214 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001215
Damien George391db862014-10-17 17:57:33 +00001216 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001217
Damien George65dc9602015-08-14 12:24:11 +01001218 // optimisation: skip everything else when "if True"
1219 if (node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001220 goto done;
1221 }
1222
1223 if (
Damien George65dc9602015-08-14 12:24:11 +01001224 // optimisation: don't jump over non-existent elif/else blocks
1225 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001226 // optimisation: don't jump if last instruction was return
1227 && !EMIT(last_emit_was_return_value)
1228 ) {
1229 // jump over elif/else blocks
1230 EMIT_ARG(jump, l_end);
1231 }
1232
1233 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001234 }
1235
Damien George235f9b32014-10-17 17:30:16 +00001236 // compile elif blocks (if any)
1237 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001238 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 +00001239 for (int i = 0; i < n_elif; i++) {
1240 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1241 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001242
Damien George65dc9602015-08-14 12:24:11 +01001243 // optimisation: don't emit anything when "if False"
1244 if (!node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001245 uint l_fail = comp_next_label(comp);
1246 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001247
Damien George391db862014-10-17 17:57:33 +00001248 compile_node(comp, pns_elif->nodes[1]); // elif block
1249
Damien George65dc9602015-08-14 12:24:11 +01001250 // optimisation: skip everything else when "elif True"
1251 if (node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001252 goto done;
1253 }
1254
1255 // optimisation: don't jump if last instruction was return
1256 if (!EMIT(last_emit_was_return_value)) {
1257 EMIT_ARG(jump, l_end);
1258 }
1259 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001260 }
1261 }
1262
1263 // compile else block
1264 compile_node(comp, pns->nodes[3]); // can be null
1265
Damien George391db862014-10-17 17:57:33 +00001266done:
Damien Georgeb9791222014-01-23 00:34:21 +00001267 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001268}
1269
Damien Georgecbddb272014-02-01 20:08:18 +00001270#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001271 uint16_t old_break_label = comp->break_label; \
1272 uint16_t old_continue_label = comp->continue_label; \
1273 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001274 uint break_label = comp_next_label(comp); \
1275 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001276 comp->break_label = break_label; \
1277 comp->continue_label = continue_label; \
1278 comp->break_continue_except_level = comp->cur_except_level;
1279
1280#define END_BREAK_CONTINUE_BLOCK \
1281 comp->break_label = old_break_label; \
1282 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001283 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001284
Damien George969a6b32014-12-10 22:07:04 +00001285STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001286 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001287
Damien George391db862014-10-17 17:57:33 +00001288 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1289 uint top_label = comp_next_label(comp);
1290 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1291 EMIT_ARG(jump, continue_label);
1292 }
1293 EMIT_ARG(label_assign, top_label);
1294 compile_node(comp, pns->nodes[1]); // body
1295 EMIT_ARG(label_assign, continue_label);
1296 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1297 }
Damience89a212013-10-15 22:25:17 +01001298
1299 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001300 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001301
1302 compile_node(comp, pns->nodes[2]); // else
1303
Damien Georgeb9791222014-01-23 00:34:21 +00001304 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001305}
1306
Damien Georgee181c0d2014-12-12 17:19:56 +00001307// This function compiles an optimised for-loop of the form:
1308// for <var> in range(<start>, <end>, <step>):
1309// <body>
1310// else:
1311// <else>
1312// <var> must be an identifier and <step> must be a small-int.
1313//
1314// Semantics of for-loop require:
1315// - final failing value should not be stored in the loop variable
1316// - if the loop never runs, the loop variable should never be assigned
1317// - assignments to <var>, <end> or <step> in the body do not alter the loop
1318// (<step> is a constant for us, so no need to worry about it changing)
1319//
1320// If <end> is a small-int, then the stack during the for-loop contains just
1321// the current value of <var>. Otherwise, the stack contains <end> then the
1322// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001323STATIC 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 +00001324 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001325
Damien George6f355fd2014-04-10 14:11:31 +01001326 uint top_label = comp_next_label(comp);
1327 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001328
Damien Georgee181c0d2014-12-12 17:19:56 +00001329 // put the end value on the stack if it's not a small-int constant
1330 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1331 if (end_on_stack) {
1332 compile_node(comp, pn_end);
1333 }
1334
1335 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001336 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001337
Damien Georgeb9791222014-01-23 00:34:21 +00001338 EMIT_ARG(jump, entry_label);
1339 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001340
Damien Georgec33ce602014-12-11 17:35:23 +00001341 // duplicate next value and store it to var
1342 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001343 c_assign(comp, pn_var, ASSIGN_STORE);
1344
Damienf3822fc2013-11-09 20:12:03 +00001345 // compile body
1346 compile_node(comp, pn_body);
1347
Damien Georgeb9791222014-01-23 00:34:21 +00001348 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001349
Damien Georgee181c0d2014-12-12 17:19:56 +00001350 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001351 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001352 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001353
Damien Georgeb9791222014-01-23 00:34:21 +00001354 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001355
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001356 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001357 if (end_on_stack) {
1358 EMIT(dup_top_two);
1359 EMIT(rot_two);
1360 } else {
1361 EMIT(dup_top);
1362 compile_node(comp, pn_end);
1363 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001364 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1365 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001366 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001367 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001368 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001369 }
Damien George63f38322015-02-28 15:04:06 +00001370 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001371
1372 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001373 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001374
1375 compile_node(comp, pn_else);
1376
Damien Georgeb9791222014-01-23 00:34:21 +00001377 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001378
1379 // discard final value of var that failed the loop condition
1380 EMIT(pop_top);
1381
1382 // discard <end> value if it's on the stack
1383 if (end_on_stack) {
1384 EMIT(pop_top);
1385 }
Damienf72fd0e2013-11-06 20:20:49 +00001386}
1387
Damien George969a6b32014-12-10 22:07:04 +00001388STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001389 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1390 // this is actually slower, but uses no heap memory
1391 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001392 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_power)) {
Damiend99b0522013-12-21 18:17:45 +00001393 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001394 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1395 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1396 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1397 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001398 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1399 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001400 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001401 mp_parse_node_t pn_range_start;
1402 mp_parse_node_t pn_range_end;
1403 mp_parse_node_t pn_range_step;
1404 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001405 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001406 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001407 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001408 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001409 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001410 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001411 } else if (n_args == 2) {
1412 pn_range_start = args[0];
1413 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001414 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001415 } else {
1416 pn_range_start = args[0];
1417 pn_range_end = args[1];
1418 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001419 // We need to know sign of step. This is possible only if it's constant
1420 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1421 optimize = false;
1422 }
Damienf72fd0e2013-11-06 20:20:49 +00001423 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001424 }
1425 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001426 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1427 return;
1428 }
1429 }
1430 }
Damienf72fd0e2013-11-06 20:20:49 +00001431
Damien Georgecbddb272014-02-01 20:08:18 +00001432 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001433 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001434
Damien George6f355fd2014-04-10 14:11:31 +01001435 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001436
Damien429d7192013-10-04 19:53:11 +01001437 compile_node(comp, pns->nodes[1]); // iterator
1438 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001439 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001440 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001441 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1442 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001443 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001444 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001445 }
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001447 EMIT(for_iter_end);
1448
1449 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001450 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001451
Damien429d7192013-10-04 19:53:11 +01001452 compile_node(comp, pns->nodes[3]); // else (not tested)
1453
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001455}
1456
Damien George6be0b0a2014-08-15 14:30:52 +01001457STATIC 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 +01001458 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001459 uint l1 = comp_next_label(comp);
1460 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001461
Damien Georgeb9791222014-01-23 00:34:21 +00001462 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001463 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001464
Damien429d7192013-10-04 19:53:11 +01001465 compile_node(comp, pn_body); // body
1466 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001467 EMIT_ARG(jump, success_label); // jump over exception handler
1468
1469 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001470 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001471
Damien George6f355fd2014-04-10 14:11:31 +01001472 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001473
1474 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001475 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1476 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001477
1478 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001479 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001480
Damiend99b0522013-12-21 18:17:45 +00001481 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001482 // this is a catch all exception handler
1483 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001484 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001485 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001486 return;
1487 }
1488 } else {
1489 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001490 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1491 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1492 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1493 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001494 // handler binds the exception to a local
1495 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001496 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001497 }
1498 }
1499 EMIT(dup_top);
1500 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001501 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001502 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001503 }
1504
1505 EMIT(pop_top);
1506
1507 if (qstr_exception_local == 0) {
1508 EMIT(pop_top);
1509 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001510 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001511 }
1512
1513 EMIT(pop_top);
1514
Damien George6f355fd2014-04-10 14:11:31 +01001515 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001516 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001517 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001518 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001519 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001520 }
1521 compile_node(comp, pns_except->nodes[1]);
1522 if (qstr_exception_local != 0) {
1523 EMIT(pop_block);
1524 }
1525 EMIT(pop_except);
1526 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001527 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1528 EMIT_ARG(label_assign, l3);
1529 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001530 compile_store_id(comp, qstr_exception_local);
1531 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001532
Damien George8dcc0c72014-03-27 10:55:21 +00001533 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001534 EMIT(end_finally);
1535 }
Damien Georgeb9791222014-01-23 00:34:21 +00001536 EMIT_ARG(jump, l2);
1537 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001538 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001539 }
1540
Damien George8dcc0c72014-03-27 10:55:21 +00001541 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001542 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001543 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001544
Damien Georgeb9791222014-01-23 00:34:21 +00001545 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001546 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001547 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001548}
1549
Damien George6be0b0a2014-08-15 14:30:52 +01001550STATIC 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 +01001551 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001552
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001554 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001555
Damien429d7192013-10-04 19:53:11 +01001556 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001557 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001558 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001559 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001560 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001561 } else {
1562 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1563 }
1564 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001565 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1566 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001567 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001568
Damien George8dcc0c72014-03-27 10:55:21 +00001569 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001570 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001571}
1572
Damien George969a6b32014-12-10 22:07:04 +00001573STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001574 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1575 {
Damiend99b0522013-12-21 18:17:45 +00001576 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1577 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001578 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001579 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1580 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001581 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001582 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001583 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001584 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001585 // no finally
1586 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1587 } else {
1588 // have finally
Damiend99b0522013-12-21 18:17:45 +00001589 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 +01001590 }
1591 } else {
1592 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001593 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001594 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001595 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001596 }
Damien429d7192013-10-04 19:53:11 +01001597 }
1598}
1599
Damien George6be0b0a2014-08-15 14:30:52 +01001600STATIC 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 +01001601 if (n == 0) {
1602 // no more pre-bits, compile the body of the with
1603 compile_node(comp, body);
1604 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001605 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001606 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001607 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001608 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001609 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001610 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001611 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1612 } else {
1613 // this pre-bit is just an expression
1614 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001615 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001616 EMIT(pop_top);
1617 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001618 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001619 // compile additional pre-bits and the body
1620 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1621 // finish this with block
1622 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001623 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1624 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001625 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001626 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001627 EMIT(end_finally);
1628 }
1629}
1630
Damien George969a6b32014-12-10 22:07:04 +00001631STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001632 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001633 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001634 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001635 assert(n > 0);
1636
1637 // compile in a nested fashion
1638 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1639}
1640
Damien George969a6b32014-12-10 22:07:04 +00001641STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001642 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001643 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1644 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001645 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001646 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001647 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001648 EMIT(pop_top);
1649
Damien429d7192013-10-04 19:53:11 +01001650 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001651 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001652 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001653 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001654 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1655 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001656 // do nothing with a lonely constant
1657 } else {
1658 compile_node(comp, pns->nodes[0]); // just an expression
1659 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1660 }
Damien429d7192013-10-04 19:53:11 +01001661 }
Damien Georgeb948de32015-10-08 14:26:01 +01001662 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001663 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1664 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001665 if (kind == PN_expr_stmt_augassign) {
1666 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1667 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001668 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001669 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001670 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001671 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1672 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1673 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1674 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1675 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1676 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1677 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1678 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1679 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1680 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1681 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001682 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001683 }
Damien George7e5fb242014-02-01 22:18:47 +00001684 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001685 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1686 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001687 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001688 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001689 // following CPython, we store left-most first
1690 if (rhs > 0) {
1691 EMIT(dup_top);
1692 }
1693 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1694 for (int i = 0; i < rhs; i++) {
1695 if (i + 1 < rhs) {
1696 EMIT(dup_top);
1697 }
Damien Georgeb948de32015-10-08 14:26:01 +01001698 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001699 }
Damien George0bb97132015-02-27 14:25:47 +00001700 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001701 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001702 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001703 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001704 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001705 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001706 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001707 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001708 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001709 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001710 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1711 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1712 // can't optimise when it's a star expression on the lhs
1713 goto no_optimisation;
1714 }
Damien429d7192013-10-04 19:53:11 +01001715 compile_node(comp, pns10->nodes[0]); // rhs
1716 compile_node(comp, pns10->nodes[1]); // rhs
1717 EMIT(rot_two);
1718 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1719 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001720 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001721 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001722 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001723 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001724 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001725 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001726 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001727 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001728 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1729 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1730 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1731 // can't optimise when it's a star expression on the lhs
1732 goto no_optimisation;
1733 }
Damien429d7192013-10-04 19:53:11 +01001734 compile_node(comp, pns10->nodes[0]); // rhs
1735 compile_node(comp, pns10->nodes[1]); // rhs
1736 compile_node(comp, pns10->nodes[2]); // rhs
1737 EMIT(rot_three);
1738 EMIT(rot_two);
1739 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1740 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1741 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1742 } else {
Damien George495d7812014-04-08 17:51:47 +01001743 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001744 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001745 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1746 }
Damien429d7192013-10-04 19:53:11 +01001747 }
Damien Georgeb948de32015-10-08 14:26:01 +01001748 } else {
1749 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001750 }
1751}
1752
Damien George6be0b0a2014-08-15 14:30:52 +01001753STATIC 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 +00001754 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001755 compile_node(comp, pns->nodes[0]);
1756 for (int i = 1; i < num_nodes; i += 1) {
1757 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001758 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001759 }
1760}
1761
Damien George969a6b32014-12-10 22:07:04 +00001762STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001763 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1764 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001765
Damien George6f355fd2014-04-10 14:11:31 +01001766 uint l_fail = comp_next_label(comp);
1767 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001768 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1769 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(jump, l_end);
1771 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001772 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001773 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001774 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001775}
1776
Damien George969a6b32014-12-10 22:07:04 +00001777STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001778 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001779 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001780 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 +01001781 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001782 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001783 }
1784
1785 // get the scope for this lambda
1786 scope_t *this_scope = (scope_t*)pns->nodes[2];
1787
Damien George2c838942015-11-17 14:00:14 +00001788 // compile the lambda definition
1789 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01001790}
1791
Damien George7711afb2015-02-28 15:10:18 +00001792STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01001793 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001794 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001795 for (int i = 0; i < n; i += 1) {
1796 compile_node(comp, pns->nodes[i]);
1797 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00001798 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01001799 }
1800 }
Damien Georgeb9791222014-01-23 00:34:21 +00001801 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001802}
1803
Damien George7711afb2015-02-28 15:10:18 +00001804STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
1805 compile_or_and_test(comp, pns, true);
1806}
1807
Damien George969a6b32014-12-10 22:07:04 +00001808STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00001809 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01001810}
1811
Damien George969a6b32014-12-10 22:07:04 +00001812STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001813 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01001814 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001815}
1816
Damien George969a6b32014-12-10 22:07:04 +00001817STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001818 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001819 compile_node(comp, pns->nodes[0]);
1820 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01001821 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01001822 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001823 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001824 }
1825 for (int i = 1; i + 1 < num_nodes; i += 2) {
1826 compile_node(comp, pns->nodes[i + 1]);
1827 if (i + 2 < num_nodes) {
1828 EMIT(dup_top);
1829 EMIT(rot_three);
1830 }
Damien George7e5fb242014-02-01 22:18:47 +00001831 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01001832 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00001833 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01001834 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
1835 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
1836 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
1837 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
1838 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
1839 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001840 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00001841 }
1842 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00001843 } else {
1844 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00001845 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1846 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001847 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01001848 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00001849 } else {
1850 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00001851 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001852 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001853 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001854 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001855 }
Damien429d7192013-10-04 19:53:11 +01001856 }
Damien429d7192013-10-04 19:53:11 +01001857 }
1858 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00001859 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01001860 }
1861 }
1862 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01001863 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001864 EMIT_ARG(jump, l_end);
1865 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001866 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01001867 EMIT(rot_two);
1868 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001869 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001870 }
1871}
1872
Damien George969a6b32014-12-10 22:07:04 +00001873STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01001874 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01001875}
1876
Damien George969a6b32014-12-10 22:07:04 +00001877STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001878 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01001879}
1880
Damien George969a6b32014-12-10 22:07:04 +00001881STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001882 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01001883}
1884
Damien George969a6b32014-12-10 22:07:04 +00001885STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01001886 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01001887}
1888
Damien George969a6b32014-12-10 22:07:04 +00001889STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001890 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001891 compile_node(comp, pns->nodes[0]);
1892 for (int i = 1; i + 1 < num_nodes; i += 2) {
1893 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001894 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001895 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01001896 } else {
Damien George0bb97132015-02-27 14:25:47 +00001897 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
1898 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01001899 }
1900 }
1901}
1902
Damien George969a6b32014-12-10 22:07:04 +00001903STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001904 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001905 compile_node(comp, pns->nodes[0]);
1906 for (int i = 1; i + 1 < num_nodes; i += 2) {
1907 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001908 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001909 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01001910 } else {
Damien George0bb97132015-02-27 14:25:47 +00001911 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
1912 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01001913 }
1914 }
1915}
1916
Damien George969a6b32014-12-10 22:07:04 +00001917STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001918 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001919 compile_node(comp, pns->nodes[0]);
1920 for (int i = 1; i + 1 < num_nodes; i += 2) {
1921 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001922 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01001923 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00001924 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01001925 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00001926 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01001927 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01001928 } else {
Damien George0bb97132015-02-27 14:25:47 +00001929 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
1930 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01001931 }
1932 }
1933}
1934
Damien George969a6b32014-12-10 22:07:04 +00001935STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001936 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00001937 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001938 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00001939 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01001940 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01001941 } else {
Damien George0bb97132015-02-27 14:25:47 +00001942 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
1943 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01001944 }
1945}
1946
Damien George969a6b32014-12-10 22:07:04 +00001947STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00001948 // this is to handle special super() call
1949 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
1950
1951 compile_generic_all_nodes(comp, pns);
1952}
1953
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001954STATIC 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 +01001955 // function to call is on top of stack
1956
Damien George35e2a4e2014-02-05 00:51:47 +00001957 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00001958 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 +00001959 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00001960 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00001961 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01001962 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00001963 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00001964 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00001965 EMIT_ARG(call_function, 2, 0, 0);
1966 return;
Damien George35e2a4e2014-02-05 00:51:47 +00001967 }
1968 }
Damien George01039b52014-12-21 17:44:27 +00001969 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00001970 return;
1971 }
Damien George35e2a4e2014-02-05 00:51:47 +00001972
Damien George2c0842b2014-04-27 16:46:51 +01001973 // get the list of arguments
1974 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001975 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01001976
Damien George2c0842b2014-04-27 16:46:51 +01001977 // compile the arguments
1978 // Rather than calling compile_node on the list, we go through the list of args
1979 // explicitly here so that we can count the number of arguments and give sensible
1980 // error messages.
1981 int n_positional = n_positional_extra;
1982 uint n_keyword = 0;
1983 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02001984 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01001985 for (int i = 0; i < n_args; i++) {
1986 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
1987 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
1988 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
1989 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
1990 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
1991 return;
1992 }
1993 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02001994 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01001995 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
1996 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
1997 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
1998 return;
1999 }
2000 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002001 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002002 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002003 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002004 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2005 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2006 return;
2007 }
Damien George59fba2d2015-06-25 14:42:13 +00002008 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002009 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002010 n_keyword += 1;
2011 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002012 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2013 n_positional++;
2014 }
2015 } else {
2016 goto normal_argument;
2017 }
2018 } else {
2019 normal_argument:
2020 if (n_keyword > 0) {
2021 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2022 return;
2023 }
2024 compile_node(comp, args[i]);
2025 n_positional++;
2026 }
Damien429d7192013-10-04 19:53:11 +01002027 }
2028
Delio Brignolie6978a42015-09-17 12:07:06 +02002029 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002030 // if we had one but not the other then we load "null" as a place holder
2031 if (star_flags != 0) {
2032 if (star_args_node == NULL) {
2033 EMIT(load_null);
2034 } else {
2035 compile_node(comp, star_args_node->nodes[0]);
2036 }
2037 if (dblstar_args_node == NULL) {
2038 EMIT(load_null);
2039 } else {
2040 compile_node(comp, dblstar_args_node->nodes[0]);
2041 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002042 }
2043
Damien George2c0842b2014-04-27 16:46:51 +01002044 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002045 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002046 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002047 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002048 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002049 }
Damien429d7192013-10-04 19:53:11 +01002050}
2051
Damien George969a6b32014-12-10 22:07:04 +00002052STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002053 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002054 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002055 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 +01002056 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002057 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2058 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002059 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002060 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002061 i += 1;
2062 } else {
2063 compile_node(comp, pns->nodes[i]);
2064 }
Damien George35e2a4e2014-02-05 00:51:47 +00002065 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002066 }
2067}
2068
Damien George969a6b32014-12-10 22:07:04 +00002069STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002070 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002071 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002072}
2073
Damien George969a6b32014-12-10 22:07:04 +00002074STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002075 // a list of strings
Damien63321742013-12-10 17:41:49 +00002076
2077 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002078 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002079 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002080 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002081 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002082 int pn_kind;
2083 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2084 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2085 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2086 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2087 } else {
2088 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2089 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002090 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2091 pn_kind = MP_PARSE_NODE_STRING;
2092 } else {
2093 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2094 pn_kind = MP_PARSE_NODE_BYTES;
2095 }
Damien George40f3c022014-07-03 13:25:24 +01002096 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002097 }
Damien63321742013-12-10 17:41:49 +00002098 if (i == 0) {
2099 string_kind = pn_kind;
2100 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002101 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002102 return;
2103 }
Damien429d7192013-10-04 19:53:11 +01002104 }
Damien63321742013-12-10 17:41:49 +00002105
Damien George65ef6b72015-01-14 21:17:27 +00002106 // if we are not in the last pass, just load a dummy object
2107 if (comp->pass != MP_PASS_EMIT) {
2108 EMIT_ARG(load_const_obj, mp_const_none);
2109 return;
2110 }
2111
Damien63321742013-12-10 17:41:49 +00002112 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002113 vstr_t vstr;
2114 vstr_init_len(&vstr, n_bytes);
2115 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002116 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002117 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002118 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002119 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2120 memcpy(s_dest, s, s_len);
2121 s_dest += s_len;
2122 } else {
2123 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002124 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2125 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002126 }
Damien63321742013-12-10 17:41:49 +00002127 }
Damien George65ef6b72015-01-14 21:17:27 +00002128
2129 // load the object
Damien George05005f62015-01-21 22:48:37 +00002130 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 +01002131}
2132
2133// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002134STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002135 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2136 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2137 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002138
Damien George36db6bc2014-05-07 17:24:22 +01002139 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002140 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002141 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 +01002142 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002143 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002144 }
2145
2146 // get the scope for this comprehension
2147 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2148
2149 // compile the comprehension
2150 close_over_variables_etc(comp, this_scope, 0, 0);
2151
2152 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2153 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002154 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002155}
2156
Damien George969a6b32014-12-10 22:07:04 +00002157STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002158 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002159 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002160 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2161 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2162 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2163 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2164 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2165 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2166 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002167 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002168 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002169 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002170 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002171 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002172 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002173 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002174 // generator expression
2175 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2176 } else {
2177 // tuple with 2 items
2178 goto tuple_with_2_items;
2179 }
2180 } else {
2181 // tuple with 2 items
2182 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002183 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002184 }
2185 } else {
2186 // parenthesis around a single item, is just that item
2187 compile_node(comp, pns->nodes[0]);
2188 }
2189}
2190
Damien George969a6b32014-12-10 22:07:04 +00002191STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002192 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002193 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002194 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002195 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2196 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2197 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2198 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2199 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002200 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002201 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002202 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002203 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002204 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002205 // list of many items
2206 compile_node(comp, pns2->nodes[0]);
2207 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002208 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002209 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002210 // list comprehension
2211 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2212 } else {
2213 // list with 2 items
2214 goto list_with_2_items;
2215 }
2216 } else {
2217 // list with 2 items
2218 list_with_2_items:
2219 compile_node(comp, pns2->nodes[0]);
2220 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002221 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002222 }
2223 } else {
2224 // list with 1 item
2225 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002226 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002227 }
2228}
2229
Damien George969a6b32014-12-10 22:07:04 +00002230STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002231 mp_parse_node_t pn = pns->nodes[0];
2232 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002233 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002234 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002235 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2236 pns = (mp_parse_node_struct_t*)pn;
2237 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002238 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002239 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002240 compile_node(comp, pn);
2241 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002242 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2243 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2244 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2245 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002246 // dict/set with multiple elements
2247
2248 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002249 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002250 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002251
2252 // first element sets whether it's a dict or set
2253 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002254 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002255 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002257 compile_node(comp, pns->nodes[0]);
2258 EMIT(store_map);
2259 is_dict = true;
2260 } else {
2261 // a set
2262 compile_node(comp, pns->nodes[0]); // 1st value of set
2263 is_dict = false;
2264 }
2265
2266 // process rest of elements
2267 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002268 mp_parse_node_t pn_i = nodes[i];
2269 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2270 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002271 if (is_dict) {
2272 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002273 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002274 return;
2275 }
2276 EMIT(store_map);
2277 } else {
2278 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002279 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002280 return;
2281 }
2282 }
2283 }
2284
Damien Georgee37dcaa2014-12-27 17:07:16 +00002285 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002286 // if it's a set, build it
2287 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002288 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002289 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002290 #endif
Damien George0bb97132015-02-27 14:25:47 +00002291 } else {
2292 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002293 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002294 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002295 // a dictionary comprehension
2296 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2297 } else {
2298 // a set comprehension
2299 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2300 }
Damien429d7192013-10-04 19:53:11 +01002301 }
2302 } else {
2303 // set with one element
2304 goto set_with_one_element;
2305 }
2306 } else {
2307 // set with one element
2308 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002309 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002310 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002311 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002312 #else
2313 assert(0);
2314 #endif
Damien429d7192013-10-04 19:53:11 +01002315 }
2316}
2317
Damien George969a6b32014-12-10 22:07:04 +00002318STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002319 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002320}
2321
Damien George969a6b32014-12-10 22:07:04 +00002322STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002323 // object who's index we want is on top of stack
2324 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002325 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002326}
2327
Damien George969a6b32014-12-10 22:07:04 +00002328STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002329 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002330 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002331}
2332
Damien George83204f32014-12-27 17:20:41 +00002333#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002334STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002335 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2336 mp_parse_node_t pn = pns->nodes[0];
2337 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002338 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002339 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2340 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002341 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2342 pns = (mp_parse_node_struct_t*)pn;
2343 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002344 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002345 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002346 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002347 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002348 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002349 } else {
2350 // [?::x]
2351 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002352 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002353 }
Damiend99b0522013-12-21 18:17:45 +00002354 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002355 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002356 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2357 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2358 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2359 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002360 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002361 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002362 } else {
2363 // [?:x:x]
2364 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002365 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002366 }
2367 } else {
2368 // [?:x]
2369 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002370 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002371 }
2372 } else {
2373 // [?:x]
2374 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002375 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002376 }
2377}
2378
Damien George969a6b32014-12-10 22:07:04 +00002379STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002380 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002381 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2382 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002383}
2384
Damien George969a6b32014-12-10 22:07:04 +00002385STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002387 compile_subscript_3_helper(comp, pns);
2388}
Damien George83204f32014-12-27 17:20:41 +00002389#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002390
Damien George969a6b32014-12-10 22:07:04 +00002391STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002392 // if this is called then we are compiling a dict key:value pair
2393 compile_node(comp, pns->nodes[1]); // value
2394 compile_node(comp, pns->nodes[0]); // key
2395}
2396
Damien George969a6b32014-12-10 22:07:04 +00002397STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002398 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002399 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002400 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002401}
2402
Damien George969a6b32014-12-10 22:07:04 +00002403STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002404 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002405 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002406 return;
2407 }
Damiend99b0522013-12-21 18:17:45 +00002408 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002409 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002410 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002411 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2412 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002413 compile_node(comp, pns->nodes[0]);
2414 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002415 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002416 EMIT(yield_from);
2417 } else {
2418 compile_node(comp, pns->nodes[0]);
2419 EMIT(yield_value);
2420 }
2421}
2422
Damien George65ef6b72015-01-14 21:17:27 +00002423STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2424 // only create and load the actual str object on the last pass
2425 if (comp->pass != MP_PASS_EMIT) {
2426 EMIT_ARG(load_const_obj, mp_const_none);
2427 } else {
2428 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2429 }
2430}
2431
2432STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2433 // only create and load the actual bytes object on the last pass
2434 if (comp->pass != MP_PASS_EMIT) {
2435 EMIT_ARG(load_const_obj, mp_const_none);
2436 } else {
2437 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2438 }
2439}
2440
Damien George7d414a12015-02-08 01:57:40 +00002441STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2442 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2443}
2444
Damiend99b0522013-12-21 18:17:45 +00002445typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002446STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002447#define nc NULL
2448#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002449#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002450#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002451#undef nc
2452#undef c
2453#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002454 NULL,
2455 compile_string,
2456 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002457 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002458};
2459
Damien George6be0b0a2014-08-15 14:30:52 +01002460STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002461 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002462 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002463 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002464 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002465 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002466 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002467 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002468 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002469 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002470 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2471 case MP_PARSE_NODE_BYTES:
2472 // only create and load the actual bytes object on the last pass
2473 if (comp->pass != MP_PASS_EMIT) {
2474 EMIT_ARG(load_const_obj, mp_const_none);
2475 } else {
2476 mp_uint_t len;
2477 const byte *data = qstr_data(arg, &len);
2478 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2479 }
2480 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002481 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002482 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002483 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002484 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002485 // do nothing
2486 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002487 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002488 }
2489 break;
Damien429d7192013-10-04 19:53:11 +01002490 }
2491 } else {
Damiend99b0522013-12-21 18:17:45 +00002492 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002493 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002494 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2495 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002496#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00002497 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2498 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01002499#endif
Damien George65ef6b72015-01-14 21:17:27 +00002500 compile_syntax_error(comp, pn, "internal compiler error");
2501 } else {
2502 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002503 }
2504 }
2505}
2506
Damien George2ac4af62014-08-15 16:45:41 +01002507STATIC 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) {
Damien429d7192013-10-04 19:53:11 +01002508 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002509 qstr param_name = MP_QSTR_NULL;
2510 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002511 if (MP_PARSE_NODE_IS_ID(pn)) {
2512 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002513 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002514 // comes after a star, so counts as a keyword-only parameter
2515 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002516 } else {
Damien George2827d622014-04-27 15:50:52 +01002517 // comes before a star, so counts as a positional parameter
2518 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002519 }
Damienb14de212013-10-06 00:28:28 +01002520 } else {
Damiend99b0522013-12-21 18:17:45 +00002521 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2522 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2523 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2524 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002525 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002526 // comes after a star, so counts as a keyword-only parameter
2527 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002528 } else {
Damien George2827d622014-04-27 15:50:52 +01002529 // comes before a star, so counts as a positional parameter
2530 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002531 }
Damiend99b0522013-12-21 18:17:45 +00002532 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002533 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002534 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002535 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002536 // bare star
2537 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002538 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002539 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002540 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002541 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002542 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002543 } else {
2544 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002545 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002546 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002547 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2548 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002549 }
Damien George0bb97132015-02-27 14:25:47 +00002550 } else {
2551 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002552 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002553 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002554 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002555 }
Damien429d7192013-10-04 19:53:11 +01002556 }
2557
Damien George2827d622014-04-27 15:50:52 +01002558 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002559 bool added;
2560 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2561 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002562 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002563 return;
2564 }
Damien429d7192013-10-04 19:53:11 +01002565 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002566 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002567 }
2568}
2569
Damien George2827d622014-04-27 15:50:52 +01002570STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002571 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002572}
2573
Damien George2827d622014-04-27 15:50:52 +01002574STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002575 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2576}
2577
Damien Georgeea5b59b2015-09-04 16:44:14 +01002578#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002579STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2580 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2581 // no annotation
2582 return;
2583 }
2584
2585 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2586 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2587 // named parameter with possible annotation
2588 // fallthrough
2589 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2590 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2591 // named star with possible annotation
2592 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2593 // fallthrough
2594 } else {
2595 // no annotation
2596 return;
2597 }
2598 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
2599 // double star with possible annotation
2600 // fallthrough
2601 } else {
2602 // no annotation
2603 return;
2604 }
2605
2606 mp_parse_node_t pn_annotation = pns->nodes[1];
2607
2608 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002609 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2610 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2611 assert(id_info != NULL);
2612
Damien Georgeea5b59b2015-09-04 16:44:14 +01002613 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2614 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2615 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2616 } else {
2617 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002618 }
Damien George2ac4af62014-08-15 16:45:41 +01002619 }
Damien429d7192013-10-04 19:53:11 +01002620}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002621#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002622
Damien George6be0b0a2014-08-15 14:30:52 +01002623STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
Damien429d7192013-10-04 19:53:11 +01002624 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002625 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002626 // no more nested if/for; compile inner expression
2627 compile_node(comp, pn_inner_expr);
2628 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002629 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002630 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002631 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002632 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002633 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002635 #endif
Damien429d7192013-10-04 19:53:11 +01002636 } else {
2637 EMIT(yield_value);
2638 EMIT(pop_top);
2639 }
Damiend99b0522013-12-21 18:17:45 +00002640 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002641 // if condition
Damiend99b0522013-12-21 18:17:45 +00002642 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002643 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2644 pn_iter = pns_comp_if->nodes[1];
2645 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002646 } else {
2647 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002648 // for loop
Damiend99b0522013-12-21 18:17:45 +00002649 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002650 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002651 uint l_end2 = comp_next_label(comp);
2652 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002653 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002654 EMIT_ARG(label_assign, l_top2);
2655 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002656 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2657 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
Damien Georgeb9791222014-01-23 00:34:21 +00002658 EMIT_ARG(jump, l_top2);
2659 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002660 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002661 }
2662}
2663
Damien George1463c1f2014-04-25 23:52:57 +01002664STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002665#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002666 // see http://www.python.org/dev/peps/pep-0257/
2667
2668 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002669 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002670 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002671 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002672 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002673 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2674 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002675 for (int i = 0; i < num_nodes; i++) {
2676 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002677 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 +00002678 // not a newline, so this is the first statement; finish search
2679 break;
2680 }
2681 }
2682 // 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 +00002683 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002684 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002685 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002686 } else {
2687 return;
2688 }
2689
2690 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002691 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002692 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002693 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2694 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2695 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2696 // compile the doc string
2697 compile_node(comp, pns->nodes[0]);
2698 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002699 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002700 }
2701 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002702#else
2703 (void)comp;
2704 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002705#endif
Damien429d7192013-10-04 19:53:11 +01002706}
2707
Damien George1463c1f2014-04-25 23:52:57 +01002708STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002709 comp->pass = pass;
2710 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002711 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002712 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002713
Damien George36db6bc2014-05-07 17:24:22 +01002714 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002715 // reset maximum stack sizes in scope
2716 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002717 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002718 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002719 }
2720
Damien429d7192013-10-04 19:53:11 +01002721 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002722 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2723 assert(scope->kind == SCOPE_MODULE);
2724 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2725 compile_node(comp, pns->nodes[0]); // compile the expression
2726 EMIT(return_value);
2727 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002728 if (!comp->is_repl) {
2729 check_for_doc_string(comp, scope->pn);
2730 }
Damien429d7192013-10-04 19:53:11 +01002731 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002733 EMIT(return_value);
2734 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002735 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2736 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2737 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002738
2739 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002740 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002741 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002742 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002743 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002744 }
2745 #if MICROPY_EMIT_NATIVE
2746 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002747 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002748 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002749
Damien George2ac4af62014-08-15 16:45:41 +01002750 // argument annotations
2751 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2752
2753 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01002754 mp_parse_node_t pn_annotation = pns->nodes[2];
2755 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01002756 // nodes[2] can be null or a test-expr
2757 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2758 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2759 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2760 } else {
2761 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002762 }
2763 }
Damien George2ac4af62014-08-15 16:45:41 +01002764 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01002765 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002766
2767 compile_node(comp, pns->nodes[3]); // 3 is function body
2768 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002769 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002771 EMIT(return_value);
2772 }
2773 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002774 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2775 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2776 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002777
2778 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002779 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002780 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002781 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002782 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2783 }
2784
2785 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002786
2787 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2788 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2789 EMIT(pop_top);
2790 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2791 }
Damien429d7192013-10-04 19:53:11 +01002792 EMIT(return_value);
2793 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2794 // a bit of a hack at the moment
2795
Damiend99b0522013-12-21 18:17:45 +00002796 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2797 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2798 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2799 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2800 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002801
Damien George708c0732014-04-27 19:23:46 +01002802 // We need a unique name for the comprehension argument (the iterator).
2803 // CPython uses .0, but we should be able to use anything that won't
2804 // clash with a user defined variable. Best to use an existing qstr,
2805 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01002806 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01002807 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002808 bool added;
2809 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2810 assert(added);
2811 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002812 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01002813 }
2814
2815 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002816 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002817 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002819 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002820 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002821 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002822 #endif
Damien429d7192013-10-04 19:53:11 +01002823 }
2824
Damien George6f355fd2014-04-10 14:11:31 +01002825 uint l_end = comp_next_label(comp);
2826 uint l_top = comp_next_label(comp);
Damien George542bd6b2015-03-26 14:42:40 +00002827 compile_load_id(comp, qstr_arg);
Damien Georgeb9791222014-01-23 00:34:21 +00002828 EMIT_ARG(label_assign, l_top);
2829 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002830 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2831 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002832 EMIT_ARG(jump, l_top);
2833 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002834 EMIT(for_iter_end);
2835
2836 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002838 }
2839 EMIT(return_value);
2840 } else {
2841 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002842 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2843 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2844 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002845
Damien George36db6bc2014-05-07 17:24:22 +01002846 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002847 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002848 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002849 assert(added);
2850 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002851 }
2852
Damien George542bd6b2015-03-26 14:42:40 +00002853 compile_load_id(comp, MP_QSTR___name__);
2854 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00002855 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00002856 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002857
2858 check_for_doc_string(comp, pns->nodes[2]);
2859 compile_node(comp, pns->nodes[2]); // 2 is class body
2860
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002861 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002862 assert(id != NULL);
2863 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002865 } else {
Damien George542bd6b2015-03-26 14:42:40 +00002866 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01002867 }
2868 EMIT(return_value);
2869 }
2870
Damien415eb6f2013-10-05 12:19:06 +01002871 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002872
2873 // make sure we match all the exception levels
2874 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002875}
2876
Damien George094d4502014-04-02 17:31:27 +01002877#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01002878// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01002879STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01002880 comp->pass = pass;
2881 comp->scope_cur = scope;
2882 comp->next_label = 1;
2883
2884 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00002885 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01002886 return;
2887 }
2888
Damien George36db6bc2014-05-07 17:24:22 +01002889 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00002890 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01002891 }
2892
Damien826005c2013-10-05 23:17:28 +01002893 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002894 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2895 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2896 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002897
Damiend99b0522013-12-21 18:17:45 +00002898 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002899
Damiena2f2f7d2013-10-06 00:14:13 +01002900 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01002901 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00002902 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00002903 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01002904 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00002905 if (comp->compile_error != MP_OBJ_NULL) {
2906 goto inline_asm_error;
2907 }
Damiena2f2f7d2013-10-06 00:14:13 +01002908 }
2909
Damiend99b0522013-12-21 18:17:45 +00002910 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002911
Damiend99b0522013-12-21 18:17:45 +00002912 mp_parse_node_t pn_body = pns->nodes[3]; // body
2913 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002914 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01002915
Damien826005c2013-10-05 23:17:28 +01002916 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002917 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2918 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01002919 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
2920 // no instructions
2921 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00002922 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01002923 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00002924 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00002925 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01002926 return;
2927 }
Damien George3d7bf5d2015-02-16 17:46:28 +00002928
2929 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00002930 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00002931 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
2932 goto not_an_instruction;
2933 }
Damiend99b0522013-12-21 18:17:45 +00002934 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
Damien George3d7bf5d2015-02-16 17:46:28 +00002935 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_power) {
2936 goto not_an_instruction;
2937 }
2938 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
2939 goto not_an_instruction;
2940 }
2941 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
2942 goto not_an_instruction;
2943 }
Damiend99b0522013-12-21 18:17:45 +00002944 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
Damien George3d7bf5d2015-02-16 17:46:28 +00002945
2946 // parse node looks like an instruction
2947 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00002948 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2949 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2950 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00002951 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01002952
2953 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01002954 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00002955 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00002956 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01002957 return;
2958 }
Damien George6f355fd2014-04-10 14:11:31 +01002959 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01002960 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00002961 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
2962 compile_syntax_error(comp, nodes[i], "label redefined");
2963 return;
2964 }
Damien826005c2013-10-05 23:17:28 +01002965 }
Damien Georgee5f8a772014-04-21 13:33:15 +01002966 } else if (op == MP_QSTR_align) {
2967 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00002968 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01002969 return;
2970 }
Damien George36db6bc2014-05-07 17:24:22 +01002971 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01002972 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
2973 }
2974 } else if (op == MP_QSTR_data) {
2975 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00002976 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01002977 return;
2978 }
Damien George36db6bc2014-05-07 17:24:22 +01002979 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01002980 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00002981 for (uint j = 1; j < n_args; j++) {
2982 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00002983 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01002984 return;
2985 }
Damien George50912e72015-01-20 11:55:10 +00002986 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01002987 }
2988 }
Damien826005c2013-10-05 23:17:28 +01002989 } else {
Damien George36db6bc2014-05-07 17:24:22 +01002990 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01002992 }
2993 }
Damien George8dfbd2d2015-02-13 01:00:51 +00002994
2995 if (comp->compile_error != MP_OBJ_NULL) {
2996 pns = pns2; // this is the parse node that had the error
2997 goto inline_asm_error;
2998 }
Damien826005c2013-10-05 23:17:28 +01002999 }
3000
Damien George36db6bc2014-05-07 17:24:22 +01003001 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003002 EMIT_INLINE_ASM(end_pass);
3003 }
3004
3005 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003006 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003007 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003008 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003009 }
Damien429d7192013-10-04 19:53:11 +01003010}
Damien George094d4502014-04-02 17:31:27 +01003011#endif
Damien429d7192013-10-04 19:53:11 +01003012
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003013STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003014 // in Micro Python we put the *x parameter after all other parameters (except **y)
3015 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3016 id_info_t *id_param = NULL;
3017 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3018 id_info_t *id = &scope->id_info[i];
3019 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3020 if (id_param != NULL) {
3021 // swap star param with last param
3022 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3023 }
3024 break;
3025 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3026 id_param = id;
3027 }
3028 }
3029 }
Damien George2827d622014-04-27 15:50:52 +01003030
Damien429d7192013-10-04 19:53:11 +01003031 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003032 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003033 scope->num_locals = 0;
3034 for (int i = 0; i < scope->id_info_len; i++) {
3035 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003036 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003037 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3038 continue;
3039 }
3040 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3041 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3042 }
Damien George2827d622014-04-27 15:50:52 +01003043 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003044 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003045 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003046 }
3047 }
3048
Damien George65dc9602015-08-14 12:24:11 +01003049 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003050 for (int i = 0; i < scope->id_info_len; i++) {
3051 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003052 // in Micro Python the cells come right after the fast locals
3053 // parameters are not counted here, since they remain at the start
3054 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003055 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003056 id->local_num = scope->num_locals;
3057 scope->num_locals += 1;
3058 }
Damien9ecbcff2013-12-11 00:41:43 +00003059 }
Damien9ecbcff2013-12-11 00:41:43 +00003060
Damien George65dc9602015-08-14 12:24:11 +01003061 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003062 // make sure they are in the order of the parent scope
3063 if (scope->parent != NULL) {
3064 int num_free = 0;
3065 for (int i = 0; i < scope->parent->id_info_len; i++) {
3066 id_info_t *id = &scope->parent->id_info[i];
3067 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3068 for (int j = 0; j < scope->id_info_len; j++) {
3069 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003070 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003071 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003072 // in Micro Python the frees come first, before the params
3073 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003074 num_free += 1;
3075 }
3076 }
3077 }
Damien429d7192013-10-04 19:53:11 +01003078 }
Damien George6baf76e2013-12-30 22:32:17 +00003079 // in Micro Python shift all other locals after the free locals
3080 if (num_free > 0) {
3081 for (int i = 0; i < scope->id_info_len; i++) {
3082 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003083 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003084 id->local_num += num_free;
3085 }
3086 }
Damien George2827d622014-04-27 15:50:52 +01003087 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 +00003088 scope->num_locals += num_free;
3089 }
Damien429d7192013-10-04 19:53:11 +01003090 }
Damien429d7192013-10-04 19:53:11 +01003091}
3092
Damien George58e0f4a2015-09-23 10:50:43 +01003093mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
Damien George9d5e5c02015-09-24 13:15:57 +01003094 // put compiler state on the stack, it's relatively small
3095 compiler_t comp_state = {0};
3096 compiler_t *comp = &comp_state;
3097
Damien Georgecbd2f742014-01-19 11:48:48 +00003098 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003099 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003100
Damien George62a3a282015-03-01 12:04:05 +00003101 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003102 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003103
Damien Georgea210c772015-03-26 15:49:53 +00003104 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003105 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003106
Damien826005c2013-10-05 23:17:28 +01003107 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003108 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003109 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003110 comp->emit_method_table = &emit_bc_method_table;
3111 #endif
Damien826005c2013-10-05 23:17:28 +01003112 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003113 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003114 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003115#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003116 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003117 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003118#endif
Damien826005c2013-10-05 23:17:28 +01003119 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003120 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003121 }
3122
3123 // update maximim number of labels needed
3124 if (comp->next_label > max_num_labels) {
3125 max_num_labels = comp->next_label;
3126 }
Damien429d7192013-10-04 19:53:11 +01003127 }
3128
Damien826005c2013-10-05 23:17:28 +01003129 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003130 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 +00003131 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003132 }
3133
Damien Georgea210c772015-03-26 15:49:53 +00003134 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003135 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003136
Damien826005c2013-10-05 23:17:28 +01003137 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003138#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003139 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003140#endif
Damien3ef4abb2013-10-12 16:53:13 +01003141#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003142 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003143#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003144 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003145 if (false) {
3146 // dummy
3147
Damien3ef4abb2013-10-12 16:53:13 +01003148#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003149 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003150 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003151 if (emit_inline_thumb == NULL) {
3152 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3153 }
3154 comp->emit = NULL;
Damien826005c2013-10-05 23:17:28 +01003155 comp->emit_inline_asm = emit_inline_thumb;
3156 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003157 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003158 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003159 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003160 }
Damienc025ebb2013-10-12 14:30:21 +01003161#endif
3162
Damien826005c2013-10-05 23:17:28 +01003163 } else {
Damienc025ebb2013-10-12 14:30:21 +01003164
3165 // choose the emit type
3166
Damien826005c2013-10-05 23:17:28 +01003167 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003168
3169#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003170 case MP_EMIT_OPT_NATIVE_PYTHON:
3171 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003172#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003173 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003174 emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003175 }
Damien13ed3a62013-10-08 09:05:10 +01003176 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003177#elif MICROPY_EMIT_X86
3178 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003179 emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels);
Damien Georgec90f59e2014-09-06 23:06:36 +01003180 }
3181 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003182#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003183 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003184 emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels);
Damienc025ebb2013-10-12 14:30:21 +01003185 }
3186 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003187#elif MICROPY_EMIT_ARM
3188 if (emit_native == NULL) {
Damien Georgec8b60f02015-04-20 13:29:31 +00003189 emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003190 }
3191 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003192#endif
3193 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003194 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003195 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003196#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003197
Damien826005c2013-10-05 23:17:28 +01003198 default:
Damien826005c2013-10-05 23:17:28 +01003199 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003200 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003201 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003202 #endif
Damien826005c2013-10-05 23:17:28 +01003203 break;
3204 }
Damienc025ebb2013-10-12 14:30:21 +01003205
Damien George1e1779e2015-01-14 00:20:28 +00003206 // need a pass to compute stack size
3207 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3208
Damien George36db6bc2014-05-07 17:24:22 +01003209 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003210 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003211 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3212 }
3213
3214 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003215 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003216 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003217 }
Damien6cdd3af2013-10-05 18:08:26 +01003218 }
Damien429d7192013-10-04 19:53:11 +01003219 }
3220
Damien Georgecfc4c332015-07-29 22:16:01 +00003221 if (comp->compile_error != MP_OBJ_NULL) {
3222 // if there is no line number for the error then use the line
3223 // number for the start of this scope
3224 compile_error_set_line(comp, comp->scope_cur->pn);
3225 // add a traceback to the exception using relevant source info
3226 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3227 comp->compile_error_line, comp->scope_cur->simple_name);
3228 }
3229
Damien George41d02b62014-01-24 22:42:28 +00003230 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003231
Damien Georgea210c772015-03-26 15:49:53 +00003232 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003233#if MICROPY_EMIT_NATIVE
3234 if (emit_native != NULL) {
3235#if MICROPY_EMIT_X64
3236 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003237#elif MICROPY_EMIT_X86
3238 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003239#elif MICROPY_EMIT_THUMB
3240 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003241#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003242 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003243#endif
3244 }
3245#endif
3246#if MICROPY_EMIT_INLINE_THUMB
3247 if (emit_inline_thumb != NULL) {
3248 emit_inline_thumb_free(emit_inline_thumb);
3249 }
3250#endif
Damien George41d02b62014-01-24 22:42:28 +00003251
Damien George52b5d762014-09-23 15:31:56 +00003252 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003253 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003254
Damien George41d02b62014-01-24 22:42:28 +00003255 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003256 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003257 for (scope_t *s = module_scope; s;) {
3258 scope_t *next = s->next;
3259 scope_free(s);
3260 s = next;
3261 }
Damien5ac1b2e2013-10-18 19:58:12 +01003262
Damien George9d5e5c02015-09-24 13:15:57 +01003263 if (comp->compile_error != MP_OBJ_NULL) {
3264 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003265 } else {
Damien George1fb03172014-01-03 14:22:03 +00003266 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003267 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003268 }
Damien429d7192013-10-04 19:53:11 +01003269}