blob: 8d9d5fa52be07af99be3cbcc942a2d3dc37091a0 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
39typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000040 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010041 uint8_t is_repl;
42 uint8_t pass; // holds enum type pass_kind_t
43 uint8_t had_error; // try to keep compiler clean from nlr
44 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010045
Damien George6f355fd2014-04-10 14:11:31 +010046 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010047
Damien George6f355fd2014-04-10 14:11:31 +010048 uint break_label;
49 uint continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000050 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010051 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010052
Damien George02a4c052014-04-09 12:50:58 +010053 uint16_t n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +010054 uint8_t star_flags;
Damien George8b19db02014-04-11 23:25:34 +010055 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000056 uint16_t num_dict_params;
57 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010058
59 scope_t *scope_head;
60 scope_t *scope_cur;
61
Damien6cdd3af2013-10-05 18:08:26 +010062 emit_t *emit; // current emitter
63 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010064
65 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
66 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010067} compiler_t;
68
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010069STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000070 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010071 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
72 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
73 } else {
74 printf(" File \"%s\"\n", qstr_str(comp->source_file));
75 }
Damien Georgef41fdd02014-03-03 23:19:11 +000076 printf("SyntaxError: %s\n", msg);
77 comp->had_error = true;
78}
79
Damien George57e99eb2014-04-10 22:42:11 +010080STATIC const mp_map_elem_t mp_constants_table[] = {
81 // Extra constants as defined by a port
82 MICROPY_EXTRA_CONSTANTS
83};
84
85STATIC const mp_map_t mp_constants_map = {
86 .all_keys_are_qstrs = 1,
87 .table_is_fixed_array = 1,
88 .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
89 .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90 .table = (mp_map_elem_t*)mp_constants_table,
91};
92
Damiend99b0522013-12-21 18:17:45 +000093mp_parse_node_t fold_constants(mp_parse_node_t pn) {
94 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
95 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
96 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010097
98 // fold arguments first
99 for (int i = 0; i < n; i++) {
100 pns->nodes[i] = fold_constants(pns->nodes[i]);
101 }
102
Damiend99b0522013-12-21 18:17:45 +0000103 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100104 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000105 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200106 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
107 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000108 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100109#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100110 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000111 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100112#endif
Damiend99b0522013-12-21 18:17:45 +0000113 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
114 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100115 } else {
116 // shouldn't happen
117 assert(0);
118 }
119 }
120 break;
121
122 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100123 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000124 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200125 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
126 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000127 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000128 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000129 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000130 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100131 } else {
132 // shouldn't happen
133 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100134 }
Damien Georgeaf272592014-04-04 11:21:58 +0000135 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100137 }
138 }
139 break;
140
141 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000142 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgeaf272592014-04-04 11:21:58 +0000143 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
144 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000145 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000146 if (!mp_small_int_mul_overflow(arg0, arg1)) {
147 arg0 *= arg1;
148 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
150 }
151 }
Damiend99b0522013-12-21 18:17:45 +0000152 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100153 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000154 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000155 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300157 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000158 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300159 }
Damien429d7192013-10-04 19:53:11 +0100160 } else {
161 // shouldn't happen
162 assert(0);
163 }
164 }
165 break;
166
167 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000168 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200169 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000170 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
171 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
172 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
173 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
174 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
175 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100176 } else {
177 // shouldn't happen
178 assert(0);
179 }
180 }
181 break;
182
183 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100184 if (0) {
185#if MICROPY_EMIT_CPYTHON
186 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
187 // int**x
188 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000189 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
190 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200191 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100192 if (power >= 0) {
193 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200194 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100195 for (; power > 0; power--) {
196 ans *= base;
197 }
Damiend99b0522013-12-21 18:17:45 +0000198 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100199 }
200 }
Damien George57e99eb2014-04-10 22:42:11 +0100201#endif
202 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
203 // id.id
204 // look it up in constant table, see if it can be replaced with an integer
205 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
206 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
207 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
208 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
209 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
210 if (elem != NULL) {
211 mp_obj_t dest[2];
212 mp_load_method_maybe(elem->value, q_attr, dest);
213 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
214 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
215 if (MP_PARSE_FITS_SMALL_INT(val)) {
216 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
217 }
218 }
219 }
Damien429d7192013-10-04 19:53:11 +0100220 }
221 break;
222 }
223 }
224
225 return pn;
226}
227
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200228STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000229STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100230
Damien George6f355fd2014-04-10 14:11:31 +0100231STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100232 return comp->next_label++;
233}
234
Damien George8dcc0c72014-03-27 10:55:21 +0000235STATIC void compile_increase_except_level(compiler_t *comp) {
236 comp->cur_except_level += 1;
237 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
238 comp->scope_cur->exc_stack_size = comp->cur_except_level;
239 }
240}
241
242STATIC void compile_decrease_except_level(compiler_t *comp) {
243 assert(comp->cur_except_level > 0);
244 comp->cur_except_level -= 1;
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000248 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100249 scope->parent = comp->scope_cur;
250 scope->next = NULL;
251 if (comp->scope_head == NULL) {
252 comp->scope_head = scope;
253 } else {
254 scope_t *s = comp->scope_head;
255 while (s->next != NULL) {
256 s = s->next;
257 }
258 s->next = scope;
259 }
260 return scope;
261}
262
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200263STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000264 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100265 return 0;
Damiend99b0522013-12-21 18:17:45 +0000266 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100267 return 1;
268 } else {
Damiend99b0522013-12-21 18:17:45 +0000269 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
270 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100271 return 1;
272 } else {
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200278STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000279 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
280 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100282 for (int i = 0; i < num_nodes; i++) {
283 f(comp, pns->nodes[i]);
284 }
Damiend99b0522013-12-21 18:17:45 +0000285 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100286 f(comp, pn);
287 }
288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000291 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100292 *nodes = NULL;
293 return 0;
Damiend99b0522013-12-21 18:17:45 +0000294 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100295 *nodes = pn;
296 return 1;
297 } else {
Damiend99b0522013-12-21 18:17:45 +0000298 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
299 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100300 *nodes = pn;
301 return 1;
302 } else {
303 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000304 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100305 }
306 }
307}
308
Damiend99b0522013-12-21 18:17:45 +0000309void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100310}
311
Damiend99b0522013-12-21 18:17:45 +0000312void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
313 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100314 for (int i = 0; i < num_nodes; i++) {
315 compile_node(comp, pns->nodes[i]);
316 }
317}
318
Damien3ef4abb2013-10-12 16:53:13 +0100319#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200320STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000321 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100322 return false;
323 }
Damiend99b0522013-12-21 18:17:45 +0000324 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100325 return false;
326 }
327 return true;
328}
329
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200330STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000331 uint len;
332 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000333 bool has_single_quote = false;
334 bool has_double_quote = false;
335 for (int i = 0; i < len; i++) {
336 if (str[i] == '\'') {
337 has_single_quote = true;
338 } else if (str[i] == '"') {
339 has_double_quote = true;
340 }
341 }
342 if (bytes) {
343 vstr_printf(vstr, "b");
344 }
345 bool quote_single = false;
346 if (has_single_quote && !has_double_quote) {
347 vstr_printf(vstr, "\"");
348 } else {
349 quote_single = true;
350 vstr_printf(vstr, "'");
351 }
352 for (int i = 0; i < len; i++) {
353 if (str[i] == '\n') {
354 vstr_printf(vstr, "\\n");
355 } else if (str[i] == '\\') {
356 vstr_printf(vstr, "\\\\");
357 } else if (str[i] == '\'' && quote_single) {
358 vstr_printf(vstr, "\\'");
359 } else {
360 vstr_printf(vstr, "%c", str[i]);
361 }
362 }
363 if (has_single_quote && !has_double_quote) {
364 vstr_printf(vstr, "\"");
365 } else {
366 vstr_printf(vstr, "'");
367 }
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000371 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200372 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
373 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
374 return;
375 }
376
Damiend99b0522013-12-21 18:17:45 +0000377 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
378 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
379 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000380 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
381 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
382 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
383 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
384 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100385 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000386 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
387 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
388 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100389 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100390 }
391 break;
392 default: assert(0);
393 }
394}
395
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200396STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100397 int n = 0;
398 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000399 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100400 }
401 int total = n;
402 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000403 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 total += 1;
Damien3a205172013-10-12 15:01:56 +0100405 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 is_const = false;
407 }
408 }
409 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100410 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100411 is_const = false;
412 break;
413 }
414 }
415 if (total > 0 && is_const) {
416 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000417 vstr_t *vstr = vstr_new();
418 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000419 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000420 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100421 need_comma = true;
422 }
423 for (int i = 0; i < n; i++) {
424 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000425 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100426 }
Damien02f89412013-12-12 15:13:36 +0000427 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100428 need_comma = true;
429 }
430 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000431 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100432 } else {
Damien02f89412013-12-12 15:13:36 +0000433 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000436 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100437 } else {
Damiend99b0522013-12-21 18:17:45 +0000438 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100439 compile_node(comp, pn);
440 }
441 for (int i = 0; i < n; i++) {
442 compile_node(comp, pns_list->nodes[i]);
443 }
Damien Georgeb9791222014-01-23 00:34:21 +0000444 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100445 }
446}
Damien3a205172013-10-12 15:01:56 +0100447#endif
448
449// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000450void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100451#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100452 cpython_c_tuple(comp, pn, pns_list);
453#else
454 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000455 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100456 compile_node(comp, pn);
457 total += 1;
458 }
459 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000460 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100461 for (int i = 0; i < n; i++) {
462 compile_node(comp, pns_list->nodes[i]);
463 }
464 total += n;
465 }
Damien Georgeb9791222014-01-23 00:34:21 +0000466 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100467#endif
468}
Damien429d7192013-10-04 19:53:11 +0100469
Damiend99b0522013-12-21 18:17:45 +0000470void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100471 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000472 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200475STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000476 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200477 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200480STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200481 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100482}
483
Damien3ef4abb2013-10-12 16:53:13 +0100484#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100485// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200486STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100487 if (node_is_const_false(pn)) {
488 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000489 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100490 }
491 return;
492 } else if (node_is_const_true(pn)) {
493 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
498 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
499 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
500 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100501 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100502 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100503 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100504 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100505 }
Damien3a205172013-10-12 15:01:56 +0100506 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000507 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100508 } else {
509 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100510 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100511 }
512 }
513 return;
Damiend99b0522013-12-21 18:17:45 +0000514 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100515 if (jump_if == false) {
516 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100517 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100518 }
519 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100520 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100521 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100522 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100523 }
Damien3a205172013-10-12 15:01:56 +0100524 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000525 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100526 }
527 return;
Damiend99b0522013-12-21 18:17:45 +0000528 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100529 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100530 return;
531 }
532 }
533
534 // nothing special, fall back to default compiling for node and jump
535 compile_node(comp, pn);
536 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100538 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000539 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100540 }
541}
Damien3a205172013-10-12 15:01:56 +0100542#endif
Damien429d7192013-10-04 19:53:11 +0100543
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200544STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100545#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100546 cpython_c_if_cond(comp, pn, jump_if, label, false);
547#else
548 if (node_is_const_false(pn)) {
549 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000550 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100551 }
552 return;
553 } else if (node_is_const_true(pn)) {
554 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
559 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
560 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
561 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100562 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100563 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100564 for (int i = 0; i < n - 1; i++) {
565 c_if_cond(comp, pns->nodes[i], true, label2);
566 }
567 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100569 } else {
570 for (int i = 0; i < n; i++) {
571 c_if_cond(comp, pns->nodes[i], true, label);
572 }
573 }
574 return;
Damiend99b0522013-12-21 18:17:45 +0000575 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100576 if (jump_if == false) {
577 for (int i = 0; i < n; i++) {
578 c_if_cond(comp, pns->nodes[i], false, label);
579 }
580 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100581 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100582 for (int i = 0; i < n - 1; i++) {
583 c_if_cond(comp, pns->nodes[i], false, label2);
584 }
585 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000586 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100587 }
588 return;
Damiend99b0522013-12-21 18:17:45 +0000589 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100590 c_if_cond(comp, pns->nodes[0], !jump_if, label);
591 return;
592 }
593 }
594
595 // nothing special, fall back to default compiling for node and jump
596 compile_node(comp, pn);
597 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100599 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000600 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100601 }
602#endif
Damien429d7192013-10-04 19:53:11 +0100603}
604
605typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000606void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100607
Damiend99b0522013-12-21 18:17:45 +0000608void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100609 if (assign_kind != ASSIGN_AUG_STORE) {
610 compile_node(comp, pns->nodes[0]);
611 }
612
Damiend99b0522013-12-21 18:17:45 +0000613 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
614 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
615 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
616 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100617 if (assign_kind != ASSIGN_AUG_STORE) {
618 for (int i = 0; i < n - 1; i++) {
619 compile_node(comp, pns1->nodes[i]);
620 }
621 }
Damiend99b0522013-12-21 18:17:45 +0000622 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
623 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100624 }
Damiend99b0522013-12-21 18:17:45 +0000625 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100626 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100628 if (assign_kind == ASSIGN_AUG_STORE) {
629 EMIT(rot_three);
630 EMIT(store_subscr);
631 } else {
632 compile_node(comp, pns1->nodes[0]);
633 if (assign_kind == ASSIGN_AUG_LOAD) {
634 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100635 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100636 } else {
637 EMIT(store_subscr);
638 }
639 }
Damiend99b0522013-12-21 18:17:45 +0000640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
641 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100642 if (assign_kind == ASSIGN_AUG_LOAD) {
643 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100645 } else {
646 if (assign_kind == ASSIGN_AUG_STORE) {
647 EMIT(rot_two);
648 }
Damien Georgeb9791222014-01-23 00:34:21 +0000649 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100650 }
651 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100652 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100653 }
654 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100655 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100656 }
657
Damiend99b0522013-12-21 18:17:45 +0000658 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100659 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100660 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100661
662 return;
663
664cannot_assign:
665 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100666}
667
Damien George0288cf02014-04-11 11:53:00 +0000668// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
669void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
670 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
671
672 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100673 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000674 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
675 EMIT_ARG(unpack_ex, 0, num_tail);
676 have_star_index = 0;
677 }
678 for (int i = 0; i < num_tail; i++) {
679 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100680 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000681 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
682 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100683 } else {
Damien George0288cf02014-04-11 11:53:00 +0000684 compile_syntax_error(comp, nodes_tail[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100685 return;
686 }
687 }
688 }
689 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000690 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100691 }
Damien George0288cf02014-04-11 11:53:00 +0000692 if (num_head != 0) {
693 if (0 == have_star_index) {
694 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100695 } else {
Damien George0288cf02014-04-11 11:53:00 +0000696 c_assign(comp, node_head, ASSIGN_STORE);
697 }
698 }
699 for (int i = 0; i < num_tail; i++) {
700 if (num_head + i == have_star_index) {
701 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
702 } else {
703 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100704 }
705 }
706}
707
708// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000709void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100710 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000711 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100712 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000713 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
714 if (MP_PARSE_NODE_IS_ID(pn)) {
715 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100716 switch (assign_kind) {
717 case ASSIGN_STORE:
718 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000719 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100720 break;
721 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000722 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100723 break;
724 }
725 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100726 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100727 return;
728 }
729 } else {
Damiend99b0522013-12-21 18:17:45 +0000730 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
731 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100732 case PN_power:
733 // lhs is an index or attribute
734 c_assign_power(comp, pns, assign_kind);
735 break;
736
737 case PN_testlist_star_expr:
738 case PN_exprlist:
739 // lhs is a tuple
740 if (assign_kind != ASSIGN_STORE) {
741 goto bad_aug;
742 }
Damien George0288cf02014-04-11 11:53:00 +0000743 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100744 break;
745
746 case PN_atom_paren:
747 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000748 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100749 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100750 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100751 return;
Damiend99b0522013-12-21 18:17:45 +0000752 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
753 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100754 goto testlist_comp;
755 } else {
756 // parenthesis around 1 item, is just that item
757 pn = pns->nodes[0];
758 goto tail_recursion;
759 }
760 break;
761
762 case PN_atom_bracket:
763 // lhs is something in brackets
764 if (assign_kind != ASSIGN_STORE) {
765 goto bad_aug;
766 }
Damiend99b0522013-12-21 18:17:45 +0000767 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100768 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000769 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000770 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
771 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100772 goto testlist_comp;
773 } else {
774 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000775 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100776 }
777 break;
778
779 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100780 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
781 return;
Damien429d7192013-10-04 19:53:11 +0100782 }
783 return;
784
785 testlist_comp:
786 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000787 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
788 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
789 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100790 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000791 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000792 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000793 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100794 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000795 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
796 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000797 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100798 // TODO can we ever get here? can it be compiled?
799 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
800 return;
Damien429d7192013-10-04 19:53:11 +0100801 } else {
802 // sequence with 2 items
803 goto sequence_with_2_items;
804 }
805 } else {
806 // sequence with 2 items
807 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000808 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100809 }
810 return;
811 }
812 return;
813
814 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100815 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100816}
817
818// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100819// if we are not in CPython compatibility mode then:
820// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
821// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
822// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100823void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
824 assert(n_pos_defaults >= 0);
825 assert(n_kw_defaults >= 0);
826
Damien429d7192013-10-04 19:53:11 +0100827 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000828 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100829 int nfree = 0;
830 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000831 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
832 id_info_t *id = &comp->scope_cur->id_info[i];
833 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
834 for (int j = 0; j < this_scope->id_info_len; j++) {
835 id_info_t *id2 = &this_scope->id_info[j];
836 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000837#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000838 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000839#else
840 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100841 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000842#endif
Damien318aec62013-12-10 18:28:17 +0000843 nfree += 1;
844 }
845 }
Damien429d7192013-10-04 19:53:11 +0100846 }
847 }
848 }
Damien429d7192013-10-04 19:53:11 +0100849
850 // make the function/closure
851 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100852 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100853 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000854 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100855 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100856 }
857}
858
Damiend99b0522013-12-21 18:17:45 +0000859void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000860 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100861 comp->have_star = true;
862 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000863 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
864 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100865 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100866 } else {
867 // named star
Damien429d7192013-10-04 19:53:11 +0100868 }
Damien George8b19db02014-04-11 23:25:34 +0100869 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000870
871 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100872 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000873 // TODO do we need to do anything with this?
874
875 } else {
876 mp_parse_node_t pn_id;
877 mp_parse_node_t pn_colon;
878 mp_parse_node_t pn_equal;
879 if (MP_PARSE_NODE_IS_ID(pn)) {
880 // this parameter is just an id
881
882 pn_id = pn;
883 pn_colon = MP_PARSE_NODE_NULL;
884 pn_equal = MP_PARSE_NODE_NULL;
885
886 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
887 // this parameter has a colon and/or equal specifier
888
889 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
890 pn_id = pns->nodes[0];
891 pn_colon = pns->nodes[1];
892 pn_equal = pns->nodes[2];
893
894 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100895 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000896 assert(0);
897 return;
898 }
899
900 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
901 // this parameter does not have a default value
902
903 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100904 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100905 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000906 return;
907 }
908
909 } else {
910 // this parameter has a default value
911 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
912
Damien George8b19db02014-04-11 23:25:34 +0100913 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000914 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100915#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000916 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
917 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100918 // in Micro Python we put the default positional parameters into a tuple using the bytecode
919 // we need to do this here before we start building the map for the default keywords
920 if (comp->num_default_params > 0) {
921 EMIT_ARG(build_tuple, comp->num_default_params);
922 }
Damien George69b89d22014-04-11 13:38:30 +0000923 // first default dict param, so make the map
924 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000925 }
Damien George69b89d22014-04-11 13:38:30 +0000926#endif
927 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
928 compile_node(comp, pn_equal);
929#if !MICROPY_EMIT_CPYTHON
930 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
931 EMIT(store_map);
932#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000933 } else {
Damien George69b89d22014-04-11 13:38:30 +0000934 comp->num_default_params += 1;
935 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000936 }
937 }
938
939 // TODO pn_colon not implemented
940 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100941 }
942}
943
944// leaves function object on stack
945// returns function name
Damiend99b0522013-12-21 18:17:45 +0000946qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100947 if (comp->pass == PASS_1) {
948 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000949 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100950 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000951 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100952 }
953
954 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100955 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000956 uint old_num_dict_params = comp->num_dict_params;
957 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100958
959 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100960 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000961 comp->num_dict_params = 0;
962 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100963 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000964
965 if (comp->had_error) {
966 return MP_QSTR_NULL;
967 }
968
Damien Georgee337f1e2014-03-31 15:18:37 +0100969#if !MICROPY_EMIT_CPYTHON
970 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100971 // the default keywords args may have already made the tuple; if not, do it now
972 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000973 EMIT_ARG(build_tuple, comp->num_default_params);
Damien Georgee337f1e2014-03-31 15:18:37 +0100974 }
975#endif
976
Damien429d7192013-10-04 19:53:11 +0100977 // get the scope for this function
978 scope_t *fscope = (scope_t*)pns->nodes[4];
979
980 // make the function
Damien George69b89d22014-04-11 13:38:30 +0000981 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100982
983 // restore variables
Damien George8b19db02014-04-11 23:25:34 +0100984 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +0000985 comp->num_dict_params = old_num_dict_params;
986 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100987
988 // return its name (the 'f' in "def f(...):")
989 return fscope->simple_name;
990}
991
992// leaves class object on stack
993// returns class name
Damiend99b0522013-12-21 18:17:45 +0000994qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100995 if (comp->pass == PASS_1) {
996 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000997 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100998 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000999 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001000 }
1001
1002 EMIT(load_build_class);
1003
1004 // scope for this class
1005 scope_t *cscope = (scope_t*)pns->nodes[3];
1006
1007 // compile the class
1008 close_over_variables_etc(comp, cscope, 0, 0);
1009
1010 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001011 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001012
1013 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001014 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1015 mp_parse_node_t parents = pns->nodes[1];
1016 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1017 parents = MP_PARSE_NODE_NULL;
1018 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001019 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001020 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001021
1022 // return its name (the 'C' in class C(...):")
1023 return cscope->simple_name;
1024}
1025
Damien6cdd3af2013-10-05 18:08:26 +01001026// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001027STATIC 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 +00001028 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001029 return false;
1030 }
1031
1032 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001033 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001034 return true;
1035 }
1036
Damiend99b0522013-12-21 18:17:45 +00001037 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001038 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001039 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001040#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001041 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001042 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001043 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001044 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001045#endif
Damien3ef4abb2013-10-12 16:53:13 +01001046#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001047 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001048 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001049#endif
Damien6cdd3af2013-10-05 18:08:26 +01001050 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001051 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001052 }
1053
1054 return true;
1055}
1056
Damiend99b0522013-12-21 18:17:45 +00001057void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001058 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001059 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001060 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1061
Damien6cdd3af2013-10-05 18:08:26 +01001062 // inherit emit options for this function/class definition
1063 uint emit_options = comp->scope_cur->emit_options;
1064
1065 // compile each decorator
1066 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001067 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001068 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1069 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001070
1071 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001072 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001073 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1074
1075 // check for built-in decorators
1076 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1077 // this was a built-in
1078 num_built_in_decorators += 1;
1079
1080 } else {
1081 // not a built-in, compile normally
1082
1083 // compile the decorator function
1084 compile_node(comp, name_nodes[0]);
1085 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001086 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001087 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001088 }
1089
1090 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001091 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001092 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001093 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001094 compile_node(comp, pns_decorator->nodes[1]);
1095 }
Damien429d7192013-10-04 19:53:11 +01001096 }
1097 }
1098
1099 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001100 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001101 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001102 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001103 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001104 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001105 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001106 } else {
1107 // shouldn't happen
1108 assert(0);
1109 }
1110
1111 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001112 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001113 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001114 }
1115
1116 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001117 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001118}
1119
Damiend99b0522013-12-21 18:17:45 +00001120void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001121 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001122 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001123 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001124}
1125
Damiend99b0522013-12-21 18:17:45 +00001126void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1127 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001128 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001129 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1130 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001131
1132 compile_node(comp, pns->nodes[0]); // base of the power node
1133
Damiend99b0522013-12-21 18:17:45 +00001134 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1135 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1136 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1137 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001138 for (int i = 0; i < n - 1; i++) {
1139 compile_node(comp, pns1->nodes[i]);
1140 }
Damiend99b0522013-12-21 18:17:45 +00001141 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1142 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001143 }
Damiend99b0522013-12-21 18:17:45 +00001144 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001145 // can't delete function calls
1146 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001147 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001148 compile_node(comp, pns1->nodes[0]);
1149 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001150 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1151 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001152 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001153 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001154 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001155 }
1156 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001157 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001158 }
1159
Damiend99b0522013-12-21 18:17:45 +00001160 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001161 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001162 }
Damiend99b0522013-12-21 18:17:45 +00001163 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1164 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1165 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1166 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001167 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1168
Damiend99b0522013-12-21 18:17:45 +00001169 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1170 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1171 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001172 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001173 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001174 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001175 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001176 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001177 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001178 c_del_stmt(comp, pns->nodes[0]);
1179 for (int i = 0; i < n; i++) {
1180 c_del_stmt(comp, pns1->nodes[i]);
1181 }
Damiend99b0522013-12-21 18:17:45 +00001182 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001183 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001184 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001185 } else {
1186 // sequence with 2 items
1187 goto sequence_with_2_items;
1188 }
1189 } else {
1190 // sequence with 2 items
1191 sequence_with_2_items:
1192 c_del_stmt(comp, pns->nodes[0]);
1193 c_del_stmt(comp, pns->nodes[1]);
1194 }
1195 } else {
1196 // tuple with 1 element
1197 c_del_stmt(comp, pn);
1198 }
1199 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001200 // TODO is there anything else to implement?
1201 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001202 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001203
1204 return;
1205
1206cannot_delete:
1207 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001208}
1209
Damiend99b0522013-12-21 18:17:45 +00001210void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001211 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1212}
1213
Damiend99b0522013-12-21 18:17:45 +00001214void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001215 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001216 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001217 }
Damien Georgecbddb272014-02-01 20:08:18 +00001218 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001219}
1220
Damiend99b0522013-12-21 18:17:45 +00001221void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001222 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001223 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001224 }
Damien Georgecbddb272014-02-01 20:08:18 +00001225 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001226}
1227
Damiend99b0522013-12-21 18:17:45 +00001228void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001229 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001230 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001231 return;
1232 }
Damiend99b0522013-12-21 18:17:45 +00001233 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001234 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001235 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001236 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001237 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001238 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1239 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 +01001240
Damien George6f355fd2014-04-10 14:11:31 +01001241 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001242 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1243 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1244 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001245 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001246 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1247 } else {
1248 compile_node(comp, pns->nodes[0]);
1249 }
1250 EMIT(return_value);
1251}
1252
Damiend99b0522013-12-21 18:17:45 +00001253void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001254 compile_node(comp, pns->nodes[0]);
1255 EMIT(pop_top);
1256}
1257
Damiend99b0522013-12-21 18:17:45 +00001258void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1259 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001260 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001261 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001262 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001263 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001264 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001265 compile_node(comp, pns->nodes[0]);
1266 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001267 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001268 } else {
1269 // raise x
1270 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001271 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001272 }
1273}
1274
Damien George635543c2014-04-10 12:56:52 +01001275// q_base holds the base of the name
1276// eg a -> q_base=a
1277// a.b.c -> q_base=a
1278void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001279 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001280 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1281 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001282 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001283 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001284 pn = pns->nodes[0];
1285 is_as = true;
1286 }
Damien George635543c2014-04-10 12:56:52 +01001287 if (MP_PARSE_NODE_IS_NULL(pn)) {
1288 // empty name (eg, from . import x)
1289 *q_base = MP_QSTR_;
1290 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1291 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001292 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001293 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001294 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001295 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001296 }
Damien George635543c2014-04-10 12:56:52 +01001297 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001298 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1299 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1300 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001301 // a name of the form a.b.c
1302 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001303 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001304 }
Damiend99b0522013-12-21 18:17:45 +00001305 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001306 int len = n - 1;
1307 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001308 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001309 }
Damien George55baff42014-01-21 21:40:13 +00001310 byte *q_ptr;
1311 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001312 for (int i = 0; i < n; i++) {
1313 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001314 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001315 }
Damien George55baff42014-01-21 21:40:13 +00001316 uint str_src_len;
1317 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001318 memcpy(str_dest, str_src, str_src_len);
1319 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001320 }
Damien George635543c2014-04-10 12:56:52 +01001321 qstr q_full = qstr_build_end(q_ptr);
1322 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001323 if (is_as) {
1324 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001325 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001326 }
1327 }
1328 } else {
Damien George635543c2014-04-10 12:56:52 +01001329 // shouldn't happen
1330 assert(0);
Damien429d7192013-10-04 19:53:11 +01001331 }
1332 } else {
Damien George635543c2014-04-10 12:56:52 +01001333 // shouldn't happen
1334 assert(0);
Damien429d7192013-10-04 19:53:11 +01001335 }
1336}
1337
Damiend99b0522013-12-21 18:17:45 +00001338void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001339 EMIT_ARG(load_const_small_int, 0); // level 0 import
1340 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1341 qstr q_base;
1342 do_import_name(comp, pn, &q_base);
1343 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001344}
1345
Damiend99b0522013-12-21 18:17:45 +00001346void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001347 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1348}
1349
Damiend99b0522013-12-21 18:17:45 +00001350void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001351 mp_parse_node_t pn_import_source = pns->nodes[0];
1352
1353 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1354 uint import_level = 0;
1355 do {
1356 mp_parse_node_t pn_rel;
1357 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)) {
1358 // This covers relative imports with dots only like "from .. import"
1359 pn_rel = pn_import_source;
1360 pn_import_source = MP_PARSE_NODE_NULL;
1361 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1362 // This covers relative imports starting with dot(s) like "from .foo import"
1363 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1364 pn_rel = pns_2b->nodes[0];
1365 pn_import_source = pns_2b->nodes[1];
1366 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1367 } else {
1368 // Not a relative import
1369 break;
1370 }
1371
1372 // get the list of . and/or ...'s
1373 mp_parse_node_t *nodes;
1374 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1375
1376 // count the total number of .'s
1377 for (int i = 0; i < n; i++) {
1378 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1379 import_level++;
1380 } else {
1381 // should be an MP_TOKEN_ELLIPSIS
1382 import_level += 3;
1383 }
1384 }
1385 } while (0);
1386
Damiend99b0522013-12-21 18:17:45 +00001387 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001388 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001389
1390 // build the "fromlist" tuple
1391#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001392 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001393#else
Damien Georgeb9791222014-01-23 00:34:21 +00001394 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1395 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001396#endif
1397
1398 // do the import
Damien George635543c2014-04-10 12:56:52 +01001399 qstr dummy_q;
1400 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001401 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001402
Damien429d7192013-10-04 19:53:11 +01001403 } else {
Damien George635543c2014-04-10 12:56:52 +01001404 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001405
1406 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001407 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001408 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001409#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001410 {
1411 vstr_t *vstr = vstr_new();
1412 vstr_printf(vstr, "(");
1413 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001414 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1415 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1416 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001417 if (i > 0) {
1418 vstr_printf(vstr, ", ");
1419 }
1420 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001421 uint len;
1422 const byte *str = qstr_data(id2, &len);
1423 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001424 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001425 }
Damien02f89412013-12-12 15:13:36 +00001426 if (n == 1) {
1427 vstr_printf(vstr, ",");
1428 }
1429 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001430 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001431 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001432 }
Damiendb4c3612013-12-10 17:27:24 +00001433#else
1434 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001435 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1436 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1437 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001439 }
Damien Georgeb9791222014-01-23 00:34:21 +00001440 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001441#endif
1442
1443 // do the import
Damien George635543c2014-04-10 12:56:52 +01001444 qstr dummy_q;
1445 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001446 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001447 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1448 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1449 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001450 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001451 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001452 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001453 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001455 }
1456 }
1457 EMIT(pop_top);
1458 }
1459}
1460
Damiend99b0522013-12-21 18:17:45 +00001461void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001462 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001463 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1464 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001465 } else {
Damiend99b0522013-12-21 18:17:45 +00001466 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1467 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001468 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001469 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001470 }
Damien429d7192013-10-04 19:53:11 +01001471 }
1472 }
1473}
1474
Damiend99b0522013-12-21 18:17:45 +00001475void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001476 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001477 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1478 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001479 } else {
Damiend99b0522013-12-21 18:17:45 +00001480 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1481 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001482 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001483 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001484 }
Damien429d7192013-10-04 19:53:11 +01001485 }
1486 }
1487}
1488
Damiend99b0522013-12-21 18:17:45 +00001489void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001490 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001491 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001493 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001494 // assertion message
1495 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001496 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001497 }
Damien Georgeb9791222014-01-23 00:34:21 +00001498 EMIT_ARG(raise_varargs, 1);
1499 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001500}
1501
Damiend99b0522013-12-21 18:17:45 +00001502void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001503 // TODO proper and/or short circuiting
1504
Damien George6f355fd2014-04-10 14:11:31 +01001505 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001506
Damien George6f355fd2014-04-10 14:11:31 +01001507 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001508 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1509
1510 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001511
1512 if (
1513#if !MICROPY_EMIT_CPYTHON
1514 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1515 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1516#endif
1517 // optimisation to not jump if last instruction was return
1518 !EMIT(last_emit_was_return_value)
1519 ) {
1520 // jump over elif/else blocks
1521 EMIT_ARG(jump, l_end);
1522 }
1523
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001525
Damiend99b0522013-12-21 18:17:45 +00001526 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001527 // compile elif blocks
1528
Damiend99b0522013-12-21 18:17:45 +00001529 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001530
Damiend99b0522013-12-21 18:17:45 +00001531 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001532 // multiple elif blocks
1533
Damiend99b0522013-12-21 18:17:45 +00001534 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001535 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001536 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001537 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001538 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1539
1540 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001541 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001542 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001543 }
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001545 }
1546
1547 } else {
1548 // a single elif block
1549
Damienb05d7072013-10-05 13:37:10 +01001550 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001551 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1552
1553 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001554 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001555 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001556 }
Damien Georgeb9791222014-01-23 00:34:21 +00001557 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001558 }
1559 }
1560
1561 // compile else block
1562 compile_node(comp, pns->nodes[3]); // can be null
1563
Damien Georgeb9791222014-01-23 00:34:21 +00001564 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001565}
1566
Damien Georgecbddb272014-02-01 20:08:18 +00001567#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001568 uint old_break_label = comp->break_label; \
1569 uint old_continue_label = comp->continue_label; \
1570 uint break_label = comp_next_label(comp); \
1571 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001572 comp->break_label = break_label; \
1573 comp->continue_label = continue_label; \
1574 comp->break_continue_except_level = comp->cur_except_level;
1575
1576#define END_BREAK_CONTINUE_BLOCK \
1577 comp->break_label = old_break_label; \
1578 comp->continue_label = old_continue_label; \
1579 comp->break_continue_except_level = comp->cur_except_level;
1580
Damiend99b0522013-12-21 18:17:45 +00001581void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001582 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001583
Damience89a212013-10-15 22:25:17 +01001584 // compared to CPython, we have an optimised version of while loops
1585#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001586 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001587 EMIT_ARG(setup_loop, break_label);
1588 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001589 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1590 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001591 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001592 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001593 }
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001595 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1596 // this is a small hack to agree with CPython
1597 if (!node_is_const_true(pns->nodes[0])) {
1598 EMIT(pop_block);
1599 }
Damience89a212013-10-15 22:25:17 +01001600#else
Damien George6f355fd2014-04-10 14:11:31 +01001601 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(jump, continue_label);
1603 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001604 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001605 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001606 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1607#endif
1608
1609 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001610 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001611
1612 compile_node(comp, pns->nodes[2]); // else
1613
Damien Georgeb9791222014-01-23 00:34:21 +00001614 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001615}
1616
Damienf72fd0e2013-11-06 20:20:49 +00001617// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001618// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1619// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001620void 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 +00001621 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001622
Damien George6f355fd2014-04-10 14:11:31 +01001623 uint top_label = comp_next_label(comp);
1624 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001625
Damien George3ff2d032014-03-31 18:02:22 +01001626 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001627 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001628 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001629
Damien Georgeb9791222014-01-23 00:34:21 +00001630 EMIT_ARG(jump, entry_label);
1631 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001632
Damien George3ff2d032014-03-31 18:02:22 +01001633 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001634 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001635
1636 // store next value to var
1637 c_assign(comp, pn_var, ASSIGN_STORE);
1638
Damienf3822fc2013-11-09 20:12:03 +00001639 // compile body
1640 compile_node(comp, pn_body);
1641
Damien Georgeb9791222014-01-23 00:34:21 +00001642 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001643
Damien George3ff2d032014-03-31 18:02:22 +01001644 // compile: var + step, duplicated on stack
1645 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001646 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001647 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001648 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001649
Damien Georgeb9791222014-01-23 00:34:21 +00001650 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001651
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001652 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001653 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001654 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1655 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001656 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001657 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001658 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001659 }
Damien Georgeb9791222014-01-23 00:34:21 +00001660 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001661
Damien George3ff2d032014-03-31 18:02:22 +01001662 // discard final value of var that failed the loop condition
1663 EMIT(pop_top);
1664
Damienf72fd0e2013-11-06 20:20:49 +00001665 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001666 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001667
1668 compile_node(comp, pn_else);
1669
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001671}
1672
Damiend99b0522013-12-21 18:17:45 +00001673void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001674#if !MICROPY_EMIT_CPYTHON
1675 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1676 // this is actually slower, but uses no heap memory
1677 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001678 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 +00001679 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001680 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1681 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1682 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1683 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001684 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1685 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001686 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001687 mp_parse_node_t pn_range_start;
1688 mp_parse_node_t pn_range_end;
1689 mp_parse_node_t pn_range_step;
1690 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001691 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001692 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001693 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001694 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001695 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001696 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001697 } else if (n_args == 2) {
1698 pn_range_start = args[0];
1699 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001700 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001701 } else {
1702 pn_range_start = args[0];
1703 pn_range_end = args[1];
1704 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001705 // We need to know sign of step. This is possible only if it's constant
1706 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1707 optimize = false;
1708 }
Damienf72fd0e2013-11-06 20:20:49 +00001709 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001710 }
1711 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001712 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1713 return;
1714 }
1715 }
1716 }
1717#endif
1718
Damien Georgecbddb272014-02-01 20:08:18 +00001719 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001720
Damien George6f355fd2014-04-10 14:11:31 +01001721 uint pop_label = comp_next_label(comp);
1722 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001723
Damience89a212013-10-15 22:25:17 +01001724 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1725#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001726 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001727#endif
1728
Damien429d7192013-10-04 19:53:11 +01001729 compile_node(comp, pns->nodes[1]); // iterator
1730 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001731 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001732 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001733 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1734 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001735 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001736 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001737 }
Damien Georgeb9791222014-01-23 00:34:21 +00001738 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001739 EMIT(for_iter_end);
1740
1741 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001742 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001743
Damience89a212013-10-15 22:25:17 +01001744#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001745 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001746#endif
Damien429d7192013-10-04 19:53:11 +01001747
1748 compile_node(comp, pns->nodes[3]); // else (not tested)
1749
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(label_assign, break_label);
1751 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001752}
1753
Damiend99b0522013-12-21 18:17:45 +00001754void 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 +01001755 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001756 uint l1 = comp_next_label(comp);
1757 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001758
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001760 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001761
Damien429d7192013-10-04 19:53:11 +01001762 compile_node(comp, pn_body); // body
1763 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001764 EMIT_ARG(jump, success_label); // jump over exception handler
1765
1766 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001767 EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
Damien George069a35e2014-04-10 17:22:19 +00001768
Damien George6f355fd2014-04-10 14:11:31 +01001769 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001770
1771 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001772 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1773 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001774
1775 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001776 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001777
Damiend99b0522013-12-21 18:17:45 +00001778 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001779 // this is a catch all exception handler
1780 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001781 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001782 return;
1783 }
1784 } else {
1785 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001786 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1787 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1788 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1789 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001790 // handler binds the exception to a local
1791 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001792 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001793 }
1794 }
1795 EMIT(dup_top);
1796 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001797 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001799 }
1800
1801 EMIT(pop_top);
1802
1803 if (qstr_exception_local == 0) {
1804 EMIT(pop_top);
1805 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001806 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001807 }
1808
1809 EMIT(pop_top);
1810
Damien George6f355fd2014-04-10 14:11:31 +01001811 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001812 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001813 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001814 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001815 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001816 }
1817 compile_node(comp, pns_except->nodes[1]);
1818 if (qstr_exception_local != 0) {
1819 EMIT(pop_block);
1820 }
1821 EMIT(pop_except);
1822 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001823 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1824 EMIT_ARG(label_assign, l3);
1825 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1826 EMIT_ARG(store_id, qstr_exception_local);
1827 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001828
Damien George8dcc0c72014-03-27 10:55:21 +00001829 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001830 EMIT(end_finally);
1831 }
Damien Georgeb9791222014-01-23 00:34:21 +00001832 EMIT_ARG(jump, l2);
1833 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001834 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001835 }
1836
Damien George8dcc0c72014-03-27 10:55:21 +00001837 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001838 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001839 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001840
Damien Georgeb9791222014-01-23 00:34:21 +00001841 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001842 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001843 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001844}
1845
Damiend99b0522013-12-21 18:17:45 +00001846void 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 +01001847 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001848
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001850 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001851
Damien429d7192013-10-04 19:53:11 +01001852 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001853 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001854 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001855 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001856 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001857 } else {
1858 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1859 }
1860 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001861 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1862 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001863 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001864
Damien George8dcc0c72014-03-27 10:55:21 +00001865 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001866 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001867}
1868
Damiend99b0522013-12-21 18:17:45 +00001869void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1870 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1871 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1872 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001873 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001874 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1875 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001876 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001877 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001878 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001879 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001880 // no finally
1881 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1882 } else {
1883 // have finally
Damiend99b0522013-12-21 18:17:45 +00001884 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 +01001885 }
1886 } else {
1887 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001888 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001889 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001890 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001891 }
1892 } else {
1893 // shouldn't happen
1894 assert(0);
1895 }
1896}
1897
Damiend99b0522013-12-21 18:17:45 +00001898void 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 +01001899 if (n == 0) {
1900 // no more pre-bits, compile the body of the with
1901 compile_node(comp, body);
1902 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001903 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001904 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001905 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001906 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001907 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001908 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001909 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1910 } else {
1911 // this pre-bit is just an expression
1912 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001913 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001914 EMIT(pop_top);
1915 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001916 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001917 // compile additional pre-bits and the body
1918 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1919 // finish this with block
1920 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001921 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1922 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001923 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001924 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001925 EMIT(end_finally);
1926 }
1927}
1928
Damiend99b0522013-12-21 18:17:45 +00001929void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001930 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001931 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001932 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1933 assert(n > 0);
1934
1935 // compile in a nested fashion
1936 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1937}
1938
Damiend99b0522013-12-21 18:17:45 +00001939void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1940 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001941 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1942 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001943 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001944 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001945 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001946 EMIT(pop_top);
1947
Damien429d7192013-10-04 19:53:11 +01001948 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001949 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001950 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001951 // do nothing with a lonely constant
1952 } else {
1953 compile_node(comp, pns->nodes[0]); // just an expression
1954 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1955 }
Damien429d7192013-10-04 19:53:11 +01001956 }
1957 } else {
Damiend99b0522013-12-21 18:17:45 +00001958 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1959 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001960 if (kind == PN_expr_stmt_augassign) {
1961 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1962 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001963 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001964 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001965 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001966 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1967 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1968 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1969 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1970 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1971 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1972 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1973 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1974 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1975 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1976 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1977 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1978 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001979 }
Damien George7e5fb242014-02-01 22:18:47 +00001980 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001981 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1982 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001983 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1984 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001985 // following CPython, we store left-most first
1986 if (rhs > 0) {
1987 EMIT(dup_top);
1988 }
1989 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1990 for (int i = 0; i < rhs; i++) {
1991 if (i + 1 < rhs) {
1992 EMIT(dup_top);
1993 }
Damiend99b0522013-12-21 18:17:45 +00001994 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001995 }
1996 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001997 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1998 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1999 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2000 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002001 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002002 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2003 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002004 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2005 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2006 // can't optimise when it's a star expression on the lhs
2007 goto no_optimisation;
2008 }
Damien429d7192013-10-04 19:53:11 +01002009 compile_node(comp, pns10->nodes[0]); // rhs
2010 compile_node(comp, pns10->nodes[1]); // rhs
2011 EMIT(rot_two);
2012 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2013 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002014 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2015 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2016 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2017 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002018 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002019 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2020 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002021 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2022 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2023 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2024 // can't optimise when it's a star expression on the lhs
2025 goto no_optimisation;
2026 }
Damien429d7192013-10-04 19:53:11 +01002027 compile_node(comp, pns10->nodes[0]); // rhs
2028 compile_node(comp, pns10->nodes[1]); // rhs
2029 compile_node(comp, pns10->nodes[2]); // rhs
2030 EMIT(rot_three);
2031 EMIT(rot_two);
2032 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2033 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2034 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2035 } else {
Damien George495d7812014-04-08 17:51:47 +01002036 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pns1->nodes[0]); // rhs
2038 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2039 }
2040 } else {
2041 // shouldn't happen
2042 assert(0);
2043 }
2044 }
2045}
2046
Damien Georged17926d2014-03-30 13:35:08 +01002047void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002048 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002049 compile_node(comp, pns->nodes[0]);
2050 for (int i = 1; i < num_nodes; i += 1) {
2051 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002053 }
2054}
2055
Damiend99b0522013-12-21 18:17:45 +00002056void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2057 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2058 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002059
Damien George6f355fd2014-04-10 14:11:31 +01002060 uint l_fail = comp_next_label(comp);
2061 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002062 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2063 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002064 EMIT_ARG(jump, l_end);
2065 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002066 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002067 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002068 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002069}
2070
Damiend99b0522013-12-21 18:17:45 +00002071void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002072 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002073 //mp_parse_node_t pn_params = pns->nodes[0];
2074 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002075
2076 if (comp->pass == PASS_1) {
2077 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002078 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 +01002079 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002080 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002081 }
2082
2083 // get the scope for this lambda
2084 scope_t *this_scope = (scope_t*)pns->nodes[2];
2085
2086 // make the lambda
2087 close_over_variables_etc(comp, this_scope, 0, 0);
2088}
2089
Damiend99b0522013-12-21 18:17:45 +00002090void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002091 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002092 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002093 for (int i = 0; i < n; i += 1) {
2094 compile_node(comp, pns->nodes[i]);
2095 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002096 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002097 }
2098 }
Damien Georgeb9791222014-01-23 00:34:21 +00002099 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002100}
2101
Damiend99b0522013-12-21 18:17:45 +00002102void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002103 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002104 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002105 for (int i = 0; i < n; i += 1) {
2106 compile_node(comp, pns->nodes[i]);
2107 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002108 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002109 }
2110 }
Damien Georgeb9791222014-01-23 00:34:21 +00002111 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002112}
2113
Damiend99b0522013-12-21 18:17:45 +00002114void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002115 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002116 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002117}
2118
Damiend99b0522013-12-21 18:17:45 +00002119void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002120 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002121 compile_node(comp, pns->nodes[0]);
2122 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002123 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002124 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002125 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002126 }
2127 for (int i = 1; i + 1 < num_nodes; i += 2) {
2128 compile_node(comp, pns->nodes[i + 1]);
2129 if (i + 2 < num_nodes) {
2130 EMIT(dup_top);
2131 EMIT(rot_three);
2132 }
Damien George7e5fb242014-02-01 22:18:47 +00002133 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002134 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002135 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2137 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2138 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2139 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2140 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2141 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2142 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2143 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002144 }
2145 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002146 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2147 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2148 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002149 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002150 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002151 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002152 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002153 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002154 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002155 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002156 }
2157 } else {
2158 // shouldn't happen
2159 assert(0);
2160 }
2161 } else {
2162 // shouldn't happen
2163 assert(0);
2164 }
2165 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002166 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002167 }
2168 }
2169 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002170 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002171 EMIT_ARG(jump, l_end);
2172 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002173 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002174 EMIT(rot_two);
2175 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002176 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002177 }
2178}
2179
Damiend99b0522013-12-21 18:17:45 +00002180void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002181 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002182}
2183
Damiend99b0522013-12-21 18:17:45 +00002184void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002185 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002186}
2187
Damiend99b0522013-12-21 18:17:45 +00002188void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002189 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002190}
2191
Damiend99b0522013-12-21 18:17:45 +00002192void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002193 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002194}
2195
Damiend99b0522013-12-21 18:17:45 +00002196void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2197 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002198 compile_node(comp, pns->nodes[0]);
2199 for (int i = 1; i + 1 < num_nodes; i += 2) {
2200 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002201 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002202 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002203 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002204 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002205 } else {
2206 // shouldn't happen
2207 assert(0);
2208 }
2209 }
2210}
2211
Damiend99b0522013-12-21 18:17:45 +00002212void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2213 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002214 compile_node(comp, pns->nodes[0]);
2215 for (int i = 1; i + 1 < num_nodes; i += 2) {
2216 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002217 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002218 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002219 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002220 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002221 } else {
2222 // shouldn't happen
2223 assert(0);
2224 }
2225 }
2226}
2227
Damiend99b0522013-12-21 18:17:45 +00002228void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2229 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002230 compile_node(comp, pns->nodes[0]);
2231 for (int i = 1; i + 1 < num_nodes; i += 2) {
2232 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002233 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002234 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002235 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002236 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002237 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002238 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002239 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002240 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002241 } else {
2242 // shouldn't happen
2243 assert(0);
2244 }
2245 }
2246}
2247
Damiend99b0522013-12-21 18:17:45 +00002248void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002249 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002250 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002251 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002252 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002253 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002254 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002255 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002256 } else {
2257 // shouldn't happen
2258 assert(0);
2259 }
2260}
2261
Damien George35e2a4e2014-02-05 00:51:47 +00002262void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2263 // this is to handle special super() call
2264 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2265
2266 compile_generic_all_nodes(comp, pns);
2267}
2268
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002269STATIC 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 +01002270 // function to call is on top of stack
2271
Damien George35e2a4e2014-02-05 00:51:47 +00002272#if !MICROPY_EMIT_CPYTHON
2273 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002274 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002275 EMIT_ARG(load_id, MP_QSTR___class__);
2276 // get first argument to function
2277 bool found = false;
2278 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002279 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2280 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002281 found = true;
2282 break;
2283 }
2284 }
2285 if (!found) {
2286 printf("TypeError: super() call cannot find self\n");
2287 return;
2288 }
Damien George922ddd62014-04-09 12:43:17 +01002289 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002290 return;
2291 }
2292#endif
2293
Damien George02a4c052014-04-09 12:50:58 +01002294 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002295 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002296 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002297 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002298
Damien Georgebbcd49a2014-02-06 20:30:16 +00002299 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002300
2301 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002302 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002303 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002304 n_positional -= 1;
2305 }
Damien George922ddd62014-04-09 12:43:17 +01002306 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002307 n_positional -= 1;
2308 }
2309
2310 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002311 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002312 } else {
Damien George922ddd62014-04-09 12:43:17 +01002313 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002314 }
2315
2316 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002317 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002318}
2319
Damiend99b0522013-12-21 18:17:45 +00002320void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2321 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002322 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002323 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 +01002324 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002325 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2326 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002327 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002328 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002329 i += 1;
2330 } else {
2331 compile_node(comp, pns->nodes[i]);
2332 }
Damien George35e2a4e2014-02-05 00:51:47 +00002333 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002334 }
2335}
2336
Damiend99b0522013-12-21 18:17:45 +00002337void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002338 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002339 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002340}
2341
Damiend99b0522013-12-21 18:17:45 +00002342void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002343 // a list of strings
Damien63321742013-12-10 17:41:49 +00002344
2345 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002346 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002347 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002348 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002349 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002350 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2351 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2352 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002353 if (i == 0) {
2354 string_kind = pn_kind;
2355 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002356 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002357 return;
2358 }
Damien George55baff42014-01-21 21:40:13 +00002359 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002360 }
Damien63321742013-12-10 17:41:49 +00002361
Damien63321742013-12-10 17:41:49 +00002362 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002363 byte *q_ptr;
2364 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002365 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002366 uint s_len;
2367 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002368 memcpy(s_dest, s, s_len);
2369 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002370 }
Damien George55baff42014-01-21 21:40:13 +00002371 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002372
Damien Georgeb9791222014-01-23 00:34:21 +00002373 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002374}
2375
2376// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002377void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2378 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2379 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2380 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002381
2382 if (comp->pass == PASS_1) {
2383 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002384 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 +01002385 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002386 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002387 }
2388
2389 // get the scope for this comprehension
2390 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2391
2392 // compile the comprehension
2393 close_over_variables_etc(comp, this_scope, 0, 0);
2394
2395 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2396 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002397 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002398}
2399
Damiend99b0522013-12-21 18:17:45 +00002400void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2401 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002402 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002403 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2404 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2405 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2406 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2407 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2408 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2409 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002410 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002411 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002412 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002413 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002414 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002415 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002416 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002417 // generator expression
2418 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2419 } else {
2420 // tuple with 2 items
2421 goto tuple_with_2_items;
2422 }
2423 } else {
2424 // tuple with 2 items
2425 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002426 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002427 }
2428 } else {
2429 // parenthesis around a single item, is just that item
2430 compile_node(comp, pns->nodes[0]);
2431 }
2432}
2433
Damiend99b0522013-12-21 18:17:45 +00002434void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2435 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002436 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002437 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002438 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2439 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2440 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2441 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2442 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002443 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002444 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002445 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002446 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002447 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002448 // list of many items
2449 compile_node(comp, pns2->nodes[0]);
2450 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002451 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002452 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002453 // list comprehension
2454 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2455 } else {
2456 // list with 2 items
2457 goto list_with_2_items;
2458 }
2459 } else {
2460 // list with 2 items
2461 list_with_2_items:
2462 compile_node(comp, pns2->nodes[0]);
2463 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002464 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002465 }
2466 } else {
2467 // list with 1 item
2468 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002469 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002470 }
2471}
2472
Damiend99b0522013-12-21 18:17:45 +00002473void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2474 mp_parse_node_t pn = pns->nodes[0];
2475 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002476 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002478 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2479 pns = (mp_parse_node_struct_t*)pn;
2480 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002481 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002482 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002483 compile_node(comp, pn);
2484 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002485 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2486 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2487 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2488 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002489 // dict/set with multiple elements
2490
2491 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002492 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002493 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2494
2495 // first element sets whether it's a dict or set
2496 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002497 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002498 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002499 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002500 compile_node(comp, pns->nodes[0]);
2501 EMIT(store_map);
2502 is_dict = true;
2503 } else {
2504 // a set
2505 compile_node(comp, pns->nodes[0]); // 1st value of set
2506 is_dict = false;
2507 }
2508
2509 // process rest of elements
2510 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002511 mp_parse_node_t pn = nodes[i];
2512 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002513 compile_node(comp, pn);
2514 if (is_dict) {
2515 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002516 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002517 return;
2518 }
2519 EMIT(store_map);
2520 } else {
2521 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002522 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002523 return;
2524 }
2525 }
2526 }
2527
2528 // if it's a set, build it
2529 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002530 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002531 }
Damiend99b0522013-12-21 18:17:45 +00002532 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002533 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002534 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002535 // a dictionary comprehension
2536 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2537 } else {
2538 // a set comprehension
2539 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2540 }
2541 } else {
2542 // shouldn't happen
2543 assert(0);
2544 }
2545 } else {
2546 // set with one element
2547 goto set_with_one_element;
2548 }
2549 } else {
2550 // set with one element
2551 set_with_one_element:
2552 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002553 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002554 }
2555}
2556
Damiend99b0522013-12-21 18:17:45 +00002557void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002558 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002559}
2560
Damiend99b0522013-12-21 18:17:45 +00002561void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002562 // object who's index we want is on top of stack
2563 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002564 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002565}
2566
Damiend99b0522013-12-21 18:17:45 +00002567void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002568 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002570}
2571
Damiend99b0522013-12-21 18:17:45 +00002572void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2573 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2574 mp_parse_node_t pn = pns->nodes[0];
2575 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002576 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002577 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2578 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002579 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2580 pns = (mp_parse_node_struct_t*)pn;
2581 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002582 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002583 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002584 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002585 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002586 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002587 } else {
2588 // [?::x]
2589 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002591 }
Damiend99b0522013-12-21 18:17:45 +00002592 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002593 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002594 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2595 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2596 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2597 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002598 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002599 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002600 } else {
2601 // [?:x:x]
2602 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002603 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002604 }
2605 } else {
2606 // [?:x]
2607 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002608 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002609 }
2610 } else {
2611 // [?:x]
2612 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002613 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002614 }
2615}
2616
Damiend99b0522013-12-21 18:17:45 +00002617void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002618 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002619 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2620 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002621}
2622
Damiend99b0522013-12-21 18:17:45 +00002623void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002624 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002625 compile_subscript_3_helper(comp, pns);
2626}
2627
Damiend99b0522013-12-21 18:17:45 +00002628void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002629 // if this is called then we are compiling a dict key:value pair
2630 compile_node(comp, pns->nodes[1]); // value
2631 compile_node(comp, pns->nodes[0]); // key
2632}
2633
Damiend99b0522013-12-21 18:17:45 +00002634void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002635 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002636 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002637 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002638}
2639
Damiend99b0522013-12-21 18:17:45 +00002640void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002641 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002642 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002643 return;
2644 }
Damien George922ddd62014-04-09 12:43:17 +01002645 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002646 compile_node(comp, pns->nodes[0]);
2647}
2648
Damiend99b0522013-12-21 18:17:45 +00002649void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002650 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002651 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002652 return;
2653 }
Damien George922ddd62014-04-09 12:43:17 +01002654 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002655 compile_node(comp, pns->nodes[0]);
2656}
2657
Damiend99b0522013-12-21 18:17:45 +00002658void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2659 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2660 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2661 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2662 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002663 compile_syntax_error(comp, (mp_parse_node_t)pns, "left-hand-side of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002664 return;
2665 }
Damien Georgeb9791222014-01-23 00:34:21 +00002666 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002667 compile_node(comp, pns2->nodes[0]);
2668 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002669 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002670 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2671 } else {
2672 // shouldn't happen
2673 assert(0);
2674 }
2675}
2676
Damiend99b0522013-12-21 18:17:45 +00002677void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002678 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002679 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002680 return;
2681 }
Damiend99b0522013-12-21 18:17:45 +00002682 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002683 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002684 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002685 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2686 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002687 compile_node(comp, pns->nodes[0]);
2688 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002690 EMIT(yield_from);
2691 } else {
2692 compile_node(comp, pns->nodes[0]);
2693 EMIT(yield_value);
2694 }
2695}
2696
Damiend99b0522013-12-21 18:17:45 +00002697typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002698STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002699 NULL,
2700#define nc NULL
2701#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002702#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002703#include "grammar.h"
2704#undef nc
2705#undef c
2706#undef DEF_RULE
2707};
2708
Damiend99b0522013-12-21 18:17:45 +00002709void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2710 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002711 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002712 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2713 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2714 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002716 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002717 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002718 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002719 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2720 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2721 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2722 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002723 case MP_PARSE_NODE_TOKEN:
2724 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002725 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002726 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002727 // do nothing
2728 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002729 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002730 }
2731 break;
Damien429d7192013-10-04 19:53:11 +01002732 default: assert(0);
2733 }
2734 } else {
Damiend99b0522013-12-21 18:17:45 +00002735 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002737 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002738 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002739 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002740#if MICROPY_DEBUG_PRINTERS
2741 mp_parse_node_print(pn, 0);
2742#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002743 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002744 } else {
2745 f(comp, pns);
2746 }
2747 }
2748}
2749
Damiend99b0522013-12-21 18:17:45 +00002750void 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, bool allow_annotations) {
Damien429d7192013-10-04 19:53:11 +01002751 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002752 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002753 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2754 if (MP_PARSE_NODE_IS_ID(pn)) {
2755 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002756 if (comp->have_star) {
Damien429d7192013-10-04 19:53:11 +01002757 // comes after a bare star, so doesn't count as a parameter
2758 } else {
2759 comp->scope_cur->num_params += 1;
2760 }
Damienb14de212013-10-06 00:28:28 +01002761 } else {
Damiend99b0522013-12-21 18:17:45 +00002762 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2763 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2764 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2765 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002766 //int node_index = 1; unused
2767 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002768 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002769 // this parameter has an annotation
2770 pn_annotation = pns->nodes[1];
2771 }
2772 //node_index = 2; unused
2773 }
2774 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002775 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002776 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002777 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002778 comp->scope_cur->num_dict_params += 1;
2779 } else {
2780 comp->scope_cur->num_default_params += 1;
2781 }
2782 }
2783 */
Damien George8b19db02014-04-11 23:25:34 +01002784 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002785 // comes after a bare star, so doesn't count as a parameter
2786 } else {
2787 comp->scope_cur->num_params += 1;
2788 }
Damiend99b0522013-12-21 18:17:45 +00002789 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002790 comp->have_star = true;
Damiend99b0522013-12-21 18:17:45 +00002791 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002792 // bare star
2793 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002794 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002795 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002796 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002797 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002798 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2799 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002800 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002801 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002802 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2803 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002804 pn_annotation = pns->nodes[1];
2805 } else {
2806 // shouldn't happen
2807 assert(0);
2808 }
Damiend99b0522013-12-21 18:17:45 +00002809 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2810 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2811 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002812 // this parameter has an annotation
2813 pn_annotation = pns->nodes[1];
2814 }
Damien George8725f8f2014-02-15 19:33:11 +00002815 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002816 } else {
Damienb14de212013-10-06 00:28:28 +01002817 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002818 assert(0);
2819 }
Damien429d7192013-10-04 19:53:11 +01002820 }
2821
2822 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002823 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002824 // TODO this parameter has an annotation
2825 }
2826 bool added;
2827 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2828 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002829 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002830 return;
2831 }
Damien429d7192013-10-04 19:53:11 +01002832 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002833 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002834 }
2835}
2836
Damiend99b0522013-12-21 18:17:45 +00002837void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002838 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2839}
2840
Damiend99b0522013-12-21 18:17:45 +00002841void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002842 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2843}
2844
Damiend99b0522013-12-21 18:17:45 +00002845void 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 +01002846 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002847 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002848 // no more nested if/for; compile inner expression
2849 compile_node(comp, pn_inner_expr);
2850 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002852 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002853 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002854 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002856 } else {
2857 EMIT(yield_value);
2858 EMIT(pop_top);
2859 }
Damiend99b0522013-12-21 18:17:45 +00002860 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002861 // if condition
Damiend99b0522013-12-21 18:17:45 +00002862 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002863 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2864 pn_iter = pns_comp_if->nodes[1];
2865 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002867 // for loop
Damiend99b0522013-12-21 18:17:45 +00002868 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002869 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002870 uint l_end2 = comp_next_label(comp);
2871 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002872 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(label_assign, l_top2);
2874 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002875 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2876 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 +00002877 EMIT_ARG(jump, l_top2);
2878 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002879 EMIT(for_iter_end);
2880 } else {
2881 // shouldn't happen
2882 assert(0);
2883 }
2884}
2885
Damiend99b0522013-12-21 18:17:45 +00002886void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002887 // see http://www.python.org/dev/peps/pep-0257/
2888
2889 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002890 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002891 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002893 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002894 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2895 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002896 for (int i = 0; i < num_nodes; i++) {
2897 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002898 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 +00002899 // not a newline, so this is the first statement; finish search
2900 break;
2901 }
2902 }
2903 // 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 +00002904 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002905 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002906 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002907 } else {
2908 return;
2909 }
2910
2911 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002912 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2913 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2914 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2915 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2916 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002917 compile_node(comp, pns->nodes[0]); // a doc string
2918 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002919 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002920 }
2921 }
2922 }
2923}
2924
2925void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2926 comp->pass = pass;
2927 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002928 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002929 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002930
2931 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002932 // reset maximum stack sizes in scope
2933 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002934 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002935 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002936 }
2937
Damien5ac1b2e2013-10-18 19:58:12 +01002938#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002939 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002940 scope_print_info(scope);
2941 }
Damien5ac1b2e2013-10-18 19:58:12 +01002942#endif
Damien429d7192013-10-04 19:53:11 +01002943
2944 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002945 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2946 assert(scope->kind == SCOPE_MODULE);
2947 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2948 compile_node(comp, pns->nodes[0]); // compile the expression
2949 EMIT(return_value);
2950 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002951 if (!comp->is_repl) {
2952 check_for_doc_string(comp, scope->pn);
2953 }
Damien429d7192013-10-04 19:53:11 +01002954 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002955 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002956 EMIT(return_value);
2957 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002958 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2959 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2960 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002961
2962 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002963 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002964 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01002965 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002966 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2967 }
2968
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002969 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002970
2971 compile_node(comp, pns->nodes[3]); // 3 is function body
2972 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002973 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002975 EMIT(return_value);
2976 }
2977 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002978 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2979 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2980 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002981
2982 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002983 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002984 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01002985 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002986 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2987 }
2988
2989 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002990
2991 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2992 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2993 EMIT(pop_top);
2994 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2995 }
Damien429d7192013-10-04 19:53:11 +01002996 EMIT(return_value);
2997 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2998 // a bit of a hack at the moment
2999
Damiend99b0522013-12-21 18:17:45 +00003000 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3001 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3002 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3003 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3004 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003005
Damien George55baff42014-01-21 21:40:13 +00003006 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003007 if (comp->pass == PASS_1) {
3008 bool added;
3009 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3010 assert(added);
3011 id_info->kind = ID_INFO_KIND_LOCAL;
3012 scope->num_params = 1;
3013 }
3014
3015 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003016 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003017 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003018 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003019 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003020 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003021 }
3022
Damien George6f355fd2014-04-10 14:11:31 +01003023 uint l_end = comp_next_label(comp);
3024 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003025 EMIT_ARG(load_id, qstr_arg);
3026 EMIT_ARG(label_assign, l_top);
3027 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003028 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3029 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003030 EMIT_ARG(jump, l_top);
3031 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003032 EMIT(for_iter_end);
3033
3034 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003035 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003036 }
3037 EMIT(return_value);
3038 } else {
3039 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003040 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3041 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3042 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003043
3044 if (comp->pass == PASS_1) {
3045 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003046 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003047 assert(added);
3048 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003049 }
3050
Damien Georgeb9791222014-01-23 00:34:21 +00003051 EMIT_ARG(load_id, MP_QSTR___name__);
3052 EMIT_ARG(store_id, MP_QSTR___module__);
3053 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3054 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003055
3056 check_for_doc_string(comp, pns->nodes[2]);
3057 compile_node(comp, pns->nodes[2]); // 2 is class body
3058
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003059 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003060 assert(id != NULL);
3061 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003062 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003063 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003064#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003065 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003066#else
Damien George2bf7c092014-04-09 15:26:46 +01003067 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003068#endif
Damien429d7192013-10-04 19:53:11 +01003069 }
3070 EMIT(return_value);
3071 }
3072
Damien415eb6f2013-10-05 12:19:06 +01003073 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003074
3075 // make sure we match all the exception levels
3076 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003077}
3078
Damien George094d4502014-04-02 17:31:27 +01003079#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003080void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3081 comp->pass = pass;
3082 comp->scope_cur = scope;
3083 comp->next_label = 1;
3084
3085 if (scope->kind != SCOPE_FUNCTION) {
3086 printf("Error: inline assembler must be a function\n");
3087 return;
3088 }
3089
Damiena2f2f7d2013-10-06 00:14:13 +01003090 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003091 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003092 }
3093
Damien826005c2013-10-05 23:17:28 +01003094 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003095 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3096 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3097 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003098
Damiend99b0522013-12-21 18:17:45 +00003099 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003100
Damiena2f2f7d2013-10-06 00:14:13 +01003101 // parameters are in pns->nodes[1]
3102 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003103 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003104 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003105 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003106 }
3107
Damiend99b0522013-12-21 18:17:45 +00003108 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003109
Damiend99b0522013-12-21 18:17:45 +00003110 mp_parse_node_t pn_body = pns->nodes[3]; // body
3111 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003112 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3113
Damien Georgecbd2f742014-01-19 11:48:48 +00003114 /*
Damien826005c2013-10-05 23:17:28 +01003115 if (comp->pass == PASS_3) {
3116 //printf("----\n");
3117 scope_print_info(scope);
3118 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003119 */
Damien826005c2013-10-05 23:17:28 +01003120
3121 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003122 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3123 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3124 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3125 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3126 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3127 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3128 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3129 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3130 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3131 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3132 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3133 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3134 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003135 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3136
3137 // emit instructions
3138 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003139 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003140 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003141 return;
3142 }
Damien George6f355fd2014-04-10 14:11:31 +01003143 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003144 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003145 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003146 }
3147 } else {
3148 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003149 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003150 }
3151 }
3152 }
3153
3154 if (comp->pass > PASS_1) {
3155 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003156 }
Damien429d7192013-10-04 19:53:11 +01003157}
Damien George094d4502014-04-02 17:31:27 +01003158#endif
Damien429d7192013-10-04 19:53:11 +01003159
3160void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3161 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003162 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003163 scope->num_locals = 0;
3164 for (int i = 0; i < scope->id_info_len; i++) {
3165 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003166 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003167 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3168 continue;
3169 }
3170 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3171 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3172 }
Damien9ecbcff2013-12-11 00:41:43 +00003173 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003174 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003175 id->local_num = scope->num_locals;
3176 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003177 }
3178 }
3179
3180 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003181#if MICROPY_EMIT_CPYTHON
3182 int num_cell = 0;
3183#endif
Damien9ecbcff2013-12-11 00:41:43 +00003184 for (int i = 0; i < scope->id_info_len; i++) {
3185 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003186#if MICROPY_EMIT_CPYTHON
3187 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003188 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003189 id->local_num = num_cell;
3190 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003191 }
Damien George6baf76e2013-12-30 22:32:17 +00003192#else
3193 // in Micro Python the cells come right after the fast locals
3194 // parameters are not counted here, since they remain at the start
3195 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003196 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003197 id->local_num = scope->num_locals;
3198 scope->num_locals += 1;
3199 }
3200#endif
Damien9ecbcff2013-12-11 00:41:43 +00003201 }
Damien9ecbcff2013-12-11 00:41:43 +00003202
3203 // compute the index of free vars (freevars[idx] in CPython)
3204 // make sure they are in the order of the parent scope
3205 if (scope->parent != NULL) {
3206 int num_free = 0;
3207 for (int i = 0; i < scope->parent->id_info_len; i++) {
3208 id_info_t *id = &scope->parent->id_info[i];
3209 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3210 for (int j = 0; j < scope->id_info_len; j++) {
3211 id_info_t *id2 = &scope->id_info[j];
3212 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003213 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003214#if MICROPY_EMIT_CPYTHON
3215 // in CPython the frees are numbered after the cells
3216 id2->local_num = num_cell + num_free;
3217#else
3218 // in Micro Python the frees come first, before the params
3219 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003220#endif
3221 num_free += 1;
3222 }
3223 }
3224 }
Damien429d7192013-10-04 19:53:11 +01003225 }
Damien George6baf76e2013-12-30 22:32:17 +00003226#if !MICROPY_EMIT_CPYTHON
3227 // in Micro Python shift all other locals after the free locals
3228 if (num_free > 0) {
3229 for (int i = 0; i < scope->id_info_len; i++) {
3230 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003231 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003232 id->local_num += num_free;
3233 }
3234 }
3235 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3236 scope->num_locals += num_free;
3237 }
3238#endif
Damien429d7192013-10-04 19:53:11 +01003239 }
3240
Damien George8725f8f2014-02-15 19:33:11 +00003241 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003242
3243#if MICROPY_EMIT_CPYTHON
3244 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003245 if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3246 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003247 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003248 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003249 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien George8725f8f2014-02-15 19:33:11 +00003250 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003251 }
3252 }
Damien George882b3632014-04-02 15:56:31 +01003253#endif
3254
Damien429d7192013-10-04 19:53:11 +01003255 int num_free = 0;
3256 for (int i = 0; i < scope->id_info_len; i++) {
3257 id_info_t *id = &scope->id_info[i];
3258 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3259 num_free += 1;
3260 }
3261 }
3262 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003263 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003264 }
3265}
3266
Damien George65cad122014-04-06 11:48:15 +01003267mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien Georgeb140bff2014-04-09 12:54:21 +01003268 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003269 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003270 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003271
Damien826005c2013-10-05 23:17:28 +01003272 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003273 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003274
3275 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003276 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003277
Damien826005c2013-10-05 23:17:28 +01003278 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003279 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003280 comp->emit_method_table = &emit_pass1_method_table;
3281 comp->emit_inline_asm = NULL;
3282 comp->emit_inline_asm_method_table = NULL;
3283 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003284 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003285 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003286#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003287 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003288 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003289#endif
Damien826005c2013-10-05 23:17:28 +01003290 } else {
3291 compile_scope(comp, s, PASS_1);
3292 }
3293
3294 // update maximim number of labels needed
3295 if (comp->next_label > max_num_labels) {
3296 max_num_labels = comp->next_label;
3297 }
Damien429d7192013-10-04 19:53:11 +01003298 }
3299
Damien826005c2013-10-05 23:17:28 +01003300 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003301 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003302 compile_scope_compute_things(comp, s);
3303 }
3304
Damien826005c2013-10-05 23:17:28 +01003305 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003306 emit_pass1_free(comp->emit);
3307
Damien826005c2013-10-05 23:17:28 +01003308 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003309#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003310 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003311#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003312 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003313#endif
Damien3ef4abb2013-10-12 16:53:13 +01003314#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003315 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003316#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003317#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003318 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003319 if (false) {
3320 // dummy
3321
Damien3ef4abb2013-10-12 16:53:13 +01003322#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003323 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003324 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003325 if (emit_inline_thumb == NULL) {
3326 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3327 }
3328 comp->emit = NULL;
3329 comp->emit_method_table = NULL;
3330 comp->emit_inline_asm = emit_inline_thumb;
3331 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3332 compile_scope_inline_asm(comp, s, PASS_2);
3333 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003334#endif
3335
Damien826005c2013-10-05 23:17:28 +01003336 } else {
Damienc025ebb2013-10-12 14:30:21 +01003337
3338 // choose the emit type
3339
Damien3ef4abb2013-10-12 16:53:13 +01003340#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003341 comp->emit = emit_cpython_new(max_num_labels);
3342 comp->emit_method_table = &emit_cpython_method_table;
3343#else
Damien826005c2013-10-05 23:17:28 +01003344 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003345
3346#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003347 case MP_EMIT_OPT_NATIVE_PYTHON:
3348 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003349#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003350 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003351 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003352 }
Damien13ed3a62013-10-08 09:05:10 +01003353 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003354#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003355 if (emit_native == NULL) {
3356 emit_native = emit_native_thumb_new(max_num_labels);
3357 }
3358 comp->emit_method_table = &emit_native_thumb_method_table;
3359#endif
3360 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003361 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003362 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003363#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003364
Damien826005c2013-10-05 23:17:28 +01003365 default:
3366 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003367 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003368 }
3369 comp->emit = emit_bc;
3370 comp->emit_method_table = &emit_bc_method_table;
3371 break;
3372 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003373#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003374
3375 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003376 compile_scope(comp, s, PASS_2);
3377 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003378 }
Damien429d7192013-10-04 19:53:11 +01003379 }
3380
Damien George41d02b62014-01-24 22:42:28 +00003381 // free the emitters
3382#if !MICROPY_EMIT_CPYTHON
3383 if (emit_bc != NULL) {
3384 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003385 }
Damien George41d02b62014-01-24 22:42:28 +00003386#if MICROPY_EMIT_NATIVE
3387 if (emit_native != NULL) {
3388#if MICROPY_EMIT_X64
3389 emit_native_x64_free(emit_native);
3390#elif MICROPY_EMIT_THUMB
3391 emit_native_thumb_free(emit_native);
3392#endif
3393 }
3394#endif
3395#if MICROPY_EMIT_INLINE_THUMB
3396 if (emit_inline_thumb != NULL) {
3397 emit_inline_thumb_free(emit_inline_thumb);
3398 }
3399#endif
3400#endif // !MICROPY_EMIT_CPYTHON
3401
3402 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003403 uint unique_code_id = module_scope->unique_code_id;
3404 for (scope_t *s = module_scope; s;) {
3405 scope_t *next = s->next;
3406 scope_free(s);
3407 s = next;
3408 }
Damien5ac1b2e2013-10-18 19:58:12 +01003409
Damien George41d02b62014-01-24 22:42:28 +00003410 // free the compiler
3411 bool had_error = comp->had_error;
3412 m_del_obj(compiler_t, comp);
3413
Damien George1fb03172014-01-03 14:22:03 +00003414 if (had_error) {
3415 // TODO return a proper error message
3416 return mp_const_none;
3417 } else {
3418#if MICROPY_EMIT_CPYTHON
3419 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003420 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003421 return mp_const_true;
3422#else
3423 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003424 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georgecdd96df2014-04-06 12:58:40 +01003425 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003426#endif
3427 }
Damien429d7192013-10-04 19:53:11 +01003428}