blob: b449ccf394e2ecf0d4e812c570687a51071778d4 [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 George9d181f62014-04-27 16:55:27 +0100706 compile_syntax_error(comp, nodes_tail[i], "multiple *x 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 George9d181f62014-04-27 16:55:27 +0100772 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000773 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
774 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100775 goto testlist_comp;
776 } else {
777 // parenthesis around 1 item, is just that item
778 pn = pns->nodes[0];
779 goto tail_recursion;
780 }
781 break;
782
783 case PN_atom_bracket:
784 // lhs is something in brackets
785 if (assign_kind != ASSIGN_STORE) {
786 goto bad_aug;
787 }
Damiend99b0522013-12-21 18:17:45 +0000788 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100789 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000790 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000791 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
792 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100793 goto testlist_comp;
794 } else {
795 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000796 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100797 }
798 break;
799
800 default:
Damien George9d181f62014-04-27 16:55:27 +0100801 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100802 }
803 return;
804
805 testlist_comp:
806 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000807 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
808 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
809 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100810 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000811 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000812 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000813 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100814 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000815 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
816 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000817 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100818 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100819 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100820 } else {
821 // sequence with 2 items
822 goto sequence_with_2_items;
823 }
824 } else {
825 // sequence with 2 items
826 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000827 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100828 }
829 return;
830 }
831 return;
832
Damien George9d181f62014-04-27 16:55:27 +0100833 cannot_assign:
834 compile_syntax_error(comp, pn, "can't assign to expression");
835 return;
836
Damien429d7192013-10-04 19:53:11 +0100837 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100838 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100839}
840
841// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100842// if we are not in CPython compatibility mode then:
843// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
844// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
845// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100846void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
847 assert(n_pos_defaults >= 0);
848 assert(n_kw_defaults >= 0);
849
Damien429d7192013-10-04 19:53:11 +0100850 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000851 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100852 int nfree = 0;
853 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000854 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
855 id_info_t *id = &comp->scope_cur->id_info[i];
856 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
857 for (int j = 0; j < this_scope->id_info_len; j++) {
858 id_info_t *id2 = &this_scope->id_info[j];
859 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000860#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000861 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000862#else
863 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100864 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000865#endif
Damien318aec62013-12-10 18:28:17 +0000866 nfree += 1;
867 }
868 }
Damien429d7192013-10-04 19:53:11 +0100869 }
870 }
871 }
Damien429d7192013-10-04 19:53:11 +0100872
873 // make the function/closure
874 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100875 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100876 } else {
Damien George3558f622014-04-20 17:50:40 +0100877 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100878 }
879}
880
Damiend99b0522013-12-21 18:17:45 +0000881void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000882 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100883 comp->have_star = true;
884 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000885 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
886 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100887 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100888 } else {
889 // named star
Damien429d7192013-10-04 19:53:11 +0100890 }
Damien George8b19db02014-04-11 23:25:34 +0100891 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000892
893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100894 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000895 // TODO do we need to do anything with this?
896
897 } else {
898 mp_parse_node_t pn_id;
899 mp_parse_node_t pn_colon;
900 mp_parse_node_t pn_equal;
901 if (MP_PARSE_NODE_IS_ID(pn)) {
902 // this parameter is just an id
903
904 pn_id = pn;
905 pn_colon = MP_PARSE_NODE_NULL;
906 pn_equal = MP_PARSE_NODE_NULL;
907
908 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
909 // this parameter has a colon and/or equal specifier
910
911 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
912 pn_id = pns->nodes[0];
913 pn_colon = pns->nodes[1];
914 pn_equal = pns->nodes[2];
915
916 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100917 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000918 assert(0);
919 return;
920 }
921
922 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
923 // this parameter does not have a default value
924
925 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100926 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100927 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000928 return;
929 }
930
931 } else {
932 // this parameter has a default value
933 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
934
Damien George8b19db02014-04-11 23:25:34 +0100935 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000936 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100937#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000938 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
939 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100940 // in Micro Python we put the default positional parameters into a tuple using the bytecode
941 // we need to do this here before we start building the map for the default keywords
942 if (comp->num_default_params > 0) {
943 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100944 } else {
945 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100946 }
Damien George69b89d22014-04-11 13:38:30 +0000947 // first default dict param, so make the map
948 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000949 }
Damien George69b89d22014-04-11 13:38:30 +0000950#endif
951 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
952 compile_node(comp, pn_equal);
953#if !MICROPY_EMIT_CPYTHON
954 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
955 EMIT(store_map);
956#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000957 } else {
Damien George69b89d22014-04-11 13:38:30 +0000958 comp->num_default_params += 1;
959 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000960 }
961 }
962
963 // TODO pn_colon not implemented
964 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100965 }
966}
967
968// leaves function object on stack
969// returns function name
Damiend99b0522013-12-21 18:17:45 +0000970qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100971 if (comp->pass == PASS_1) {
972 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000973 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100974 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000975 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100976 }
977
978 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +0100979 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +0000980 uint old_num_dict_params = comp->num_dict_params;
981 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100982
983 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +0100984 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000985 comp->num_dict_params = 0;
986 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100987 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000988
989 if (comp->had_error) {
990 return MP_QSTR_NULL;
991 }
992
Damien Georgee337f1e2014-03-31 15:18:37 +0100993#if !MICROPY_EMIT_CPYTHON
994 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +0100995 // the default keywords args may have already made the tuple; if not, do it now
996 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +0000997 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100998 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +0100999 }
1000#endif
1001
Damien429d7192013-10-04 19:53:11 +01001002 // get the scope for this function
1003 scope_t *fscope = (scope_t*)pns->nodes[4];
1004
1005 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001006 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001007
1008 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001009 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001010 comp->num_dict_params = old_num_dict_params;
1011 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001012
1013 // return its name (the 'f' in "def f(...):")
1014 return fscope->simple_name;
1015}
1016
1017// leaves class object on stack
1018// returns class name
Damiend99b0522013-12-21 18:17:45 +00001019qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001020 if (comp->pass == PASS_1) {
1021 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001022 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001023 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001024 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001025 }
1026
1027 EMIT(load_build_class);
1028
1029 // scope for this class
1030 scope_t *cscope = (scope_t*)pns->nodes[3];
1031
1032 // compile the class
1033 close_over_variables_etc(comp, cscope, 0, 0);
1034
1035 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001036 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001037
1038 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001039 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1040 mp_parse_node_t parents = pns->nodes[1];
1041 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1042 parents = MP_PARSE_NODE_NULL;
1043 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001044 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001045 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001046
1047 // return its name (the 'C' in class C(...):")
1048 return cscope->simple_name;
1049}
1050
Damien6cdd3af2013-10-05 18:08:26 +01001051// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001052STATIC 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 +00001053 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001054 return false;
1055 }
1056
1057 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001058 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001059 return true;
1060 }
1061
Damiend99b0522013-12-21 18:17:45 +00001062 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001063 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001064 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001065#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001066 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001067 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001068 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001069 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001070#endif
Damien3ef4abb2013-10-12 16:53:13 +01001071#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001072 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001073 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001074#endif
Damien6cdd3af2013-10-05 18:08:26 +01001075 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001076 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001077 }
1078
1079 return true;
1080}
1081
Damiend99b0522013-12-21 18:17:45 +00001082void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001083 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001084 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001085 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1086
Damien6cdd3af2013-10-05 18:08:26 +01001087 // inherit emit options for this function/class definition
1088 uint emit_options = comp->scope_cur->emit_options;
1089
1090 // compile each decorator
1091 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001092 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001093 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1094 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001095
1096 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001097 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001098 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1099
1100 // check for built-in decorators
1101 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1102 // this was a built-in
1103 num_built_in_decorators += 1;
1104
1105 } else {
1106 // not a built-in, compile normally
1107
1108 // compile the decorator function
1109 compile_node(comp, name_nodes[0]);
1110 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001111 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001112 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001113 }
1114
1115 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001116 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001117 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001118 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001119 compile_node(comp, pns_decorator->nodes[1]);
1120 }
Damien429d7192013-10-04 19:53:11 +01001121 }
1122 }
1123
1124 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001125 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001126 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001127 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001128 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001129 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001130 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001131 } else {
1132 // shouldn't happen
1133 assert(0);
1134 }
1135
1136 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001137 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001138 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001139 }
1140
1141 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001142 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001143}
1144
Damiend99b0522013-12-21 18:17:45 +00001145void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001146 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001147 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001148 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001149}
1150
Damiend99b0522013-12-21 18:17:45 +00001151void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1152 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001153 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001154 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1155 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001156
1157 compile_node(comp, pns->nodes[0]); // base of the power node
1158
Damiend99b0522013-12-21 18:17:45 +00001159 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1160 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1161 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1162 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001163 for (int i = 0; i < n - 1; i++) {
1164 compile_node(comp, pns1->nodes[i]);
1165 }
Damiend99b0522013-12-21 18:17:45 +00001166 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1167 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001168 }
Damiend99b0522013-12-21 18:17:45 +00001169 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001170 // can't delete function calls
1171 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001172 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001173 compile_node(comp, pns1->nodes[0]);
1174 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001175 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1176 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001177 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001178 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001179 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001180 }
1181 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001182 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001183 }
1184
Damiend99b0522013-12-21 18:17:45 +00001185 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001186 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001187 }
Damiend99b0522013-12-21 18:17:45 +00001188 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1189 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1190 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1191 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001192 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1193
Damiend99b0522013-12-21 18:17:45 +00001194 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1195 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1196 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001197 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001198 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001199 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001200 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001201 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001202 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001203 c_del_stmt(comp, pns->nodes[0]);
1204 for (int i = 0; i < n; i++) {
1205 c_del_stmt(comp, pns1->nodes[i]);
1206 }
Damiend99b0522013-12-21 18:17:45 +00001207 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001208 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001209 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001210 } else {
1211 // sequence with 2 items
1212 goto sequence_with_2_items;
1213 }
1214 } else {
1215 // sequence with 2 items
1216 sequence_with_2_items:
1217 c_del_stmt(comp, pns->nodes[0]);
1218 c_del_stmt(comp, pns->nodes[1]);
1219 }
1220 } else {
1221 // tuple with 1 element
1222 c_del_stmt(comp, pn);
1223 }
1224 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001225 // TODO is there anything else to implement?
1226 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001227 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001228
1229 return;
1230
1231cannot_delete:
1232 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001233}
1234
Damiend99b0522013-12-21 18:17:45 +00001235void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001236 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1237}
1238
Damiend99b0522013-12-21 18:17:45 +00001239void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001240 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001241 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001242 }
Damien Georgecbddb272014-02-01 20:08:18 +00001243 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001244}
1245
Damiend99b0522013-12-21 18:17:45 +00001246void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001247 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001248 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001249 }
Damien Georgecbddb272014-02-01 20:08:18 +00001250 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001251}
1252
Damiend99b0522013-12-21 18:17:45 +00001253void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001254 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001255 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001256 return;
1257 }
Damiend99b0522013-12-21 18:17:45 +00001258 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001259 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001260 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001261 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001262 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001263 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1264 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 +01001265
Damien George6f355fd2014-04-10 14:11:31 +01001266 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001267 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1268 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1269 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001270 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001271 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1272 } else {
1273 compile_node(comp, pns->nodes[0]);
1274 }
1275 EMIT(return_value);
1276}
1277
Damiend99b0522013-12-21 18:17:45 +00001278void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001279 compile_node(comp, pns->nodes[0]);
1280 EMIT(pop_top);
1281}
1282
Damiend99b0522013-12-21 18:17:45 +00001283void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1284 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001285 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001286 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001288 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001289 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001290 compile_node(comp, pns->nodes[0]);
1291 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001292 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001293 } else {
1294 // raise x
1295 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001296 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001297 }
1298}
1299
Damien George635543c2014-04-10 12:56:52 +01001300// q_base holds the base of the name
1301// eg a -> q_base=a
1302// a.b.c -> q_base=a
1303void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001304 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001305 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1306 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001307 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001308 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001309 pn = pns->nodes[0];
1310 is_as = true;
1311 }
Damien George635543c2014-04-10 12:56:52 +01001312 if (MP_PARSE_NODE_IS_NULL(pn)) {
1313 // empty name (eg, from . import x)
1314 *q_base = MP_QSTR_;
1315 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1316 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001317 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001318 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001319 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001320 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001321 }
Damien George635543c2014-04-10 12:56:52 +01001322 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001323 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1324 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1325 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001326 // a name of the form a.b.c
1327 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001328 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001329 }
Damiend99b0522013-12-21 18:17:45 +00001330 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001331 int len = n - 1;
1332 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001333 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001334 }
Damien George55baff42014-01-21 21:40:13 +00001335 byte *q_ptr;
1336 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001337 for (int i = 0; i < n; i++) {
1338 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001339 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001340 }
Damien George55baff42014-01-21 21:40:13 +00001341 uint str_src_len;
1342 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001343 memcpy(str_dest, str_src, str_src_len);
1344 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001345 }
Damien George635543c2014-04-10 12:56:52 +01001346 qstr q_full = qstr_build_end(q_ptr);
1347 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001348 if (is_as) {
1349 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001350 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001351 }
1352 }
1353 } else {
Damien George635543c2014-04-10 12:56:52 +01001354 // shouldn't happen
1355 assert(0);
Damien429d7192013-10-04 19:53:11 +01001356 }
1357 } else {
Damien George635543c2014-04-10 12:56:52 +01001358 // shouldn't happen
1359 assert(0);
Damien429d7192013-10-04 19:53:11 +01001360 }
1361}
1362
Damiend99b0522013-12-21 18:17:45 +00001363void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001364 EMIT_ARG(load_const_small_int, 0); // level 0 import
1365 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1366 qstr q_base;
1367 do_import_name(comp, pn, &q_base);
1368 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001369}
1370
Damiend99b0522013-12-21 18:17:45 +00001371void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001372 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1373}
1374
Damiend99b0522013-12-21 18:17:45 +00001375void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001376 mp_parse_node_t pn_import_source = pns->nodes[0];
1377
1378 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1379 uint import_level = 0;
1380 do {
1381 mp_parse_node_t pn_rel;
1382 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)) {
1383 // This covers relative imports with dots only like "from .. import"
1384 pn_rel = pn_import_source;
1385 pn_import_source = MP_PARSE_NODE_NULL;
1386 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1387 // This covers relative imports starting with dot(s) like "from .foo import"
1388 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1389 pn_rel = pns_2b->nodes[0];
1390 pn_import_source = pns_2b->nodes[1];
1391 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1392 } else {
1393 // Not a relative import
1394 break;
1395 }
1396
1397 // get the list of . and/or ...'s
1398 mp_parse_node_t *nodes;
1399 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1400
1401 // count the total number of .'s
1402 for (int i = 0; i < n; i++) {
1403 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1404 import_level++;
1405 } else {
1406 // should be an MP_TOKEN_ELLIPSIS
1407 import_level += 3;
1408 }
1409 }
1410 } while (0);
1411
Damiend99b0522013-12-21 18:17:45 +00001412 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001413 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001414
1415 // build the "fromlist" tuple
1416#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001417 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001418#else
Damien Georgeb9791222014-01-23 00:34:21 +00001419 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1420 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001421#endif
1422
1423 // do the import
Damien George635543c2014-04-10 12:56:52 +01001424 qstr dummy_q;
1425 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001426 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001427
Damien429d7192013-10-04 19:53:11 +01001428 } else {
Damien George635543c2014-04-10 12:56:52 +01001429 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001430
1431 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001432 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001433 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001434#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001435 {
1436 vstr_t *vstr = vstr_new();
1437 vstr_printf(vstr, "(");
1438 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001439 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1440 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1441 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001442 if (i > 0) {
1443 vstr_printf(vstr, ", ");
1444 }
1445 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001446 uint len;
1447 const byte *str = qstr_data(id2, &len);
1448 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001449 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001450 }
Damien02f89412013-12-12 15:13:36 +00001451 if (n == 1) {
1452 vstr_printf(vstr, ",");
1453 }
1454 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001455 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001456 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001457 }
Damiendb4c3612013-12-10 17:27:24 +00001458#else
1459 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001460 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1461 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1462 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001464 }
Damien Georgeb9791222014-01-23 00:34:21 +00001465 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001466#endif
1467
1468 // do the import
Damien George635543c2014-04-10 12:56:52 +01001469 qstr dummy_q;
1470 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001471 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001472 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1473 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1474 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001475 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001476 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001477 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001478 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001480 }
1481 }
1482 EMIT(pop_top);
1483 }
1484}
1485
Damiend99b0522013-12-21 18:17:45 +00001486void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001487 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001488 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1489 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001490 } else {
Damiend99b0522013-12-21 18:17:45 +00001491 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1492 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001493 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001494 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001495 }
Damien429d7192013-10-04 19:53:11 +01001496 }
1497 }
1498}
1499
Damiend99b0522013-12-21 18:17:45 +00001500void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001501 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001502 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1503 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001504 } else {
Damiend99b0522013-12-21 18:17:45 +00001505 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1506 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001507 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001508 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001509 }
Damien429d7192013-10-04 19:53:11 +01001510 }
1511 }
1512}
1513
Damiend99b0522013-12-21 18:17:45 +00001514void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001515 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001516 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001517 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 +00001518 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001519 // assertion message
1520 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001521 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001522 }
Damien Georgeb9791222014-01-23 00:34:21 +00001523 EMIT_ARG(raise_varargs, 1);
1524 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001525}
1526
Damiend99b0522013-12-21 18:17:45 +00001527void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001528 // TODO proper and/or short circuiting
1529
Damien George6f355fd2014-04-10 14:11:31 +01001530 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001531
Damien George6f355fd2014-04-10 14:11:31 +01001532 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001533 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1534
1535 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001536
1537 if (
1538#if !MICROPY_EMIT_CPYTHON
1539 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1540 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1541#endif
1542 // optimisation to not jump if last instruction was return
1543 !EMIT(last_emit_was_return_value)
1544 ) {
1545 // jump over elif/else blocks
1546 EMIT_ARG(jump, l_end);
1547 }
1548
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001550
Damiend99b0522013-12-21 18:17:45 +00001551 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001552 // compile elif blocks
1553
Damiend99b0522013-12-21 18:17:45 +00001554 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001555
Damiend99b0522013-12-21 18:17:45 +00001556 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001557 // multiple elif blocks
1558
Damiend99b0522013-12-21 18:17:45 +00001559 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001560 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001561 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001562 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001563 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1564
1565 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001566 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001567 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001568 }
Damien Georgeb9791222014-01-23 00:34:21 +00001569 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001570 }
1571
1572 } else {
1573 // a single elif block
1574
Damienb05d7072013-10-05 13:37:10 +01001575 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001576 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1577
1578 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001579 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001581 }
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001583 }
1584 }
1585
1586 // compile else block
1587 compile_node(comp, pns->nodes[3]); // can be null
1588
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001590}
1591
Damien Georgecbddb272014-02-01 20:08:18 +00001592#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001593 uint old_break_label = comp->break_label; \
1594 uint old_continue_label = comp->continue_label; \
1595 uint break_label = comp_next_label(comp); \
1596 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001597 comp->break_label = break_label; \
1598 comp->continue_label = continue_label; \
1599 comp->break_continue_except_level = comp->cur_except_level;
1600
1601#define END_BREAK_CONTINUE_BLOCK \
1602 comp->break_label = old_break_label; \
1603 comp->continue_label = old_continue_label; \
1604 comp->break_continue_except_level = comp->cur_except_level;
1605
Damiend99b0522013-12-21 18:17:45 +00001606void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001607 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001608
Damience89a212013-10-15 22:25:17 +01001609 // compared to CPython, we have an optimised version of while loops
1610#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001611 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(setup_loop, break_label);
1613 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001614 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1615 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001616 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001617 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001618 }
Damien Georgeb9791222014-01-23 00:34:21 +00001619 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001620 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1621 // this is a small hack to agree with CPython
1622 if (!node_is_const_true(pns->nodes[0])) {
1623 EMIT(pop_block);
1624 }
Damience89a212013-10-15 22:25:17 +01001625#else
Damien George6f355fd2014-04-10 14:11:31 +01001626 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001627 EMIT_ARG(jump, continue_label);
1628 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001629 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001630 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001631 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1632#endif
1633
1634 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001635 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001636
1637 compile_node(comp, pns->nodes[2]); // else
1638
Damien Georgeb9791222014-01-23 00:34:21 +00001639 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001640}
1641
Damienf72fd0e2013-11-06 20:20:49 +00001642// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001643// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1644// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001645void 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 +00001646 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001647
Damien George6f355fd2014-04-10 14:11:31 +01001648 uint top_label = comp_next_label(comp);
1649 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001650
Damien George3ff2d032014-03-31 18:02:22 +01001651 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001652 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001653 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001654
Damien Georgeb9791222014-01-23 00:34:21 +00001655 EMIT_ARG(jump, entry_label);
1656 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001657
Damien George3ff2d032014-03-31 18:02:22 +01001658 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001659 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001660
1661 // store next value to var
1662 c_assign(comp, pn_var, ASSIGN_STORE);
1663
Damienf3822fc2013-11-09 20:12:03 +00001664 // compile body
1665 compile_node(comp, pn_body);
1666
Damien Georgeb9791222014-01-23 00:34:21 +00001667 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001668
Damien George3ff2d032014-03-31 18:02:22 +01001669 // compile: var + step, duplicated on stack
1670 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001671 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001672 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001673 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001674
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001676
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001677 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001678 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001679 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1680 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001681 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001682 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001683 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001684 }
Damien Georgeb9791222014-01-23 00:34:21 +00001685 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001686
Damien George3ff2d032014-03-31 18:02:22 +01001687 // discard final value of var that failed the loop condition
1688 EMIT(pop_top);
1689
Damienf72fd0e2013-11-06 20:20:49 +00001690 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001691 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001692
1693 compile_node(comp, pn_else);
1694
Damien Georgeb9791222014-01-23 00:34:21 +00001695 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001696}
1697
Damiend99b0522013-12-21 18:17:45 +00001698void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001699#if !MICROPY_EMIT_CPYTHON
1700 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1701 // this is actually slower, but uses no heap memory
1702 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001703 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 +00001704 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001705 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1706 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1707 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1708 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001709 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1710 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001711 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001712 mp_parse_node_t pn_range_start;
1713 mp_parse_node_t pn_range_end;
1714 mp_parse_node_t pn_range_step;
1715 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001716 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001717 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001718 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001719 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001720 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001721 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001722 } else if (n_args == 2) {
1723 pn_range_start = args[0];
1724 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001725 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001726 } else {
1727 pn_range_start = args[0];
1728 pn_range_end = args[1];
1729 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001730 // We need to know sign of step. This is possible only if it's constant
1731 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1732 optimize = false;
1733 }
Damienf72fd0e2013-11-06 20:20:49 +00001734 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001735 }
1736 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001737 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1738 return;
1739 }
1740 }
1741 }
1742#endif
1743
Damien Georgecbddb272014-02-01 20:08:18 +00001744 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001745
Damien George6f355fd2014-04-10 14:11:31 +01001746 uint pop_label = comp_next_label(comp);
1747 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001748
Damience89a212013-10-15 22:25:17 +01001749 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1750#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001752#endif
1753
Damien429d7192013-10-04 19:53:11 +01001754 compile_node(comp, pns->nodes[1]); // iterator
1755 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001756 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001757 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001758 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1759 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001760 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001761 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001762 }
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001764 EMIT(for_iter_end);
1765
1766 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001767 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001768
Damience89a212013-10-15 22:25:17 +01001769#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001770 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001771#endif
Damien429d7192013-10-04 19:53:11 +01001772
1773 compile_node(comp, pns->nodes[3]); // else (not tested)
1774
Damien Georgeb9791222014-01-23 00:34:21 +00001775 EMIT_ARG(label_assign, break_label);
1776 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001777}
1778
Damiend99b0522013-12-21 18:17:45 +00001779void 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 +01001780 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001781 uint l1 = comp_next_label(comp);
1782 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001783
Damien Georgeb9791222014-01-23 00:34:21 +00001784 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001785 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001786
Damien429d7192013-10-04 19:53:11 +01001787 compile_node(comp, pn_body); // body
1788 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001789 EMIT_ARG(jump, success_label); // jump over exception handler
1790
1791 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001792 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 +00001793
Damien George6f355fd2014-04-10 14:11:31 +01001794 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001795
1796 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001797 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1798 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001799
1800 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001801 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001802
Damiend99b0522013-12-21 18:17:45 +00001803 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001804 // this is a catch all exception handler
1805 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001806 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001807 return;
1808 }
1809 } else {
1810 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001811 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1812 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1813 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1814 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001815 // handler binds the exception to a local
1816 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001817 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001818 }
1819 }
1820 EMIT(dup_top);
1821 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001822 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001823 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001824 }
1825
1826 EMIT(pop_top);
1827
1828 if (qstr_exception_local == 0) {
1829 EMIT(pop_top);
1830 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001831 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001832 }
1833
1834 EMIT(pop_top);
1835
Damien George6f355fd2014-04-10 14:11:31 +01001836 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001837 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001838 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001839 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001840 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001841 }
1842 compile_node(comp, pns_except->nodes[1]);
1843 if (qstr_exception_local != 0) {
1844 EMIT(pop_block);
1845 }
1846 EMIT(pop_except);
1847 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001848 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1849 EMIT_ARG(label_assign, l3);
1850 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1851 EMIT_ARG(store_id, qstr_exception_local);
1852 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001853
Damien George8dcc0c72014-03-27 10:55:21 +00001854 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001855 EMIT(end_finally);
1856 }
Damien Georgeb9791222014-01-23 00:34:21 +00001857 EMIT_ARG(jump, l2);
1858 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001859 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001860 }
1861
Damien George8dcc0c72014-03-27 10:55:21 +00001862 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001863 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001864 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001865
Damien Georgeb9791222014-01-23 00:34:21 +00001866 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001867 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001868 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001869}
1870
Damiend99b0522013-12-21 18:17:45 +00001871void 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 +01001872 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001873
Damien Georgeb9791222014-01-23 00:34:21 +00001874 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001875 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001876
Damien429d7192013-10-04 19:53:11 +01001877 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001878 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001879 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001880 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001881 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001882 } else {
1883 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1884 }
1885 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001886 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1887 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001888 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001889
Damien George8dcc0c72014-03-27 10:55:21 +00001890 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001891 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001892}
1893
Damiend99b0522013-12-21 18:17:45 +00001894void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1895 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1896 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1897 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001898 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001899 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1900 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001901 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001902 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001903 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001904 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001905 // no finally
1906 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1907 } else {
1908 // have finally
Damiend99b0522013-12-21 18:17:45 +00001909 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 +01001910 }
1911 } else {
1912 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001913 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001914 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001915 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001916 }
1917 } else {
1918 // shouldn't happen
1919 assert(0);
1920 }
1921}
1922
Damiend99b0522013-12-21 18:17:45 +00001923void 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 +01001924 if (n == 0) {
1925 // no more pre-bits, compile the body of the with
1926 compile_node(comp, body);
1927 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001928 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001929 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001930 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001931 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001932 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001933 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001934 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1935 } else {
1936 // this pre-bit is just an expression
1937 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001938 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001939 EMIT(pop_top);
1940 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001941 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001942 // compile additional pre-bits and the body
1943 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1944 // finish this with block
1945 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001946 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1947 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001948 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001949 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001950 EMIT(end_finally);
1951 }
1952}
1953
Damiend99b0522013-12-21 18:17:45 +00001954void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001955 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001956 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001957 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1958 assert(n > 0);
1959
1960 // compile in a nested fashion
1961 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1962}
1963
Damiend99b0522013-12-21 18:17:45 +00001964void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1965 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001966 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1967 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001969 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001970 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001971 EMIT(pop_top);
1972
Damien429d7192013-10-04 19:53:11 +01001973 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001974 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001975 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001976 // do nothing with a lonely constant
1977 } else {
1978 compile_node(comp, pns->nodes[0]); // just an expression
1979 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1980 }
Damien429d7192013-10-04 19:53:11 +01001981 }
1982 } else {
Damiend99b0522013-12-21 18:17:45 +00001983 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1984 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001985 if (kind == PN_expr_stmt_augassign) {
1986 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1987 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001988 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001989 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001990 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001991 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1992 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1993 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1994 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1995 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1996 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1997 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1998 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1999 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2000 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2001 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2002 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2003 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002004 }
Damien George7e5fb242014-02-01 22:18:47 +00002005 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002006 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2007 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002008 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2009 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002010 // following CPython, we store left-most first
2011 if (rhs > 0) {
2012 EMIT(dup_top);
2013 }
2014 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2015 for (int i = 0; i < rhs; i++) {
2016 if (i + 1 < rhs) {
2017 EMIT(dup_top);
2018 }
Damiend99b0522013-12-21 18:17:45 +00002019 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002020 }
2021 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002022 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2023 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2024 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2025 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002026 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002027 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2028 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002029 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2030 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2031 // can't optimise when it's a star expression on the lhs
2032 goto no_optimisation;
2033 }
Damien429d7192013-10-04 19:53:11 +01002034 compile_node(comp, pns10->nodes[0]); // rhs
2035 compile_node(comp, pns10->nodes[1]); // rhs
2036 EMIT(rot_two);
2037 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2038 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002039 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2040 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2041 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2042 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002043 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002044 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2045 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002046 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2047 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2048 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2049 // can't optimise when it's a star expression on the lhs
2050 goto no_optimisation;
2051 }
Damien429d7192013-10-04 19:53:11 +01002052 compile_node(comp, pns10->nodes[0]); // rhs
2053 compile_node(comp, pns10->nodes[1]); // rhs
2054 compile_node(comp, pns10->nodes[2]); // rhs
2055 EMIT(rot_three);
2056 EMIT(rot_two);
2057 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2058 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2059 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2060 } else {
Damien George495d7812014-04-08 17:51:47 +01002061 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002062 compile_node(comp, pns1->nodes[0]); // rhs
2063 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2064 }
2065 } else {
2066 // shouldn't happen
2067 assert(0);
2068 }
2069 }
2070}
2071
Damien Georged17926d2014-03-30 13:35:08 +01002072void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002073 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002074 compile_node(comp, pns->nodes[0]);
2075 for (int i = 1; i < num_nodes; i += 1) {
2076 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002077 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002078 }
2079}
2080
Damiend99b0522013-12-21 18:17:45 +00002081void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2082 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2083 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002084
Damien George6f355fd2014-04-10 14:11:31 +01002085 uint l_fail = comp_next_label(comp);
2086 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002087 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2088 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002089 EMIT_ARG(jump, l_end);
2090 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002091 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002092 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002094}
2095
Damiend99b0522013-12-21 18:17:45 +00002096void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002097 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002098 //mp_parse_node_t pn_params = pns->nodes[0];
2099 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002100
2101 if (comp->pass == PASS_1) {
2102 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002103 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 +01002104 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002105 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002106 }
2107
2108 // get the scope for this lambda
2109 scope_t *this_scope = (scope_t*)pns->nodes[2];
2110
2111 // make the lambda
2112 close_over_variables_etc(comp, this_scope, 0, 0);
2113}
2114
Damiend99b0522013-12-21 18:17:45 +00002115void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002116 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002117 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002118 for (int i = 0; i < n; i += 1) {
2119 compile_node(comp, pns->nodes[i]);
2120 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002121 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002122 }
2123 }
Damien Georgeb9791222014-01-23 00:34:21 +00002124 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002125}
2126
Damiend99b0522013-12-21 18:17:45 +00002127void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002128 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002129 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002130 for (int i = 0; i < n; i += 1) {
2131 compile_node(comp, pns->nodes[i]);
2132 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002133 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002134 }
2135 }
Damien Georgeb9791222014-01-23 00:34:21 +00002136 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002137}
2138
Damiend99b0522013-12-21 18:17:45 +00002139void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002140 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002141 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002142}
2143
Damiend99b0522013-12-21 18:17:45 +00002144void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002145 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002146 compile_node(comp, pns->nodes[0]);
2147 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002148 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002149 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002150 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002151 }
2152 for (int i = 1; i + 1 < num_nodes; i += 2) {
2153 compile_node(comp, pns->nodes[i + 1]);
2154 if (i + 2 < num_nodes) {
2155 EMIT(dup_top);
2156 EMIT(rot_three);
2157 }
Damien George7e5fb242014-02-01 22:18:47 +00002158 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002159 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002160 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002161 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2162 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2163 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2164 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2165 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2166 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2167 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2168 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002169 }
2170 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002171 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2172 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2173 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002174 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002175 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002176 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002177 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002178 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002179 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002180 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002181 }
2182 } else {
2183 // shouldn't happen
2184 assert(0);
2185 }
2186 } else {
2187 // shouldn't happen
2188 assert(0);
2189 }
2190 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002191 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002192 }
2193 }
2194 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002195 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002196 EMIT_ARG(jump, l_end);
2197 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002198 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002199 EMIT(rot_two);
2200 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002201 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002202 }
2203}
2204
Damiend99b0522013-12-21 18:17:45 +00002205void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002206 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002207}
2208
Damiend99b0522013-12-21 18:17:45 +00002209void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002210 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002211}
2212
Damiend99b0522013-12-21 18:17:45 +00002213void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002214 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002215}
2216
Damiend99b0522013-12-21 18:17:45 +00002217void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002218 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002219}
2220
Damiend99b0522013-12-21 18:17:45 +00002221void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2222 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002223 compile_node(comp, pns->nodes[0]);
2224 for (int i = 1; i + 1 < num_nodes; i += 2) {
2225 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002226 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002227 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002228 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002229 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002230 } else {
2231 // shouldn't happen
2232 assert(0);
2233 }
2234 }
2235}
2236
Damiend99b0522013-12-21 18:17:45 +00002237void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2238 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002239 compile_node(comp, pns->nodes[0]);
2240 for (int i = 1; i + 1 < num_nodes; i += 2) {
2241 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002243 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002244 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002245 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002246 } else {
2247 // shouldn't happen
2248 assert(0);
2249 }
2250 }
2251}
2252
Damiend99b0522013-12-21 18:17:45 +00002253void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2254 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns->nodes[0]);
2256 for (int i = 1; i + 1 < num_nodes; i += 2) {
2257 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002258 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002259 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002260 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002261 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002262 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002263 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002264 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002265 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002266 } else {
2267 // shouldn't happen
2268 assert(0);
2269 }
2270 }
2271}
2272
Damiend99b0522013-12-21 18:17:45 +00002273void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002274 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002275 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002276 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002278 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002279 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002280 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002281 } else {
2282 // shouldn't happen
2283 assert(0);
2284 }
2285}
2286
Damien George35e2a4e2014-02-05 00:51:47 +00002287void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2288 // this is to handle special super() call
2289 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2290
2291 compile_generic_all_nodes(comp, pns);
2292}
2293
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002294STATIC 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 +01002295 // function to call is on top of stack
2296
Damien George35e2a4e2014-02-05 00:51:47 +00002297#if !MICROPY_EMIT_CPYTHON
2298 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002299 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 +00002300 EMIT_ARG(load_id, MP_QSTR___class__);
2301 // get first argument to function
2302 bool found = false;
2303 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002304 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2305 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 +00002306 found = true;
2307 break;
2308 }
2309 }
2310 if (!found) {
2311 printf("TypeError: super() call cannot find self\n");
2312 return;
2313 }
Damien George922ddd62014-04-09 12:43:17 +01002314 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002315 return;
2316 }
2317#endif
2318
Damien George2c0842b2014-04-27 16:46:51 +01002319 // get the list of arguments
2320 mp_parse_node_t *args;
2321 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002322
Damien George2c0842b2014-04-27 16:46:51 +01002323 // compile the arguments
2324 // Rather than calling compile_node on the list, we go through the list of args
2325 // explicitly here so that we can count the number of arguments and give sensible
2326 // error messages.
2327 int n_positional = n_positional_extra;
2328 uint n_keyword = 0;
2329 uint star_flags = 0;
2330 for (int i = 0; i < n_args; i++) {
2331 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2332 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2333 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2334 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2335 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2336 return;
2337 }
2338 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2339 compile_node(comp, pns_arg->nodes[0]);
2340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2341 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2342 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2343 return;
2344 }
2345 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2346 compile_node(comp, pns_arg->nodes[0]);
2347 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2348 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2349 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2350 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2351 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2352 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2353 return;
2354 }
2355 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
2356 compile_node(comp, pns2->nodes[0]);
2357 n_keyword += 1;
2358 } else {
2359 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2360 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2361 n_positional++;
2362 }
2363 } else {
2364 goto normal_argument;
2365 }
2366 } else {
2367 normal_argument:
2368 if (n_keyword > 0) {
2369 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2370 return;
2371 }
2372 compile_node(comp, args[i]);
2373 n_positional++;
2374 }
Damien429d7192013-10-04 19:53:11 +01002375 }
2376
Damien George2c0842b2014-04-27 16:46:51 +01002377 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002378 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002379 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002380 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002381 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002382 }
Damien429d7192013-10-04 19:53:11 +01002383}
2384
Damiend99b0522013-12-21 18:17:45 +00002385void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2386 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002387 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002388 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 +01002389 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002390 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2391 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002392 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002393 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002394 i += 1;
2395 } else {
2396 compile_node(comp, pns->nodes[i]);
2397 }
Damien George35e2a4e2014-02-05 00:51:47 +00002398 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002399 }
2400}
2401
Damiend99b0522013-12-21 18:17:45 +00002402void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002403 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002404 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002405}
2406
Damiend99b0522013-12-21 18:17:45 +00002407void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002408 // a list of strings
Damien63321742013-12-10 17:41:49 +00002409
2410 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002411 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002412 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002413 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002414 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002415 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2416 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2417 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002418 if (i == 0) {
2419 string_kind = pn_kind;
2420 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002421 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002422 return;
2423 }
Damien George55baff42014-01-21 21:40:13 +00002424 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002425 }
Damien63321742013-12-10 17:41:49 +00002426
Damien63321742013-12-10 17:41:49 +00002427 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002428 byte *q_ptr;
2429 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002430 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002431 uint s_len;
2432 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002433 memcpy(s_dest, s, s_len);
2434 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002435 }
Damien George55baff42014-01-21 21:40:13 +00002436 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002437
Damien Georgeb9791222014-01-23 00:34:21 +00002438 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002439}
2440
2441// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002442void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2443 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2444 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2445 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002446
2447 if (comp->pass == PASS_1) {
2448 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002449 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 +01002450 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002451 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002452 }
2453
2454 // get the scope for this comprehension
2455 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2456
2457 // compile the comprehension
2458 close_over_variables_etc(comp, this_scope, 0, 0);
2459
2460 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2461 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002462 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002463}
2464
Damiend99b0522013-12-21 18:17:45 +00002465void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2466 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002467 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002468 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2469 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2470 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2471 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2472 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2473 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2474 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002475 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002476 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002477 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002478 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002479 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002480 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002481 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002482 // generator expression
2483 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2484 } else {
2485 // tuple with 2 items
2486 goto tuple_with_2_items;
2487 }
2488 } else {
2489 // tuple with 2 items
2490 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002491 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002492 }
2493 } else {
2494 // parenthesis around a single item, is just that item
2495 compile_node(comp, pns->nodes[0]);
2496 }
2497}
2498
Damiend99b0522013-12-21 18:17:45 +00002499void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2500 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002501 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002502 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002503 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2504 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2505 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2506 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2507 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002508 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002509 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002510 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002511 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002512 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002513 // list of many items
2514 compile_node(comp, pns2->nodes[0]);
2515 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002517 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002518 // list comprehension
2519 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2520 } else {
2521 // list with 2 items
2522 goto list_with_2_items;
2523 }
2524 } else {
2525 // list with 2 items
2526 list_with_2_items:
2527 compile_node(comp, pns2->nodes[0]);
2528 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002529 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002530 }
2531 } else {
2532 // list with 1 item
2533 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002534 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002535 }
2536}
2537
Damiend99b0522013-12-21 18:17:45 +00002538void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2539 mp_parse_node_t pn = pns->nodes[0];
2540 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002541 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002543 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2544 pns = (mp_parse_node_struct_t*)pn;
2545 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002546 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002547 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002548 compile_node(comp, pn);
2549 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002550 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2551 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2552 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2553 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002554 // dict/set with multiple elements
2555
2556 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002557 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002558 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2559
2560 // first element sets whether it's a dict or set
2561 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002562 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002563 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002565 compile_node(comp, pns->nodes[0]);
2566 EMIT(store_map);
2567 is_dict = true;
2568 } else {
2569 // a set
2570 compile_node(comp, pns->nodes[0]); // 1st value of set
2571 is_dict = false;
2572 }
2573
2574 // process rest of elements
2575 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002576 mp_parse_node_t pn = nodes[i];
2577 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002578 compile_node(comp, pn);
2579 if (is_dict) {
2580 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002581 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002582 return;
2583 }
2584 EMIT(store_map);
2585 } else {
2586 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002587 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002588 return;
2589 }
2590 }
2591 }
2592
2593 // if it's a set, build it
2594 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002595 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002596 }
Damiend99b0522013-12-21 18:17:45 +00002597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002598 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002599 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002600 // a dictionary comprehension
2601 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2602 } else {
2603 // a set comprehension
2604 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2605 }
2606 } else {
2607 // shouldn't happen
2608 assert(0);
2609 }
2610 } else {
2611 // set with one element
2612 goto set_with_one_element;
2613 }
2614 } else {
2615 // set with one element
2616 set_with_one_element:
2617 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002618 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002619 }
2620}
2621
Damiend99b0522013-12-21 18:17:45 +00002622void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002623 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002624}
2625
Damiend99b0522013-12-21 18:17:45 +00002626void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002627 // object who's index we want is on top of stack
2628 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002629 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002630}
2631
Damiend99b0522013-12-21 18:17:45 +00002632void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002633 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002635}
2636
Damiend99b0522013-12-21 18:17:45 +00002637void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2638 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2639 mp_parse_node_t pn = pns->nodes[0];
2640 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002641 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002642 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2643 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002644 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2645 pns = (mp_parse_node_struct_t*)pn;
2646 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002647 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002648 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002649 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002650 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002652 } else {
2653 // [?::x]
2654 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002655 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002656 }
Damiend99b0522013-12-21 18:17:45 +00002657 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002658 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002659 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2660 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2661 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2662 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002663 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002664 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002665 } else {
2666 // [?:x:x]
2667 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002668 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002669 }
2670 } else {
2671 // [?:x]
2672 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002674 }
2675 } else {
2676 // [?:x]
2677 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002678 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002679 }
2680}
2681
Damiend99b0522013-12-21 18:17:45 +00002682void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002683 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002684 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2685 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002686}
2687
Damiend99b0522013-12-21 18:17:45 +00002688void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002690 compile_subscript_3_helper(comp, pns);
2691}
2692
Damiend99b0522013-12-21 18:17:45 +00002693void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002694 // if this is called then we are compiling a dict key:value pair
2695 compile_node(comp, pns->nodes[1]); // value
2696 compile_node(comp, pns->nodes[0]); // key
2697}
2698
Damiend99b0522013-12-21 18:17:45 +00002699void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002700 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002701 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002702 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002703}
2704
Damiend99b0522013-12-21 18:17:45 +00002705void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002706 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002707 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002708 return;
2709 }
Damiend99b0522013-12-21 18:17:45 +00002710 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002711 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002712 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002713 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2714 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002715 compile_node(comp, pns->nodes[0]);
2716 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002717 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002718 EMIT(yield_from);
2719 } else {
2720 compile_node(comp, pns->nodes[0]);
2721 EMIT(yield_value);
2722 }
2723}
2724
Damiend99b0522013-12-21 18:17:45 +00002725typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002726STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002727 NULL,
2728#define nc NULL
2729#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002730#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002731#include "grammar.h"
2732#undef nc
2733#undef c
2734#undef DEF_RULE
2735};
2736
Damiend99b0522013-12-21 18:17:45 +00002737void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2738 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002739 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002740 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2741 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2742 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002743 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002744 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002745 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002746 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002747 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2748 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2749 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2750 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002751 case MP_PARSE_NODE_TOKEN:
2752 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002753 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002754 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002755 // do nothing
2756 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002757 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002758 }
2759 break;
Damien429d7192013-10-04 19:53:11 +01002760 default: assert(0);
2761 }
2762 } else {
Damiend99b0522013-12-21 18:17:45 +00002763 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002764 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002765 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002766 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002767 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002768#if MICROPY_DEBUG_PRINTERS
2769 mp_parse_node_print(pn, 0);
2770#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002771 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002772 } else {
2773 f(comp, pns);
2774 }
2775 }
2776}
2777
Damiend99b0522013-12-21 18:17:45 +00002778void 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 +01002779 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002780 qstr param_name = MP_QSTR_NULL;
2781 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002782 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2783 if (MP_PARSE_NODE_IS_ID(pn)) {
2784 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002785 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002786 // comes after a star, so counts as a keyword-only parameter
2787 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002788 } else {
Damien George2827d622014-04-27 15:50:52 +01002789 // comes before a star, so counts as a positional parameter
2790 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002791 }
Damienb14de212013-10-06 00:28:28 +01002792 } else {
Damiend99b0522013-12-21 18:17:45 +00002793 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2794 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2795 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2796 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002797 //int node_index = 1; unused
2798 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002799 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002800 // this parameter has an annotation
2801 pn_annotation = pns->nodes[1];
2802 }
2803 //node_index = 2; unused
2804 }
2805 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002806 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002807 // this parameter has a default value
Damien George8b19db02014-04-11 23:25:34 +01002808 if (comp->have_star) {
Damienb14de212013-10-06 00:28:28 +01002809 comp->scope_cur->num_dict_params += 1;
2810 } else {
2811 comp->scope_cur->num_default_params += 1;
2812 }
2813 }
2814 */
Damien George8b19db02014-04-11 23:25:34 +01002815 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002816 // comes after a star, so counts as a keyword-only parameter
2817 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002818 } else {
Damien George2827d622014-04-27 15:50:52 +01002819 // comes before a star, so counts as a positional parameter
2820 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002821 }
Damiend99b0522013-12-21 18:17:45 +00002822 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002823 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002824 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002825 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002826 // bare star
2827 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002828 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002829 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002830 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002831 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002832 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2833 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002834 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002835 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002836 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2837 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002838 pn_annotation = pns->nodes[1];
2839 } else {
2840 // shouldn't happen
2841 assert(0);
2842 }
Damiend99b0522013-12-21 18:17:45 +00002843 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2844 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002845 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002846 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002847 // this parameter has an annotation
2848 pn_annotation = pns->nodes[1];
2849 }
Damien George8725f8f2014-02-15 19:33:11 +00002850 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002851 } else {
Damienb14de212013-10-06 00:28:28 +01002852 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002853 assert(0);
2854 }
Damien429d7192013-10-04 19:53:11 +01002855 }
2856
Damien George2827d622014-04-27 15:50:52 +01002857 if (param_name != MP_QSTR_NULL) {
Damiend99b0522013-12-21 18:17:45 +00002858 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002859 // TODO this parameter has an annotation
2860 }
2861 bool added;
2862 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2863 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002864 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002865 return;
2866 }
Damien429d7192013-10-04 19:53:11 +01002867 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002868 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002869 }
2870}
2871
Damien George2827d622014-04-27 15:50:52 +01002872STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002873 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2874}
2875
Damien George2827d622014-04-27 15:50:52 +01002876STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002877 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2878}
2879
Damiend99b0522013-12-21 18:17:45 +00002880void 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 +01002881 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002882 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002883 // no more nested if/for; compile inner expression
2884 compile_node(comp, pn_inner_expr);
2885 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002887 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002888 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002889 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002890 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002891 } else {
2892 EMIT(yield_value);
2893 EMIT(pop_top);
2894 }
Damiend99b0522013-12-21 18:17:45 +00002895 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002896 // if condition
Damiend99b0522013-12-21 18:17:45 +00002897 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002898 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2899 pn_iter = pns_comp_if->nodes[1];
2900 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002902 // for loop
Damiend99b0522013-12-21 18:17:45 +00002903 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002904 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002905 uint l_end2 = comp_next_label(comp);
2906 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002907 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002908 EMIT_ARG(label_assign, l_top2);
2909 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002910 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2911 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 +00002912 EMIT_ARG(jump, l_top2);
2913 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002914 EMIT(for_iter_end);
2915 } else {
2916 // shouldn't happen
2917 assert(0);
2918 }
2919}
2920
Damien George1463c1f2014-04-25 23:52:57 +01002921STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
2922#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002923 // see http://www.python.org/dev/peps/pep-0257/
2924
2925 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002926 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002927 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002928 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002929 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002930 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2931 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002932 for (int i = 0; i < num_nodes; i++) {
2933 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002934 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 +00002935 // not a newline, so this is the first statement; finish search
2936 break;
2937 }
2938 }
2939 // 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 +00002940 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002941 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002942 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002943 } else {
2944 return;
2945 }
2946
2947 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002948 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2949 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2950 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2951 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2952 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002953 compile_node(comp, pns->nodes[0]); // a doc string
2954 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002955 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002956 }
2957 }
2958 }
Damien George1463c1f2014-04-25 23:52:57 +01002959#endif
Damien429d7192013-10-04 19:53:11 +01002960}
2961
Damien George1463c1f2014-04-25 23:52:57 +01002962STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002963 comp->pass = pass;
2964 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002965 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002966 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002967
2968 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002969 // reset maximum stack sizes in scope
2970 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002971 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002972 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002973 }
2974
Damien5ac1b2e2013-10-18 19:58:12 +01002975#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002976 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002977 scope_print_info(scope);
2978 }
Damien5ac1b2e2013-10-18 19:58:12 +01002979#endif
Damien429d7192013-10-04 19:53:11 +01002980
2981 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002982 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2983 assert(scope->kind == SCOPE_MODULE);
2984 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2985 compile_node(comp, pns->nodes[0]); // compile the expression
2986 EMIT(return_value);
2987 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002988 if (!comp->is_repl) {
2989 check_for_doc_string(comp, scope->pn);
2990 }
Damien429d7192013-10-04 19:53:11 +01002991 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002992 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002993 EMIT(return_value);
2994 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002995 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2996 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2997 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002998
2999 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003000 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003001 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003002 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003003 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3004 }
3005
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02003006 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01003007
3008 compile_node(comp, pns->nodes[3]); // 3 is function body
3009 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003010 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003011 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003012 EMIT(return_value);
3013 }
3014 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003015 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3016 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3017 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003018
3019 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003020 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01003021 if (comp->pass == PASS_1) {
Damien George8b19db02014-04-11 23:25:34 +01003022 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003023 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3024 }
3025
3026 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003027
3028 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3029 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3030 EMIT(pop_top);
3031 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3032 }
Damien429d7192013-10-04 19:53:11 +01003033 EMIT(return_value);
3034 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3035 // a bit of a hack at the moment
3036
Damiend99b0522013-12-21 18:17:45 +00003037 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3038 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3039 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3040 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3041 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003042
Damien George55baff42014-01-21 21:40:13 +00003043 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003044 if (comp->pass == PASS_1) {
3045 bool added;
3046 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3047 assert(added);
3048 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003049 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003050 }
3051
3052 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003054 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003055 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003056 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003057 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003058 }
3059
Damien George6f355fd2014-04-10 14:11:31 +01003060 uint l_end = comp_next_label(comp);
3061 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003062 EMIT_ARG(load_id, qstr_arg);
3063 EMIT_ARG(label_assign, l_top);
3064 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003065 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3066 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003067 EMIT_ARG(jump, l_top);
3068 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003069 EMIT(for_iter_end);
3070
3071 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003072 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003073 }
3074 EMIT(return_value);
3075 } else {
3076 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003077 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3078 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3079 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003080
3081 if (comp->pass == PASS_1) {
3082 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003083 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003084 assert(added);
3085 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003086 }
3087
Damien Georgeb9791222014-01-23 00:34:21 +00003088 EMIT_ARG(load_id, MP_QSTR___name__);
3089 EMIT_ARG(store_id, MP_QSTR___module__);
3090 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3091 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003092
3093 check_for_doc_string(comp, pns->nodes[2]);
3094 compile_node(comp, pns->nodes[2]); // 2 is class body
3095
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003096 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003097 assert(id != NULL);
3098 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003099 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003100 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003101#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003102 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003103#else
Damien George2bf7c092014-04-09 15:26:46 +01003104 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003105#endif
Damien429d7192013-10-04 19:53:11 +01003106 }
3107 EMIT(return_value);
3108 }
3109
Damien415eb6f2013-10-05 12:19:06 +01003110 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003111
3112 // make sure we match all the exception levels
3113 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003114}
3115
Damien George094d4502014-04-02 17:31:27 +01003116#if MICROPY_EMIT_INLINE_THUMB
Damien George1463c1f2014-04-25 23:52:57 +01003117STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003118 comp->pass = pass;
3119 comp->scope_cur = scope;
3120 comp->next_label = 1;
3121
3122 if (scope->kind != SCOPE_FUNCTION) {
3123 printf("Error: inline assembler must be a function\n");
3124 return;
3125 }
3126
Damiena2f2f7d2013-10-06 00:14:13 +01003127 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003128 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003129 }
3130
Damien826005c2013-10-05 23:17:28 +01003131 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003132 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3133 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3134 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003135
Damiend99b0522013-12-21 18:17:45 +00003136 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003137
Damiena2f2f7d2013-10-06 00:14:13 +01003138 // parameters are in pns->nodes[1]
3139 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003140 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003141 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003142 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003143 }
3144
Damiend99b0522013-12-21 18:17:45 +00003145 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003146
Damiend99b0522013-12-21 18:17:45 +00003147 mp_parse_node_t pn_body = pns->nodes[3]; // body
3148 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003149 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3150
Damien Georgecbd2f742014-01-19 11:48:48 +00003151 /*
Damien826005c2013-10-05 23:17:28 +01003152 if (comp->pass == PASS_3) {
3153 //printf("----\n");
3154 scope_print_info(scope);
3155 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003156 */
Damien826005c2013-10-05 23:17:28 +01003157
3158 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003159 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3160 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003161 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3162 // no instructions
3163 continue;
3164 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3165 // an instruction; fall through
3166 } else {
3167 // not an instruction; error
3168 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3169 return;
3170 }
Damiend99b0522013-12-21 18:17:45 +00003171 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3172 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3173 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3174 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3175 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3176 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3177 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3178 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3179 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3180 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003181 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3182
3183 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003184 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003185 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003186 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003187 return;
3188 }
Damien George6f355fd2014-04-10 14:11:31 +01003189 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003190 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003191 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003192 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003193 } else if (op == MP_QSTR_align) {
3194 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3195 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3196 return;
3197 }
3198 if (pass > PASS_1) {
3199 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3200 }
3201 } else if (op == MP_QSTR_data) {
3202 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3203 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3204 return;
3205 }
3206 if (pass > PASS_1) {
3207 machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3208 for (uint i = 1; i < n_args; i++) {
3209 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3210 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3211 return;
3212 }
3213 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3214 }
3215 }
Damien826005c2013-10-05 23:17:28 +01003216 } else {
3217 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003218 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003219 }
3220 }
3221 }
3222
3223 if (comp->pass > PASS_1) {
Damien Georgea26dc502014-04-12 17:54:52 +01003224 bool success = EMIT_INLINE_ASM(end_pass);
3225 if (!success) {
3226 comp->had_error = true;
3227 }
Damienb05d7072013-10-05 13:37:10 +01003228 }
Damien429d7192013-10-04 19:53:11 +01003229}
Damien George094d4502014-04-02 17:31:27 +01003230#endif
Damien429d7192013-10-04 19:53:11 +01003231
Damien George1463c1f2014-04-25 23:52:57 +01003232STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003233#if !MICROPY_EMIT_CPYTHON
3234 // in Micro Python we put the *x parameter after all other parameters (except **y)
3235 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3236 id_info_t *id_param = NULL;
3237 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3238 id_info_t *id = &scope->id_info[i];
3239 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3240 if (id_param != NULL) {
3241 // swap star param with last param
3242 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3243 }
3244 break;
3245 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3246 id_param = id;
3247 }
3248 }
3249 }
3250#endif
3251
Damien429d7192013-10-04 19:53:11 +01003252 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003253 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003254 scope->num_locals = 0;
3255 for (int i = 0; i < scope->id_info_len; i++) {
3256 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003257 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003258 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3259 continue;
3260 }
3261 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3262 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3263 }
Damien George2827d622014-04-27 15:50:52 +01003264 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003265 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003266 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003267 }
3268 }
3269
3270 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003271#if MICROPY_EMIT_CPYTHON
3272 int num_cell = 0;
3273#endif
Damien9ecbcff2013-12-11 00:41:43 +00003274 for (int i = 0; i < scope->id_info_len; i++) {
3275 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003276#if MICROPY_EMIT_CPYTHON
3277 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003278 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003279 id->local_num = num_cell;
3280 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003281 }
Damien George6baf76e2013-12-30 22:32:17 +00003282#else
3283 // in Micro Python the cells come right after the fast locals
3284 // parameters are not counted here, since they remain at the start
3285 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003286 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003287 id->local_num = scope->num_locals;
3288 scope->num_locals += 1;
3289 }
3290#endif
Damien9ecbcff2013-12-11 00:41:43 +00003291 }
Damien9ecbcff2013-12-11 00:41:43 +00003292
3293 // compute the index of free vars (freevars[idx] in CPython)
3294 // make sure they are in the order of the parent scope
3295 if (scope->parent != NULL) {
3296 int num_free = 0;
3297 for (int i = 0; i < scope->parent->id_info_len; i++) {
3298 id_info_t *id = &scope->parent->id_info[i];
3299 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3300 for (int j = 0; j < scope->id_info_len; j++) {
3301 id_info_t *id2 = &scope->id_info[j];
3302 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003303 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003304#if MICROPY_EMIT_CPYTHON
3305 // in CPython the frees are numbered after the cells
3306 id2->local_num = num_cell + num_free;
3307#else
3308 // in Micro Python the frees come first, before the params
3309 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003310#endif
3311 num_free += 1;
3312 }
3313 }
3314 }
Damien429d7192013-10-04 19:53:11 +01003315 }
Damien George6baf76e2013-12-30 22:32:17 +00003316#if !MICROPY_EMIT_CPYTHON
3317 // in Micro Python shift all other locals after the free locals
3318 if (num_free > 0) {
3319 for (int i = 0; i < scope->id_info_len; i++) {
3320 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003321 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003322 id->local_num += num_free;
3323 }
3324 }
Damien George2827d622014-04-27 15:50:52 +01003325 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 +00003326 scope->num_locals += num_free;
3327 }
3328#endif
Damien429d7192013-10-04 19:53:11 +01003329 }
3330
Damien George8725f8f2014-02-15 19:33:11 +00003331 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003332
3333#if MICROPY_EMIT_CPYTHON
3334 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003335 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) {
3336 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003337 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003338 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003339 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 +00003340 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003341 }
3342 }
Damien George882b3632014-04-02 15:56:31 +01003343#endif
3344
Damien429d7192013-10-04 19:53:11 +01003345 int num_free = 0;
3346 for (int i = 0; i < scope->id_info_len; i++) {
3347 id_info_t *id = &scope->id_info[i];
3348 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3349 num_free += 1;
3350 }
3351 }
3352 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003353 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003354 }
3355}
3356
Damien George65cad122014-04-06 11:48:15 +01003357mp_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 +01003358 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003359 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003360 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003361
Damien826005c2013-10-05 23:17:28 +01003362 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003363 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003364
3365 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003366 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003367
Damien826005c2013-10-05 23:17:28 +01003368 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003369 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003370 comp->emit_method_table = &emit_pass1_method_table;
3371 comp->emit_inline_asm = NULL;
3372 comp->emit_inline_asm_method_table = NULL;
3373 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003374 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003375 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003376#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003377 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003378 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003379#endif
Damien826005c2013-10-05 23:17:28 +01003380 } else {
3381 compile_scope(comp, s, PASS_1);
3382 }
3383
3384 // update maximim number of labels needed
3385 if (comp->next_label > max_num_labels) {
3386 max_num_labels = comp->next_label;
3387 }
Damien429d7192013-10-04 19:53:11 +01003388 }
3389
Damien826005c2013-10-05 23:17:28 +01003390 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003391 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003392 compile_scope_compute_things(comp, s);
3393 }
3394
Damien826005c2013-10-05 23:17:28 +01003395 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003396 emit_pass1_free(comp->emit);
3397
Damien826005c2013-10-05 23:17:28 +01003398 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003399#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003400 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003401#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003402 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003403#endif
Damien3ef4abb2013-10-12 16:53:13 +01003404#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003405 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003406#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003407#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003408 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003409 if (false) {
3410 // dummy
3411
Damien3ef4abb2013-10-12 16:53:13 +01003412#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003413 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003414 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003415 if (emit_inline_thumb == NULL) {
3416 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3417 }
3418 comp->emit = NULL;
3419 comp->emit_method_table = NULL;
3420 comp->emit_inline_asm = emit_inline_thumb;
3421 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3422 compile_scope_inline_asm(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003423 if (!comp->had_error) {
3424 compile_scope_inline_asm(comp, s, PASS_3);
3425 }
Damienc025ebb2013-10-12 14:30:21 +01003426#endif
3427
Damien826005c2013-10-05 23:17:28 +01003428 } else {
Damienc025ebb2013-10-12 14:30:21 +01003429
3430 // choose the emit type
3431
Damien3ef4abb2013-10-12 16:53:13 +01003432#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003433 comp->emit = emit_cpython_new(max_num_labels);
3434 comp->emit_method_table = &emit_cpython_method_table;
3435#else
Damien826005c2013-10-05 23:17:28 +01003436 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003437
3438#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003439 case MP_EMIT_OPT_NATIVE_PYTHON:
3440 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003441#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003442 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003443 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003444 }
Damien13ed3a62013-10-08 09:05:10 +01003445 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003446#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003447 if (emit_native == NULL) {
3448 emit_native = emit_native_thumb_new(max_num_labels);
3449 }
3450 comp->emit_method_table = &emit_native_thumb_method_table;
3451#endif
3452 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003453 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003454 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003455#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003456
Damien826005c2013-10-05 23:17:28 +01003457 default:
3458 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003459 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003460 }
3461 comp->emit = emit_bc;
3462 comp->emit_method_table = &emit_bc_method_table;
3463 break;
3464 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003465#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003466
3467 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003468 compile_scope(comp, s, PASS_2);
Damien Georgea26dc502014-04-12 17:54:52 +01003469 if (!comp->had_error) {
3470 compile_scope(comp, s, PASS_3);
3471 }
Damien6cdd3af2013-10-05 18:08:26 +01003472 }
Damien429d7192013-10-04 19:53:11 +01003473 }
3474
Damien George41d02b62014-01-24 22:42:28 +00003475 // free the emitters
3476#if !MICROPY_EMIT_CPYTHON
3477 if (emit_bc != NULL) {
3478 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003479 }
Damien George41d02b62014-01-24 22:42:28 +00003480#if MICROPY_EMIT_NATIVE
3481 if (emit_native != NULL) {
3482#if MICROPY_EMIT_X64
3483 emit_native_x64_free(emit_native);
3484#elif MICROPY_EMIT_THUMB
3485 emit_native_thumb_free(emit_native);
3486#endif
3487 }
3488#endif
3489#if MICROPY_EMIT_INLINE_THUMB
3490 if (emit_inline_thumb != NULL) {
3491 emit_inline_thumb_free(emit_inline_thumb);
3492 }
3493#endif
3494#endif // !MICROPY_EMIT_CPYTHON
3495
3496 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003497 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003498 for (scope_t *s = module_scope; s;) {
3499 scope_t *next = s->next;
3500 scope_free(s);
3501 s = next;
3502 }
Damien5ac1b2e2013-10-18 19:58:12 +01003503
Damien George41d02b62014-01-24 22:42:28 +00003504 // free the compiler
3505 bool had_error = comp->had_error;
3506 m_del_obj(compiler_t, comp);
3507
Damien George1fb03172014-01-03 14:22:03 +00003508 if (had_error) {
3509 // TODO return a proper error message
3510 return mp_const_none;
3511 } else {
3512#if MICROPY_EMIT_CPYTHON
3513 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003514 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003515 return mp_const_true;
3516#else
3517 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003518 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003519#endif
3520 }
Damien429d7192013-10-04 19:53:11 +01003521}