blob: 57f6f1546f83964d84ba3346595f458bacf67d49 [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"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000014#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010015#include "emitglue.h"
16#include "scope.h"
17#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000018#include "compile.h"
19#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010020#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000021#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010022
23// TODO need to mangle __attr names
24
Damience89a212013-10-15 22:25:17 +010025#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
26
Damien429d7192013-10-04 19:53:11 +010027typedef enum {
28 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000029#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010030#include "grammar.h"
31#undef DEF_RULE
32 PN_maximum_number_of,
33} pn_kind_t;
34
Damien Georgeb9791222014-01-23 00:34:21 +000035#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
36#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
37#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
38#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 +010039
40typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000041 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010042 uint8_t is_repl;
43 uint8_t pass; // holds enum type pass_kind_t
44 uint8_t had_error; // try to keep compiler clean from nlr
45 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010046
Damien George6f355fd2014-04-10 14:11:31 +010047 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010048
Damien George6f355fd2014-04-10 14:11:31 +010049 uint break_label;
50 uint continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000051 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010052 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010053
Damien George02a4c052014-04-09 12:50:58 +010054 uint16_t n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +010055 uint8_t star_flags;
Damien George8b19db02014-04-11 23:25:34 +010056 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000057 uint16_t num_dict_params;
58 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010059
60 scope_t *scope_head;
61 scope_t *scope_cur;
62
Damien6cdd3af2013-10-05 18:08:26 +010063 emit_t *emit; // current emitter
64 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010065
66 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
67 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 +010068} compiler_t;
69
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010070STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000071 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010072 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
73 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
74 } else {
75 printf(" File \"%s\"\n", qstr_str(comp->source_file));
76 }
Damien Georgef41fdd02014-03-03 23:19:11 +000077 printf("SyntaxError: %s\n", msg);
78 comp->had_error = true;
79}
80
Damien George57e99eb2014-04-10 22:42:11 +010081STATIC const mp_map_elem_t mp_constants_table[] = {
82 // Extra constants as defined by a port
83 MICROPY_EXTRA_CONSTANTS
84};
85
86STATIC const mp_map_t mp_constants_map = {
87 .all_keys_are_qstrs = 1,
88 .table_is_fixed_array = 1,
89 .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90 .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
91 .table = (mp_map_elem_t*)mp_constants_table,
92};
93
Damiend99b0522013-12-21 18:17:45 +000094mp_parse_node_t fold_constants(mp_parse_node_t pn) {
95 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
96 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
97 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010098
99 // fold arguments first
100 for (int i = 0; i < n; i++) {
101 pns->nodes[i] = fold_constants(pns->nodes[i]);
102 }
103
Damien Georgea26dc502014-04-12 17:54:52 +0100104 // now try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000105 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100106 case PN_atom_paren:
107 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
108 // (int)
109 pn = pns->nodes[0];
110 }
111 break;
112
113 case PN_expr:
114 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
115 // int | int
116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
118 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
119 }
120 break;
121
122 case PN_and_expr:
123 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
124 // int & int
125 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[1]);
127 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
128 }
129 break;
130
Damien429d7192013-10-04 19:53:11 +0100131 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000132 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100133 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
134 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100136 // int << int
137 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
138 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
139 }
Damiend99b0522013-12-21 18:17:45 +0000140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100141 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000142 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100143 } else {
144 // shouldn't happen
145 assert(0);
146 }
147 }
148 break;
149
150 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100151 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000152 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 +0200153 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
154 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000155 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100156 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000157 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000158 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100159 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000160 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100161 } else {
162 // shouldn't happen
163 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100164 }
Damien Georgeaf272592014-04-04 11:21:58 +0000165 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100166 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000167 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100168 }
169 }
170 break;
171
172 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000173 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 +0000174 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
175 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000176 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100177 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000178 if (!mp_small_int_mul_overflow(arg0, arg1)) {
179 arg0 *= arg1;
180 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
181 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
182 }
183 }
Damiend99b0522013-12-21 18:17:45 +0000184 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100185 // int / int
186 // pass
Damiend99b0522013-12-21 18:17:45 +0000187 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100188 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000189 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000190 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300191 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100192 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000193 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 +0300194 }
Damien429d7192013-10-04 19:53:11 +0100195 } else {
196 // shouldn't happen
197 assert(0);
198 }
199 }
200 break;
201
202 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000203 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200204 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000205 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100206 // +int
Damiend99b0522013-12-21 18:17:45 +0000207 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
208 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100209 // -int
Damiend99b0522013-12-21 18:17:45 +0000210 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
211 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100212 // ~int
Damiend99b0522013-12-21 18:17:45 +0000213 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100214 } else {
215 // shouldn't happen
216 assert(0);
217 }
218 }
219 break;
220
221 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100222 if (0) {
223#if MICROPY_EMIT_CPYTHON
224 } 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])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100225 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100226 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000227 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
228 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200229 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100230 if (power >= 0) {
231 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200232 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100233 for (; power > 0; power--) {
234 ans *= base;
235 }
Damiend99b0522013-12-21 18:17:45 +0000236 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100237 }
238 }
Damien George57e99eb2014-04-10 22:42:11 +0100239#endif
240 } 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])) {
241 // id.id
242 // look it up in constant table, see if it can be replaced with an integer
243 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
244 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
245 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
246 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
247 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
248 if (elem != NULL) {
249 mp_obj_t dest[2];
250 mp_load_method_maybe(elem->value, q_attr, dest);
251 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
252 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
253 if (MP_PARSE_FITS_SMALL_INT(val)) {
254 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
255 }
256 }
257 }
Damien429d7192013-10-04 19:53:11 +0100258 }
259 break;
260 }
261 }
262
263 return pn;
264}
265
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200266STATIC 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 +0000267STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100268
Damien George6f355fd2014-04-10 14:11:31 +0100269STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100270 return comp->next_label++;
271}
272
Damien George8dcc0c72014-03-27 10:55:21 +0000273STATIC void compile_increase_except_level(compiler_t *comp) {
274 comp->cur_except_level += 1;
275 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
276 comp->scope_cur->exc_stack_size = comp->cur_except_level;
277 }
278}
279
280STATIC void compile_decrease_except_level(compiler_t *comp) {
281 assert(comp->cur_except_level > 0);
282 comp->cur_except_level -= 1;
283}
284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100286 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100287 scope->parent = comp->scope_cur;
288 scope->next = NULL;
289 if (comp->scope_head == NULL) {
290 comp->scope_head = scope;
291 } else {
292 scope_t *s = comp->scope_head;
293 while (s->next != NULL) {
294 s = s->next;
295 }
296 s->next = scope;
297 }
298 return scope;
299}
300
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200301STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000302 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100303 return 0;
Damiend99b0522013-12-21 18:17:45 +0000304 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100305 return 1;
306 } else {
Damiend99b0522013-12-21 18:17:45 +0000307 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
308 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100309 return 1;
310 } else {
Damiend99b0522013-12-21 18:17:45 +0000311 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100312 }
313 }
314}
315
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200316STATIC 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 +0000317 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
318 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
319 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100320 for (int i = 0; i < num_nodes; i++) {
321 f(comp, pns->nodes[i]);
322 }
Damiend99b0522013-12-21 18:17:45 +0000323 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100324 f(comp, pn);
325 }
326}
327
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200328STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000329 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100330 *nodes = NULL;
331 return 0;
Damiend99b0522013-12-21 18:17:45 +0000332 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100333 *nodes = pn;
334 return 1;
335 } else {
Damiend99b0522013-12-21 18:17:45 +0000336 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
337 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100338 *nodes = pn;
339 return 1;
340 } else {
341 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000342 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100343 }
344 }
345}
346
Damiend99b0522013-12-21 18:17:45 +0000347void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100348}
349
Damiend99b0522013-12-21 18:17:45 +0000350void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
351 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100352 for (int i = 0; i < num_nodes; i++) {
353 compile_node(comp, pns->nodes[i]);
354 }
355}
356
Damien3ef4abb2013-10-12 16:53:13 +0100357#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200358STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000359 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100360 return false;
361 }
Damiend99b0522013-12-21 18:17:45 +0000362 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100363 return false;
364 }
365 return true;
366}
367
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200368STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000369 uint len;
370 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000371 bool has_single_quote = false;
372 bool has_double_quote = false;
373 for (int i = 0; i < len; i++) {
374 if (str[i] == '\'') {
375 has_single_quote = true;
376 } else if (str[i] == '"') {
377 has_double_quote = true;
378 }
379 }
380 if (bytes) {
381 vstr_printf(vstr, "b");
382 }
383 bool quote_single = false;
384 if (has_single_quote && !has_double_quote) {
385 vstr_printf(vstr, "\"");
386 } else {
387 quote_single = true;
388 vstr_printf(vstr, "'");
389 }
390 for (int i = 0; i < len; i++) {
391 if (str[i] == '\n') {
392 vstr_printf(vstr, "\\n");
393 } else if (str[i] == '\\') {
394 vstr_printf(vstr, "\\\\");
395 } else if (str[i] == '\'' && quote_single) {
396 vstr_printf(vstr, "\\'");
397 } else {
398 vstr_printf(vstr, "%c", str[i]);
399 }
400 }
401 if (has_single_quote && !has_double_quote) {
402 vstr_printf(vstr, "\"");
403 } else {
404 vstr_printf(vstr, "'");
405 }
406}
407
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200408STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000409 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200410 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
411 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
412 return;
413 }
414
Damiend99b0522013-12-21 18:17:45 +0000415 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
416 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
417 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000418 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
419 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
420 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
421 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
422 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100423 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000424 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
425 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
426 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100427 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100428 }
429 break;
430 default: assert(0);
431 }
432}
433
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200434STATIC 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 +0100435 int n = 0;
436 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000437 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100438 }
439 int total = n;
440 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000441 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100442 total += 1;
Damien3a205172013-10-12 15:01:56 +0100443 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100444 is_const = false;
445 }
446 }
447 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100448 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100449 is_const = false;
450 break;
451 }
452 }
453 if (total > 0 && is_const) {
454 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000455 vstr_t *vstr = vstr_new();
456 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000457 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000458 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100459 need_comma = true;
460 }
461 for (int i = 0; i < n; i++) {
462 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000463 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100464 }
Damien02f89412013-12-12 15:13:36 +0000465 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100466 need_comma = true;
467 }
468 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000469 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100470 } else {
Damien02f89412013-12-12 15:13:36 +0000471 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100472 }
Damien Georgeb9791222014-01-23 00:34:21 +0000473 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000474 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100475 } else {
Damiend99b0522013-12-21 18:17:45 +0000476 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100477 compile_node(comp, pn);
478 }
479 for (int i = 0; i < n; i++) {
480 compile_node(comp, pns_list->nodes[i]);
481 }
Damien Georgeb9791222014-01-23 00:34:21 +0000482 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100483 }
484}
Damien3a205172013-10-12 15:01:56 +0100485#endif
486
487// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000488void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100489#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100490 cpython_c_tuple(comp, pn, pns_list);
491#else
492 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000493 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100494 compile_node(comp, pn);
495 total += 1;
496 }
497 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000498 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100499 for (int i = 0; i < n; i++) {
500 compile_node(comp, pns_list->nodes[i]);
501 }
502 total += n;
503 }
Damien Georgeb9791222014-01-23 00:34:21 +0000504 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100505#endif
506}
Damien429d7192013-10-04 19:53:11 +0100507
Damiend99b0522013-12-21 18:17:45 +0000508void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100509 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000510 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000514 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200515 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100516}
517
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200518STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200519 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 +0100520}
521
Damien3ef4abb2013-10-12 16:53:13 +0100522#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100523// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200524STATIC 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 +0100525 if (node_is_const_false(pn)) {
526 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000527 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100528 }
529 return;
530 } else if (node_is_const_true(pn)) {
531 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000532 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100533 }
534 return;
Damiend99b0522013-12-21 18:17:45 +0000535 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
536 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
537 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
538 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100539 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100540 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100541 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100542 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100543 }
Damien3a205172013-10-12 15:01:56 +0100544 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000545 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100546 } else {
547 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100548 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100549 }
550 }
551 return;
Damiend99b0522013-12-21 18:17:45 +0000552 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100553 if (jump_if == false) {
554 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100555 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100556 }
557 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100558 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100559 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100560 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100561 }
Damien3a205172013-10-12 15:01:56 +0100562 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000563 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100564 }
565 return;
Damiend99b0522013-12-21 18:17:45 +0000566 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100567 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100568 return;
569 }
570 }
571
572 // nothing special, fall back to default compiling for node and jump
573 compile_node(comp, pn);
574 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000575 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100576 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000577 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100578 }
579}
Damien3a205172013-10-12 15:01:56 +0100580#endif
Damien429d7192013-10-04 19:53:11 +0100581
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200582STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100583#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100584 cpython_c_if_cond(comp, pn, jump_if, label, false);
585#else
586 if (node_is_const_false(pn)) {
587 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000588 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100589 }
590 return;
591 } else if (node_is_const_true(pn)) {
592 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000593 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100594 }
595 return;
Damiend99b0522013-12-21 18:17:45 +0000596 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
597 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
598 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
599 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100600 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100601 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100602 for (int i = 0; i < n - 1; i++) {
603 c_if_cond(comp, pns->nodes[i], true, label2);
604 }
605 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000606 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100607 } else {
608 for (int i = 0; i < n; i++) {
609 c_if_cond(comp, pns->nodes[i], true, label);
610 }
611 }
612 return;
Damiend99b0522013-12-21 18:17:45 +0000613 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100614 if (jump_if == false) {
615 for (int i = 0; i < n; i++) {
616 c_if_cond(comp, pns->nodes[i], false, label);
617 }
618 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100619 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100620 for (int i = 0; i < n - 1; i++) {
621 c_if_cond(comp, pns->nodes[i], false, label2);
622 }
623 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000624 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100625 }
626 return;
Damiend99b0522013-12-21 18:17:45 +0000627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100628 c_if_cond(comp, pns->nodes[0], !jump_if, label);
629 return;
630 }
631 }
632
633 // nothing special, fall back to default compiling for node and jump
634 compile_node(comp, pn);
635 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000636 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100637 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000638 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100639 }
640#endif
Damien429d7192013-10-04 19:53:11 +0100641}
642
643typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000644void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100645
Damiend99b0522013-12-21 18:17:45 +0000646void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100647 if (assign_kind != ASSIGN_AUG_STORE) {
648 compile_node(comp, pns->nodes[0]);
649 }
650
Damiend99b0522013-12-21 18:17:45 +0000651 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
652 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
653 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
654 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100655 if (assign_kind != ASSIGN_AUG_STORE) {
656 for (int i = 0; i < n - 1; i++) {
657 compile_node(comp, pns1->nodes[i]);
658 }
659 }
Damiend99b0522013-12-21 18:17:45 +0000660 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
661 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100662 }
Damiend99b0522013-12-21 18:17:45 +0000663 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100664 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000665 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100666 if (assign_kind == ASSIGN_AUG_STORE) {
667 EMIT(rot_three);
668 EMIT(store_subscr);
669 } else {
670 compile_node(comp, pns1->nodes[0]);
671 if (assign_kind == ASSIGN_AUG_LOAD) {
672 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100673 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100674 } else {
675 EMIT(store_subscr);
676 }
677 }
Damiend99b0522013-12-21 18:17:45 +0000678 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
679 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100680 if (assign_kind == ASSIGN_AUG_LOAD) {
681 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000682 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100683 } else {
684 if (assign_kind == ASSIGN_AUG_STORE) {
685 EMIT(rot_two);
686 }
Damien Georgeb9791222014-01-23 00:34:21 +0000687 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100688 }
689 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100690 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100691 }
692 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100693 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100694 }
695
Damiend99b0522013-12-21 18:17:45 +0000696 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100697 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100698 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100699
700 return;
701
702cannot_assign:
703 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100704}
705
Damien George0288cf02014-04-11 11:53:00 +0000706// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
707void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
708 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
709
710 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100711 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000712 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
713 EMIT_ARG(unpack_ex, 0, num_tail);
714 have_star_index = 0;
715 }
716 for (int i = 0; i < num_tail; i++) {
717 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100718 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000719 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
720 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100721 } else {
Damien George0288cf02014-04-11 11:53:00 +0000722 compile_syntax_error(comp, nodes_tail[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100723 return;
724 }
725 }
726 }
727 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000728 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100729 }
Damien George0288cf02014-04-11 11:53:00 +0000730 if (num_head != 0) {
731 if (0 == have_star_index) {
732 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100733 } else {
Damien George0288cf02014-04-11 11:53:00 +0000734 c_assign(comp, node_head, ASSIGN_STORE);
735 }
736 }
737 for (int i = 0; i < num_tail; i++) {
738 if (num_head + i == have_star_index) {
739 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
740 } else {
741 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100742 }
743 }
744}
745
746// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000747void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100748 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100750 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000751 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
752 if (MP_PARSE_NODE_IS_ID(pn)) {
753 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100754 switch (assign_kind) {
755 case ASSIGN_STORE:
756 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000757 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100758 break;
759 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000760 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100761 break;
762 }
763 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100764 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100765 return;
766 }
767 } else {
Damiend99b0522013-12-21 18:17:45 +0000768 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
769 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100770 case PN_power:
771 // lhs is an index or attribute
772 c_assign_power(comp, pns, assign_kind);
773 break;
774
775 case PN_testlist_star_expr:
776 case PN_exprlist:
777 // lhs is a tuple
778 if (assign_kind != ASSIGN_STORE) {
779 goto bad_aug;
780 }
Damien George0288cf02014-04-11 11:53:00 +0000781 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100782 break;
783
784 case PN_atom_paren:
785 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000786 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100787 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100788 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100789 return;
Damiend99b0522013-12-21 18:17:45 +0000790 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
791 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100792 goto testlist_comp;
793 } else {
794 // parenthesis around 1 item, is just that item
795 pn = pns->nodes[0];
796 goto tail_recursion;
797 }
798 break;
799
800 case PN_atom_bracket:
801 // lhs is something in brackets
802 if (assign_kind != ASSIGN_STORE) {
803 goto bad_aug;
804 }
Damiend99b0522013-12-21 18:17:45 +0000805 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100806 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000807 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000808 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
809 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100810 goto testlist_comp;
811 } else {
812 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000813 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100814 }
815 break;
816
817 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100818 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
819 return;
Damien429d7192013-10-04 19:53:11 +0100820 }
821 return;
822
823 testlist_comp:
824 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000825 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
826 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
827 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100828 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000829 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000830 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000831 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100832 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000833 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
834 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000835 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100836 // TODO can we ever get here? can it be compiled?
837 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
838 return;
Damien429d7192013-10-04 19:53:11 +0100839 } else {
840 // sequence with 2 items
841 goto sequence_with_2_items;
842 }
843 } else {
844 // sequence with 2 items
845 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000846 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100847 }
848 return;
849 }
850 return;
851
852 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100853 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100854}
855
856// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100857// if we are not in CPython compatibility mode then:
858// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
859// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
860// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100861void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
862 assert(n_pos_defaults >= 0);
863 assert(n_kw_defaults >= 0);
864
Damien429d7192013-10-04 19:53:11 +0100865 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000866 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100867 int nfree = 0;
868 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000869 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
870 id_info_t *id = &comp->scope_cur->id_info[i];
871 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
872 for (int j = 0; j < this_scope->id_info_len; j++) {
873 id_info_t *id2 = &this_scope->id_info[j];
874 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000875#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000876 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000877#else
878 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100879 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000880#endif
Damien318aec62013-12-10 18:28:17 +0000881 nfree += 1;
882 }
883 }
Damien429d7192013-10-04 19:53:11 +0100884 }
885 }
886 }
Damien429d7192013-10-04 19:53:11 +0100887
888 // make the function/closure
889 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100890 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100891 } else {
Damien George3558f622014-04-20 17:50:40 +0100892 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100893 }
894}
895
Damiend99b0522013-12-21 18:17:45 +0000896void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000897 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100898 comp->have_star = true;
899 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000900 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
901 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100902 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100903 } else {
904 // named star
Damien429d7192013-10-04 19:53:11 +0100905 }
Damien George8b19db02014-04-11 23:25:34 +0100906 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000907
908 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100909 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000910 // TODO do we need to do anything with this?
911
912 } else {
913 mp_parse_node_t pn_id;
914 mp_parse_node_t pn_colon;
915 mp_parse_node_t pn_equal;
916 if (MP_PARSE_NODE_IS_ID(pn)) {
917 // this parameter is just an id
918
919 pn_id = pn;
920 pn_colon = MP_PARSE_NODE_NULL;
921 pn_equal = MP_PARSE_NODE_NULL;
922
923 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
924 // this parameter has a colon and/or equal specifier
925
926 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
927 pn_id = pns->nodes[0];
928 pn_colon = pns->nodes[1];
929 pn_equal = pns->nodes[2];
930
931 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100932 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000933 assert(0);
934 return;
935 }
936
937 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
938 // this parameter does not have a default value
939
940 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100941 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100942 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000943 return;
944 }
945
946 } else {
947 // this parameter has a default value
948 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
949
Damien George8b19db02014-04-11 23:25:34 +0100950 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000951 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100952#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000953 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
954 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100955 // in Micro Python we put the default positional parameters into a tuple using the bytecode
956 // we need to do this here before we start building the map for the default keywords
957 if (comp->num_default_params > 0) {
958 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100959 } else {
960 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100961 }
Damien George69b89d22014-04-11 13:38:30 +0000962 // first default dict param, so make the map
963 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000964 }
Damien George69b89d22014-04-11 13:38:30 +0000965#endif
966 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
967 compile_node(comp, pn_equal);
968#if !MICROPY_EMIT_CPYTHON
969 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
970 EMIT(store_map);
971#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000972 } else {
Damien George69b89d22014-04-11 13:38:30 +0000973 comp->num_default_params += 1;
974 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000975 }
976 }
977
978 // TODO pn_colon not implemented
979 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100980 }
981}
982
983// leaves function object on stack
984// returns function name
Damiend99b0522013-12-21 18:17:45 +0000985qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100986 if (comp->pass == PASS_1) {
987 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000988 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100989 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000990 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100991 }
992
993 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100994 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000995 uint old_num_dict_params = comp->num_dict_params;
996 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100997
998 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100999 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001000 comp->num_dict_params = 0;
1001 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001002 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001003
1004 if (comp->had_error) {
1005 return MP_QSTR_NULL;
1006 }
1007
Damien Georgee337f1e2014-03-31 15:18:37 +01001008#if !MICROPY_EMIT_CPYTHON
1009 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001010 // the default keywords args may have already made the tuple; if not, do it now
1011 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001012 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001013 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001014 }
1015#endif
1016
Damien429d7192013-10-04 19:53:11 +01001017 // get the scope for this function
1018 scope_t *fscope = (scope_t*)pns->nodes[4];
1019
1020 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001021 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001022
1023 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001024 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001025 comp->num_dict_params = old_num_dict_params;
1026 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001027
1028 // return its name (the 'f' in "def f(...):")
1029 return fscope->simple_name;
1030}
1031
1032// leaves class object on stack
1033// returns class name
Damiend99b0522013-12-21 18:17:45 +00001034qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001035 if (comp->pass == PASS_1) {
1036 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001037 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001038 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001039 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001040 }
1041
1042 EMIT(load_build_class);
1043
1044 // scope for this class
1045 scope_t *cscope = (scope_t*)pns->nodes[3];
1046
1047 // compile the class
1048 close_over_variables_etc(comp, cscope, 0, 0);
1049
1050 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001051 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001052
1053 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001054 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1055 mp_parse_node_t parents = pns->nodes[1];
1056 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1057 parents = MP_PARSE_NODE_NULL;
1058 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001059 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001060 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001061
1062 // return its name (the 'C' in class C(...):")
1063 return cscope->simple_name;
1064}
1065
Damien6cdd3af2013-10-05 18:08:26 +01001066// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001067STATIC 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 +00001068 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001069 return false;
1070 }
1071
1072 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001073 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001074 return true;
1075 }
1076
Damiend99b0522013-12-21 18:17:45 +00001077 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001078 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001079 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001080#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001081 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001082 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001083 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001084 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001085#endif
Damien3ef4abb2013-10-12 16:53:13 +01001086#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001087 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001088 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001089#endif
Damien6cdd3af2013-10-05 18:08:26 +01001090 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001091 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001092 }
1093
1094 return true;
1095}
1096
Damiend99b0522013-12-21 18:17:45 +00001097void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001098 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001099 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001100 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1101
Damien6cdd3af2013-10-05 18:08:26 +01001102 // inherit emit options for this function/class definition
1103 uint emit_options = comp->scope_cur->emit_options;
1104
1105 // compile each decorator
1106 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001107 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001108 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1109 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001110
1111 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001112 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001113 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1114
1115 // check for built-in decorators
1116 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1117 // this was a built-in
1118 num_built_in_decorators += 1;
1119
1120 } else {
1121 // not a built-in, compile normally
1122
1123 // compile the decorator function
1124 compile_node(comp, name_nodes[0]);
1125 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001126 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001127 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001128 }
1129
1130 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001131 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001132 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001133 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001134 compile_node(comp, pns_decorator->nodes[1]);
1135 }
Damien429d7192013-10-04 19:53:11 +01001136 }
1137 }
1138
1139 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001140 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001141 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001142 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001143 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001144 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001145 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001146 } else {
1147 // shouldn't happen
1148 assert(0);
1149 }
1150
1151 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001152 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001153 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001154 }
1155
1156 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001157 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001158}
1159
Damiend99b0522013-12-21 18:17:45 +00001160void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001161 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001162 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001163 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001164}
1165
Damiend99b0522013-12-21 18:17:45 +00001166void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1167 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001168 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001169 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1170 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001171
1172 compile_node(comp, pns->nodes[0]); // base of the power node
1173
Damiend99b0522013-12-21 18:17:45 +00001174 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1175 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1176 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1177 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001178 for (int i = 0; i < n - 1; i++) {
1179 compile_node(comp, pns1->nodes[i]);
1180 }
Damiend99b0522013-12-21 18:17:45 +00001181 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1182 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001183 }
Damiend99b0522013-12-21 18:17:45 +00001184 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001185 // can't delete function calls
1186 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001187 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001188 compile_node(comp, pns1->nodes[0]);
1189 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001190 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1191 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001192 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001193 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001194 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001195 }
1196 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001197 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001198 }
1199
Damiend99b0522013-12-21 18:17:45 +00001200 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001201 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001202 }
Damiend99b0522013-12-21 18:17:45 +00001203 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1204 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1205 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1206 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001207 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1208
Damiend99b0522013-12-21 18:17:45 +00001209 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1210 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1211 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001212 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001213 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001214 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001215 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001216 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001217 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001218 c_del_stmt(comp, pns->nodes[0]);
1219 for (int i = 0; i < n; i++) {
1220 c_del_stmt(comp, pns1->nodes[i]);
1221 }
Damiend99b0522013-12-21 18:17:45 +00001222 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001223 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001224 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001225 } else {
1226 // sequence with 2 items
1227 goto sequence_with_2_items;
1228 }
1229 } else {
1230 // sequence with 2 items
1231 sequence_with_2_items:
1232 c_del_stmt(comp, pns->nodes[0]);
1233 c_del_stmt(comp, pns->nodes[1]);
1234 }
1235 } else {
1236 // tuple with 1 element
1237 c_del_stmt(comp, pn);
1238 }
1239 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001240 // TODO is there anything else to implement?
1241 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001242 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001243
1244 return;
1245
1246cannot_delete:
1247 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001248}
1249
Damiend99b0522013-12-21 18:17:45 +00001250void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001251 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1252}
1253
Damiend99b0522013-12-21 18:17:45 +00001254void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001255 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001256 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001257 }
Damien Georgecbddb272014-02-01 20:08:18 +00001258 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001259}
1260
Damiend99b0522013-12-21 18:17:45 +00001261void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001262 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001263 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001264 }
Damien Georgecbddb272014-02-01 20:08:18 +00001265 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001266}
1267
Damiend99b0522013-12-21 18:17:45 +00001268void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001269 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001270 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001271 return;
1272 }
Damiend99b0522013-12-21 18:17:45 +00001273 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001274 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001276 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001277 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001278 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1279 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 +01001280
Damien George6f355fd2014-04-10 14:11:31 +01001281 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001282 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1283 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1284 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001285 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001286 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1287 } else {
1288 compile_node(comp, pns->nodes[0]);
1289 }
1290 EMIT(return_value);
1291}
1292
Damiend99b0522013-12-21 18:17:45 +00001293void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001294 compile_node(comp, pns->nodes[0]);
1295 EMIT(pop_top);
1296}
1297
Damiend99b0522013-12-21 18:17:45 +00001298void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1299 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001300 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001301 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001302 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001303 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001304 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001305 compile_node(comp, pns->nodes[0]);
1306 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001307 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001308 } else {
1309 // raise x
1310 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001311 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001312 }
1313}
1314
Damien George635543c2014-04-10 12:56:52 +01001315// q_base holds the base of the name
1316// eg a -> q_base=a
1317// a.b.c -> q_base=a
1318void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001319 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001320 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1321 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001322 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001323 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001324 pn = pns->nodes[0];
1325 is_as = true;
1326 }
Damien George635543c2014-04-10 12:56:52 +01001327 if (MP_PARSE_NODE_IS_NULL(pn)) {
1328 // empty name (eg, from . import x)
1329 *q_base = MP_QSTR_;
1330 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1331 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001332 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001333 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001334 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001335 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001336 }
Damien George635543c2014-04-10 12:56:52 +01001337 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001338 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1339 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1340 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001341 // a name of the form a.b.c
1342 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001343 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001344 }
Damiend99b0522013-12-21 18:17:45 +00001345 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001346 int len = n - 1;
1347 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001348 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001349 }
Damien George55baff42014-01-21 21:40:13 +00001350 byte *q_ptr;
1351 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001352 for (int i = 0; i < n; i++) {
1353 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001354 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001355 }
Damien George55baff42014-01-21 21:40:13 +00001356 uint str_src_len;
1357 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001358 memcpy(str_dest, str_src, str_src_len);
1359 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001360 }
Damien George635543c2014-04-10 12:56:52 +01001361 qstr q_full = qstr_build_end(q_ptr);
1362 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001363 if (is_as) {
1364 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001365 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001366 }
1367 }
1368 } else {
Damien George635543c2014-04-10 12:56:52 +01001369 // shouldn't happen
1370 assert(0);
Damien429d7192013-10-04 19:53:11 +01001371 }
1372 } else {
Damien George635543c2014-04-10 12:56:52 +01001373 // shouldn't happen
1374 assert(0);
Damien429d7192013-10-04 19:53:11 +01001375 }
1376}
1377
Damiend99b0522013-12-21 18:17:45 +00001378void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001379 EMIT_ARG(load_const_small_int, 0); // level 0 import
1380 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1381 qstr q_base;
1382 do_import_name(comp, pn, &q_base);
1383 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001384}
1385
Damiend99b0522013-12-21 18:17:45 +00001386void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001387 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1388}
1389
Damiend99b0522013-12-21 18:17:45 +00001390void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001391 mp_parse_node_t pn_import_source = pns->nodes[0];
1392
1393 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1394 uint import_level = 0;
1395 do {
1396 mp_parse_node_t pn_rel;
1397 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)) {
1398 // This covers relative imports with dots only like "from .. import"
1399 pn_rel = pn_import_source;
1400 pn_import_source = MP_PARSE_NODE_NULL;
1401 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1402 // This covers relative imports starting with dot(s) like "from .foo import"
1403 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1404 pn_rel = pns_2b->nodes[0];
1405 pn_import_source = pns_2b->nodes[1];
1406 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1407 } else {
1408 // Not a relative import
1409 break;
1410 }
1411
1412 // get the list of . and/or ...'s
1413 mp_parse_node_t *nodes;
1414 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1415
1416 // count the total number of .'s
1417 for (int i = 0; i < n; i++) {
1418 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1419 import_level++;
1420 } else {
1421 // should be an MP_TOKEN_ELLIPSIS
1422 import_level += 3;
1423 }
1424 }
1425 } while (0);
1426
Damiend99b0522013-12-21 18:17:45 +00001427 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001428 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001429
1430 // build the "fromlist" tuple
1431#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001432 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001433#else
Damien Georgeb9791222014-01-23 00:34:21 +00001434 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1435 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001436#endif
1437
1438 // do the import
Damien George635543c2014-04-10 12:56:52 +01001439 qstr dummy_q;
1440 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001441 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001442
Damien429d7192013-10-04 19:53:11 +01001443 } else {
Damien George635543c2014-04-10 12:56:52 +01001444 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001445
1446 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001447 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001448 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001449#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001450 {
1451 vstr_t *vstr = vstr_new();
1452 vstr_printf(vstr, "(");
1453 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001454 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1455 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1456 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001457 if (i > 0) {
1458 vstr_printf(vstr, ", ");
1459 }
1460 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001461 uint len;
1462 const byte *str = qstr_data(id2, &len);
1463 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001464 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001465 }
Damien02f89412013-12-12 15:13:36 +00001466 if (n == 1) {
1467 vstr_printf(vstr, ",");
1468 }
1469 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001470 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001471 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001472 }
Damiendb4c3612013-12-10 17:27:24 +00001473#else
1474 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001475 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1476 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1477 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001478 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001479 }
Damien Georgeb9791222014-01-23 00:34:21 +00001480 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001481#endif
1482
1483 // do the import
Damien George635543c2014-04-10 12:56:52 +01001484 qstr dummy_q;
1485 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001486 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001487 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1488 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1489 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001490 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001491 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001493 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001495 }
1496 }
1497 EMIT(pop_top);
1498 }
1499}
1500
Damiend99b0522013-12-21 18:17:45 +00001501void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001502 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001503 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1504 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001505 } else {
Damiend99b0522013-12-21 18:17:45 +00001506 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1507 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001508 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001509 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001510 }
Damien429d7192013-10-04 19:53:11 +01001511 }
1512 }
1513}
1514
Damiend99b0522013-12-21 18:17:45 +00001515void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001516 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001517 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1518 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001519 } else {
Damiend99b0522013-12-21 18:17:45 +00001520 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1521 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001522 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001523 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001524 }
Damien429d7192013-10-04 19:53:11 +01001525 }
1526 }
1527}
1528
Damiend99b0522013-12-21 18:17:45 +00001529void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001530 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001531 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001532 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 +00001533 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001534 // assertion message
1535 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001536 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001537 }
Damien Georgeb9791222014-01-23 00:34:21 +00001538 EMIT_ARG(raise_varargs, 1);
1539 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001540}
1541
Damiend99b0522013-12-21 18:17:45 +00001542void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001543 // TODO proper and/or short circuiting
1544
Damien George6f355fd2014-04-10 14:11:31 +01001545 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001546
Damien George6f355fd2014-04-10 14:11:31 +01001547 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001548 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1549
1550 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001551
1552 if (
1553#if !MICROPY_EMIT_CPYTHON
1554 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1555 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1556#endif
1557 // optimisation to not jump if last instruction was return
1558 !EMIT(last_emit_was_return_value)
1559 ) {
1560 // jump over elif/else blocks
1561 EMIT_ARG(jump, l_end);
1562 }
1563
Damien Georgeb9791222014-01-23 00:34:21 +00001564 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001565
Damiend99b0522013-12-21 18:17:45 +00001566 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001567 // compile elif blocks
1568
Damiend99b0522013-12-21 18:17:45 +00001569 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001570
Damiend99b0522013-12-21 18:17:45 +00001571 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001572 // multiple elif blocks
1573
Damiend99b0522013-12-21 18:17:45 +00001574 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001575 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001576 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001577 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001578 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1579
1580 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001581 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001583 }
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001585 }
1586
1587 } else {
1588 // a single elif block
1589
Damienb05d7072013-10-05 13:37:10 +01001590 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001591 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1592
1593 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001594 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001596 }
Damien Georgeb9791222014-01-23 00:34:21 +00001597 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001598 }
1599 }
1600
1601 // compile else block
1602 compile_node(comp, pns->nodes[3]); // can be null
1603
Damien Georgeb9791222014-01-23 00:34:21 +00001604 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001605}
1606
Damien Georgecbddb272014-02-01 20:08:18 +00001607#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001608 uint old_break_label = comp->break_label; \
1609 uint old_continue_label = comp->continue_label; \
1610 uint break_label = comp_next_label(comp); \
1611 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001612 comp->break_label = break_label; \
1613 comp->continue_label = continue_label; \
1614 comp->break_continue_except_level = comp->cur_except_level;
1615
1616#define END_BREAK_CONTINUE_BLOCK \
1617 comp->break_label = old_break_label; \
1618 comp->continue_label = old_continue_label; \
1619 comp->break_continue_except_level = comp->cur_except_level;
1620
Damiend99b0522013-12-21 18:17:45 +00001621void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001622 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001623
Damience89a212013-10-15 22:25:17 +01001624 // compared to CPython, we have an optimised version of while loops
1625#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001626 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001627 EMIT_ARG(setup_loop, break_label);
1628 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001629 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1630 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001631 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001632 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001633 }
Damien Georgeb9791222014-01-23 00:34:21 +00001634 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001635 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1636 // this is a small hack to agree with CPython
1637 if (!node_is_const_true(pns->nodes[0])) {
1638 EMIT(pop_block);
1639 }
Damience89a212013-10-15 22:25:17 +01001640#else
Damien George6f355fd2014-04-10 14:11:31 +01001641 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001642 EMIT_ARG(jump, continue_label);
1643 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001644 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001645 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001646 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1647#endif
1648
1649 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001650 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001651
1652 compile_node(comp, pns->nodes[2]); // else
1653
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001655}
1656
Damienf72fd0e2013-11-06 20:20:49 +00001657// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001658// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1659// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001660void 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 +00001661 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001662
Damien George6f355fd2014-04-10 14:11:31 +01001663 uint top_label = comp_next_label(comp);
1664 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001665
Damien George3ff2d032014-03-31 18:02:22 +01001666 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001667 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001668 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001669
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(jump, entry_label);
1671 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001672
Damien George3ff2d032014-03-31 18:02:22 +01001673 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001674 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001675
1676 // store next value to var
1677 c_assign(comp, pn_var, ASSIGN_STORE);
1678
Damienf3822fc2013-11-09 20:12:03 +00001679 // compile body
1680 compile_node(comp, pn_body);
1681
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001683
Damien George3ff2d032014-03-31 18:02:22 +01001684 // compile: var + step, duplicated on stack
1685 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001686 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001687 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001688 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001689
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001691
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001692 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001693 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001694 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1695 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001696 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001697 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001698 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001699 }
Damien Georgeb9791222014-01-23 00:34:21 +00001700 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001701
Damien George3ff2d032014-03-31 18:02:22 +01001702 // discard final value of var that failed the loop condition
1703 EMIT(pop_top);
1704
Damienf72fd0e2013-11-06 20:20:49 +00001705 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001706 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001707
1708 compile_node(comp, pn_else);
1709
Damien Georgeb9791222014-01-23 00:34:21 +00001710 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001711}
1712
Damiend99b0522013-12-21 18:17:45 +00001713void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001714#if !MICROPY_EMIT_CPYTHON
1715 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1716 // this is actually slower, but uses no heap memory
1717 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001718 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 +00001719 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001720 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1721 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1722 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1723 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001724 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1725 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001726 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001727 mp_parse_node_t pn_range_start;
1728 mp_parse_node_t pn_range_end;
1729 mp_parse_node_t pn_range_step;
1730 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001731 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001732 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001733 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001734 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001735 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001736 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001737 } else if (n_args == 2) {
1738 pn_range_start = args[0];
1739 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001740 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001741 } else {
1742 pn_range_start = args[0];
1743 pn_range_end = args[1];
1744 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001745 // We need to know sign of step. This is possible only if it's constant
1746 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1747 optimize = false;
1748 }
Damienf72fd0e2013-11-06 20:20:49 +00001749 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001750 }
1751 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001752 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1753 return;
1754 }
1755 }
1756 }
1757#endif
1758
Damien Georgecbddb272014-02-01 20:08:18 +00001759 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001760
Damien George6f355fd2014-04-10 14:11:31 +01001761 uint pop_label = comp_next_label(comp);
1762 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001763
Damience89a212013-10-15 22:25:17 +01001764 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1765#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001766 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001767#endif
1768
Damien429d7192013-10-04 19:53:11 +01001769 compile_node(comp, pns->nodes[1]); // iterator
1770 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001771 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001772 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001773 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1774 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001775 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001776 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001777 }
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001779 EMIT(for_iter_end);
1780
1781 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001782 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001783
Damience89a212013-10-15 22:25:17 +01001784#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001785 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001786#endif
Damien429d7192013-10-04 19:53:11 +01001787
1788 compile_node(comp, pns->nodes[3]); // else (not tested)
1789
Damien Georgeb9791222014-01-23 00:34:21 +00001790 EMIT_ARG(label_assign, break_label);
1791 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001792}
1793
Damiend99b0522013-12-21 18:17:45 +00001794void 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 +01001795 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001796 uint l1 = comp_next_label(comp);
1797 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001798
Damien Georgeb9791222014-01-23 00:34:21 +00001799 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001800 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001801
Damien429d7192013-10-04 19:53:11 +01001802 compile_node(comp, pn_body); // body
1803 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001804 EMIT_ARG(jump, success_label); // jump over exception handler
1805
1806 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001807 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 +00001808
Damien George6f355fd2014-04-10 14:11:31 +01001809 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001810
1811 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001812 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1813 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001814
1815 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001816 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001817
Damiend99b0522013-12-21 18:17:45 +00001818 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001819 // this is a catch all exception handler
1820 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001821 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001822 return;
1823 }
1824 } else {
1825 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001826 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1827 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1828 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1829 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001830 // handler binds the exception to a local
1831 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001832 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001833 }
1834 }
1835 EMIT(dup_top);
1836 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001837 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001839 }
1840
1841 EMIT(pop_top);
1842
1843 if (qstr_exception_local == 0) {
1844 EMIT(pop_top);
1845 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001846 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001847 }
1848
1849 EMIT(pop_top);
1850
Damien George6f355fd2014-04-10 14:11:31 +01001851 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001852 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001853 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001854 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001855 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001856 }
1857 compile_node(comp, pns_except->nodes[1]);
1858 if (qstr_exception_local != 0) {
1859 EMIT(pop_block);
1860 }
1861 EMIT(pop_except);
1862 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001863 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1864 EMIT_ARG(label_assign, l3);
1865 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1866 EMIT_ARG(store_id, qstr_exception_local);
1867 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001868
Damien George8dcc0c72014-03-27 10:55:21 +00001869 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001870 EMIT(end_finally);
1871 }
Damien Georgeb9791222014-01-23 00:34:21 +00001872 EMIT_ARG(jump, l2);
1873 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001874 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001875 }
1876
Damien George8dcc0c72014-03-27 10:55:21 +00001877 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001878 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001879 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001880
Damien Georgeb9791222014-01-23 00:34:21 +00001881 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001882 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001883 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001884}
1885
Damiend99b0522013-12-21 18:17:45 +00001886void 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 +01001887 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001888
Damien Georgeb9791222014-01-23 00:34:21 +00001889 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001890 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001891
Damien429d7192013-10-04 19:53:11 +01001892 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001893 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001894 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001895 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001896 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001897 } else {
1898 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1899 }
1900 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001901 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1902 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001903 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001904
Damien George8dcc0c72014-03-27 10:55:21 +00001905 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001906 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001907}
1908
Damiend99b0522013-12-21 18:17:45 +00001909void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1910 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1911 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1912 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001913 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001914 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1915 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001916 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001917 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001918 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001919 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001920 // no finally
1921 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1922 } else {
1923 // have finally
Damiend99b0522013-12-21 18:17:45 +00001924 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 +01001925 }
1926 } else {
1927 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001928 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001929 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001930 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001931 }
1932 } else {
1933 // shouldn't happen
1934 assert(0);
1935 }
1936}
1937
Damiend99b0522013-12-21 18:17:45 +00001938void 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 +01001939 if (n == 0) {
1940 // no more pre-bits, compile the body of the with
1941 compile_node(comp, body);
1942 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001943 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001944 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001945 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001946 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001947 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001948 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001949 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1950 } else {
1951 // this pre-bit is just an expression
1952 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001953 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001954 EMIT(pop_top);
1955 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001956 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001957 // compile additional pre-bits and the body
1958 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1959 // finish this with block
1960 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001961 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1962 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001964 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001965 EMIT(end_finally);
1966 }
1967}
1968
Damiend99b0522013-12-21 18:17:45 +00001969void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001970 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001971 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001972 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1973 assert(n > 0);
1974
1975 // compile in a nested fashion
1976 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1977}
1978
Damiend99b0522013-12-21 18:17:45 +00001979void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1980 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001981 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1982 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001983 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001984 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001985 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001986 EMIT(pop_top);
1987
Damien429d7192013-10-04 19:53:11 +01001988 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001989 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001990 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001991 // do nothing with a lonely constant
1992 } else {
1993 compile_node(comp, pns->nodes[0]); // just an expression
1994 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1995 }
Damien429d7192013-10-04 19:53:11 +01001996 }
1997 } else {
Damiend99b0522013-12-21 18:17:45 +00001998 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1999 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002000 if (kind == PN_expr_stmt_augassign) {
2001 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2002 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002003 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002004 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002005 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002006 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2007 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2008 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2009 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2010 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2011 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2012 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2013 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2014 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2015 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2016 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2017 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2018 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002019 }
Damien George7e5fb242014-02-01 22:18:47 +00002020 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002021 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2022 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002023 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2024 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002025 // following CPython, we store left-most first
2026 if (rhs > 0) {
2027 EMIT(dup_top);
2028 }
2029 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2030 for (int i = 0; i < rhs; i++) {
2031 if (i + 1 < rhs) {
2032 EMIT(dup_top);
2033 }
Damiend99b0522013-12-21 18:17:45 +00002034 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002035 }
2036 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002037 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2038 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2039 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2040 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002041 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002042 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2043 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002044 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2045 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2046 // can't optimise when it's a star expression on the lhs
2047 goto no_optimisation;
2048 }
Damien429d7192013-10-04 19:53:11 +01002049 compile_node(comp, pns10->nodes[0]); // rhs
2050 compile_node(comp, pns10->nodes[1]); // rhs
2051 EMIT(rot_two);
2052 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2053 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002054 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2055 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2056 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2057 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002058 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002059 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2060 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002061 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2062 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2063 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2064 // can't optimise when it's a star expression on the lhs
2065 goto no_optimisation;
2066 }
Damien429d7192013-10-04 19:53:11 +01002067 compile_node(comp, pns10->nodes[0]); // rhs
2068 compile_node(comp, pns10->nodes[1]); // rhs
2069 compile_node(comp, pns10->nodes[2]); // rhs
2070 EMIT(rot_three);
2071 EMIT(rot_two);
2072 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2073 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2074 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2075 } else {
Damien George495d7812014-04-08 17:51:47 +01002076 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002077 compile_node(comp, pns1->nodes[0]); // rhs
2078 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2079 }
2080 } else {
2081 // shouldn't happen
2082 assert(0);
2083 }
2084 }
2085}
2086
Damien Georged17926d2014-03-30 13:35:08 +01002087void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002088 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pns->nodes[0]);
2090 for (int i = 1; i < num_nodes; i += 1) {
2091 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002092 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002093 }
2094}
2095
Damiend99b0522013-12-21 18:17:45 +00002096void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2097 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2098 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002099
Damien George6f355fd2014-04-10 14:11:31 +01002100 uint l_fail = comp_next_label(comp);
2101 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002102 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2103 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002104 EMIT_ARG(jump, l_end);
2105 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002106 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002107 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002108 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damiend99b0522013-12-21 18:17:45 +00002111void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002112 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002113 //mp_parse_node_t pn_params = pns->nodes[0];
2114 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002115
2116 if (comp->pass == PASS_1) {
2117 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002118 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 +01002119 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002120 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002121 }
2122
2123 // get the scope for this lambda
2124 scope_t *this_scope = (scope_t*)pns->nodes[2];
2125
2126 // make the lambda
2127 close_over_variables_etc(comp, this_scope, 0, 0);
2128}
2129
Damiend99b0522013-12-21 18:17:45 +00002130void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002131 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002132 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002133 for (int i = 0; i < n; i += 1) {
2134 compile_node(comp, pns->nodes[i]);
2135 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002136 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002137 }
2138 }
Damien Georgeb9791222014-01-23 00:34:21 +00002139 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002140}
2141
Damiend99b0522013-12-21 18:17:45 +00002142void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002143 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002144 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002145 for (int i = 0; i < n; i += 1) {
2146 compile_node(comp, pns->nodes[i]);
2147 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002148 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002149 }
2150 }
Damien Georgeb9791222014-01-23 00:34:21 +00002151 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002152}
2153
Damiend99b0522013-12-21 18:17:45 +00002154void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002155 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002156 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002157}
2158
Damiend99b0522013-12-21 18:17:45 +00002159void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002160 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002161 compile_node(comp, pns->nodes[0]);
2162 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002163 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002164 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002165 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002166 }
2167 for (int i = 1; i + 1 < num_nodes; i += 2) {
2168 compile_node(comp, pns->nodes[i + 1]);
2169 if (i + 2 < num_nodes) {
2170 EMIT(dup_top);
2171 EMIT(rot_three);
2172 }
Damien George7e5fb242014-02-01 22:18:47 +00002173 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002175 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002176 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2177 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2178 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2179 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2180 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2181 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2182 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2183 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002184 }
2185 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002186 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2187 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2188 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002189 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002190 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002191 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002192 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002193 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002194 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002195 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002196 }
2197 } else {
2198 // shouldn't happen
2199 assert(0);
2200 }
2201 } else {
2202 // shouldn't happen
2203 assert(0);
2204 }
2205 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002206 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002207 }
2208 }
2209 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002210 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002211 EMIT_ARG(jump, l_end);
2212 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002213 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002214 EMIT(rot_two);
2215 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002216 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002217 }
2218}
2219
Damiend99b0522013-12-21 18:17:45 +00002220void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002221 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002222}
2223
Damiend99b0522013-12-21 18:17:45 +00002224void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002225 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002226}
2227
Damiend99b0522013-12-21 18:17:45 +00002228void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002229 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002230}
2231
Damiend99b0522013-12-21 18:17:45 +00002232void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002233 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002234}
2235
Damiend99b0522013-12-21 18:17:45 +00002236void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2237 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002238 compile_node(comp, pns->nodes[0]);
2239 for (int i = 1; i + 1 < num_nodes; i += 2) {
2240 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002241 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002242 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002243 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002244 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002245 } else {
2246 // shouldn't happen
2247 assert(0);
2248 }
2249 }
2250}
2251
Damiend99b0522013-12-21 18:17:45 +00002252void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2253 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002254 compile_node(comp, pns->nodes[0]);
2255 for (int i = 1; i + 1 < num_nodes; i += 2) {
2256 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002257 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002258 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002259 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002260 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002261 } else {
2262 // shouldn't happen
2263 assert(0);
2264 }
2265 }
2266}
2267
Damiend99b0522013-12-21 18:17:45 +00002268void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2269 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002270 compile_node(comp, pns->nodes[0]);
2271 for (int i = 1; i + 1 < num_nodes; i += 2) {
2272 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002273 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002274 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002275 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002276 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002278 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002279 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002280 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002281 } else {
2282 // shouldn't happen
2283 assert(0);
2284 }
2285 }
2286}
2287
Damiend99b0522013-12-21 18:17:45 +00002288void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002289 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002290 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002291 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002293 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002294 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002295 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002296 } else {
2297 // shouldn't happen
2298 assert(0);
2299 }
2300}
2301
Damien George35e2a4e2014-02-05 00:51:47 +00002302void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2303 // this is to handle special super() call
2304 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2305
2306 compile_generic_all_nodes(comp, pns);
2307}
2308
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002309STATIC 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 +01002310 // function to call is on top of stack
2311
Damien George35e2a4e2014-02-05 00:51:47 +00002312#if !MICROPY_EMIT_CPYTHON
2313 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002314 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 +00002315 EMIT_ARG(load_id, MP_QSTR___class__);
2316 // get first argument to function
2317 bool found = false;
2318 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002319 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2320 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 +00002321 found = true;
2322 break;
2323 }
2324 }
2325 if (!found) {
2326 printf("TypeError: super() call cannot find self\n");
2327 return;
2328 }
Damien George922ddd62014-04-09 12:43:17 +01002329 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002330 return;
2331 }
2332#endif
2333
Damien George02a4c052014-04-09 12:50:58 +01002334 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002335 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002336 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002337 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002338
Damien Georgebbcd49a2014-02-06 20:30:16 +00002339 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002340
2341 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002342 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002343 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002344 n_positional -= 1;
2345 }
Damien George922ddd62014-04-09 12:43:17 +01002346 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002347 n_positional -= 1;
2348 }
2349
2350 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002351 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002352 } else {
Damien George922ddd62014-04-09 12:43:17 +01002353 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002354 }
2355
2356 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002357 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002358}
2359
Damiend99b0522013-12-21 18:17:45 +00002360void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2361 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002362 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002363 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 +01002364 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002365 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2366 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002367 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002368 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002369 i += 1;
2370 } else {
2371 compile_node(comp, pns->nodes[i]);
2372 }
Damien George35e2a4e2014-02-05 00:51:47 +00002373 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002374 }
2375}
2376
Damiend99b0522013-12-21 18:17:45 +00002377void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002378 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002379 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002380}
2381
Damiend99b0522013-12-21 18:17:45 +00002382void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002383 // a list of strings
Damien63321742013-12-10 17:41:49 +00002384
2385 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002386 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002387 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002388 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002389 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002390 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2391 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2392 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002393 if (i == 0) {
2394 string_kind = pn_kind;
2395 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002396 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002397 return;
2398 }
Damien George55baff42014-01-21 21:40:13 +00002399 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002400 }
Damien63321742013-12-10 17:41:49 +00002401
Damien63321742013-12-10 17:41:49 +00002402 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002403 byte *q_ptr;
2404 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002405 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002406 uint s_len;
2407 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002408 memcpy(s_dest, s, s_len);
2409 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002410 }
Damien George55baff42014-01-21 21:40:13 +00002411 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002412
Damien Georgeb9791222014-01-23 00:34:21 +00002413 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002414}
2415
2416// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002417void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2418 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2419 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2420 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002421
2422 if (comp->pass == PASS_1) {
2423 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002424 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 +01002425 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002426 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002427 }
2428
2429 // get the scope for this comprehension
2430 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2431
2432 // compile the comprehension
2433 close_over_variables_etc(comp, this_scope, 0, 0);
2434
2435 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2436 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002437 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2441 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002442 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002443 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2444 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2445 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2446 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2447 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2448 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2449 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002450 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002451 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002452 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002453 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002454 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002455 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002456 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002457 // generator expression
2458 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2459 } else {
2460 // tuple with 2 items
2461 goto tuple_with_2_items;
2462 }
2463 } else {
2464 // tuple with 2 items
2465 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002466 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002467 }
2468 } else {
2469 // parenthesis around a single item, is just that item
2470 compile_node(comp, pns->nodes[0]);
2471 }
2472}
2473
Damiend99b0522013-12-21 18:17:45 +00002474void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2475 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002476 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002478 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2479 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2480 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2481 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2482 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002483 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002484 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002485 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002486 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002487 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002488 // list of many items
2489 compile_node(comp, pns2->nodes[0]);
2490 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002491 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002492 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002493 // list comprehension
2494 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2495 } else {
2496 // list with 2 items
2497 goto list_with_2_items;
2498 }
2499 } else {
2500 // list with 2 items
2501 list_with_2_items:
2502 compile_node(comp, pns2->nodes[0]);
2503 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002504 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002505 }
2506 } else {
2507 // list with 1 item
2508 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002509 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002510 }
2511}
2512
Damiend99b0522013-12-21 18:17:45 +00002513void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2514 mp_parse_node_t pn = pns->nodes[0];
2515 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002516 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002517 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002518 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2519 pns = (mp_parse_node_struct_t*)pn;
2520 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002521 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002522 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002523 compile_node(comp, pn);
2524 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002525 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2526 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2527 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2528 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002529 // dict/set with multiple elements
2530
2531 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002532 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002533 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2534
2535 // first element sets whether it's a dict or set
2536 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002537 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002538 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002539 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002540 compile_node(comp, pns->nodes[0]);
2541 EMIT(store_map);
2542 is_dict = true;
2543 } else {
2544 // a set
2545 compile_node(comp, pns->nodes[0]); // 1st value of set
2546 is_dict = false;
2547 }
2548
2549 // process rest of elements
2550 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002551 mp_parse_node_t pn = nodes[i];
2552 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002553 compile_node(comp, pn);
2554 if (is_dict) {
2555 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002556 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002557 return;
2558 }
2559 EMIT(store_map);
2560 } else {
2561 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002562 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002563 return;
2564 }
2565 }
2566 }
2567
2568 // if it's a set, build it
2569 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002570 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002571 }
Damiend99b0522013-12-21 18:17:45 +00002572 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002573 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002574 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002575 // a dictionary comprehension
2576 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2577 } else {
2578 // a set comprehension
2579 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2580 }
2581 } else {
2582 // shouldn't happen
2583 assert(0);
2584 }
2585 } else {
2586 // set with one element
2587 goto set_with_one_element;
2588 }
2589 } else {
2590 // set with one element
2591 set_with_one_element:
2592 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002593 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002594 }
2595}
2596
Damiend99b0522013-12-21 18:17:45 +00002597void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002598 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002599}
2600
Damiend99b0522013-12-21 18:17:45 +00002601void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002602 // object who's index we want is on top of stack
2603 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002604 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002605}
2606
Damiend99b0522013-12-21 18:17:45 +00002607void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002608 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002609 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002610}
2611
Damiend99b0522013-12-21 18:17:45 +00002612void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2613 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2614 mp_parse_node_t pn = pns->nodes[0];
2615 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002616 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002617 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2618 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002619 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2620 pns = (mp_parse_node_struct_t*)pn;
2621 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002622 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002623 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002624 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002625 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002626 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002627 } else {
2628 // [?::x]
2629 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002630 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002631 }
Damiend99b0522013-12-21 18:17:45 +00002632 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002633 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002634 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2635 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2636 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2637 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002638 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002639 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002640 } else {
2641 // [?:x:x]
2642 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002643 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002644 }
2645 } else {
2646 // [?:x]
2647 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002648 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002649 }
2650 } else {
2651 // [?:x]
2652 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002653 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002654 }
2655}
2656
Damiend99b0522013-12-21 18:17:45 +00002657void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002658 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002659 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2660 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002661}
2662
Damiend99b0522013-12-21 18:17:45 +00002663void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002664 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002665 compile_subscript_3_helper(comp, pns);
2666}
2667
Damiend99b0522013-12-21 18:17:45 +00002668void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002669 // if this is called then we are compiling a dict key:value pair
2670 compile_node(comp, pns->nodes[1]); // value
2671 compile_node(comp, pns->nodes[0]); // key
2672}
2673
Damiend99b0522013-12-21 18:17:45 +00002674void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002675 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002676 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002677 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002678}
2679
Damiend99b0522013-12-21 18:17:45 +00002680void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002681 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002682 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002683 return;
2684 }
Damien George922ddd62014-04-09 12:43:17 +01002685 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002686 compile_node(comp, pns->nodes[0]);
2687}
2688
Damiend99b0522013-12-21 18:17:45 +00002689void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002690 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002691 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002692 return;
2693 }
Damien George922ddd62014-04-09 12:43:17 +01002694 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002695 compile_node(comp, pns->nodes[0]);
2696}
2697
Damiend99b0522013-12-21 18:17:45 +00002698void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2699 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2700 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2701 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2702 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002703 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 +01002704 return;
2705 }
Damien Georgeb9791222014-01-23 00:34:21 +00002706 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002707 compile_node(comp, pns2->nodes[0]);
2708 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002709 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002710 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2711 } else {
2712 // shouldn't happen
2713 assert(0);
2714 }
2715}
2716
Damiend99b0522013-12-21 18:17:45 +00002717void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002718 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002719 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002720 return;
2721 }
Damiend99b0522013-12-21 18:17:45 +00002722 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002724 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002725 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2726 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002727 compile_node(comp, pns->nodes[0]);
2728 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002729 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002730 EMIT(yield_from);
2731 } else {
2732 compile_node(comp, pns->nodes[0]);
2733 EMIT(yield_value);
2734 }
2735}
2736
Damiend99b0522013-12-21 18:17:45 +00002737typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002738STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002739 NULL,
2740#define nc NULL
2741#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002742#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002743#include "grammar.h"
2744#undef nc
2745#undef c
2746#undef DEF_RULE
2747};
2748
Damiend99b0522013-12-21 18:17:45 +00002749void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2750 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002751 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002752 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2753 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2754 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002755 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002756 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002757 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002758 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002759 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2760 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2761 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2762 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002763 case MP_PARSE_NODE_TOKEN:
2764 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002765 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002766 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002767 // do nothing
2768 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002769 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002770 }
2771 break;
Damien429d7192013-10-04 19:53:11 +01002772 default: assert(0);
2773 }
2774 } else {
Damiend99b0522013-12-21 18:17:45 +00002775 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002776 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002777 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002778 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002779 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002780#if MICROPY_DEBUG_PRINTERS
2781 mp_parse_node_print(pn, 0);
2782#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002783 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002784 } else {
2785 f(comp, pns);
2786 }
2787 }
2788}
2789
Damiend99b0522013-12-21 18:17:45 +00002790void 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 +01002791 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002792 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002793 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2794 if (MP_PARSE_NODE_IS_ID(pn)) {
2795 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002796 if (comp->have_star) {
Damien429d7192013-10-04 19:53:11 +01002797 // comes after a bare star, so doesn't count as a parameter
2798 } else {
2799 comp->scope_cur->num_params += 1;
2800 }
Damienb14de212013-10-06 00:28:28 +01002801 } else {
Damiend99b0522013-12-21 18:17:45 +00002802 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2803 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2804 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2805 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002806 //int node_index = 1; unused
2807 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002808 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002809 // this parameter has an annotation
2810 pn_annotation = pns->nodes[1];
2811 }
2812 //node_index = 2; unused
2813 }
2814 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002815 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002816 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002817 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002818 comp->scope_cur->num_dict_params += 1;
2819 } else {
2820 comp->scope_cur->num_default_params += 1;
2821 }
2822 }
2823 */
Damien George8b19db02014-04-11 23:25:34 +01002824 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002825 // comes after a bare star, so doesn't count as a parameter
2826 } else {
2827 comp->scope_cur->num_params += 1;
2828 }
Damiend99b0522013-12-21 18:17:45 +00002829 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002830 comp->have_star = true;
Damiend99b0522013-12-21 18:17:45 +00002831 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002832 // bare star
2833 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002834 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002835 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002836 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002837 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002838 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2839 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002840 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002841 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002842 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2843 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002844 pn_annotation = pns->nodes[1];
2845 } else {
2846 // shouldn't happen
2847 assert(0);
2848 }
Damiend99b0522013-12-21 18:17:45 +00002849 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2850 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2851 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002852 // this parameter has an annotation
2853 pn_annotation = pns->nodes[1];
2854 }
Damien George8725f8f2014-02-15 19:33:11 +00002855 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002856 } else {
Damienb14de212013-10-06 00:28:28 +01002857 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002858 assert(0);
2859 }
Damien429d7192013-10-04 19:53:11 +01002860 }
2861
2862 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002863 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002864 // TODO this parameter has an annotation
2865 }
2866 bool added;
2867 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2868 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002869 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002870 return;
2871 }
Damien429d7192013-10-04 19:53:11 +01002872 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002873 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002874 }
2875}
2876
Damiend99b0522013-12-21 18:17:45 +00002877void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002878 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2879}
2880
Damiend99b0522013-12-21 18:17:45 +00002881void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002882 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2883}
2884
Damiend99b0522013-12-21 18:17:45 +00002885void 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 +01002886 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002887 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002888 // no more nested if/for; compile inner expression
2889 compile_node(comp, pn_inner_expr);
2890 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002892 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002894 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002896 } else {
2897 EMIT(yield_value);
2898 EMIT(pop_top);
2899 }
Damiend99b0522013-12-21 18:17:45 +00002900 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002901 // if condition
Damiend99b0522013-12-21 18:17:45 +00002902 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002903 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2904 pn_iter = pns_comp_if->nodes[1];
2905 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002906 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002907 // for loop
Damiend99b0522013-12-21 18:17:45 +00002908 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002909 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002910 uint l_end2 = comp_next_label(comp);
2911 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002912 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002913 EMIT_ARG(label_assign, l_top2);
2914 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002915 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2916 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 +00002917 EMIT_ARG(jump, l_top2);
2918 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002919 EMIT(for_iter_end);
2920 } else {
2921 // shouldn't happen
2922 assert(0);
2923 }
2924}
2925
Damiend99b0522013-12-21 18:17:45 +00002926void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002927 // see http://www.python.org/dev/peps/pep-0257/
2928
2929 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002930 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002931 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002932 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002933 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002934 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2935 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002936 for (int i = 0; i < num_nodes; i++) {
2937 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002938 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 +00002939 // not a newline, so this is the first statement; finish search
2940 break;
2941 }
2942 }
2943 // 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 +00002944 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002945 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002946 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002947 } else {
2948 return;
2949 }
2950
2951 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002952 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2953 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2954 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2955 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2956 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002957 compile_node(comp, pns->nodes[0]); // a doc string
2958 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002959 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002960 }
2961 }
2962 }
2963}
2964
2965void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2966 comp->pass = pass;
2967 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002968 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002969 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002970
2971 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002972 // reset maximum stack sizes in scope
2973 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002974 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002975 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002976 }
2977
Damien5ac1b2e2013-10-18 19:58:12 +01002978#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002979 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002980 scope_print_info(scope);
2981 }
Damien5ac1b2e2013-10-18 19:58:12 +01002982#endif
Damien429d7192013-10-04 19:53:11 +01002983
2984 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002985 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2986 assert(scope->kind == SCOPE_MODULE);
2987 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2988 compile_node(comp, pns->nodes[0]); // compile the expression
2989 EMIT(return_value);
2990 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002991 if (!comp->is_repl) {
2992 check_for_doc_string(comp, scope->pn);
2993 }
Damien429d7192013-10-04 19:53:11 +01002994 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002995 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002996 EMIT(return_value);
2997 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002998 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2999 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3000 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003001
3002 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003003 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003004 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003005 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003006 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3007 }
3008
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003009 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003010
3011 compile_node(comp, pns->nodes[3]); // 3 is function body
3012 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003013 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003014 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003015 EMIT(return_value);
3016 }
3017 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003018 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3019 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3020 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003021
3022 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003023 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003024 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003025 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003026 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3027 }
3028
3029 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003030
3031 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3032 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3033 EMIT(pop_top);
3034 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3035 }
Damien429d7192013-10-04 19:53:11 +01003036 EMIT(return_value);
3037 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3038 // a bit of a hack at the moment
3039
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_NUM_NODES(pns) == 2);
3043 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3044 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003045
Damien George55baff42014-01-21 21:40:13 +00003046 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003047 if (comp->pass == PASS_1) {
3048 bool added;
3049 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3050 assert(added);
3051 id_info->kind = ID_INFO_KIND_LOCAL;
3052 scope->num_params = 1;
3053 }
3054
3055 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003056 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003057 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003058 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003059 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003060 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003061 }
3062
Damien George6f355fd2014-04-10 14:11:31 +01003063 uint l_end = comp_next_label(comp);
3064 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003065 EMIT_ARG(load_id, qstr_arg);
3066 EMIT_ARG(label_assign, l_top);
3067 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003068 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3069 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003070 EMIT_ARG(jump, l_top);
3071 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003072 EMIT(for_iter_end);
3073
3074 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003075 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003076 }
3077 EMIT(return_value);
3078 } else {
3079 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003080 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3081 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3082 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003083
3084 if (comp->pass == PASS_1) {
3085 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003086 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003087 assert(added);
3088 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003089 }
3090
Damien Georgeb9791222014-01-23 00:34:21 +00003091 EMIT_ARG(load_id, MP_QSTR___name__);
3092 EMIT_ARG(store_id, MP_QSTR___module__);
3093 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3094 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003095
3096 check_for_doc_string(comp, pns->nodes[2]);
3097 compile_node(comp, pns->nodes[2]); // 2 is class body
3098
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003099 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003100 assert(id != NULL);
3101 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003102 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003103 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003104#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003105 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003106#else
Damien George2bf7c092014-04-09 15:26:46 +01003107 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003108#endif
Damien429d7192013-10-04 19:53:11 +01003109 }
3110 EMIT(return_value);
3111 }
3112
Damien415eb6f2013-10-05 12:19:06 +01003113 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003114
3115 // make sure we match all the exception levels
3116 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003117}
3118
Damien George094d4502014-04-02 17:31:27 +01003119#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003120void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3121 comp->pass = pass;
3122 comp->scope_cur = scope;
3123 comp->next_label = 1;
3124
3125 if (scope->kind != SCOPE_FUNCTION) {
3126 printf("Error: inline assembler must be a function\n");
3127 return;
3128 }
3129
Damiena2f2f7d2013-10-06 00:14:13 +01003130 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003131 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003132 }
3133
Damien826005c2013-10-05 23:17:28 +01003134 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003135 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3136 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3137 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003138
Damiend99b0522013-12-21 18:17:45 +00003139 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003140
Damiena2f2f7d2013-10-06 00:14:13 +01003141 // parameters are in pns->nodes[1]
3142 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003143 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003144 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003145 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003146 }
3147
Damiend99b0522013-12-21 18:17:45 +00003148 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003149
Damiend99b0522013-12-21 18:17:45 +00003150 mp_parse_node_t pn_body = pns->nodes[3]; // body
3151 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003152 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3153
Damien Georgecbd2f742014-01-19 11:48:48 +00003154 /*
Damien826005c2013-10-05 23:17:28 +01003155 if (comp->pass == PASS_3) {
3156 //printf("----\n");
3157 scope_print_info(scope);
3158 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003159 */
Damien826005c2013-10-05 23:17:28 +01003160
3161 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003162 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3163 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003164 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3165 // no instructions
3166 continue;
3167 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3168 // an instruction; fall through
3169 } else {
3170 // not an instruction; error
3171 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3172 return;
3173 }
Damiend99b0522013-12-21 18:17:45 +00003174 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3175 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3176 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3177 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3178 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3179 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3180 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3181 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3182 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3183 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003184 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3185
3186 // emit instructions
3187 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003188 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003189 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003190 return;
3191 }
Damien George6f355fd2014-04-10 14:11:31 +01003192 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003193 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003194 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003195 }
3196 } else {
3197 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003198 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003199 }
3200 }
3201 }
3202
3203 if (comp->pass > PASS_1) {
Damien Georgea26dc502014-04-12 17:54:52 +01003204 bool success = EMIT_INLINE_ASM(end_pass);
3205 if (!success) {
3206 comp->had_error = true;
3207 }
Damienb05d7072013-10-05 13:37:10 +01003208 }
Damien429d7192013-10-04 19:53:11 +01003209}
Damien George094d4502014-04-02 17:31:27 +01003210#endif
Damien429d7192013-10-04 19:53:11 +01003211
3212void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3213 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003214 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003215 scope->num_locals = 0;
3216 for (int i = 0; i < scope->id_info_len; i++) {
3217 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003218 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003219 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3220 continue;
3221 }
3222 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3223 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3224 }
Damien9ecbcff2013-12-11 00:41:43 +00003225 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003226 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003227 id->local_num = scope->num_locals;
3228 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003229 }
3230 }
3231
3232 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003233#if MICROPY_EMIT_CPYTHON
3234 int num_cell = 0;
3235#endif
Damien9ecbcff2013-12-11 00:41:43 +00003236 for (int i = 0; i < scope->id_info_len; i++) {
3237 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003238#if MICROPY_EMIT_CPYTHON
3239 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003240 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003241 id->local_num = num_cell;
3242 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003243 }
Damien George6baf76e2013-12-30 22:32:17 +00003244#else
3245 // in Micro Python the cells come right after the fast locals
3246 // parameters are not counted here, since they remain at the start
3247 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003248 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003249 id->local_num = scope->num_locals;
3250 scope->num_locals += 1;
3251 }
3252#endif
Damien9ecbcff2013-12-11 00:41:43 +00003253 }
Damien9ecbcff2013-12-11 00:41:43 +00003254
3255 // compute the index of free vars (freevars[idx] in CPython)
3256 // make sure they are in the order of the parent scope
3257 if (scope->parent != NULL) {
3258 int num_free = 0;
3259 for (int i = 0; i < scope->parent->id_info_len; i++) {
3260 id_info_t *id = &scope->parent->id_info[i];
3261 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3262 for (int j = 0; j < scope->id_info_len; j++) {
3263 id_info_t *id2 = &scope->id_info[j];
3264 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003265 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003266#if MICROPY_EMIT_CPYTHON
3267 // in CPython the frees are numbered after the cells
3268 id2->local_num = num_cell + num_free;
3269#else
3270 // in Micro Python the frees come first, before the params
3271 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003272#endif
3273 num_free += 1;
3274 }
3275 }
3276 }
Damien429d7192013-10-04 19:53:11 +01003277 }
Damien George6baf76e2013-12-30 22:32:17 +00003278#if !MICROPY_EMIT_CPYTHON
3279 // in Micro Python shift all other locals after the free locals
3280 if (num_free > 0) {
3281 for (int i = 0; i < scope->id_info_len; i++) {
3282 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003283 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003284 id->local_num += num_free;
3285 }
3286 }
3287 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3288 scope->num_locals += num_free;
3289 }
3290#endif
Damien429d7192013-10-04 19:53:11 +01003291 }
3292
Damien George8725f8f2014-02-15 19:33:11 +00003293 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003294
3295#if MICROPY_EMIT_CPYTHON
3296 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003297 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) {
3298 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003299 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003300 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003301 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 +00003302 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003303 }
3304 }
Damien George882b3632014-04-02 15:56:31 +01003305#endif
3306
Damien429d7192013-10-04 19:53:11 +01003307 int num_free = 0;
3308 for (int i = 0; i < scope->id_info_len; i++) {
3309 id_info_t *id = &scope->id_info[i];
3310 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3311 num_free += 1;
3312 }
3313 }
3314 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003315 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003316 }
3317}
3318
Damien George65cad122014-04-06 11:48:15 +01003319mp_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 +01003320 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003321 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003322 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003323
Damien826005c2013-10-05 23:17:28 +01003324 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003325 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003326
3327 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003328 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003329
Damien826005c2013-10-05 23:17:28 +01003330 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003331 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003332 comp->emit_method_table = &emit_pass1_method_table;
3333 comp->emit_inline_asm = NULL;
3334 comp->emit_inline_asm_method_table = NULL;
3335 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003336 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003337 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003338#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003339 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003340 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003341#endif
Damien826005c2013-10-05 23:17:28 +01003342 } else {
3343 compile_scope(comp, s, PASS_1);
3344 }
3345
3346 // update maximim number of labels needed
3347 if (comp->next_label > max_num_labels) {
3348 max_num_labels = comp->next_label;
3349 }
Damien429d7192013-10-04 19:53:11 +01003350 }
3351
Damien826005c2013-10-05 23:17:28 +01003352 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003353 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003354 compile_scope_compute_things(comp, s);
3355 }
3356
Damien826005c2013-10-05 23:17:28 +01003357 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003358 emit_pass1_free(comp->emit);
3359
Damien826005c2013-10-05 23:17:28 +01003360 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003361#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003362 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003363#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003364 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003365#endif
Damien3ef4abb2013-10-12 16:53:13 +01003366#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003367 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003368#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003369#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003370 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003371 if (false) {
3372 // dummy
3373
Damien3ef4abb2013-10-12 16:53:13 +01003374#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003375 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003376 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003377 if (emit_inline_thumb == NULL) {
3378 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3379 }
3380 comp->emit = NULL;
3381 comp->emit_method_table = NULL;
3382 comp->emit_inline_asm = emit_inline_thumb;
3383 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3384 compile_scope_inline_asm(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003385 if (!comp->had_error) {
3386 compile_scope_inline_asm(comp, s, PASS_3);
3387 }
Damienc025ebb2013-10-12 14:30:21 +01003388#endif
3389
Damien826005c2013-10-05 23:17:28 +01003390 } else {
Damienc025ebb2013-10-12 14:30:21 +01003391
3392 // choose the emit type
3393
Damien3ef4abb2013-10-12 16:53:13 +01003394#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003395 comp->emit = emit_cpython_new(max_num_labels);
3396 comp->emit_method_table = &emit_cpython_method_table;
3397#else
Damien826005c2013-10-05 23:17:28 +01003398 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003399
3400#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003401 case MP_EMIT_OPT_NATIVE_PYTHON:
3402 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003403#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003404 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003405 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003406 }
Damien13ed3a62013-10-08 09:05:10 +01003407 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003408#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003409 if (emit_native == NULL) {
3410 emit_native = emit_native_thumb_new(max_num_labels);
3411 }
3412 comp->emit_method_table = &emit_native_thumb_method_table;
3413#endif
3414 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003415 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003416 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003417#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003418
Damien826005c2013-10-05 23:17:28 +01003419 default:
3420 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003421 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003422 }
3423 comp->emit = emit_bc;
3424 comp->emit_method_table = &emit_bc_method_table;
3425 break;
3426 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003427#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003428
3429 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003430 compile_scope(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003431 if (!comp->had_error) {
3432 compile_scope(comp, s, PASS_3);
3433 }
Damien6cdd3af2013-10-05 18:08:26 +01003434 }
Damien429d7192013-10-04 19:53:11 +01003435 }
3436
Damien George41d02b62014-01-24 22:42:28 +00003437 // free the emitters
3438#if !MICROPY_EMIT_CPYTHON
3439 if (emit_bc != NULL) {
3440 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003441 }
Damien George41d02b62014-01-24 22:42:28 +00003442#if MICROPY_EMIT_NATIVE
3443 if (emit_native != NULL) {
3444#if MICROPY_EMIT_X64
3445 emit_native_x64_free(emit_native);
3446#elif MICROPY_EMIT_THUMB
3447 emit_native_thumb_free(emit_native);
3448#endif
3449 }
3450#endif
3451#if MICROPY_EMIT_INLINE_THUMB
3452 if (emit_inline_thumb != NULL) {
3453 emit_inline_thumb_free(emit_inline_thumb);
3454 }
3455#endif
3456#endif // !MICROPY_EMIT_CPYTHON
3457
3458 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003459 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003460 for (scope_t *s = module_scope; s;) {
3461 scope_t *next = s->next;
3462 scope_free(s);
3463 s = next;
3464 }
Damien5ac1b2e2013-10-18 19:58:12 +01003465
Damien George41d02b62014-01-24 22:42:28 +00003466 // free the compiler
3467 bool had_error = comp->had_error;
3468 m_del_obj(compiler_t, comp);
3469
Damien George1fb03172014-01-03 14:22:03 +00003470 if (had_error) {
3471 // TODO return a proper error message
3472 return mp_const_none;
3473 } else {
3474#if MICROPY_EMIT_CPYTHON
3475 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003476 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003477 return mp_const_true;
3478#else
3479 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003480 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003481#endif
3482 }
Damien429d7192013-10-04 19:53:11 +01003483}