blob: b0f1f9e00d0a9ca0c7c051542d5fa242ad4efc21 [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 Georgebdcbf0f2014-03-26 23:15:35 +0000892 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100893 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100894 }
895}
896
Damiend99b0522013-12-21 18:17:45 +0000897void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000898 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100899 comp->have_star = true;
900 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000901 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
902 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100903 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100904 } else {
905 // named star
Damien429d7192013-10-04 19:53:11 +0100906 }
Damien George8b19db02014-04-11 23:25:34 +0100907 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100910 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000911 // TODO do we need to do anything with this?
912
913 } else {
914 mp_parse_node_t pn_id;
915 mp_parse_node_t pn_colon;
916 mp_parse_node_t pn_equal;
917 if (MP_PARSE_NODE_IS_ID(pn)) {
918 // this parameter is just an id
919
920 pn_id = pn;
921 pn_colon = MP_PARSE_NODE_NULL;
922 pn_equal = MP_PARSE_NODE_NULL;
923
924 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
925 // this parameter has a colon and/or equal specifier
926
927 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
928 pn_id = pns->nodes[0];
929 pn_colon = pns->nodes[1];
930 pn_equal = pns->nodes[2];
931
932 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100933 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000934 assert(0);
935 return;
936 }
937
938 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
939 // this parameter does not have a default value
940
941 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100942 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100943 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000944 return;
945 }
946
947 } else {
948 // this parameter has a default value
949 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
950
Damien George8b19db02014-04-11 23:25:34 +0100951 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000952 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100953#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000954 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
955 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100956 // in Micro Python we put the default positional parameters into a tuple using the bytecode
957 // we need to do this here before we start building the map for the default keywords
958 if (comp->num_default_params > 0) {
959 EMIT_ARG(build_tuple, comp->num_default_params);
960 }
Damien George69b89d22014-04-11 13:38:30 +0000961 // first default dict param, so make the map
962 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000963 }
Damien George69b89d22014-04-11 13:38:30 +0000964#endif
965 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
966 compile_node(comp, pn_equal);
967#if !MICROPY_EMIT_CPYTHON
968 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
969 EMIT(store_map);
970#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000971 } else {
Damien George69b89d22014-04-11 13:38:30 +0000972 comp->num_default_params += 1;
973 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000974 }
975 }
976
977 // TODO pn_colon not implemented
978 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100979 }
980}
981
982// leaves function object on stack
983// returns function name
Damiend99b0522013-12-21 18:17:45 +0000984qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100985 if (comp->pass == PASS_1) {
986 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000987 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100988 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000989 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100990 }
991
992 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100993 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000994 uint old_num_dict_params = comp->num_dict_params;
995 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100996
997 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100998 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000999 comp->num_dict_params = 0;
1000 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001001 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001002
1003 if (comp->had_error) {
1004 return MP_QSTR_NULL;
1005 }
1006
Damien Georgee337f1e2014-03-31 15:18:37 +01001007#if !MICROPY_EMIT_CPYTHON
1008 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001009 // the default keywords args may have already made the tuple; if not, do it now
1010 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001011 EMIT_ARG(build_tuple, comp->num_default_params);
Damien Georgee337f1e2014-03-31 15:18:37 +01001012 }
1013#endif
1014
Damien429d7192013-10-04 19:53:11 +01001015 // get the scope for this function
1016 scope_t *fscope = (scope_t*)pns->nodes[4];
1017
1018 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001019 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001020
1021 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001022 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001023 comp->num_dict_params = old_num_dict_params;
1024 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001025
1026 // return its name (the 'f' in "def f(...):")
1027 return fscope->simple_name;
1028}
1029
1030// leaves class object on stack
1031// returns class name
Damiend99b0522013-12-21 18:17:45 +00001032qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001033 if (comp->pass == PASS_1) {
1034 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001035 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001036 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001037 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001038 }
1039
1040 EMIT(load_build_class);
1041
1042 // scope for this class
1043 scope_t *cscope = (scope_t*)pns->nodes[3];
1044
1045 // compile the class
1046 close_over_variables_etc(comp, cscope, 0, 0);
1047
1048 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001049 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001050
1051 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001052 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1053 mp_parse_node_t parents = pns->nodes[1];
1054 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1055 parents = MP_PARSE_NODE_NULL;
1056 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001057 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001058 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001059
1060 // return its name (the 'C' in class C(...):")
1061 return cscope->simple_name;
1062}
1063
Damien6cdd3af2013-10-05 18:08:26 +01001064// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001065STATIC 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 +00001066 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001067 return false;
1068 }
1069
1070 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001071 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001072 return true;
1073 }
1074
Damiend99b0522013-12-21 18:17:45 +00001075 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001076 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001077 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001078#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001079 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001080 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001081 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001082 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001083#endif
Damien3ef4abb2013-10-12 16:53:13 +01001084#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001085 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001086 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001087#endif
Damien6cdd3af2013-10-05 18:08:26 +01001088 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001089 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001090 }
1091
1092 return true;
1093}
1094
Damiend99b0522013-12-21 18:17:45 +00001095void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001096 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001097 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001098 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1099
Damien6cdd3af2013-10-05 18:08:26 +01001100 // inherit emit options for this function/class definition
1101 uint emit_options = comp->scope_cur->emit_options;
1102
1103 // compile each decorator
1104 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001105 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001106 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1107 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001108
1109 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001110 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001111 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1112
1113 // check for built-in decorators
1114 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1115 // this was a built-in
1116 num_built_in_decorators += 1;
1117
1118 } else {
1119 // not a built-in, compile normally
1120
1121 // compile the decorator function
1122 compile_node(comp, name_nodes[0]);
1123 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001124 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001125 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001126 }
1127
1128 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001129 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001130 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001131 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001132 compile_node(comp, pns_decorator->nodes[1]);
1133 }
Damien429d7192013-10-04 19:53:11 +01001134 }
1135 }
1136
1137 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001138 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001139 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001140 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001141 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001142 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001143 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001144 } else {
1145 // shouldn't happen
1146 assert(0);
1147 }
1148
1149 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001150 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001151 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001152 }
1153
1154 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001155 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001156}
1157
Damiend99b0522013-12-21 18:17:45 +00001158void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001159 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001160 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001161 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001162}
1163
Damiend99b0522013-12-21 18:17:45 +00001164void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1165 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001166 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001167 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1168 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001169
1170 compile_node(comp, pns->nodes[0]); // base of the power node
1171
Damiend99b0522013-12-21 18:17:45 +00001172 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1173 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1174 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1175 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001176 for (int i = 0; i < n - 1; i++) {
1177 compile_node(comp, pns1->nodes[i]);
1178 }
Damiend99b0522013-12-21 18:17:45 +00001179 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1180 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001181 }
Damiend99b0522013-12-21 18:17:45 +00001182 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001183 // can't delete function calls
1184 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001185 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001186 compile_node(comp, pns1->nodes[0]);
1187 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001188 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1189 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001190 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001191 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001192 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001193 }
1194 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001195 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001196 }
1197
Damiend99b0522013-12-21 18:17:45 +00001198 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001199 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001200 }
Damiend99b0522013-12-21 18:17:45 +00001201 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1202 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1203 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1204 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001205 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1206
Damiend99b0522013-12-21 18:17:45 +00001207 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1208 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1209 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001210 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001211 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001212 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001213 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001214 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001215 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001216 c_del_stmt(comp, pns->nodes[0]);
1217 for (int i = 0; i < n; i++) {
1218 c_del_stmt(comp, pns1->nodes[i]);
1219 }
Damiend99b0522013-12-21 18:17:45 +00001220 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001221 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001222 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001223 } else {
1224 // sequence with 2 items
1225 goto sequence_with_2_items;
1226 }
1227 } else {
1228 // sequence with 2 items
1229 sequence_with_2_items:
1230 c_del_stmt(comp, pns->nodes[0]);
1231 c_del_stmt(comp, pns->nodes[1]);
1232 }
1233 } else {
1234 // tuple with 1 element
1235 c_del_stmt(comp, pn);
1236 }
1237 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001238 // TODO is there anything else to implement?
1239 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001240 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001241
1242 return;
1243
1244cannot_delete:
1245 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001246}
1247
Damiend99b0522013-12-21 18:17:45 +00001248void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001249 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1250}
1251
Damiend99b0522013-12-21 18:17:45 +00001252void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001253 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001254 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001255 }
Damien Georgecbddb272014-02-01 20:08:18 +00001256 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001257}
1258
Damiend99b0522013-12-21 18:17:45 +00001259void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001260 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001261 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001262 }
Damien Georgecbddb272014-02-01 20:08:18 +00001263 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001264}
1265
Damiend99b0522013-12-21 18:17:45 +00001266void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001267 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001268 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001269 return;
1270 }
Damiend99b0522013-12-21 18:17:45 +00001271 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001272 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001273 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001274 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001275 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001276 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1277 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 +01001278
Damien George6f355fd2014-04-10 14:11:31 +01001279 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001280 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1281 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1282 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001283 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001284 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1285 } else {
1286 compile_node(comp, pns->nodes[0]);
1287 }
1288 EMIT(return_value);
1289}
1290
Damiend99b0522013-12-21 18:17:45 +00001291void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001292 compile_node(comp, pns->nodes[0]);
1293 EMIT(pop_top);
1294}
1295
Damiend99b0522013-12-21 18:17:45 +00001296void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1297 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001298 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001299 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001300 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001301 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001302 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001303 compile_node(comp, pns->nodes[0]);
1304 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001305 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001306 } else {
1307 // raise x
1308 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001309 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001310 }
1311}
1312
Damien George635543c2014-04-10 12:56:52 +01001313// q_base holds the base of the name
1314// eg a -> q_base=a
1315// a.b.c -> q_base=a
1316void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001317 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001318 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1319 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001320 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001321 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001322 pn = pns->nodes[0];
1323 is_as = true;
1324 }
Damien George635543c2014-04-10 12:56:52 +01001325 if (MP_PARSE_NODE_IS_NULL(pn)) {
1326 // empty name (eg, from . import x)
1327 *q_base = MP_QSTR_;
1328 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1329 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001330 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001331 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001332 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001333 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001334 }
Damien George635543c2014-04-10 12:56:52 +01001335 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001336 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1337 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1338 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001339 // a name of the form a.b.c
1340 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001341 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001342 }
Damiend99b0522013-12-21 18:17:45 +00001343 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001344 int len = n - 1;
1345 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001346 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001347 }
Damien George55baff42014-01-21 21:40:13 +00001348 byte *q_ptr;
1349 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001350 for (int i = 0; i < n; i++) {
1351 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001352 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001353 }
Damien George55baff42014-01-21 21:40:13 +00001354 uint str_src_len;
1355 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001356 memcpy(str_dest, str_src, str_src_len);
1357 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001358 }
Damien George635543c2014-04-10 12:56:52 +01001359 qstr q_full = qstr_build_end(q_ptr);
1360 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001361 if (is_as) {
1362 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001363 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001364 }
1365 }
1366 } else {
Damien George635543c2014-04-10 12:56:52 +01001367 // shouldn't happen
1368 assert(0);
Damien429d7192013-10-04 19:53:11 +01001369 }
1370 } else {
Damien George635543c2014-04-10 12:56:52 +01001371 // shouldn't happen
1372 assert(0);
Damien429d7192013-10-04 19:53:11 +01001373 }
1374}
1375
Damiend99b0522013-12-21 18:17:45 +00001376void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001377 EMIT_ARG(load_const_small_int, 0); // level 0 import
1378 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1379 qstr q_base;
1380 do_import_name(comp, pn, &q_base);
1381 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001382}
1383
Damiend99b0522013-12-21 18:17:45 +00001384void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001385 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1386}
1387
Damiend99b0522013-12-21 18:17:45 +00001388void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001389 mp_parse_node_t pn_import_source = pns->nodes[0];
1390
1391 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1392 uint import_level = 0;
1393 do {
1394 mp_parse_node_t pn_rel;
1395 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)) {
1396 // This covers relative imports with dots only like "from .. import"
1397 pn_rel = pn_import_source;
1398 pn_import_source = MP_PARSE_NODE_NULL;
1399 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1400 // This covers relative imports starting with dot(s) like "from .foo import"
1401 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1402 pn_rel = pns_2b->nodes[0];
1403 pn_import_source = pns_2b->nodes[1];
1404 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1405 } else {
1406 // Not a relative import
1407 break;
1408 }
1409
1410 // get the list of . and/or ...'s
1411 mp_parse_node_t *nodes;
1412 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1413
1414 // count the total number of .'s
1415 for (int i = 0; i < n; i++) {
1416 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1417 import_level++;
1418 } else {
1419 // should be an MP_TOKEN_ELLIPSIS
1420 import_level += 3;
1421 }
1422 }
1423 } while (0);
1424
Damiend99b0522013-12-21 18:17:45 +00001425 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001426 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001427
1428 // build the "fromlist" tuple
1429#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001430 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001431#else
Damien Georgeb9791222014-01-23 00:34:21 +00001432 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1433 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001434#endif
1435
1436 // do the import
Damien George635543c2014-04-10 12:56:52 +01001437 qstr dummy_q;
1438 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001439 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001440
Damien429d7192013-10-04 19:53:11 +01001441 } else {
Damien George635543c2014-04-10 12:56:52 +01001442 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001443
1444 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001445 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001446 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001447#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001448 {
1449 vstr_t *vstr = vstr_new();
1450 vstr_printf(vstr, "(");
1451 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001452 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1453 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1454 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001455 if (i > 0) {
1456 vstr_printf(vstr, ", ");
1457 }
1458 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001459 uint len;
1460 const byte *str = qstr_data(id2, &len);
1461 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001462 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien02f89412013-12-12 15:13:36 +00001464 if (n == 1) {
1465 vstr_printf(vstr, ",");
1466 }
1467 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001468 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001469 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001470 }
Damiendb4c3612013-12-10 17:27:24 +00001471#else
1472 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001473 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1474 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1475 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001476 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001477 }
Damien Georgeb9791222014-01-23 00:34:21 +00001478 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001479#endif
1480
1481 // do the import
Damien George635543c2014-04-10 12:56:52 +01001482 qstr dummy_q;
1483 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001484 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001485 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1486 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1487 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001489 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001490 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001491 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001493 }
1494 }
1495 EMIT(pop_top);
1496 }
1497}
1498
Damiend99b0522013-12-21 18:17:45 +00001499void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001500 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001501 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1502 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001503 } else {
Damiend99b0522013-12-21 18:17:45 +00001504 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1505 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001506 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001507 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001508 }
Damien429d7192013-10-04 19:53:11 +01001509 }
1510 }
1511}
1512
Damiend99b0522013-12-21 18:17:45 +00001513void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001514 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001515 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1516 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001517 } else {
Damiend99b0522013-12-21 18:17:45 +00001518 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1519 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001520 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001521 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001522 }
Damien429d7192013-10-04 19:53:11 +01001523 }
1524 }
1525}
1526
Damiend99b0522013-12-21 18:17:45 +00001527void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001528 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001529 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001530 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 +00001531 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001532 // assertion message
1533 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001534 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001535 }
Damien Georgeb9791222014-01-23 00:34:21 +00001536 EMIT_ARG(raise_varargs, 1);
1537 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001538}
1539
Damiend99b0522013-12-21 18:17:45 +00001540void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001541 // TODO proper and/or short circuiting
1542
Damien George6f355fd2014-04-10 14:11:31 +01001543 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001544
Damien George6f355fd2014-04-10 14:11:31 +01001545 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001546 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1547
1548 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001549
1550 if (
1551#if !MICROPY_EMIT_CPYTHON
1552 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1553 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1554#endif
1555 // optimisation to not jump if last instruction was return
1556 !EMIT(last_emit_was_return_value)
1557 ) {
1558 // jump over elif/else blocks
1559 EMIT_ARG(jump, l_end);
1560 }
1561
Damien Georgeb9791222014-01-23 00:34:21 +00001562 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001563
Damiend99b0522013-12-21 18:17:45 +00001564 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001565 // compile elif blocks
1566
Damiend99b0522013-12-21 18:17:45 +00001567 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001568
Damiend99b0522013-12-21 18:17:45 +00001569 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001570 // multiple elif blocks
1571
Damiend99b0522013-12-21 18:17:45 +00001572 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001573 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001574 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001575 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001576 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1577
1578 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001579 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001581 }
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001583 }
1584
1585 } else {
1586 // a single elif block
1587
Damienb05d7072013-10-05 13:37:10 +01001588 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001589 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1590
1591 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001592 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001593 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001594 }
Damien Georgeb9791222014-01-23 00:34:21 +00001595 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001596 }
1597 }
1598
1599 // compile else block
1600 compile_node(comp, pns->nodes[3]); // can be null
1601
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001603}
1604
Damien Georgecbddb272014-02-01 20:08:18 +00001605#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001606 uint old_break_label = comp->break_label; \
1607 uint old_continue_label = comp->continue_label; \
1608 uint break_label = comp_next_label(comp); \
1609 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001610 comp->break_label = break_label; \
1611 comp->continue_label = continue_label; \
1612 comp->break_continue_except_level = comp->cur_except_level;
1613
1614#define END_BREAK_CONTINUE_BLOCK \
1615 comp->break_label = old_break_label; \
1616 comp->continue_label = old_continue_label; \
1617 comp->break_continue_except_level = comp->cur_except_level;
1618
Damiend99b0522013-12-21 18:17:45 +00001619void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001620 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001621
Damience89a212013-10-15 22:25:17 +01001622 // compared to CPython, we have an optimised version of while loops
1623#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001624 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001625 EMIT_ARG(setup_loop, break_label);
1626 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001627 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1628 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001629 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001630 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001631 }
Damien Georgeb9791222014-01-23 00:34:21 +00001632 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001633 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1634 // this is a small hack to agree with CPython
1635 if (!node_is_const_true(pns->nodes[0])) {
1636 EMIT(pop_block);
1637 }
Damience89a212013-10-15 22:25:17 +01001638#else
Damien George6f355fd2014-04-10 14:11:31 +01001639 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001640 EMIT_ARG(jump, continue_label);
1641 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001642 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001643 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001644 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1645#endif
1646
1647 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001648 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001649
1650 compile_node(comp, pns->nodes[2]); // else
1651
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001653}
1654
Damienf72fd0e2013-11-06 20:20:49 +00001655// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001656// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1657// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001658void 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 +00001659 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001660
Damien George6f355fd2014-04-10 14:11:31 +01001661 uint top_label = comp_next_label(comp);
1662 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001663
Damien George3ff2d032014-03-31 18:02:22 +01001664 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001665 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001666 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001667
Damien Georgeb9791222014-01-23 00:34:21 +00001668 EMIT_ARG(jump, entry_label);
1669 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001670
Damien George3ff2d032014-03-31 18:02:22 +01001671 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001672 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001673
1674 // store next value to var
1675 c_assign(comp, pn_var, ASSIGN_STORE);
1676
Damienf3822fc2013-11-09 20:12:03 +00001677 // compile body
1678 compile_node(comp, pn_body);
1679
Damien Georgeb9791222014-01-23 00:34:21 +00001680 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001681
Damien George3ff2d032014-03-31 18:02:22 +01001682 // compile: var + step, duplicated on stack
1683 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001684 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001685 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001686 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001687
Damien Georgeb9791222014-01-23 00:34:21 +00001688 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001689
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001690 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001691 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001692 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1693 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001694 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001695 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001696 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001697 }
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001699
Damien George3ff2d032014-03-31 18:02:22 +01001700 // discard final value of var that failed the loop condition
1701 EMIT(pop_top);
1702
Damienf72fd0e2013-11-06 20:20:49 +00001703 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001704 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001705
1706 compile_node(comp, pn_else);
1707
Damien Georgeb9791222014-01-23 00:34:21 +00001708 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001709}
1710
Damiend99b0522013-12-21 18:17:45 +00001711void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001712#if !MICROPY_EMIT_CPYTHON
1713 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1714 // this is actually slower, but uses no heap memory
1715 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001716 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 +00001717 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001718 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1719 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1720 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1721 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001722 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1723 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001724 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001725 mp_parse_node_t pn_range_start;
1726 mp_parse_node_t pn_range_end;
1727 mp_parse_node_t pn_range_step;
1728 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001729 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001730 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001731 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001732 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001733 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001734 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001735 } else if (n_args == 2) {
1736 pn_range_start = args[0];
1737 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001738 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001739 } else {
1740 pn_range_start = args[0];
1741 pn_range_end = args[1];
1742 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001743 // We need to know sign of step. This is possible only if it's constant
1744 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1745 optimize = false;
1746 }
Damienf72fd0e2013-11-06 20:20:49 +00001747 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001748 }
1749 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001750 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1751 return;
1752 }
1753 }
1754 }
1755#endif
1756
Damien Georgecbddb272014-02-01 20:08:18 +00001757 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001758
Damien George6f355fd2014-04-10 14:11:31 +01001759 uint pop_label = comp_next_label(comp);
1760 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001761
Damience89a212013-10-15 22:25:17 +01001762 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1763#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001764 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001765#endif
1766
Damien429d7192013-10-04 19:53:11 +01001767 compile_node(comp, pns->nodes[1]); // iterator
1768 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001769 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001771 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1772 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001773 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001774 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001775 }
Damien Georgeb9791222014-01-23 00:34:21 +00001776 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001777 EMIT(for_iter_end);
1778
1779 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001780 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001781
Damience89a212013-10-15 22:25:17 +01001782#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001783 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001784#endif
Damien429d7192013-10-04 19:53:11 +01001785
1786 compile_node(comp, pns->nodes[3]); // else (not tested)
1787
Damien Georgeb9791222014-01-23 00:34:21 +00001788 EMIT_ARG(label_assign, break_label);
1789 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001790}
1791
Damiend99b0522013-12-21 18:17:45 +00001792void 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 +01001793 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001794 uint l1 = comp_next_label(comp);
1795 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001796
Damien Georgeb9791222014-01-23 00:34:21 +00001797 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001798 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001799
Damien429d7192013-10-04 19:53:11 +01001800 compile_node(comp, pn_body); // body
1801 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001802 EMIT_ARG(jump, success_label); // jump over exception handler
1803
1804 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001805 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 +00001806
Damien George6f355fd2014-04-10 14:11:31 +01001807 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001808
1809 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001810 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1811 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001812
1813 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001814 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001815
Damiend99b0522013-12-21 18:17:45 +00001816 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001817 // this is a catch all exception handler
1818 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001819 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001820 return;
1821 }
1822 } else {
1823 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001824 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1825 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1826 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1827 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001828 // handler binds the exception to a local
1829 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001830 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001831 }
1832 }
1833 EMIT(dup_top);
1834 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001835 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001836 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001837 }
1838
1839 EMIT(pop_top);
1840
1841 if (qstr_exception_local == 0) {
1842 EMIT(pop_top);
1843 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001844 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001845 }
1846
1847 EMIT(pop_top);
1848
Damien George6f355fd2014-04-10 14:11:31 +01001849 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001850 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001851 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001852 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001853 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001854 }
1855 compile_node(comp, pns_except->nodes[1]);
1856 if (qstr_exception_local != 0) {
1857 EMIT(pop_block);
1858 }
1859 EMIT(pop_except);
1860 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001861 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1862 EMIT_ARG(label_assign, l3);
1863 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1864 EMIT_ARG(store_id, qstr_exception_local);
1865 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001866
Damien George8dcc0c72014-03-27 10:55:21 +00001867 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001868 EMIT(end_finally);
1869 }
Damien Georgeb9791222014-01-23 00:34:21 +00001870 EMIT_ARG(jump, l2);
1871 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001872 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001873 }
1874
Damien George8dcc0c72014-03-27 10:55:21 +00001875 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001876 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001877 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001878
Damien Georgeb9791222014-01-23 00:34:21 +00001879 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001880 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001881 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001882}
1883
Damiend99b0522013-12-21 18:17:45 +00001884void 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 +01001885 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001886
Damien Georgeb9791222014-01-23 00:34:21 +00001887 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001888 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001889
Damien429d7192013-10-04 19:53:11 +01001890 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001891 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001892 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001893 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001894 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001895 } else {
1896 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1897 }
1898 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001899 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1900 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001901 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001902
Damien George8dcc0c72014-03-27 10:55:21 +00001903 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001904 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001905}
1906
Damiend99b0522013-12-21 18:17:45 +00001907void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1908 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1909 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1910 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001911 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001912 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1913 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001914 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001915 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001916 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001917 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001918 // no finally
1919 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1920 } else {
1921 // have finally
Damiend99b0522013-12-21 18:17:45 +00001922 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 +01001923 }
1924 } else {
1925 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001926 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001927 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001928 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001929 }
1930 } else {
1931 // shouldn't happen
1932 assert(0);
1933 }
1934}
1935
Damiend99b0522013-12-21 18:17:45 +00001936void 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 +01001937 if (n == 0) {
1938 // no more pre-bits, compile the body of the with
1939 compile_node(comp, body);
1940 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001941 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001942 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001943 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001944 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001945 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001946 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001947 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1948 } else {
1949 // this pre-bit is just an expression
1950 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001951 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001952 EMIT(pop_top);
1953 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001954 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001955 // compile additional pre-bits and the body
1956 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1957 // finish this with block
1958 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001959 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1960 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001961 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001962 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001963 EMIT(end_finally);
1964 }
1965}
1966
Damiend99b0522013-12-21 18:17:45 +00001967void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001968 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001969 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001970 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1971 assert(n > 0);
1972
1973 // compile in a nested fashion
1974 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1975}
1976
Damiend99b0522013-12-21 18:17:45 +00001977void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1978 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001979 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1980 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001981 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001982 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001983 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001984 EMIT(pop_top);
1985
Damien429d7192013-10-04 19:53:11 +01001986 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001987 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001988 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001989 // do nothing with a lonely constant
1990 } else {
1991 compile_node(comp, pns->nodes[0]); // just an expression
1992 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1993 }
Damien429d7192013-10-04 19:53:11 +01001994 }
1995 } else {
Damiend99b0522013-12-21 18:17:45 +00001996 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1997 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001998 if (kind == PN_expr_stmt_augassign) {
1999 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2000 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002001 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002002 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002003 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002004 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2005 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2006 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2007 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2008 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2009 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2010 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2011 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2012 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2013 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2014 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2015 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2016 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002017 }
Damien George7e5fb242014-02-01 22:18:47 +00002018 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002019 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2020 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002021 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2022 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002023 // following CPython, we store left-most first
2024 if (rhs > 0) {
2025 EMIT(dup_top);
2026 }
2027 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2028 for (int i = 0; i < rhs; i++) {
2029 if (i + 1 < rhs) {
2030 EMIT(dup_top);
2031 }
Damiend99b0522013-12-21 18:17:45 +00002032 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002033 }
2034 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002035 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2036 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2037 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2038 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002039 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002040 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2041 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002042 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2043 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2044 // can't optimise when it's a star expression on the lhs
2045 goto no_optimisation;
2046 }
Damien429d7192013-10-04 19:53:11 +01002047 compile_node(comp, pns10->nodes[0]); // rhs
2048 compile_node(comp, pns10->nodes[1]); // rhs
2049 EMIT(rot_two);
2050 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2051 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002052 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2053 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2054 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2055 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002056 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002057 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2058 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002059 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2060 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2061 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2062 // can't optimise when it's a star expression on the lhs
2063 goto no_optimisation;
2064 }
Damien429d7192013-10-04 19:53:11 +01002065 compile_node(comp, pns10->nodes[0]); // rhs
2066 compile_node(comp, pns10->nodes[1]); // rhs
2067 compile_node(comp, pns10->nodes[2]); // rhs
2068 EMIT(rot_three);
2069 EMIT(rot_two);
2070 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2071 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2072 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2073 } else {
Damien George495d7812014-04-08 17:51:47 +01002074 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002075 compile_node(comp, pns1->nodes[0]); // rhs
2076 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2077 }
2078 } else {
2079 // shouldn't happen
2080 assert(0);
2081 }
2082 }
2083}
2084
Damien Georged17926d2014-03-30 13:35:08 +01002085void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002086 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002087 compile_node(comp, pns->nodes[0]);
2088 for (int i = 1; i < num_nodes; i += 1) {
2089 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002090 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002091 }
2092}
2093
Damiend99b0522013-12-21 18:17:45 +00002094void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2095 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2096 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002097
Damien George6f355fd2014-04-10 14:11:31 +01002098 uint l_fail = comp_next_label(comp);
2099 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002100 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2101 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002102 EMIT_ARG(jump, l_end);
2103 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002104 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002105 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002106 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002107}
2108
Damiend99b0522013-12-21 18:17:45 +00002109void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002110 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002111 //mp_parse_node_t pn_params = pns->nodes[0];
2112 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002113
2114 if (comp->pass == PASS_1) {
2115 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002116 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 +01002117 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002118 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002119 }
2120
2121 // get the scope for this lambda
2122 scope_t *this_scope = (scope_t*)pns->nodes[2];
2123
2124 // make the lambda
2125 close_over_variables_etc(comp, this_scope, 0, 0);
2126}
2127
Damiend99b0522013-12-21 18:17:45 +00002128void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002129 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002130 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002131 for (int i = 0; i < n; i += 1) {
2132 compile_node(comp, pns->nodes[i]);
2133 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002134 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002135 }
2136 }
Damien Georgeb9791222014-01-23 00:34:21 +00002137 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002138}
2139
Damiend99b0522013-12-21 18:17:45 +00002140void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002141 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002142 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002143 for (int i = 0; i < n; i += 1) {
2144 compile_node(comp, pns->nodes[i]);
2145 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002146 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002147 }
2148 }
Damien Georgeb9791222014-01-23 00:34:21 +00002149 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002150}
2151
Damiend99b0522013-12-21 18:17:45 +00002152void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002153 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002154 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002155}
2156
Damiend99b0522013-12-21 18:17:45 +00002157void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002158 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002159 compile_node(comp, pns->nodes[0]);
2160 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002161 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002162 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002163 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002164 }
2165 for (int i = 1; i + 1 < num_nodes; i += 2) {
2166 compile_node(comp, pns->nodes[i + 1]);
2167 if (i + 2 < num_nodes) {
2168 EMIT(dup_top);
2169 EMIT(rot_three);
2170 }
Damien George7e5fb242014-02-01 22:18:47 +00002171 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002172 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002173 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2175 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2176 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2177 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2178 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2179 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2180 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2181 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002182 }
2183 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002184 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2185 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2186 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002187 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002188 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002189 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002190 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002191 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002192 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002193 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002194 }
2195 } else {
2196 // shouldn't happen
2197 assert(0);
2198 }
2199 } else {
2200 // shouldn't happen
2201 assert(0);
2202 }
2203 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002204 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002205 }
2206 }
2207 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002208 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002209 EMIT_ARG(jump, l_end);
2210 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002211 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002212 EMIT(rot_two);
2213 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002214 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002215 }
2216}
2217
Damiend99b0522013-12-21 18:17:45 +00002218void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002219 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002220}
2221
Damiend99b0522013-12-21 18:17:45 +00002222void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002223 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002224}
2225
Damiend99b0522013-12-21 18:17:45 +00002226void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002227 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002228}
2229
Damiend99b0522013-12-21 18:17:45 +00002230void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002231 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002232}
2233
Damiend99b0522013-12-21 18:17:45 +00002234void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2235 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns->nodes[0]);
2237 for (int i = 1; i + 1 < num_nodes; i += 2) {
2238 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002240 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002241 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002242 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002243 } else {
2244 // shouldn't happen
2245 assert(0);
2246 }
2247 }
2248}
2249
Damiend99b0522013-12-21 18:17:45 +00002250void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2251 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002252 compile_node(comp, pns->nodes[0]);
2253 for (int i = 1; i + 1 < num_nodes; i += 2) {
2254 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002255 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002256 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002257 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002258 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002259 } else {
2260 // shouldn't happen
2261 assert(0);
2262 }
2263 }
2264}
2265
Damiend99b0522013-12-21 18:17:45 +00002266void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2267 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002268 compile_node(comp, pns->nodes[0]);
2269 for (int i = 1; i + 1 < num_nodes; i += 2) {
2270 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002271 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002272 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002273 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002274 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002275 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002276 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002278 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002279 } else {
2280 // shouldn't happen
2281 assert(0);
2282 }
2283 }
2284}
2285
Damiend99b0522013-12-21 18:17:45 +00002286void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002287 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002288 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002289 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002290 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002291 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002292 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002293 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002294 } else {
2295 // shouldn't happen
2296 assert(0);
2297 }
2298}
2299
Damien George35e2a4e2014-02-05 00:51:47 +00002300void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2301 // this is to handle special super() call
2302 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2303
2304 compile_generic_all_nodes(comp, pns);
2305}
2306
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002307STATIC 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 +01002308 // function to call is on top of stack
2309
Damien George35e2a4e2014-02-05 00:51:47 +00002310#if !MICROPY_EMIT_CPYTHON
2311 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002312 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 +00002313 EMIT_ARG(load_id, MP_QSTR___class__);
2314 // get first argument to function
2315 bool found = false;
2316 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002317 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2318 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 +00002319 found = true;
2320 break;
2321 }
2322 }
2323 if (!found) {
2324 printf("TypeError: super() call cannot find self\n");
2325 return;
2326 }
Damien George922ddd62014-04-09 12:43:17 +01002327 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002328 return;
2329 }
2330#endif
2331
Damien George02a4c052014-04-09 12:50:58 +01002332 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002333 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002334 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002335 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002336
Damien Georgebbcd49a2014-02-06 20:30:16 +00002337 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002338
2339 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002340 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002341 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002342 n_positional -= 1;
2343 }
Damien George922ddd62014-04-09 12:43:17 +01002344 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002345 n_positional -= 1;
2346 }
2347
2348 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002349 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002350 } else {
Damien George922ddd62014-04-09 12:43:17 +01002351 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002352 }
2353
2354 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002355 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002356}
2357
Damiend99b0522013-12-21 18:17:45 +00002358void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2359 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002360 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002361 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 +01002362 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002363 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2364 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002365 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002366 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002367 i += 1;
2368 } else {
2369 compile_node(comp, pns->nodes[i]);
2370 }
Damien George35e2a4e2014-02-05 00:51:47 +00002371 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002372 }
2373}
2374
Damiend99b0522013-12-21 18:17:45 +00002375void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002376 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002377 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002378}
2379
Damiend99b0522013-12-21 18:17:45 +00002380void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002381 // a list of strings
Damien63321742013-12-10 17:41:49 +00002382
2383 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002384 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002385 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002386 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002387 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002388 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2389 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2390 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002391 if (i == 0) {
2392 string_kind = pn_kind;
2393 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002394 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002395 return;
2396 }
Damien George55baff42014-01-21 21:40:13 +00002397 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002398 }
Damien63321742013-12-10 17:41:49 +00002399
Damien63321742013-12-10 17:41:49 +00002400 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002401 byte *q_ptr;
2402 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002403 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002404 uint s_len;
2405 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002406 memcpy(s_dest, s, s_len);
2407 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002408 }
Damien George55baff42014-01-21 21:40:13 +00002409 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002410
Damien Georgeb9791222014-01-23 00:34:21 +00002411 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002412}
2413
2414// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002415void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2416 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2417 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2418 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002419
2420 if (comp->pass == PASS_1) {
2421 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002422 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 +01002423 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002424 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002425 }
2426
2427 // get the scope for this comprehension
2428 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2429
2430 // compile the comprehension
2431 close_over_variables_etc(comp, this_scope, 0, 0);
2432
2433 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2434 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002435 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002436}
2437
Damiend99b0522013-12-21 18:17:45 +00002438void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2439 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002440 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002441 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2442 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2443 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2444 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2445 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2446 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2447 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002448 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002449 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002450 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002451 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002452 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002453 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002454 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002455 // generator expression
2456 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2457 } else {
2458 // tuple with 2 items
2459 goto tuple_with_2_items;
2460 }
2461 } else {
2462 // tuple with 2 items
2463 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002464 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002465 }
2466 } else {
2467 // parenthesis around a single item, is just that item
2468 compile_node(comp, pns->nodes[0]);
2469 }
2470}
2471
Damiend99b0522013-12-21 18:17:45 +00002472void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2473 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002474 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002475 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002476 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2477 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2478 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2479 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2480 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002481 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002482 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002483 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002484 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002485 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002486 // list of many items
2487 compile_node(comp, pns2->nodes[0]);
2488 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002489 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002490 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002491 // list comprehension
2492 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2493 } else {
2494 // list with 2 items
2495 goto list_with_2_items;
2496 }
2497 } else {
2498 // list with 2 items
2499 list_with_2_items:
2500 compile_node(comp, pns2->nodes[0]);
2501 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002502 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002503 }
2504 } else {
2505 // list with 1 item
2506 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002508 }
2509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2512 mp_parse_node_t pn = pns->nodes[0];
2513 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002514 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002516 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2517 pns = (mp_parse_node_struct_t*)pn;
2518 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002519 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002520 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002521 compile_node(comp, pn);
2522 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002523 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2524 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2525 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2526 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002527 // dict/set with multiple elements
2528
2529 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002530 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002531 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2532
2533 // first element sets whether it's a dict or set
2534 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002535 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002536 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002538 compile_node(comp, pns->nodes[0]);
2539 EMIT(store_map);
2540 is_dict = true;
2541 } else {
2542 // a set
2543 compile_node(comp, pns->nodes[0]); // 1st value of set
2544 is_dict = false;
2545 }
2546
2547 // process rest of elements
2548 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002549 mp_parse_node_t pn = nodes[i];
2550 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002551 compile_node(comp, pn);
2552 if (is_dict) {
2553 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002554 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002555 return;
2556 }
2557 EMIT(store_map);
2558 } else {
2559 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002560 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002561 return;
2562 }
2563 }
2564 }
2565
2566 // if it's a set, build it
2567 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002568 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002569 }
Damiend99b0522013-12-21 18:17:45 +00002570 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002571 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002572 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002573 // a dictionary comprehension
2574 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2575 } else {
2576 // a set comprehension
2577 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2578 }
2579 } else {
2580 // shouldn't happen
2581 assert(0);
2582 }
2583 } else {
2584 // set with one element
2585 goto set_with_one_element;
2586 }
2587 } else {
2588 // set with one element
2589 set_with_one_element:
2590 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002591 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002592 }
2593}
2594
Damiend99b0522013-12-21 18:17:45 +00002595void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002596 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002597}
2598
Damiend99b0522013-12-21 18:17:45 +00002599void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002600 // object who's index we want is on top of stack
2601 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002602 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002603}
2604
Damiend99b0522013-12-21 18:17:45 +00002605void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002606 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002607 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002608}
2609
Damiend99b0522013-12-21 18:17:45 +00002610void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2611 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2612 mp_parse_node_t pn = pns->nodes[0];
2613 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002614 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002615 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2616 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002617 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2618 pns = (mp_parse_node_struct_t*)pn;
2619 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002620 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002621 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002622 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002623 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002624 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002625 } else {
2626 // [?::x]
2627 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002628 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002629 }
Damiend99b0522013-12-21 18:17:45 +00002630 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002631 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002632 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2633 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2634 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2635 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002636 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002637 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002638 } else {
2639 // [?:x:x]
2640 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002641 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002642 }
2643 } else {
2644 // [?:x]
2645 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002646 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002647 }
2648 } else {
2649 // [?:x]
2650 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002652 }
2653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002656 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002657 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2658 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002659}
2660
Damiend99b0522013-12-21 18:17:45 +00002661void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002662 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002663 compile_subscript_3_helper(comp, pns);
2664}
2665
Damiend99b0522013-12-21 18:17:45 +00002666void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002667 // if this is called then we are compiling a dict key:value pair
2668 compile_node(comp, pns->nodes[1]); // value
2669 compile_node(comp, pns->nodes[0]); // key
2670}
2671
Damiend99b0522013-12-21 18:17:45 +00002672void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002673 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002674 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002675 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002676}
2677
Damiend99b0522013-12-21 18:17:45 +00002678void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002679 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002680 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002681 return;
2682 }
Damien George922ddd62014-04-09 12:43:17 +01002683 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002684 compile_node(comp, pns->nodes[0]);
2685}
2686
Damiend99b0522013-12-21 18:17:45 +00002687void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002688 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002689 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002690 return;
2691 }
Damien George922ddd62014-04-09 12:43:17 +01002692 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002693 compile_node(comp, pns->nodes[0]);
2694}
2695
Damiend99b0522013-12-21 18:17:45 +00002696void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2697 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2698 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2699 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2700 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002701 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 +01002702 return;
2703 }
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002705 compile_node(comp, pns2->nodes[0]);
2706 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002707 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002708 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2709 } else {
2710 // shouldn't happen
2711 assert(0);
2712 }
2713}
2714
Damiend99b0522013-12-21 18:17:45 +00002715void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002716 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002717 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002718 return;
2719 }
Damiend99b0522013-12-21 18:17:45 +00002720 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002721 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002722 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002723 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2724 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002725 compile_node(comp, pns->nodes[0]);
2726 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002727 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002728 EMIT(yield_from);
2729 } else {
2730 compile_node(comp, pns->nodes[0]);
2731 EMIT(yield_value);
2732 }
2733}
2734
Damiend99b0522013-12-21 18:17:45 +00002735typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002736STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002737 NULL,
2738#define nc NULL
2739#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002740#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002741#include "grammar.h"
2742#undef nc
2743#undef c
2744#undef DEF_RULE
2745};
2746
Damiend99b0522013-12-21 18:17:45 +00002747void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2748 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002749 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002750 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2751 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2752 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002753 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002754 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002755 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002756 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002757 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2758 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2759 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2760 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002761 case MP_PARSE_NODE_TOKEN:
2762 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002763 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002764 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002765 // do nothing
2766 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002767 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002768 }
2769 break;
Damien429d7192013-10-04 19:53:11 +01002770 default: assert(0);
2771 }
2772 } else {
Damiend99b0522013-12-21 18:17:45 +00002773 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002774 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002775 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002776 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002777 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002778#if MICROPY_DEBUG_PRINTERS
2779 mp_parse_node_print(pn, 0);
2780#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002781 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002782 } else {
2783 f(comp, pns);
2784 }
2785 }
2786}
2787
Damiend99b0522013-12-21 18:17:45 +00002788void 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 +01002789 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002790 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002791 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2792 if (MP_PARSE_NODE_IS_ID(pn)) {
2793 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002794 if (comp->have_star) {
Damien429d7192013-10-04 19:53:11 +01002795 // comes after a bare star, so doesn't count as a parameter
2796 } else {
2797 comp->scope_cur->num_params += 1;
2798 }
Damienb14de212013-10-06 00:28:28 +01002799 } else {
Damiend99b0522013-12-21 18:17:45 +00002800 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2801 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2802 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2803 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002804 //int node_index = 1; unused
2805 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002806 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002807 // this parameter has an annotation
2808 pn_annotation = pns->nodes[1];
2809 }
2810 //node_index = 2; unused
2811 }
2812 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002813 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002814 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002815 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002816 comp->scope_cur->num_dict_params += 1;
2817 } else {
2818 comp->scope_cur->num_default_params += 1;
2819 }
2820 }
2821 */
Damien George8b19db02014-04-11 23:25:34 +01002822 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002823 // comes after a bare star, so doesn't count as a parameter
2824 } else {
2825 comp->scope_cur->num_params += 1;
2826 }
Damiend99b0522013-12-21 18:17:45 +00002827 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002828 comp->have_star = true;
Damiend99b0522013-12-21 18:17:45 +00002829 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002830 // bare star
2831 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002832 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002833 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002834 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002835 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002836 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2837 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002838 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002839 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002840 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2841 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002842 pn_annotation = pns->nodes[1];
2843 } else {
2844 // shouldn't happen
2845 assert(0);
2846 }
Damiend99b0522013-12-21 18:17:45 +00002847 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2848 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2849 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002850 // this parameter has an annotation
2851 pn_annotation = pns->nodes[1];
2852 }
Damien George8725f8f2014-02-15 19:33:11 +00002853 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002854 } else {
Damienb14de212013-10-06 00:28:28 +01002855 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002856 assert(0);
2857 }
Damien429d7192013-10-04 19:53:11 +01002858 }
2859
2860 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002861 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002862 // TODO this parameter has an annotation
2863 }
2864 bool added;
2865 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2866 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002867 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002868 return;
2869 }
Damien429d7192013-10-04 19:53:11 +01002870 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002871 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002872 }
2873}
2874
Damiend99b0522013-12-21 18:17:45 +00002875void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002876 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2877}
2878
Damiend99b0522013-12-21 18:17:45 +00002879void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002880 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2881}
2882
Damiend99b0522013-12-21 18:17:45 +00002883void 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 +01002884 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002885 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002886 // no more nested if/for; compile inner expression
2887 compile_node(comp, pn_inner_expr);
2888 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002889 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002890 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002892 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002894 } else {
2895 EMIT(yield_value);
2896 EMIT(pop_top);
2897 }
Damiend99b0522013-12-21 18:17:45 +00002898 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002899 // if condition
Damiend99b0522013-12-21 18:17:45 +00002900 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002901 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2902 pn_iter = pns_comp_if->nodes[1];
2903 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002904 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002905 // for loop
Damiend99b0522013-12-21 18:17:45 +00002906 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002907 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002908 uint l_end2 = comp_next_label(comp);
2909 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002910 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002911 EMIT_ARG(label_assign, l_top2);
2912 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002913 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2914 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 +00002915 EMIT_ARG(jump, l_top2);
2916 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002917 EMIT(for_iter_end);
2918 } else {
2919 // shouldn't happen
2920 assert(0);
2921 }
2922}
2923
Damiend99b0522013-12-21 18:17:45 +00002924void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002925 // see http://www.python.org/dev/peps/pep-0257/
2926
2927 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002928 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002929 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002930 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002931 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002932 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2933 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002934 for (int i = 0; i < num_nodes; i++) {
2935 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002936 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 +00002937 // not a newline, so this is the first statement; finish search
2938 break;
2939 }
2940 }
2941 // 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 +00002942 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002943 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002944 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002945 } else {
2946 return;
2947 }
2948
2949 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002950 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2951 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2952 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2953 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2954 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002955 compile_node(comp, pns->nodes[0]); // a doc string
2956 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002957 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002958 }
2959 }
2960 }
2961}
2962
2963void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2964 comp->pass = pass;
2965 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002966 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002967 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002968
2969 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002970 // reset maximum stack sizes in scope
2971 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002972 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002973 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002974 }
2975
Damien5ac1b2e2013-10-18 19:58:12 +01002976#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002977 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002978 scope_print_info(scope);
2979 }
Damien5ac1b2e2013-10-18 19:58:12 +01002980#endif
Damien429d7192013-10-04 19:53:11 +01002981
2982 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002983 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2984 assert(scope->kind == SCOPE_MODULE);
2985 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2986 compile_node(comp, pns->nodes[0]); // compile the expression
2987 EMIT(return_value);
2988 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002989 if (!comp->is_repl) {
2990 check_for_doc_string(comp, scope->pn);
2991 }
Damien429d7192013-10-04 19:53:11 +01002992 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002993 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002994 EMIT(return_value);
2995 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002996 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2997 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2998 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002999
3000 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003001 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003002 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003003 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003004 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3005 }
3006
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003007 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003008
3009 compile_node(comp, pns->nodes[3]); // 3 is function body
3010 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003011 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003012 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003013 EMIT(return_value);
3014 }
3015 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003016 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3017 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3018 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003019
3020 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003021 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003022 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003023 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003024 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3025 }
3026
3027 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003028
3029 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3030 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3031 EMIT(pop_top);
3032 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3033 }
Damien429d7192013-10-04 19:53:11 +01003034 EMIT(return_value);
3035 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3036 // a bit of a hack at the moment
3037
Damiend99b0522013-12-21 18:17:45 +00003038 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3039 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3040 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3041 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3042 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003043
Damien George55baff42014-01-21 21:40:13 +00003044 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003045 if (comp->pass == PASS_1) {
3046 bool added;
3047 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3048 assert(added);
3049 id_info->kind = ID_INFO_KIND_LOCAL;
3050 scope->num_params = 1;
3051 }
3052
3053 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003055 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003056 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003057 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003058 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003059 }
3060
Damien George6f355fd2014-04-10 14:11:31 +01003061 uint l_end = comp_next_label(comp);
3062 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_ARG(load_id, qstr_arg);
3064 EMIT_ARG(label_assign, l_top);
3065 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003066 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3067 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_ARG(jump, l_top);
3069 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003070 EMIT(for_iter_end);
3071
3072 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003073 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003074 }
3075 EMIT(return_value);
3076 } else {
3077 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003078 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3079 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3080 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003081
3082 if (comp->pass == PASS_1) {
3083 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003084 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003085 assert(added);
3086 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003087 }
3088
Damien Georgeb9791222014-01-23 00:34:21 +00003089 EMIT_ARG(load_id, MP_QSTR___name__);
3090 EMIT_ARG(store_id, MP_QSTR___module__);
3091 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3092 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003093
3094 check_for_doc_string(comp, pns->nodes[2]);
3095 compile_node(comp, pns->nodes[2]); // 2 is class body
3096
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003097 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003098 assert(id != NULL);
3099 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003100 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003101 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003102#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003103 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003104#else
Damien George2bf7c092014-04-09 15:26:46 +01003105 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003106#endif
Damien429d7192013-10-04 19:53:11 +01003107 }
3108 EMIT(return_value);
3109 }
3110
Damien415eb6f2013-10-05 12:19:06 +01003111 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003112
3113 // make sure we match all the exception levels
3114 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003115}
3116
Damien George094d4502014-04-02 17:31:27 +01003117#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003118void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3119 comp->pass = pass;
3120 comp->scope_cur = scope;
3121 comp->next_label = 1;
3122
3123 if (scope->kind != SCOPE_FUNCTION) {
3124 printf("Error: inline assembler must be a function\n");
3125 return;
3126 }
3127
Damiena2f2f7d2013-10-06 00:14:13 +01003128 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003129 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003130 }
3131
Damien826005c2013-10-05 23:17:28 +01003132 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003133 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3134 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3135 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003136
Damiend99b0522013-12-21 18:17:45 +00003137 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003138
Damiena2f2f7d2013-10-06 00:14:13 +01003139 // parameters are in pns->nodes[1]
3140 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003141 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003142 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003143 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003144 }
3145
Damiend99b0522013-12-21 18:17:45 +00003146 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003147
Damiend99b0522013-12-21 18:17:45 +00003148 mp_parse_node_t pn_body = pns->nodes[3]; // body
3149 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003150 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3151
Damien Georgecbd2f742014-01-19 11:48:48 +00003152 /*
Damien826005c2013-10-05 23:17:28 +01003153 if (comp->pass == PASS_3) {
3154 //printf("----\n");
3155 scope_print_info(scope);
3156 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003157 */
Damien826005c2013-10-05 23:17:28 +01003158
3159 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003160 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3161 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003162 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3163 // no instructions
3164 continue;
3165 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3166 // an instruction; fall through
3167 } else {
3168 // not an instruction; error
3169 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3170 return;
3171 }
Damiend99b0522013-12-21 18:17:45 +00003172 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3173 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3174 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3175 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3176 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3177 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3178 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3179 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3180 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3181 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003182 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3183
3184 // emit instructions
3185 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003186 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003187 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003188 return;
3189 }
Damien George6f355fd2014-04-10 14:11:31 +01003190 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003191 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003192 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003193 }
3194 } else {
3195 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003196 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003197 }
3198 }
3199 }
3200
3201 if (comp->pass > PASS_1) {
Damien Georgea26dc502014-04-12 17:54:52 +01003202 bool success = EMIT_INLINE_ASM(end_pass);
3203 if (!success) {
3204 comp->had_error = true;
3205 }
Damienb05d7072013-10-05 13:37:10 +01003206 }
Damien429d7192013-10-04 19:53:11 +01003207}
Damien George094d4502014-04-02 17:31:27 +01003208#endif
Damien429d7192013-10-04 19:53:11 +01003209
3210void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3211 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003212 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003213 scope->num_locals = 0;
3214 for (int i = 0; i < scope->id_info_len; i++) {
3215 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003216 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003217 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3218 continue;
3219 }
3220 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3221 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3222 }
Damien9ecbcff2013-12-11 00:41:43 +00003223 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003224 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003225 id->local_num = scope->num_locals;
3226 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003227 }
3228 }
3229
3230 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003231#if MICROPY_EMIT_CPYTHON
3232 int num_cell = 0;
3233#endif
Damien9ecbcff2013-12-11 00:41:43 +00003234 for (int i = 0; i < scope->id_info_len; i++) {
3235 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003236#if MICROPY_EMIT_CPYTHON
3237 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003238 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003239 id->local_num = num_cell;
3240 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003241 }
Damien George6baf76e2013-12-30 22:32:17 +00003242#else
3243 // in Micro Python the cells come right after the fast locals
3244 // parameters are not counted here, since they remain at the start
3245 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003246 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003247 id->local_num = scope->num_locals;
3248 scope->num_locals += 1;
3249 }
3250#endif
Damien9ecbcff2013-12-11 00:41:43 +00003251 }
Damien9ecbcff2013-12-11 00:41:43 +00003252
3253 // compute the index of free vars (freevars[idx] in CPython)
3254 // make sure they are in the order of the parent scope
3255 if (scope->parent != NULL) {
3256 int num_free = 0;
3257 for (int i = 0; i < scope->parent->id_info_len; i++) {
3258 id_info_t *id = &scope->parent->id_info[i];
3259 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3260 for (int j = 0; j < scope->id_info_len; j++) {
3261 id_info_t *id2 = &scope->id_info[j];
3262 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003263 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003264#if MICROPY_EMIT_CPYTHON
3265 // in CPython the frees are numbered after the cells
3266 id2->local_num = num_cell + num_free;
3267#else
3268 // in Micro Python the frees come first, before the params
3269 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003270#endif
3271 num_free += 1;
3272 }
3273 }
3274 }
Damien429d7192013-10-04 19:53:11 +01003275 }
Damien George6baf76e2013-12-30 22:32:17 +00003276#if !MICROPY_EMIT_CPYTHON
3277 // in Micro Python shift all other locals after the free locals
3278 if (num_free > 0) {
3279 for (int i = 0; i < scope->id_info_len; i++) {
3280 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003281 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003282 id->local_num += num_free;
3283 }
3284 }
3285 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3286 scope->num_locals += num_free;
3287 }
3288#endif
Damien429d7192013-10-04 19:53:11 +01003289 }
3290
Damien George8725f8f2014-02-15 19:33:11 +00003291 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003292
3293#if MICROPY_EMIT_CPYTHON
3294 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003295 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) {
3296 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003297 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003298 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003299 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 +00003300 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003301 }
3302 }
Damien George882b3632014-04-02 15:56:31 +01003303#endif
3304
Damien429d7192013-10-04 19:53:11 +01003305 int num_free = 0;
3306 for (int i = 0; i < scope->id_info_len; i++) {
3307 id_info_t *id = &scope->id_info[i];
3308 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3309 num_free += 1;
3310 }
3311 }
3312 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003313 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003314 }
3315}
3316
Damien George65cad122014-04-06 11:48:15 +01003317mp_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 +01003318 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003319 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003320 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003321
Damien826005c2013-10-05 23:17:28 +01003322 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003323 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003324
3325 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003326 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003327
Damien826005c2013-10-05 23:17:28 +01003328 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003329 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003330 comp->emit_method_table = &emit_pass1_method_table;
3331 comp->emit_inline_asm = NULL;
3332 comp->emit_inline_asm_method_table = NULL;
3333 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003334 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003335 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003336#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003337 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003338 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003339#endif
Damien826005c2013-10-05 23:17:28 +01003340 } else {
3341 compile_scope(comp, s, PASS_1);
3342 }
3343
3344 // update maximim number of labels needed
3345 if (comp->next_label > max_num_labels) {
3346 max_num_labels = comp->next_label;
3347 }
Damien429d7192013-10-04 19:53:11 +01003348 }
3349
Damien826005c2013-10-05 23:17:28 +01003350 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003351 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003352 compile_scope_compute_things(comp, s);
3353 }
3354
Damien826005c2013-10-05 23:17:28 +01003355 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003356 emit_pass1_free(comp->emit);
3357
Damien826005c2013-10-05 23:17:28 +01003358 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003359#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003360 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003361#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003362 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003363#endif
Damien3ef4abb2013-10-12 16:53:13 +01003364#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003365 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003366#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003367#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003368 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003369 if (false) {
3370 // dummy
3371
Damien3ef4abb2013-10-12 16:53:13 +01003372#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003373 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003374 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003375 if (emit_inline_thumb == NULL) {
3376 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3377 }
3378 comp->emit = NULL;
3379 comp->emit_method_table = NULL;
3380 comp->emit_inline_asm = emit_inline_thumb;
3381 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3382 compile_scope_inline_asm(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003383 if (!comp->had_error) {
3384 compile_scope_inline_asm(comp, s, PASS_3);
3385 }
Damienc025ebb2013-10-12 14:30:21 +01003386#endif
3387
Damien826005c2013-10-05 23:17:28 +01003388 } else {
Damienc025ebb2013-10-12 14:30:21 +01003389
3390 // choose the emit type
3391
Damien3ef4abb2013-10-12 16:53:13 +01003392#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003393 comp->emit = emit_cpython_new(max_num_labels);
3394 comp->emit_method_table = &emit_cpython_method_table;
3395#else
Damien826005c2013-10-05 23:17:28 +01003396 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003397
3398#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003399 case MP_EMIT_OPT_NATIVE_PYTHON:
3400 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003401#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003402 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003403 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003404 }
Damien13ed3a62013-10-08 09:05:10 +01003405 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003406#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003407 if (emit_native == NULL) {
3408 emit_native = emit_native_thumb_new(max_num_labels);
3409 }
3410 comp->emit_method_table = &emit_native_thumb_method_table;
3411#endif
3412 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003413 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003414 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003415#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003416
Damien826005c2013-10-05 23:17:28 +01003417 default:
3418 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003419 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003420 }
3421 comp->emit = emit_bc;
3422 comp->emit_method_table = &emit_bc_method_table;
3423 break;
3424 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003425#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003426
3427 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003428 compile_scope(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003429 if (!comp->had_error) {
3430 compile_scope(comp, s, PASS_3);
3431 }
Damien6cdd3af2013-10-05 18:08:26 +01003432 }
Damien429d7192013-10-04 19:53:11 +01003433 }
3434
Damien George41d02b62014-01-24 22:42:28 +00003435 // free the emitters
3436#if !MICROPY_EMIT_CPYTHON
3437 if (emit_bc != NULL) {
3438 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003439 }
Damien George41d02b62014-01-24 22:42:28 +00003440#if MICROPY_EMIT_NATIVE
3441 if (emit_native != NULL) {
3442#if MICROPY_EMIT_X64
3443 emit_native_x64_free(emit_native);
3444#elif MICROPY_EMIT_THUMB
3445 emit_native_thumb_free(emit_native);
3446#endif
3447 }
3448#endif
3449#if MICROPY_EMIT_INLINE_THUMB
3450 if (emit_inline_thumb != NULL) {
3451 emit_inline_thumb_free(emit_inline_thumb);
3452 }
3453#endif
3454#endif // !MICROPY_EMIT_CPYTHON
3455
3456 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003457 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003458 for (scope_t *s = module_scope; s;) {
3459 scope_t *next = s->next;
3460 scope_free(s);
3461 s = next;
3462 }
Damien5ac1b2e2013-10-18 19:58:12 +01003463
Damien George41d02b62014-01-24 22:42:28 +00003464 // free the compiler
3465 bool had_error = comp->had_error;
3466 m_del_obj(compiler_t, comp);
3467
Damien George1fb03172014-01-03 14:22:03 +00003468 if (had_error) {
3469 // TODO return a proper error message
3470 return mp_const_none;
3471 } else {
3472#if MICROPY_EMIT_CPYTHON
3473 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003474 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003475 return mp_const_true;
3476#else
3477 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003478 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003479#endif
3480 }
Damien429d7192013-10-04 19:53:11 +01003481}