blob: 6abe9ea2d7a1066935db33c42b98323108ca765b [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 George8b19db02014-04-11 23:25:34 +010054 uint8_t have_star;
Damien George69b89d22014-04-11 13:38:30 +000055 uint16_t num_dict_params;
56 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010057
58 scope_t *scope_head;
59 scope_t *scope_cur;
60
Damien6cdd3af2013-10-05 18:08:26 +010061 emit_t *emit; // current emitter
62 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010063
64 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
65 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 +010066} compiler_t;
67
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010068STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000069 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010070 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
71 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
72 } else {
73 printf(" File \"%s\"\n", qstr_str(comp->source_file));
74 }
Damien Georgef41fdd02014-03-03 23:19:11 +000075 printf("SyntaxError: %s\n", msg);
76 comp->had_error = true;
77}
78
Damien George57e99eb2014-04-10 22:42:11 +010079STATIC const mp_map_elem_t mp_constants_table[] = {
80 // Extra constants as defined by a port
81 MICROPY_EXTRA_CONSTANTS
82};
83
84STATIC const mp_map_t mp_constants_map = {
85 .all_keys_are_qstrs = 1,
86 .table_is_fixed_array = 1,
Damien George6d3c5e42014-04-26 10:47:29 +010087 .used = ARRAY_SIZE(mp_constants_table),
88 .alloc = ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +010089 .table = (mp_map_elem_t*)mp_constants_table,
90};
91
Damien George1463c1f2014-04-25 23:52:57 +010092STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +000093 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
94 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
95 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010096
97 // fold arguments first
98 for (int i = 0; i < n; i++) {
99 pns->nodes[i] = fold_constants(pns->nodes[i]);
100 }
101
Damien Georgea26dc502014-04-12 17:54:52 +0100102 // now try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000103 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100104 case PN_atom_paren:
105 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
106 // (int)
107 pn = pns->nodes[0];
108 }
109 break;
110
111 case PN_expr:
112 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
113 // int | int
114 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
115 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
116 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
117 }
118 break;
119
120 case PN_and_expr:
121 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
122 // int & int
123 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
124 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
125 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
126 }
127 break;
128
Damien429d7192013-10-04 19:53:11 +0100129 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000130 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 +0100131 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
132 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000133 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100134 // int << int
135 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
137 }
Damiend99b0522013-12-21 18:17:45 +0000138 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100139 // int >> int
Damiend99b0522013-12-21 18:17:45 +0000140 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100141 } else {
142 // shouldn't happen
143 assert(0);
144 }
145 }
146 break;
147
148 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100149 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000150 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 +0200151 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
152 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000153 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100154 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000155 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100157 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000158 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100159 } else {
160 // shouldn't happen
161 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100162 }
Damien Georgeaf272592014-04-04 11:21:58 +0000163 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100164 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000165 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100166 }
167 }
168 break;
169
170 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000171 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 +0000172 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
173 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000174 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100175 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000176 if (!mp_small_int_mul_overflow(arg0, arg1)) {
177 arg0 *= arg1;
178 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
179 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
180 }
181 }
Damiend99b0522013-12-21 18:17:45 +0000182 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100183 // int / int
184 // pass
Damiend99b0522013-12-21 18:17:45 +0000185 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100186 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000187 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000188 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300189 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100190 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000191 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 +0300192 }
Damien429d7192013-10-04 19:53:11 +0100193 } else {
194 // shouldn't happen
195 assert(0);
196 }
197 }
198 break;
199
200 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000201 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200202 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000203 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100204 // +int
Damiend99b0522013-12-21 18:17:45 +0000205 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
206 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100207 // -int
Damiend99b0522013-12-21 18:17:45 +0000208 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
209 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100210 // ~int
Damiend99b0522013-12-21 18:17:45 +0000211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100212 } else {
213 // shouldn't happen
214 assert(0);
215 }
216 }
217 break;
218
219 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100220 if (0) {
221#if MICROPY_EMIT_CPYTHON
222 } 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 +0100223 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100224 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000225 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
226 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200227 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100228 if (power >= 0) {
229 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200230 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100231 for (; power > 0; power--) {
232 ans *= base;
233 }
Damiend99b0522013-12-21 18:17:45 +0000234 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100235 }
236 }
Damien George57e99eb2014-04-10 22:42:11 +0100237#endif
238 } 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])) {
239 // id.id
240 // look it up in constant table, see if it can be replaced with an integer
241 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
242 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
243 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
244 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
245 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
246 if (elem != NULL) {
247 mp_obj_t dest[2];
248 mp_load_method_maybe(elem->value, q_attr, dest);
249 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
250 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
251 if (MP_PARSE_FITS_SMALL_INT(val)) {
252 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
253 }
254 }
255 }
Damien429d7192013-10-04 19:53:11 +0100256 }
257 break;
258 }
259 }
260
261 return pn;
262}
263
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200264STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George2c0842b2014-04-27 16:46:51 +0100265void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000266STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100267
Damien George6f355fd2014-04-10 14:11:31 +0100268STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100269 return comp->next_label++;
270}
271
Damien George8dcc0c72014-03-27 10:55:21 +0000272STATIC void compile_increase_except_level(compiler_t *comp) {
273 comp->cur_except_level += 1;
274 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
275 comp->scope_cur->exc_stack_size = comp->cur_except_level;
276 }
277}
278
279STATIC void compile_decrease_except_level(compiler_t *comp) {
280 assert(comp->cur_except_level > 0);
281 comp->cur_except_level -= 1;
282}
283
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200284STATIC 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 +0100285 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100286 scope->parent = comp->scope_cur;
287 scope->next = NULL;
288 if (comp->scope_head == NULL) {
289 comp->scope_head = scope;
290 } else {
291 scope_t *s = comp->scope_head;
292 while (s->next != NULL) {
293 s = s->next;
294 }
295 s->next = scope;
296 }
297 return scope;
298}
299
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200300STATIC 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 +0000301 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
302 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
303 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100304 for (int i = 0; i < num_nodes; i++) {
305 f(comp, pns->nodes[i]);
306 }
Damiend99b0522013-12-21 18:17:45 +0000307 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100308 f(comp, pn);
309 }
310}
311
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200312STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000313 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100314 *nodes = NULL;
315 return 0;
Damiend99b0522013-12-21 18:17:45 +0000316 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100317 *nodes = pn;
318 return 1;
319 } else {
Damiend99b0522013-12-21 18:17:45 +0000320 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
321 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100322 *nodes = pn;
323 return 1;
324 } else {
325 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000326 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100327 }
328 }
329}
330
Damiend99b0522013-12-21 18:17:45 +0000331void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100332}
333
Damiend99b0522013-12-21 18:17:45 +0000334void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
335 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100336 for (int i = 0; i < num_nodes; i++) {
337 compile_node(comp, pns->nodes[i]);
338 }
339}
340
Damien3ef4abb2013-10-12 16:53:13 +0100341#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200342STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000343 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100344 return false;
345 }
Damiend99b0522013-12-21 18:17:45 +0000346 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100347 return false;
348 }
349 return true;
350}
351
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200352STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000353 uint len;
354 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000355 bool has_single_quote = false;
356 bool has_double_quote = false;
357 for (int i = 0; i < len; i++) {
358 if (str[i] == '\'') {
359 has_single_quote = true;
360 } else if (str[i] == '"') {
361 has_double_quote = true;
362 }
363 }
364 if (bytes) {
365 vstr_printf(vstr, "b");
366 }
367 bool quote_single = false;
368 if (has_single_quote && !has_double_quote) {
369 vstr_printf(vstr, "\"");
370 } else {
371 quote_single = true;
372 vstr_printf(vstr, "'");
373 }
374 for (int i = 0; i < len; i++) {
375 if (str[i] == '\n') {
376 vstr_printf(vstr, "\\n");
377 } else if (str[i] == '\\') {
378 vstr_printf(vstr, "\\\\");
379 } else if (str[i] == '\'' && quote_single) {
380 vstr_printf(vstr, "\\'");
381 } else {
382 vstr_printf(vstr, "%c", str[i]);
383 }
384 }
385 if (has_single_quote && !has_double_quote) {
386 vstr_printf(vstr, "\"");
387 } else {
388 vstr_printf(vstr, "'");
389 }
390}
391
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200392STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000393 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200394 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
395 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
396 return;
397 }
398
Damiend99b0522013-12-21 18:17:45 +0000399 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
400 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
401 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000402 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
403 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
404 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
405 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
406 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100407 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000408 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
409 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
410 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100411 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100412 }
413 break;
414 default: assert(0);
415 }
416}
417
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200418STATIC 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 +0100419 int n = 0;
420 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000421 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100422 }
423 int total = n;
424 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000425 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100426 total += 1;
Damien3a205172013-10-12 15:01:56 +0100427 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100428 is_const = false;
429 }
430 }
431 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100432 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100433 is_const = false;
434 break;
435 }
436 }
437 if (total > 0 && is_const) {
438 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000439 vstr_t *vstr = vstr_new();
440 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000441 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000442 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100443 need_comma = true;
444 }
445 for (int i = 0; i < n; i++) {
446 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000447 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100448 }
Damien02f89412013-12-12 15:13:36 +0000449 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100450 need_comma = true;
451 }
452 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000453 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100454 } else {
Damien02f89412013-12-12 15:13:36 +0000455 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100456 }
Damien Georgeb9791222014-01-23 00:34:21 +0000457 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000458 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100459 } else {
Damiend99b0522013-12-21 18:17:45 +0000460 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100461 compile_node(comp, pn);
462 }
463 for (int i = 0; i < n; i++) {
464 compile_node(comp, pns_list->nodes[i]);
465 }
Damien Georgeb9791222014-01-23 00:34:21 +0000466 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100467 }
468}
Damien3a205172013-10-12 15:01:56 +0100469#endif
470
471// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100472STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100473#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100474 cpython_c_tuple(comp, pn, pns_list);
475#else
476 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000477 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100478 compile_node(comp, pn);
479 total += 1;
480 }
481 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000482 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100483 for (int i = 0; i < n; i++) {
484 compile_node(comp, pns_list->nodes[i]);
485 }
486 total += n;
487 }
Damien Georgeb9791222014-01-23 00:34:21 +0000488 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100489#endif
490}
Damien429d7192013-10-04 19:53:11 +0100491
Damiend99b0522013-12-21 18:17:45 +0000492void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100493 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000494 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100495}
496
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200497STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000498 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200499 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100500}
501
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200502STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200503 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 +0100504}
505
Damien3ef4abb2013-10-12 16:53:13 +0100506#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100507// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200508STATIC 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 +0100509 if (node_is_const_false(pn)) {
510 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000511 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100512 }
513 return;
514 } else if (node_is_const_true(pn)) {
515 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000516 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100517 }
518 return;
Damiend99b0522013-12-21 18:17:45 +0000519 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
520 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
521 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
522 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100523 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100524 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100525 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100526 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100527 }
Damien3a205172013-10-12 15:01:56 +0100528 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000529 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100530 } else {
531 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100532 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100533 }
534 }
535 return;
Damiend99b0522013-12-21 18:17:45 +0000536 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100537 if (jump_if == false) {
538 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100539 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100540 }
541 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100542 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100543 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100544 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100545 }
Damien3a205172013-10-12 15:01:56 +0100546 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000547 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100548 }
549 return;
Damiend99b0522013-12-21 18:17:45 +0000550 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100551 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100552 return;
553 }
554 }
555
556 // nothing special, fall back to default compiling for node and jump
557 compile_node(comp, pn);
558 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000559 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100560 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000561 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100562 }
563}
Damien3a205172013-10-12 15:01:56 +0100564#endif
Damien429d7192013-10-04 19:53:11 +0100565
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200566STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100567#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100568 cpython_c_if_cond(comp, pn, jump_if, label, false);
569#else
570 if (node_is_const_false(pn)) {
571 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000572 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100573 }
574 return;
575 } else if (node_is_const_true(pn)) {
576 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000577 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100578 }
579 return;
Damiend99b0522013-12-21 18:17:45 +0000580 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
581 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
582 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
583 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100584 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100585 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100586 for (int i = 0; i < n - 1; i++) {
587 c_if_cond(comp, pns->nodes[i], true, label2);
588 }
589 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000590 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100591 } else {
592 for (int i = 0; i < n; i++) {
593 c_if_cond(comp, pns->nodes[i], true, label);
594 }
595 }
596 return;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100598 if (jump_if == false) {
599 for (int i = 0; i < n; i++) {
600 c_if_cond(comp, pns->nodes[i], false, label);
601 }
602 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100603 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100604 for (int i = 0; i < n - 1; i++) {
605 c_if_cond(comp, pns->nodes[i], false, label2);
606 }
607 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000608 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100609 }
610 return;
Damiend99b0522013-12-21 18:17:45 +0000611 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100612 c_if_cond(comp, pns->nodes[0], !jump_if, label);
613 return;
614 }
615 }
616
617 // nothing special, fall back to default compiling for node and jump
618 compile_node(comp, pn);
619 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000620 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100621 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000622 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100623 }
624#endif
Damien429d7192013-10-04 19:53:11 +0100625}
626
627typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000628void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100629
Damiend99b0522013-12-21 18:17:45 +0000630void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100631 if (assign_kind != ASSIGN_AUG_STORE) {
632 compile_node(comp, pns->nodes[0]);
633 }
634
Damiend99b0522013-12-21 18:17:45 +0000635 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
636 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
637 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
638 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100639 if (assign_kind != ASSIGN_AUG_STORE) {
640 for (int i = 0; i < n - 1; i++) {
641 compile_node(comp, pns1->nodes[i]);
642 }
643 }
Damiend99b0522013-12-21 18:17:45 +0000644 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
645 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100646 }
Damiend99b0522013-12-21 18:17:45 +0000647 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100648 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000649 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100650 if (assign_kind == ASSIGN_AUG_STORE) {
651 EMIT(rot_three);
652 EMIT(store_subscr);
653 } else {
654 compile_node(comp, pns1->nodes[0]);
655 if (assign_kind == ASSIGN_AUG_LOAD) {
656 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100657 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100658 } else {
659 EMIT(store_subscr);
660 }
661 }
Damiend99b0522013-12-21 18:17:45 +0000662 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
663 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100664 if (assign_kind == ASSIGN_AUG_LOAD) {
665 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000666 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100667 } else {
668 if (assign_kind == ASSIGN_AUG_STORE) {
669 EMIT(rot_two);
670 }
Damien Georgeb9791222014-01-23 00:34:21 +0000671 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100672 }
673 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100674 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100675 }
676 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100677 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100678 }
679
Damiend99b0522013-12-21 18:17:45 +0000680 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100681 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100682 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100683
684 return;
685
686cannot_assign:
687 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100688}
689
Damien George0288cf02014-04-11 11:53:00 +0000690// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
691void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
692 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
693
694 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100695 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000696 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
697 EMIT_ARG(unpack_ex, 0, num_tail);
698 have_star_index = 0;
699 }
700 for (int i = 0; i < num_tail; i++) {
701 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100702 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000703 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
704 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100705 } else {
Damien George0288cf02014-04-11 11:53:00 +0000706 compile_syntax_error(comp, nodes_tail[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100707 return;
708 }
709 }
710 }
711 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000712 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100713 }
Damien George0288cf02014-04-11 11:53:00 +0000714 if (num_head != 0) {
715 if (0 == have_star_index) {
716 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100717 } else {
Damien George0288cf02014-04-11 11:53:00 +0000718 c_assign(comp, node_head, ASSIGN_STORE);
719 }
720 }
721 for (int i = 0; i < num_tail; i++) {
722 if (num_head + i == have_star_index) {
723 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
724 } else {
725 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100726 }
727 }
728}
729
730// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000731void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100732 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000733 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100734 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000735 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
736 if (MP_PARSE_NODE_IS_ID(pn)) {
737 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100738 switch (assign_kind) {
739 case ASSIGN_STORE:
740 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000741 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100742 break;
743 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000744 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100745 break;
746 }
747 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100748 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100749 return;
750 }
751 } else {
Damiend99b0522013-12-21 18:17:45 +0000752 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
753 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100754 case PN_power:
755 // lhs is an index or attribute
756 c_assign_power(comp, pns, assign_kind);
757 break;
758
759 case PN_testlist_star_expr:
760 case PN_exprlist:
761 // lhs is a tuple
762 if (assign_kind != ASSIGN_STORE) {
763 goto bad_aug;
764 }
Damien George0288cf02014-04-11 11:53:00 +0000765 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100766 break;
767
768 case PN_atom_paren:
769 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000770 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100771 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100772 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100773 return;
Damiend99b0522013-12-21 18:17:45 +0000774 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
775 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100776 goto testlist_comp;
777 } else {
778 // parenthesis around 1 item, is just that item
779 pn = pns->nodes[0];
780 goto tail_recursion;
781 }
782 break;
783
784 case PN_atom_bracket:
785 // lhs is something in brackets
786 if (assign_kind != ASSIGN_STORE) {
787 goto bad_aug;
788 }
Damiend99b0522013-12-21 18:17:45 +0000789 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100790 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000791 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000792 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
793 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100794 goto testlist_comp;
795 } else {
796 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000797 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100798 }
799 break;
800
801 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
803 return;
Damien429d7192013-10-04 19:53:11 +0100804 }
805 return;
806
807 testlist_comp:
808 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000809 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
810 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
811 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100812 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000813 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000814 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000815 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100816 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000817 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
818 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000819 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100820 // TODO can we ever get here? can it be compiled?
821 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
822 return;
Damien429d7192013-10-04 19:53:11 +0100823 } else {
824 // sequence with 2 items
825 goto sequence_with_2_items;
826 }
827 } else {
828 // sequence with 2 items
829 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000830 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100831 }
832 return;
833 }
834 return;
835
836 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100837 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100838}
839
840// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100841// if we are not in CPython compatibility mode then:
842// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
843// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
844// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100845void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
846 assert(n_pos_defaults >= 0);
847 assert(n_kw_defaults >= 0);
848
Damien429d7192013-10-04 19:53:11 +0100849 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000850 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100851 int nfree = 0;
852 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000853 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
854 id_info_t *id = &comp->scope_cur->id_info[i];
855 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
856 for (int j = 0; j < this_scope->id_info_len; j++) {
857 id_info_t *id2 = &this_scope->id_info[j];
858 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000859#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000860 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000861#else
862 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100863 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000864#endif
Damien318aec62013-12-10 18:28:17 +0000865 nfree += 1;
866 }
867 }
Damien429d7192013-10-04 19:53:11 +0100868 }
869 }
870 }
Damien429d7192013-10-04 19:53:11 +0100871
872 // make the function/closure
873 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100874 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100875 } else {
Damien George3558f622014-04-20 17:50:40 +0100876 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100877 }
878}
879
Damiend99b0522013-12-21 18:17:45 +0000880void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000881 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100882 comp->have_star = true;
883 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000884 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
885 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100886 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100887 } else {
888 // named star
Damien429d7192013-10-04 19:53:11 +0100889 }
Damien George8b19db02014-04-11 23:25:34 +0100890 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000891
892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100893 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000894 // TODO do we need to do anything with this?
895
896 } else {
897 mp_parse_node_t pn_id;
898 mp_parse_node_t pn_colon;
899 mp_parse_node_t pn_equal;
900 if (MP_PARSE_NODE_IS_ID(pn)) {
901 // this parameter is just an id
902
903 pn_id = pn;
904 pn_colon = MP_PARSE_NODE_NULL;
905 pn_equal = MP_PARSE_NODE_NULL;
906
907 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
908 // this parameter has a colon and/or equal specifier
909
910 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
911 pn_id = pns->nodes[0];
912 pn_colon = pns->nodes[1];
913 pn_equal = pns->nodes[2];
914
915 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100916 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000917 assert(0);
918 return;
919 }
920
921 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
922 // this parameter does not have a default value
923
924 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100925 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100926 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000927 return;
928 }
929
930 } else {
931 // this parameter has a default value
932 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
933
Damien George8b19db02014-04-11 23:25:34 +0100934 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000935 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100936#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000937 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
938 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100939 // in Micro Python we put the default positional parameters into a tuple using the bytecode
940 // we need to do this here before we start building the map for the default keywords
941 if (comp->num_default_params > 0) {
942 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100943 } else {
944 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100945 }
Damien George69b89d22014-04-11 13:38:30 +0000946 // first default dict param, so make the map
947 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000948 }
Damien George69b89d22014-04-11 13:38:30 +0000949#endif
950 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
951 compile_node(comp, pn_equal);
952#if !MICROPY_EMIT_CPYTHON
953 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
954 EMIT(store_map);
955#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000956 } else {
Damien George69b89d22014-04-11 13:38:30 +0000957 comp->num_default_params += 1;
958 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000959 }
960 }
961
962 // TODO pn_colon not implemented
963 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100964 }
965}
966
967// leaves function object on stack
968// returns function name
Damiend99b0522013-12-21 18:17:45 +0000969qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100970 if (comp->pass == PASS_1) {
971 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000972 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100973 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000974 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100975 }
976
977 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100978 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000979 uint old_num_dict_params = comp->num_dict_params;
980 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100981
982 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100983 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000984 comp->num_dict_params = 0;
985 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100986 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000987
988 if (comp->had_error) {
989 return MP_QSTR_NULL;
990 }
991
Damien Georgee337f1e2014-03-31 15:18:37 +0100992#if !MICROPY_EMIT_CPYTHON
993 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100994 // the default keywords args may have already made the tuple; if not, do it now
995 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000996 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100997 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +0100998 }
999#endif
1000
Damien429d7192013-10-04 19:53:11 +01001001 // get the scope for this function
1002 scope_t *fscope = (scope_t*)pns->nodes[4];
1003
1004 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001005 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001006
1007 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001008 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001009 comp->num_dict_params = old_num_dict_params;
1010 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001011
1012 // return its name (the 'f' in "def f(...):")
1013 return fscope->simple_name;
1014}
1015
1016// leaves class object on stack
1017// returns class name
Damiend99b0522013-12-21 18:17:45 +00001018qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001019 if (comp->pass == PASS_1) {
1020 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001021 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001022 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001023 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001024 }
1025
1026 EMIT(load_build_class);
1027
1028 // scope for this class
1029 scope_t *cscope = (scope_t*)pns->nodes[3];
1030
1031 // compile the class
1032 close_over_variables_etc(comp, cscope, 0, 0);
1033
1034 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001035 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001036
1037 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001038 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1039 mp_parse_node_t parents = pns->nodes[1];
1040 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1041 parents = MP_PARSE_NODE_NULL;
1042 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001043 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001044 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001045
1046 // return its name (the 'C' in class C(...):")
1047 return cscope->simple_name;
1048}
1049
Damien6cdd3af2013-10-05 18:08:26 +01001050// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001051STATIC 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 +00001052 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001053 return false;
1054 }
1055
1056 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001057 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001058 return true;
1059 }
1060
Damiend99b0522013-12-21 18:17:45 +00001061 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001062 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001063 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001064#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001065 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001066 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001067 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001068 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001069#endif
Damien3ef4abb2013-10-12 16:53:13 +01001070#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001071 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001072 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001073#endif
Damien6cdd3af2013-10-05 18:08:26 +01001074 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001075 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001076 }
1077
1078 return true;
1079}
1080
Damiend99b0522013-12-21 18:17:45 +00001081void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001082 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001083 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001084 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1085
Damien6cdd3af2013-10-05 18:08:26 +01001086 // inherit emit options for this function/class definition
1087 uint emit_options = comp->scope_cur->emit_options;
1088
1089 // compile each decorator
1090 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001091 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001092 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1093 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001094
1095 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001096 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001097 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1098
1099 // check for built-in decorators
1100 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1101 // this was a built-in
1102 num_built_in_decorators += 1;
1103
1104 } else {
1105 // not a built-in, compile normally
1106
1107 // compile the decorator function
1108 compile_node(comp, name_nodes[0]);
1109 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001110 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001111 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001112 }
1113
1114 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001115 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001116 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001117 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001118 compile_node(comp, pns_decorator->nodes[1]);
1119 }
Damien429d7192013-10-04 19:53:11 +01001120 }
1121 }
1122
1123 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001124 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001125 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001126 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001127 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001128 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001129 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001130 } else {
1131 // shouldn't happen
1132 assert(0);
1133 }
1134
1135 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001136 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001137 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001138 }
1139
1140 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001141 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001142}
1143
Damiend99b0522013-12-21 18:17:45 +00001144void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001145 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001146 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001147 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001148}
1149
Damiend99b0522013-12-21 18:17:45 +00001150void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1151 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001152 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001153 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1154 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001155
1156 compile_node(comp, pns->nodes[0]); // base of the power node
1157
Damiend99b0522013-12-21 18:17:45 +00001158 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1159 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1160 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1161 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001162 for (int i = 0; i < n - 1; i++) {
1163 compile_node(comp, pns1->nodes[i]);
1164 }
Damiend99b0522013-12-21 18:17:45 +00001165 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1166 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001167 }
Damiend99b0522013-12-21 18:17:45 +00001168 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001169 // can't delete function calls
1170 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001171 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001172 compile_node(comp, pns1->nodes[0]);
1173 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001174 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1175 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001176 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001177 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001178 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001179 }
1180 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001181 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001182 }
1183
Damiend99b0522013-12-21 18:17:45 +00001184 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001185 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001186 }
Damiend99b0522013-12-21 18:17:45 +00001187 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1188 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1189 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1190 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001191 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1192
Damiend99b0522013-12-21 18:17:45 +00001193 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1194 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1195 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001196 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001197 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001198 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001199 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001200 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001201 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001202 c_del_stmt(comp, pns->nodes[0]);
1203 for (int i = 0; i < n; i++) {
1204 c_del_stmt(comp, pns1->nodes[i]);
1205 }
Damiend99b0522013-12-21 18:17:45 +00001206 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001207 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001208 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001209 } else {
1210 // sequence with 2 items
1211 goto sequence_with_2_items;
1212 }
1213 } else {
1214 // sequence with 2 items
1215 sequence_with_2_items:
1216 c_del_stmt(comp, pns->nodes[0]);
1217 c_del_stmt(comp, pns->nodes[1]);
1218 }
1219 } else {
1220 // tuple with 1 element
1221 c_del_stmt(comp, pn);
1222 }
1223 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001224 // TODO is there anything else to implement?
1225 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001226 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001227
1228 return;
1229
1230cannot_delete:
1231 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001232}
1233
Damiend99b0522013-12-21 18:17:45 +00001234void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001235 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1236}
1237
Damiend99b0522013-12-21 18:17:45 +00001238void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001239 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001240 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001241 }
Damien Georgecbddb272014-02-01 20:08:18 +00001242 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001243}
1244
Damiend99b0522013-12-21 18:17:45 +00001245void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001246 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001247 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001248 }
Damien Georgecbddb272014-02-01 20:08:18 +00001249 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001250}
1251
Damiend99b0522013-12-21 18:17:45 +00001252void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001253 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001254 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001255 return;
1256 }
Damiend99b0522013-12-21 18:17:45 +00001257 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001258 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001259 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001260 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001261 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001262 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1263 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 +01001264
Damien George6f355fd2014-04-10 14:11:31 +01001265 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001266 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1267 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1268 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001270 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1271 } else {
1272 compile_node(comp, pns->nodes[0]);
1273 }
1274 EMIT(return_value);
1275}
1276
Damiend99b0522013-12-21 18:17:45 +00001277void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001278 compile_node(comp, pns->nodes[0]);
1279 EMIT(pop_top);
1280}
1281
Damiend99b0522013-12-21 18:17:45 +00001282void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1283 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001284 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001285 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001286 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001287 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001288 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001289 compile_node(comp, pns->nodes[0]);
1290 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001291 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001292 } else {
1293 // raise x
1294 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001295 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001296 }
1297}
1298
Damien George635543c2014-04-10 12:56:52 +01001299// q_base holds the base of the name
1300// eg a -> q_base=a
1301// a.b.c -> q_base=a
1302void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001303 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001304 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1305 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001306 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001307 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001308 pn = pns->nodes[0];
1309 is_as = true;
1310 }
Damien George635543c2014-04-10 12:56:52 +01001311 if (MP_PARSE_NODE_IS_NULL(pn)) {
1312 // empty name (eg, from . import x)
1313 *q_base = MP_QSTR_;
1314 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1315 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001316 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001317 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001318 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001319 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001320 }
Damien George635543c2014-04-10 12:56:52 +01001321 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001322 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1323 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1324 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001325 // a name of the form a.b.c
1326 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001327 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001328 }
Damiend99b0522013-12-21 18:17:45 +00001329 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001330 int len = n - 1;
1331 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001332 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001333 }
Damien George55baff42014-01-21 21:40:13 +00001334 byte *q_ptr;
1335 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001336 for (int i = 0; i < n; i++) {
1337 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001338 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001339 }
Damien George55baff42014-01-21 21:40:13 +00001340 uint str_src_len;
1341 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001342 memcpy(str_dest, str_src, str_src_len);
1343 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001344 }
Damien George635543c2014-04-10 12:56:52 +01001345 qstr q_full = qstr_build_end(q_ptr);
1346 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001347 if (is_as) {
1348 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001349 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001350 }
1351 }
1352 } else {
Damien George635543c2014-04-10 12:56:52 +01001353 // shouldn't happen
1354 assert(0);
Damien429d7192013-10-04 19:53:11 +01001355 }
1356 } else {
Damien George635543c2014-04-10 12:56:52 +01001357 // shouldn't happen
1358 assert(0);
Damien429d7192013-10-04 19:53:11 +01001359 }
1360}
1361
Damiend99b0522013-12-21 18:17:45 +00001362void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001363 EMIT_ARG(load_const_small_int, 0); // level 0 import
1364 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1365 qstr q_base;
1366 do_import_name(comp, pn, &q_base);
1367 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001368}
1369
Damiend99b0522013-12-21 18:17:45 +00001370void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001371 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001375 mp_parse_node_t pn_import_source = pns->nodes[0];
1376
1377 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1378 uint import_level = 0;
1379 do {
1380 mp_parse_node_t pn_rel;
1381 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)) {
1382 // This covers relative imports with dots only like "from .. import"
1383 pn_rel = pn_import_source;
1384 pn_import_source = MP_PARSE_NODE_NULL;
1385 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1386 // This covers relative imports starting with dot(s) like "from .foo import"
1387 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1388 pn_rel = pns_2b->nodes[0];
1389 pn_import_source = pns_2b->nodes[1];
1390 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1391 } else {
1392 // Not a relative import
1393 break;
1394 }
1395
1396 // get the list of . and/or ...'s
1397 mp_parse_node_t *nodes;
1398 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1399
1400 // count the total number of .'s
1401 for (int i = 0; i < n; i++) {
1402 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1403 import_level++;
1404 } else {
1405 // should be an MP_TOKEN_ELLIPSIS
1406 import_level += 3;
1407 }
1408 }
1409 } while (0);
1410
Damiend99b0522013-12-21 18:17:45 +00001411 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001412 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001413
1414 // build the "fromlist" tuple
1415#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001417#else
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1419 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001420#endif
1421
1422 // do the import
Damien George635543c2014-04-10 12:56:52 +01001423 qstr dummy_q;
1424 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001425 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001426
Damien429d7192013-10-04 19:53:11 +01001427 } else {
Damien George635543c2014-04-10 12:56:52 +01001428 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001429
1430 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001431 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001432 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001433#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001434 {
1435 vstr_t *vstr = vstr_new();
1436 vstr_printf(vstr, "(");
1437 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001438 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1439 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1440 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001441 if (i > 0) {
1442 vstr_printf(vstr, ", ");
1443 }
1444 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001445 uint len;
1446 const byte *str = qstr_data(id2, &len);
1447 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001448 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001449 }
Damien02f89412013-12-12 15:13:36 +00001450 if (n == 1) {
1451 vstr_printf(vstr, ",");
1452 }
1453 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001455 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001456 }
Damiendb4c3612013-12-10 17:27:24 +00001457#else
1458 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001459 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1460 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1461 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001462 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001463 }
Damien Georgeb9791222014-01-23 00:34:21 +00001464 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001465#endif
1466
1467 // do the import
Damien George635543c2014-04-10 12:56:52 +01001468 qstr dummy_q;
1469 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001470 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001471 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1472 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1473 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001474 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001475 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001476 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001477 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001478 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001479 }
1480 }
1481 EMIT(pop_top);
1482 }
1483}
1484
Damiend99b0522013-12-21 18:17:45 +00001485void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001486 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001487 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1488 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001489 } else {
Damiend99b0522013-12-21 18:17:45 +00001490 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1491 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001492 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001493 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001494 }
Damien429d7192013-10-04 19:53:11 +01001495 }
1496 }
1497}
1498
Damiend99b0522013-12-21 18:17:45 +00001499void compile_nonlocal_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_nonlocal(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_nonlocal(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_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001514 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001515 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001516 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 +00001517 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001518 // assertion message
1519 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001520 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001521 }
Damien Georgeb9791222014-01-23 00:34:21 +00001522 EMIT_ARG(raise_varargs, 1);
1523 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001524}
1525
Damiend99b0522013-12-21 18:17:45 +00001526void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001527 // TODO proper and/or short circuiting
1528
Damien George6f355fd2014-04-10 14:11:31 +01001529 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001530
Damien George6f355fd2014-04-10 14:11:31 +01001531 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001532 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1533
1534 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001535
1536 if (
1537#if !MICROPY_EMIT_CPYTHON
1538 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1539 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1540#endif
1541 // optimisation to not jump if last instruction was return
1542 !EMIT(last_emit_was_return_value)
1543 ) {
1544 // jump over elif/else blocks
1545 EMIT_ARG(jump, l_end);
1546 }
1547
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001549
Damiend99b0522013-12-21 18:17:45 +00001550 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001551 // compile elif blocks
1552
Damiend99b0522013-12-21 18:17:45 +00001553 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001554
Damiend99b0522013-12-21 18:17:45 +00001555 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001556 // multiple elif blocks
1557
Damiend99b0522013-12-21 18:17:45 +00001558 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001559 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001560 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001561 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001562 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1563
1564 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001565 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001567 }
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001569 }
1570
1571 } else {
1572 // a single elif block
1573
Damienb05d7072013-10-05 13:37:10 +01001574 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001575 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1576
1577 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001578 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001579 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001580 }
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001582 }
1583 }
1584
1585 // compile else block
1586 compile_node(comp, pns->nodes[3]); // can be null
1587
Damien Georgeb9791222014-01-23 00:34:21 +00001588 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001589}
1590
Damien Georgecbddb272014-02-01 20:08:18 +00001591#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001592 uint old_break_label = comp->break_label; \
1593 uint old_continue_label = comp->continue_label; \
1594 uint break_label = comp_next_label(comp); \
1595 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001596 comp->break_label = break_label; \
1597 comp->continue_label = continue_label; \
1598 comp->break_continue_except_level = comp->cur_except_level;
1599
1600#define END_BREAK_CONTINUE_BLOCK \
1601 comp->break_label = old_break_label; \
1602 comp->continue_label = old_continue_label; \
1603 comp->break_continue_except_level = comp->cur_except_level;
1604
Damiend99b0522013-12-21 18:17:45 +00001605void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001606 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001607
Damience89a212013-10-15 22:25:17 +01001608 // compared to CPython, we have an optimised version of while loops
1609#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001610 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(setup_loop, break_label);
1612 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001613 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1614 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001615 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001616 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001617 }
Damien Georgeb9791222014-01-23 00:34:21 +00001618 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001619 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1620 // this is a small hack to agree with CPython
1621 if (!node_is_const_true(pns->nodes[0])) {
1622 EMIT(pop_block);
1623 }
Damience89a212013-10-15 22:25:17 +01001624#else
Damien George6f355fd2014-04-10 14:11:31 +01001625 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001626 EMIT_ARG(jump, continue_label);
1627 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001628 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001629 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001630 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1631#endif
1632
1633 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001634 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001635
1636 compile_node(comp, pns->nodes[2]); // else
1637
Damien Georgeb9791222014-01-23 00:34:21 +00001638 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001639}
1640
Damienf72fd0e2013-11-06 20:20:49 +00001641// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001642// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1643// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001644void 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 +00001645 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001646
Damien George6f355fd2014-04-10 14:11:31 +01001647 uint top_label = comp_next_label(comp);
1648 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001649
Damien George3ff2d032014-03-31 18:02:22 +01001650 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001651 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001652 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001653
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(jump, entry_label);
1655 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001656
Damien George3ff2d032014-03-31 18:02:22 +01001657 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001658 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001659
1660 // store next value to var
1661 c_assign(comp, pn_var, ASSIGN_STORE);
1662
Damienf3822fc2013-11-09 20:12:03 +00001663 // compile body
1664 compile_node(comp, pn_body);
1665
Damien Georgeb9791222014-01-23 00:34:21 +00001666 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001667
Damien George3ff2d032014-03-31 18:02:22 +01001668 // compile: var + step, duplicated on stack
1669 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001670 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001671 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001672 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001673
Damien Georgeb9791222014-01-23 00:34:21 +00001674 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001675
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001676 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001677 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001678 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1679 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001680 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001681 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001682 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001683 }
Damien Georgeb9791222014-01-23 00:34:21 +00001684 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001685
Damien George3ff2d032014-03-31 18:02:22 +01001686 // discard final value of var that failed the loop condition
1687 EMIT(pop_top);
1688
Damienf72fd0e2013-11-06 20:20:49 +00001689 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001690 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001691
1692 compile_node(comp, pn_else);
1693
Damien Georgeb9791222014-01-23 00:34:21 +00001694 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001695}
1696
Damiend99b0522013-12-21 18:17:45 +00001697void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001698#if !MICROPY_EMIT_CPYTHON
1699 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1700 // this is actually slower, but uses no heap memory
1701 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001702 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 +00001703 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001704 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1705 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1706 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1707 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001708 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1709 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001710 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001711 mp_parse_node_t pn_range_start;
1712 mp_parse_node_t pn_range_end;
1713 mp_parse_node_t pn_range_step;
1714 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001715 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001716 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001717 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001718 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001719 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001720 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001721 } else if (n_args == 2) {
1722 pn_range_start = args[0];
1723 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001724 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001725 } else {
1726 pn_range_start = args[0];
1727 pn_range_end = args[1];
1728 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001729 // We need to know sign of step. This is possible only if it's constant
1730 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1731 optimize = false;
1732 }
Damienf72fd0e2013-11-06 20:20:49 +00001733 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001734 }
1735 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001736 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1737 return;
1738 }
1739 }
1740 }
1741#endif
1742
Damien Georgecbddb272014-02-01 20:08:18 +00001743 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001744
Damien George6f355fd2014-04-10 14:11:31 +01001745 uint pop_label = comp_next_label(comp);
1746 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001747
Damience89a212013-10-15 22:25:17 +01001748 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1749#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001751#endif
1752
Damien429d7192013-10-04 19:53:11 +01001753 compile_node(comp, pns->nodes[1]); // iterator
1754 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001755 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001757 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1758 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001759 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001760 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001761 }
Damien Georgeb9791222014-01-23 00:34:21 +00001762 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001763 EMIT(for_iter_end);
1764
1765 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001766 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001767
Damience89a212013-10-15 22:25:17 +01001768#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001769 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001770#endif
Damien429d7192013-10-04 19:53:11 +01001771
1772 compile_node(comp, pns->nodes[3]); // else (not tested)
1773
Damien Georgeb9791222014-01-23 00:34:21 +00001774 EMIT_ARG(label_assign, break_label);
1775 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001776}
1777
Damiend99b0522013-12-21 18:17:45 +00001778void 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 +01001779 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001780 uint l1 = comp_next_label(comp);
1781 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001782
Damien Georgeb9791222014-01-23 00:34:21 +00001783 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001784 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001785
Damien429d7192013-10-04 19:53:11 +01001786 compile_node(comp, pn_body); // body
1787 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001788 EMIT_ARG(jump, success_label); // jump over exception handler
1789
1790 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001791 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 +00001792
Damien George6f355fd2014-04-10 14:11:31 +01001793 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001794
1795 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001796 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1797 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001798
1799 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001800 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001801
Damiend99b0522013-12-21 18:17:45 +00001802 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001803 // this is a catch all exception handler
1804 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001805 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001806 return;
1807 }
1808 } else {
1809 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001810 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1811 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1812 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1813 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001814 // handler binds the exception to a local
1815 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001816 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001817 }
1818 }
1819 EMIT(dup_top);
1820 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001821 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001822 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001823 }
1824
1825 EMIT(pop_top);
1826
1827 if (qstr_exception_local == 0) {
1828 EMIT(pop_top);
1829 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001831 }
1832
1833 EMIT(pop_top);
1834
Damien George6f355fd2014-04-10 14:11:31 +01001835 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001836 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001837 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001839 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001840 }
1841 compile_node(comp, pns_except->nodes[1]);
1842 if (qstr_exception_local != 0) {
1843 EMIT(pop_block);
1844 }
1845 EMIT(pop_except);
1846 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001847 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1848 EMIT_ARG(label_assign, l3);
1849 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1850 EMIT_ARG(store_id, qstr_exception_local);
1851 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001852
Damien George8dcc0c72014-03-27 10:55:21 +00001853 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001854 EMIT(end_finally);
1855 }
Damien Georgeb9791222014-01-23 00:34:21 +00001856 EMIT_ARG(jump, l2);
1857 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001858 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001859 }
1860
Damien George8dcc0c72014-03-27 10:55:21 +00001861 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001862 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001863 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001864
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001866 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001868}
1869
Damiend99b0522013-12-21 18:17:45 +00001870void 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 +01001871 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001872
Damien Georgeb9791222014-01-23 00:34:21 +00001873 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001874 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001875
Damien429d7192013-10-04 19:53:11 +01001876 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001877 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001878 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001879 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001880 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001881 } else {
1882 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1883 }
1884 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001885 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1886 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001887 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001888
Damien George8dcc0c72014-03-27 10:55:21 +00001889 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001890 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001891}
1892
Damiend99b0522013-12-21 18:17:45 +00001893void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1894 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1895 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1896 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001897 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001898 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1899 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001900 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001901 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001902 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001903 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001904 // no finally
1905 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1906 } else {
1907 // have finally
Damiend99b0522013-12-21 18:17:45 +00001908 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 +01001909 }
1910 } else {
1911 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001912 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001913 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001914 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001915 }
1916 } else {
1917 // shouldn't happen
1918 assert(0);
1919 }
1920}
1921
Damiend99b0522013-12-21 18:17:45 +00001922void 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 +01001923 if (n == 0) {
1924 // no more pre-bits, compile the body of the with
1925 compile_node(comp, body);
1926 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001927 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001928 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001929 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001930 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001931 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001932 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001933 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1934 } else {
1935 // this pre-bit is just an expression
1936 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001937 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001938 EMIT(pop_top);
1939 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001940 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001941 // compile additional pre-bits and the body
1942 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1943 // finish this with block
1944 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001945 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1946 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001947 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001948 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001949 EMIT(end_finally);
1950 }
1951}
1952
Damiend99b0522013-12-21 18:17:45 +00001953void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001954 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001955 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001956 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1957 assert(n > 0);
1958
1959 // compile in a nested fashion
1960 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1961}
1962
Damiend99b0522013-12-21 18:17:45 +00001963void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1964 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001965 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1966 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001967 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001968 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001969 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001970 EMIT(pop_top);
1971
Damien429d7192013-10-04 19:53:11 +01001972 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001973 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001974 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001975 // do nothing with a lonely constant
1976 } else {
1977 compile_node(comp, pns->nodes[0]); // just an expression
1978 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1979 }
Damien429d7192013-10-04 19:53:11 +01001980 }
1981 } else {
Damiend99b0522013-12-21 18:17:45 +00001982 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1983 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001984 if (kind == PN_expr_stmt_augassign) {
1985 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1986 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001987 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001988 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001989 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001990 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1991 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1992 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1993 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1994 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1995 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1996 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1997 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1998 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1999 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2000 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2001 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2002 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002003 }
Damien George7e5fb242014-02-01 22:18:47 +00002004 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002005 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2006 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002007 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2008 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002009 // following CPython, we store left-most first
2010 if (rhs > 0) {
2011 EMIT(dup_top);
2012 }
2013 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2014 for (int i = 0; i < rhs; i++) {
2015 if (i + 1 < rhs) {
2016 EMIT(dup_top);
2017 }
Damiend99b0522013-12-21 18:17:45 +00002018 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002019 }
2020 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002021 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2022 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2023 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2024 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002025 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002026 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2027 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002028 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2029 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2030 // can't optimise when it's a star expression on the lhs
2031 goto no_optimisation;
2032 }
Damien429d7192013-10-04 19:53:11 +01002033 compile_node(comp, pns10->nodes[0]); // rhs
2034 compile_node(comp, pns10->nodes[1]); // rhs
2035 EMIT(rot_two);
2036 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2037 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002038 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2039 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2040 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2041 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002042 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002043 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2044 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002045 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2046 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2047 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2048 // can't optimise when it's a star expression on the lhs
2049 goto no_optimisation;
2050 }
Damien429d7192013-10-04 19:53:11 +01002051 compile_node(comp, pns10->nodes[0]); // rhs
2052 compile_node(comp, pns10->nodes[1]); // rhs
2053 compile_node(comp, pns10->nodes[2]); // rhs
2054 EMIT(rot_three);
2055 EMIT(rot_two);
2056 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2057 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2058 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2059 } else {
Damien George495d7812014-04-08 17:51:47 +01002060 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002061 compile_node(comp, pns1->nodes[0]); // rhs
2062 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2063 }
2064 } else {
2065 // shouldn't happen
2066 assert(0);
2067 }
2068 }
2069}
2070
Damien Georged17926d2014-03-30 13:35:08 +01002071void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002072 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002073 compile_node(comp, pns->nodes[0]);
2074 for (int i = 1; i < num_nodes; i += 1) {
2075 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002076 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002077 }
2078}
2079
Damiend99b0522013-12-21 18:17:45 +00002080void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2081 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2082 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002083
Damien George6f355fd2014-04-10 14:11:31 +01002084 uint l_fail = comp_next_label(comp);
2085 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002086 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2087 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(jump, l_end);
2089 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002090 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002091 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002092 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002093}
2094
Damiend99b0522013-12-21 18:17:45 +00002095void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002096 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002097 //mp_parse_node_t pn_params = pns->nodes[0];
2098 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002099
2100 if (comp->pass == PASS_1) {
2101 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002102 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 +01002103 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002104 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002105 }
2106
2107 // get the scope for this lambda
2108 scope_t *this_scope = (scope_t*)pns->nodes[2];
2109
2110 // make the lambda
2111 close_over_variables_etc(comp, this_scope, 0, 0);
2112}
2113
Damiend99b0522013-12-21 18:17:45 +00002114void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002115 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002116 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002117 for (int i = 0; i < n; i += 1) {
2118 compile_node(comp, pns->nodes[i]);
2119 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002120 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002121 }
2122 }
Damien Georgeb9791222014-01-23 00:34:21 +00002123 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002124}
2125
Damiend99b0522013-12-21 18:17:45 +00002126void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002127 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002128 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002129 for (int i = 0; i < n; i += 1) {
2130 compile_node(comp, pns->nodes[i]);
2131 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002132 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002133 }
2134 }
Damien Georgeb9791222014-01-23 00:34:21 +00002135 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002136}
2137
Damiend99b0522013-12-21 18:17:45 +00002138void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002139 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002140 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002141}
2142
Damiend99b0522013-12-21 18:17:45 +00002143void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002144 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002145 compile_node(comp, pns->nodes[0]);
2146 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002147 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002148 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002149 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002150 }
2151 for (int i = 1; i + 1 < num_nodes; i += 2) {
2152 compile_node(comp, pns->nodes[i + 1]);
2153 if (i + 2 < num_nodes) {
2154 EMIT(dup_top);
2155 EMIT(rot_three);
2156 }
Damien George7e5fb242014-02-01 22:18:47 +00002157 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002158 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002159 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002160 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2161 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2162 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2163 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2164 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2165 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2166 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2167 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002168 }
2169 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002170 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2171 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2172 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002173 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002175 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002176 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002177 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002178 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002179 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002180 }
2181 } else {
2182 // shouldn't happen
2183 assert(0);
2184 }
2185 } else {
2186 // shouldn't happen
2187 assert(0);
2188 }
2189 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002190 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002191 }
2192 }
2193 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002194 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002195 EMIT_ARG(jump, l_end);
2196 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002197 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002198 EMIT(rot_two);
2199 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002200 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002201 }
2202}
2203
Damiend99b0522013-12-21 18:17:45 +00002204void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002205 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002206}
2207
Damiend99b0522013-12-21 18:17:45 +00002208void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002209 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002210}
2211
Damiend99b0522013-12-21 18:17:45 +00002212void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002213 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002214}
2215
Damiend99b0522013-12-21 18:17:45 +00002216void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002217 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002218}
2219
Damiend99b0522013-12-21 18:17:45 +00002220void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2221 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002222 compile_node(comp, pns->nodes[0]);
2223 for (int i = 1; i + 1 < num_nodes; i += 2) {
2224 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002225 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002226 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002227 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002228 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002229 } else {
2230 // shouldn't happen
2231 assert(0);
2232 }
2233 }
2234}
2235
Damiend99b0522013-12-21 18:17:45 +00002236void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2237 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002238 compile_node(comp, pns->nodes[0]);
2239 for (int i = 1; i + 1 < num_nodes; i += 2) {
2240 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002241 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002242 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002243 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002244 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002245 } else {
2246 // shouldn't happen
2247 assert(0);
2248 }
2249 }
2250}
2251
Damiend99b0522013-12-21 18:17:45 +00002252void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2253 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002254 compile_node(comp, pns->nodes[0]);
2255 for (int i = 1; i + 1 < num_nodes; i += 2) {
2256 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002257 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002258 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002259 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002260 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002261 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002262 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002263 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002264 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002265 } else {
2266 // shouldn't happen
2267 assert(0);
2268 }
2269 }
2270}
2271
Damiend99b0522013-12-21 18:17:45 +00002272void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002273 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002274 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002275 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002276 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002277 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002278 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002279 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002280 } else {
2281 // shouldn't happen
2282 assert(0);
2283 }
2284}
2285
Damien George35e2a4e2014-02-05 00:51:47 +00002286void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2287 // this is to handle special super() call
2288 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2289
2290 compile_generic_all_nodes(comp, pns);
2291}
2292
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002293STATIC 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 +01002294 // function to call is on top of stack
2295
Damien George35e2a4e2014-02-05 00:51:47 +00002296#if !MICROPY_EMIT_CPYTHON
2297 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002298 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 +00002299 EMIT_ARG(load_id, MP_QSTR___class__);
2300 // get first argument to function
2301 bool found = false;
2302 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002303 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2304 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 +00002305 found = true;
2306 break;
2307 }
2308 }
2309 if (!found) {
2310 printf("TypeError: super() call cannot find self\n");
2311 return;
2312 }
Damien George922ddd62014-04-09 12:43:17 +01002313 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002314 return;
2315 }
2316#endif
2317
Damien George2c0842b2014-04-27 16:46:51 +01002318 // get the list of arguments
2319 mp_parse_node_t *args;
2320 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002321
Damien George2c0842b2014-04-27 16:46:51 +01002322 // compile the arguments
2323 // Rather than calling compile_node on the list, we go through the list of args
2324 // explicitly here so that we can count the number of arguments and give sensible
2325 // error messages.
2326 int n_positional = n_positional_extra;
2327 uint n_keyword = 0;
2328 uint star_flags = 0;
2329 for (int i = 0; i < n_args; i++) {
2330 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2331 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2332 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2333 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2334 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2335 return;
2336 }
2337 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2338 compile_node(comp, pns_arg->nodes[0]);
2339 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2340 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2341 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2342 return;
2343 }
2344 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2345 compile_node(comp, pns_arg->nodes[0]);
2346 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2347 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2348 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2349 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2350 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2351 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2352 return;
2353 }
2354 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
2355 compile_node(comp, pns2->nodes[0]);
2356 n_keyword += 1;
2357 } else {
2358 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2359 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2360 n_positional++;
2361 }
2362 } else {
2363 goto normal_argument;
2364 }
2365 } else {
2366 normal_argument:
2367 if (n_keyword > 0) {
2368 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2369 return;
2370 }
2371 compile_node(comp, args[i]);
2372 n_positional++;
2373 }
Damien429d7192013-10-04 19:53:11 +01002374 }
2375
Damien George2c0842b2014-04-27 16:46:51 +01002376 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002377 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002378 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002379 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002380 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002381 }
Damien429d7192013-10-04 19:53:11 +01002382}
2383
Damiend99b0522013-12-21 18:17:45 +00002384void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2385 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002386 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002387 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 +01002388 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002389 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2390 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002391 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002392 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002393 i += 1;
2394 } else {
2395 compile_node(comp, pns->nodes[i]);
2396 }
Damien George35e2a4e2014-02-05 00:51:47 +00002397 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002398 }
2399}
2400
Damiend99b0522013-12-21 18:17:45 +00002401void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002402 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002403 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002404}
2405
Damiend99b0522013-12-21 18:17:45 +00002406void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002407 // a list of strings
Damien63321742013-12-10 17:41:49 +00002408
2409 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002410 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002411 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002412 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002413 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002414 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2415 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2416 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002417 if (i == 0) {
2418 string_kind = pn_kind;
2419 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002420 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002421 return;
2422 }
Damien George55baff42014-01-21 21:40:13 +00002423 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002424 }
Damien63321742013-12-10 17:41:49 +00002425
Damien63321742013-12-10 17:41:49 +00002426 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002427 byte *q_ptr;
2428 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002429 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002430 uint s_len;
2431 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002432 memcpy(s_dest, s, s_len);
2433 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002434 }
Damien George55baff42014-01-21 21:40:13 +00002435 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002436
Damien Georgeb9791222014-01-23 00:34:21 +00002437 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002438}
2439
2440// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002441void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2442 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2443 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2444 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002445
2446 if (comp->pass == PASS_1) {
2447 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002448 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 +01002449 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002450 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002451 }
2452
2453 // get the scope for this comprehension
2454 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2455
2456 // compile the comprehension
2457 close_over_variables_etc(comp, this_scope, 0, 0);
2458
2459 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2460 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002461 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002462}
2463
Damiend99b0522013-12-21 18:17:45 +00002464void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2465 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002466 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002467 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2468 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2469 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2470 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2471 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2472 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2473 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002474 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002475 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002476 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002477 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002478 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002479 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002480 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002481 // generator expression
2482 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2483 } else {
2484 // tuple with 2 items
2485 goto tuple_with_2_items;
2486 }
2487 } else {
2488 // tuple with 2 items
2489 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002490 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002491 }
2492 } else {
2493 // parenthesis around a single item, is just that item
2494 compile_node(comp, pns->nodes[0]);
2495 }
2496}
2497
Damiend99b0522013-12-21 18:17:45 +00002498void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2499 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002500 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002502 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2503 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2504 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2505 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2506 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002507 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002508 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002509 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002510 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002511 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002512 // list of many items
2513 compile_node(comp, pns2->nodes[0]);
2514 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002516 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002517 // list comprehension
2518 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2519 } else {
2520 // list with 2 items
2521 goto list_with_2_items;
2522 }
2523 } else {
2524 // list with 2 items
2525 list_with_2_items:
2526 compile_node(comp, pns2->nodes[0]);
2527 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002529 }
2530 } else {
2531 // list with 1 item
2532 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002534 }
2535}
2536
Damiend99b0522013-12-21 18:17:45 +00002537void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2538 mp_parse_node_t pn = pns->nodes[0];
2539 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002540 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002542 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2543 pns = (mp_parse_node_struct_t*)pn;
2544 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002545 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002546 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002547 compile_node(comp, pn);
2548 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002549 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2550 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2551 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2552 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002553 // dict/set with multiple elements
2554
2555 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002556 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002557 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2558
2559 // first element sets whether it's a dict or set
2560 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002561 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002562 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002563 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002564 compile_node(comp, pns->nodes[0]);
2565 EMIT(store_map);
2566 is_dict = true;
2567 } else {
2568 // a set
2569 compile_node(comp, pns->nodes[0]); // 1st value of set
2570 is_dict = false;
2571 }
2572
2573 // process rest of elements
2574 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002575 mp_parse_node_t pn = nodes[i];
2576 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002577 compile_node(comp, pn);
2578 if (is_dict) {
2579 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002580 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002581 return;
2582 }
2583 EMIT(store_map);
2584 } else {
2585 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002586 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002587 return;
2588 }
2589 }
2590 }
2591
2592 // if it's a set, build it
2593 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002594 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002595 }
Damiend99b0522013-12-21 18:17:45 +00002596 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002597 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002598 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002599 // a dictionary comprehension
2600 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2601 } else {
2602 // a set comprehension
2603 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2604 }
2605 } else {
2606 // shouldn't happen
2607 assert(0);
2608 }
2609 } else {
2610 // set with one element
2611 goto set_with_one_element;
2612 }
2613 } else {
2614 // set with one element
2615 set_with_one_element:
2616 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002617 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002618 }
2619}
2620
Damiend99b0522013-12-21 18:17:45 +00002621void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002622 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002623}
2624
Damiend99b0522013-12-21 18:17:45 +00002625void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002626 // object who's index we want is on top of stack
2627 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002628 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002629}
2630
Damiend99b0522013-12-21 18:17:45 +00002631void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002632 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002633 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002634}
2635
Damiend99b0522013-12-21 18:17:45 +00002636void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2637 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2638 mp_parse_node_t pn = pns->nodes[0];
2639 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002640 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002641 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2642 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002643 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2644 pns = (mp_parse_node_struct_t*)pn;
2645 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002646 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002647 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002648 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002649 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002650 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002651 } else {
2652 // [?::x]
2653 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002654 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002655 }
Damiend99b0522013-12-21 18:17:45 +00002656 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002657 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002658 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2659 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2660 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2661 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002662 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002663 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002664 } else {
2665 // [?:x:x]
2666 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002667 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002668 }
2669 } else {
2670 // [?:x]
2671 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002672 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002673 }
2674 } else {
2675 // [?:x]
2676 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002677 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002678 }
2679}
2680
Damiend99b0522013-12-21 18:17:45 +00002681void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002682 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002683 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2684 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002685}
2686
Damiend99b0522013-12-21 18:17:45 +00002687void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002688 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002689 compile_subscript_3_helper(comp, pns);
2690}
2691
Damiend99b0522013-12-21 18:17:45 +00002692void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002693 // if this is called then we are compiling a dict key:value pair
2694 compile_node(comp, pns->nodes[1]); // value
2695 compile_node(comp, pns->nodes[0]); // key
2696}
2697
Damiend99b0522013-12-21 18:17:45 +00002698void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002699 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002700 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002701 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002702}
2703
Damiend99b0522013-12-21 18:17:45 +00002704void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002705 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002706 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002707 return;
2708 }
Damiend99b0522013-12-21 18:17:45 +00002709 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002710 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002711 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002712 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2713 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002714 compile_node(comp, pns->nodes[0]);
2715 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002716 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002717 EMIT(yield_from);
2718 } else {
2719 compile_node(comp, pns->nodes[0]);
2720 EMIT(yield_value);
2721 }
2722}
2723
Damiend99b0522013-12-21 18:17:45 +00002724typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002725STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002726 NULL,
2727#define nc NULL
2728#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002729#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002730#include "grammar.h"
2731#undef nc
2732#undef c
2733#undef DEF_RULE
2734};
2735
Damiend99b0522013-12-21 18:17:45 +00002736void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2737 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002738 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002739 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2740 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2741 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002742 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002743 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002744 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002745 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002746 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2747 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2748 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2749 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002750 case MP_PARSE_NODE_TOKEN:
2751 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002752 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002753 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002754 // do nothing
2755 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002756 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002757 }
2758 break;
Damien429d7192013-10-04 19:53:11 +01002759 default: assert(0);
2760 }
2761 } else {
Damiend99b0522013-12-21 18:17:45 +00002762 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002763 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002764 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002765 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002766 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002767#if MICROPY_DEBUG_PRINTERS
2768 mp_parse_node_print(pn, 0);
2769#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002770 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002771 } else {
2772 f(comp, pns);
2773 }
2774 }
2775}
2776
Damiend99b0522013-12-21 18:17:45 +00002777void 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 +01002778 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002779 qstr param_name = MP_QSTR_NULL;
2780 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002781 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2782 if (MP_PARSE_NODE_IS_ID(pn)) {
2783 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002784 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002785 // comes after a star, so counts as a keyword-only parameter
2786 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002787 } else {
Damien George2827d622014-04-27 15:50:52 +01002788 // comes before a star, so counts as a positional parameter
2789 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002790 }
Damienb14de212013-10-06 00:28:28 +01002791 } else {
Damiend99b0522013-12-21 18:17:45 +00002792 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2793 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2794 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2795 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002796 //int node_index = 1; unused
2797 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002798 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002799 // this parameter has an annotation
2800 pn_annotation = pns->nodes[1];
2801 }
2802 //node_index = 2; unused
2803 }
2804 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002805 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002806 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002807 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002808 comp->scope_cur->num_dict_params += 1;
2809 } else {
2810 comp->scope_cur->num_default_params += 1;
2811 }
2812 }
2813 */
Damien George8b19db02014-04-11 23:25:34 +01002814 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002815 // comes after a star, so counts as a keyword-only parameter
2816 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002817 } else {
Damien George2827d622014-04-27 15:50:52 +01002818 // comes before a star, so counts as a positional parameter
2819 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002820 }
Damiend99b0522013-12-21 18:17:45 +00002821 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002822 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002823 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002824 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002825 // bare star
2826 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002827 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002828 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002829 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002830 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002831 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2832 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002833 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002834 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002835 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2836 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002837 pn_annotation = pns->nodes[1];
2838 } else {
2839 // shouldn't happen
2840 assert(0);
2841 }
Damiend99b0522013-12-21 18:17:45 +00002842 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2843 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002844 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002845 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002846 // this parameter has an annotation
2847 pn_annotation = pns->nodes[1];
2848 }
Damien George8725f8f2014-02-15 19:33:11 +00002849 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002850 } else {
Damienb14de212013-10-06 00:28:28 +01002851 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002852 assert(0);
2853 }
Damien429d7192013-10-04 19:53:11 +01002854 }
2855
Damien George2827d622014-04-27 15:50:52 +01002856 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002857 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002858 // TODO this parameter has an annotation
2859 }
2860 bool added;
2861 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2862 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002863 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002864 return;
2865 }
Damien429d7192013-10-04 19:53:11 +01002866 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002867 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002868 }
2869}
2870
Damien George2827d622014-04-27 15:50:52 +01002871STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002872 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2873}
2874
Damien George2827d622014-04-27 15:50:52 +01002875STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002876 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2877}
2878
Damiend99b0522013-12-21 18:17:45 +00002879void 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 +01002880 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002881 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002882 // no more nested if/for; compile inner expression
2883 compile_node(comp, pn_inner_expr);
2884 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002885 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002886 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002888 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002889 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002890 } else {
2891 EMIT(yield_value);
2892 EMIT(pop_top);
2893 }
Damiend99b0522013-12-21 18:17:45 +00002894 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002895 // if condition
Damiend99b0522013-12-21 18:17:45 +00002896 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002897 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2898 pn_iter = pns_comp_if->nodes[1];
2899 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002900 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002901 // for loop
Damiend99b0522013-12-21 18:17:45 +00002902 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002903 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002904 uint l_end2 = comp_next_label(comp);
2905 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002906 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002907 EMIT_ARG(label_assign, l_top2);
2908 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002909 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2910 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 +00002911 EMIT_ARG(jump, l_top2);
2912 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002913 EMIT(for_iter_end);
2914 } else {
2915 // shouldn't happen
2916 assert(0);
2917 }
2918}
2919
Damien George1463c1f2014-04-25 23:52:57 +01002920STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
2921#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002922 // see http://www.python.org/dev/peps/pep-0257/
2923
2924 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002925 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002926 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002927 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002928 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002929 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2930 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002931 for (int i = 0; i < num_nodes; i++) {
2932 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002933 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 +00002934 // not a newline, so this is the first statement; finish search
2935 break;
2936 }
2937 }
2938 // 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 +00002939 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002940 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002941 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002942 } else {
2943 return;
2944 }
2945
2946 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002947 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2948 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2949 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2950 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2951 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002952 compile_node(comp, pns->nodes[0]); // a doc string
2953 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002954 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002955 }
2956 }
2957 }
Damien George1463c1f2014-04-25 23:52:57 +01002958#endif
Damien429d7192013-10-04 19:53:11 +01002959}
2960
Damien George1463c1f2014-04-25 23:52:57 +01002961STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002962 comp->pass = pass;
2963 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002964 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002965 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002966
2967 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002968 // reset maximum stack sizes in scope
2969 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002970 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002971 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002972 }
2973
Damien5ac1b2e2013-10-18 19:58:12 +01002974#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002975 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002976 scope_print_info(scope);
2977 }
Damien5ac1b2e2013-10-18 19:58:12 +01002978#endif
Damien429d7192013-10-04 19:53:11 +01002979
2980 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002981 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2982 assert(scope->kind == SCOPE_MODULE);
2983 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2984 compile_node(comp, pns->nodes[0]); // compile the expression
2985 EMIT(return_value);
2986 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002987 if (!comp->is_repl) {
2988 check_for_doc_string(comp, scope->pn);
2989 }
Damien429d7192013-10-04 19:53:11 +01002990 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002992 EMIT(return_value);
2993 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002994 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2995 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2996 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002997
2998 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002999 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003000 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003001 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003002 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3003 }
3004
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003005 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003006
3007 compile_node(comp, pns->nodes[3]); // 3 is function body
3008 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003009 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003011 EMIT(return_value);
3012 }
3013 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003014 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3015 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3016 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003017
3018 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003019 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003020 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003021 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003022 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3023 }
3024
3025 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003026
3027 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3028 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3029 EMIT(pop_top);
3030 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3031 }
Damien429d7192013-10-04 19:53:11 +01003032 EMIT(return_value);
3033 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3034 // a bit of a hack at the moment
3035
Damiend99b0522013-12-21 18:17:45 +00003036 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3037 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3038 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3039 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3040 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003041
Damien George55baff42014-01-21 21:40:13 +00003042 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003043 if (comp->pass == PASS_1) {
3044 bool added;
3045 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3046 assert(added);
3047 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003048 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003049 }
3050
3051 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003052 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003053 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003055 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003056 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003057 }
3058
Damien George6f355fd2014-04-10 14:11:31 +01003059 uint l_end = comp_next_label(comp);
3060 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003061 EMIT_ARG(load_id, qstr_arg);
3062 EMIT_ARG(label_assign, l_top);
3063 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003064 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3065 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003066 EMIT_ARG(jump, l_top);
3067 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003068 EMIT(for_iter_end);
3069
3070 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003071 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003072 }
3073 EMIT(return_value);
3074 } else {
3075 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003076 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3077 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3078 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003079
3080 if (comp->pass == PASS_1) {
3081 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003082 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003083 assert(added);
3084 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003085 }
3086
Damien Georgeb9791222014-01-23 00:34:21 +00003087 EMIT_ARG(load_id, MP_QSTR___name__);
3088 EMIT_ARG(store_id, MP_QSTR___module__);
3089 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3090 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003091
3092 check_for_doc_string(comp, pns->nodes[2]);
3093 compile_node(comp, pns->nodes[2]); // 2 is class body
3094
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003095 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003096 assert(id != NULL);
3097 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003098 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003099 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003100#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003101 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003102#else
Damien George2bf7c092014-04-09 15:26:46 +01003103 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003104#endif
Damien429d7192013-10-04 19:53:11 +01003105 }
3106 EMIT(return_value);
3107 }
3108
Damien415eb6f2013-10-05 12:19:06 +01003109 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003110
3111 // make sure we match all the exception levels
3112 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003113}
3114
Damien George094d4502014-04-02 17:31:27 +01003115#if MICROPY_EMIT_INLINE_THUMB
Damien George1463c1f2014-04-25 23:52:57 +01003116STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003117 comp->pass = pass;
3118 comp->scope_cur = scope;
3119 comp->next_label = 1;
3120
3121 if (scope->kind != SCOPE_FUNCTION) {
3122 printf("Error: inline assembler must be a function\n");
3123 return;
3124 }
3125
Damiena2f2f7d2013-10-06 00:14:13 +01003126 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003127 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003128 }
3129
Damien826005c2013-10-05 23:17:28 +01003130 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003131 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3132 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3133 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003134
Damiend99b0522013-12-21 18:17:45 +00003135 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003136
Damiena2f2f7d2013-10-06 00:14:13 +01003137 // parameters are in pns->nodes[1]
3138 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003139 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003140 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003141 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003142 }
3143
Damiend99b0522013-12-21 18:17:45 +00003144 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003145
Damiend99b0522013-12-21 18:17:45 +00003146 mp_parse_node_t pn_body = pns->nodes[3]; // body
3147 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003148 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3149
Damien Georgecbd2f742014-01-19 11:48:48 +00003150 /*
Damien826005c2013-10-05 23:17:28 +01003151 if (comp->pass == PASS_3) {
3152 //printf("----\n");
3153 scope_print_info(scope);
3154 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003155 */
Damien826005c2013-10-05 23:17:28 +01003156
3157 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003158 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3159 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003160 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3161 // no instructions
3162 continue;
3163 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3164 // an instruction; fall through
3165 } else {
3166 // not an instruction; error
3167 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3168 return;
3169 }
Damiend99b0522013-12-21 18:17:45 +00003170 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3171 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3172 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3173 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3174 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3175 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3176 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3177 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3178 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3179 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003180 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3181
3182 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003183 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003184 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003185 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003186 return;
3187 }
Damien George6f355fd2014-04-10 14:11:31 +01003188 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003189 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003190 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003191 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003192 } else if (op == MP_QSTR_align) {
3193 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3194 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3195 return;
3196 }
3197 if (pass > PASS_1) {
3198 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3199 }
3200 } else if (op == MP_QSTR_data) {
3201 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3202 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3203 return;
3204 }
3205 if (pass > PASS_1) {
3206 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3207 for (uint i = 1; i < n_args; i++) {
3208 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3209 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3210 return;
3211 }
3212 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3213 }
3214 }
Damien826005c2013-10-05 23:17:28 +01003215 } else {
3216 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003217 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003218 }
3219 }
3220 }
3221
3222 if (comp->pass > PASS_1) {
Damien Georgea26dc502014-04-12 17:54:52 +01003223 bool success = EMIT_INLINE_ASM(end_pass);
3224 if (!success) {
3225 comp->had_error = true;
3226 }
Damienb05d7072013-10-05 13:37:10 +01003227 }
Damien429d7192013-10-04 19:53:11 +01003228}
Damien George094d4502014-04-02 17:31:27 +01003229#endif
Damien429d7192013-10-04 19:53:11 +01003230
Damien George1463c1f2014-04-25 23:52:57 +01003231STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003232#if !MICROPY_EMIT_CPYTHON
3233 // in Micro Python we put the *x parameter after all other parameters (except **y)
3234 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3235 id_info_t *id_param = NULL;
3236 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3237 id_info_t *id = &scope->id_info[i];
3238 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3239 if (id_param != NULL) {
3240 // swap star param with last param
3241 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3242 }
3243 break;
3244 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3245 id_param = id;
3246 }
3247 }
3248 }
3249#endif
3250
Damien429d7192013-10-04 19:53:11 +01003251 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003252 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003253 scope->num_locals = 0;
3254 for (int i = 0; i < scope->id_info_len; i++) {
3255 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003256 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003257 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3258 continue;
3259 }
3260 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3261 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3262 }
Damien George2827d622014-04-27 15:50:52 +01003263 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003264 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003265 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003266 }
3267 }
3268
3269 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003270#if MICROPY_EMIT_CPYTHON
3271 int num_cell = 0;
3272#endif
Damien9ecbcff2013-12-11 00:41:43 +00003273 for (int i = 0; i < scope->id_info_len; i++) {
3274 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003275#if MICROPY_EMIT_CPYTHON
3276 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003277 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003278 id->local_num = num_cell;
3279 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003280 }
Damien George6baf76e2013-12-30 22:32:17 +00003281#else
3282 // in Micro Python the cells come right after the fast locals
3283 // parameters are not counted here, since they remain at the start
3284 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003285 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003286 id->local_num = scope->num_locals;
3287 scope->num_locals += 1;
3288 }
3289#endif
Damien9ecbcff2013-12-11 00:41:43 +00003290 }
Damien9ecbcff2013-12-11 00:41:43 +00003291
3292 // compute the index of free vars (freevars[idx] in CPython)
3293 // make sure they are in the order of the parent scope
3294 if (scope->parent != NULL) {
3295 int num_free = 0;
3296 for (int i = 0; i < scope->parent->id_info_len; i++) {
3297 id_info_t *id = &scope->parent->id_info[i];
3298 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3299 for (int j = 0; j < scope->id_info_len; j++) {
3300 id_info_t *id2 = &scope->id_info[j];
3301 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003302 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003303#if MICROPY_EMIT_CPYTHON
3304 // in CPython the frees are numbered after the cells
3305 id2->local_num = num_cell + num_free;
3306#else
3307 // in Micro Python the frees come first, before the params
3308 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003309#endif
3310 num_free += 1;
3311 }
3312 }
3313 }
Damien429d7192013-10-04 19:53:11 +01003314 }
Damien George6baf76e2013-12-30 22:32:17 +00003315#if !MICROPY_EMIT_CPYTHON
3316 // in Micro Python shift all other locals after the free locals
3317 if (num_free > 0) {
3318 for (int i = 0; i < scope->id_info_len; i++) {
3319 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003320 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003321 id->local_num += num_free;
3322 }
3323 }
Damien George2827d622014-04-27 15:50:52 +01003324 scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function
Damien George6baf76e2013-12-30 22:32:17 +00003325 scope->num_locals += num_free;
3326 }
3327#endif
Damien429d7192013-10-04 19:53:11 +01003328 }
3329
Damien George8725f8f2014-02-15 19:33:11 +00003330 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003331
3332#if MICROPY_EMIT_CPYTHON
3333 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003334 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) {
3335 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003336 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003337 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003338 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 +00003339 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003340 }
3341 }
Damien George882b3632014-04-02 15:56:31 +01003342#endif
3343
Damien429d7192013-10-04 19:53:11 +01003344 int num_free = 0;
3345 for (int i = 0; i < scope->id_info_len; i++) {
3346 id_info_t *id = &scope->id_info[i];
3347 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3348 num_free += 1;
3349 }
3350 }
3351 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003352 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003353 }
3354}
3355
Damien George65cad122014-04-06 11:48:15 +01003356mp_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 +01003357 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003358 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003359 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003360
Damien826005c2013-10-05 23:17:28 +01003361 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003362 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003363
3364 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003365 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003366
Damien826005c2013-10-05 23:17:28 +01003367 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003368 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003369 comp->emit_method_table = &emit_pass1_method_table;
3370 comp->emit_inline_asm = NULL;
3371 comp->emit_inline_asm_method_table = NULL;
3372 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003373 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003374 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003375#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003376 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003377 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003378#endif
Damien826005c2013-10-05 23:17:28 +01003379 } else {
3380 compile_scope(comp, s, PASS_1);
3381 }
3382
3383 // update maximim number of labels needed
3384 if (comp->next_label > max_num_labels) {
3385 max_num_labels = comp->next_label;
3386 }
Damien429d7192013-10-04 19:53:11 +01003387 }
3388
Damien826005c2013-10-05 23:17:28 +01003389 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003390 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003391 compile_scope_compute_things(comp, s);
3392 }
3393
Damien826005c2013-10-05 23:17:28 +01003394 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003395 emit_pass1_free(comp->emit);
3396
Damien826005c2013-10-05 23:17:28 +01003397 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003398#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003399 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003400#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003401 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003402#endif
Damien3ef4abb2013-10-12 16:53:13 +01003403#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003404 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003405#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003406#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003407 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003408 if (false) {
3409 // dummy
3410
Damien3ef4abb2013-10-12 16:53:13 +01003411#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003412 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003413 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003414 if (emit_inline_thumb == NULL) {
3415 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3416 }
3417 comp->emit = NULL;
3418 comp->emit_method_table = NULL;
3419 comp->emit_inline_asm = emit_inline_thumb;
3420 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3421 compile_scope_inline_asm(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003422 if (!comp->had_error) {
3423 compile_scope_inline_asm(comp, s, PASS_3);
3424 }
Damienc025ebb2013-10-12 14:30:21 +01003425#endif
3426
Damien826005c2013-10-05 23:17:28 +01003427 } else {
Damienc025ebb2013-10-12 14:30:21 +01003428
3429 // choose the emit type
3430
Damien3ef4abb2013-10-12 16:53:13 +01003431#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003432 comp->emit = emit_cpython_new(max_num_labels);
3433 comp->emit_method_table = &emit_cpython_method_table;
3434#else
Damien826005c2013-10-05 23:17:28 +01003435 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003436
3437#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003438 case MP_EMIT_OPT_NATIVE_PYTHON:
3439 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003440#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003441 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003442 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003443 }
Damien13ed3a62013-10-08 09:05:10 +01003444 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003445#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003446 if (emit_native == NULL) {
3447 emit_native = emit_native_thumb_new(max_num_labels);
3448 }
3449 comp->emit_method_table = &emit_native_thumb_method_table;
3450#endif
3451 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003452 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003453 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003454#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003455
Damien826005c2013-10-05 23:17:28 +01003456 default:
3457 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003458 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003459 }
3460 comp->emit = emit_bc;
3461 comp->emit_method_table = &emit_bc_method_table;
3462 break;
3463 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003464#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003465
3466 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003467 compile_scope(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003468 if (!comp->had_error) {
3469 compile_scope(comp, s, PASS_3);
3470 }
Damien6cdd3af2013-10-05 18:08:26 +01003471 }
Damien429d7192013-10-04 19:53:11 +01003472 }
3473
Damien George41d02b62014-01-24 22:42:28 +00003474 // free the emitters
3475#if !MICROPY_EMIT_CPYTHON
3476 if (emit_bc != NULL) {
3477 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003478 }
Damien George41d02b62014-01-24 22:42:28 +00003479#if MICROPY_EMIT_NATIVE
3480 if (emit_native != NULL) {
3481#if MICROPY_EMIT_X64
3482 emit_native_x64_free(emit_native);
3483#elif MICROPY_EMIT_THUMB
3484 emit_native_thumb_free(emit_native);
3485#endif
3486 }
3487#endif
3488#if MICROPY_EMIT_INLINE_THUMB
3489 if (emit_inline_thumb != NULL) {
3490 emit_inline_thumb_free(emit_inline_thumb);
3491 }
3492#endif
3493#endif // !MICROPY_EMIT_CPYTHON
3494
3495 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003496 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003497 for (scope_t *s = module_scope; s;) {
3498 scope_t *next = s->next;
3499 scope_free(s);
3500 s = next;
3501 }
Damien5ac1b2e2013-10-18 19:58:12 +01003502
Damien George41d02b62014-01-24 22:42:28 +00003503 // free the compiler
3504 bool had_error = comp->had_error;
3505 m_del_obj(compiler_t, comp);
3506
Damien George1fb03172014-01-03 14:22:03 +00003507 if (had_error) {
3508 // TODO return a proper error message
3509 return mp_const_none;
3510 } else {
3511#if MICROPY_EMIT_CPYTHON
3512 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003513 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003514 return mp_const_true;
3515#else
3516 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003517 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003518#endif
3519 }
Damien429d7192013-10-04 19:53:11 +01003520}