blob: 9f081ebbcf04c6bdca2d27a95fc4c48850602f20 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
39typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000040 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010041 uint8_t is_repl;
42 uint8_t pass; // holds enum type pass_kind_t
43 uint8_t had_error; // try to keep compiler clean from nlr
44 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010045
Damien George6f355fd2014-04-10 14:11:31 +010046 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010047
Damien George6f355fd2014-04-10 14:11:31 +010048 uint break_label;
49 uint continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000050 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010051 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010052
Damien George02a4c052014-04-09 12:50:58 +010053 uint16_t n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +010054 uint8_t star_flags;
Damien George02a4c052014-04-09 12:50:58 +010055 uint8_t have_bare_star;
56 uint8_t param_pass;
57 uint16_t param_pass_num_dict_params;
58 uint16_t param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +010059
60 scope_t *scope_head;
61 scope_t *scope_cur;
62
Damien6cdd3af2013-10-05 18:08:26 +010063 emit_t *emit; // current emitter
64 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010065
66 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
67 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010068} compiler_t;
69
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010070STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000071 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010072 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
73 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
74 } else {
75 printf(" File \"%s\"\n", qstr_str(comp->source_file));
76 }
Damien Georgef41fdd02014-03-03 23:19:11 +000077 printf("SyntaxError: %s\n", msg);
78 comp->had_error = true;
79}
80
Damien George57e99eb2014-04-10 22:42:11 +010081STATIC const mp_map_elem_t mp_constants_table[] = {
82 // Extra constants as defined by a port
83 MICROPY_EXTRA_CONSTANTS
84};
85
86STATIC const mp_map_t mp_constants_map = {
87 .all_keys_are_qstrs = 1,
88 .table_is_fixed_array = 1,
89 .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90 .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
91 .table = (mp_map_elem_t*)mp_constants_table,
92};
93
Damiend99b0522013-12-21 18:17:45 +000094mp_parse_node_t fold_constants(mp_parse_node_t pn) {
95 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
96 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
97 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010098
99 // fold arguments first
100 for (int i = 0; i < n; i++) {
101 pns->nodes[i] = fold_constants(pns->nodes[i]);
102 }
103
Damiend99b0522013-12-21 18:17:45 +0000104 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100105 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000106 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 +0200107 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
108 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000109 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100110#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100111 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000112 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100113#endif
Damiend99b0522013-12-21 18:17:45 +0000114 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
115 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100116 } else {
117 // shouldn't happen
118 assert(0);
119 }
120 }
121 break;
122
123 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100124 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000125 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 +0200126 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
127 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000128 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000129 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000131 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100132 } else {
133 // shouldn't happen
134 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100135 }
Damien Georgeaf272592014-04-04 11:21:58 +0000136 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
137 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100138 }
139 }
140 break;
141
142 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000143 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 +0000144 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
145 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000146 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000147 if (!mp_small_int_mul_overflow(arg0, arg1)) {
148 arg0 *= arg1;
149 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
150 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
151 }
152 }
Damiend99b0522013-12-21 18:17:45 +0000153 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100154 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000155 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000156 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300158 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000159 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 +0300160 }
Damien429d7192013-10-04 19:53:11 +0100161 } else {
162 // shouldn't happen
163 assert(0);
164 }
165 }
166 break;
167
168 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000169 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200170 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000171 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
172 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
173 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
174 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
175 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
176 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100177 } else {
178 // shouldn't happen
179 assert(0);
180 }
181 }
182 break;
183
184 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100185 if (0) {
186#if MICROPY_EMIT_CPYTHON
187 } 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])) {
188 // int**x
189 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000190 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
191 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200192 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100193 if (power >= 0) {
194 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200195 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100196 for (; power > 0; power--) {
197 ans *= base;
198 }
Damiend99b0522013-12-21 18:17:45 +0000199 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100200 }
201 }
Damien George57e99eb2014-04-10 22:42:11 +0100202#endif
203 } 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])) {
204 // id.id
205 // look it up in constant table, see if it can be replaced with an integer
206 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
207 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
208 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
209 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
210 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
211 if (elem != NULL) {
212 mp_obj_t dest[2];
213 mp_load_method_maybe(elem->value, q_attr, dest);
214 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
215 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
216 if (MP_PARSE_FITS_SMALL_INT(val)) {
217 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
218 }
219 }
220 }
Damien429d7192013-10-04 19:53:11 +0100221 }
222 break;
223 }
224 }
225
226 return pn;
227}
228
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200229STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000230STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100231
Damien George6f355fd2014-04-10 14:11:31 +0100232STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100233 return comp->next_label++;
234}
235
Damien George8dcc0c72014-03-27 10:55:21 +0000236STATIC void compile_increase_except_level(compiler_t *comp) {
237 comp->cur_except_level += 1;
238 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
239 comp->scope_cur->exc_stack_size = comp->cur_except_level;
240 }
241}
242
243STATIC void compile_decrease_except_level(compiler_t *comp) {
244 assert(comp->cur_except_level > 0);
245 comp->cur_except_level -= 1;
246}
247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200248STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000249 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100250 scope->parent = comp->scope_cur;
251 scope->next = NULL;
252 if (comp->scope_head == NULL) {
253 comp->scope_head = scope;
254 } else {
255 scope_t *s = comp->scope_head;
256 while (s->next != NULL) {
257 s = s->next;
258 }
259 s->next = scope;
260 }
261 return scope;
262}
263
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200264STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100266 return 0;
Damiend99b0522013-12-21 18:17:45 +0000267 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100268 return 1;
269 } else {
Damiend99b0522013-12-21 18:17:45 +0000270 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
271 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100272 return 1;
273 } else {
Damiend99b0522013-12-21 18:17:45 +0000274 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100275 }
276 }
277}
278
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200279STATIC 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 +0000280 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
281 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 f(comp, pns->nodes[i]);
285 }
Damiend99b0522013-12-21 18:17:45 +0000286 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100287 f(comp, pn);
288 }
289}
290
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200291STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 *nodes = NULL;
294 return 0;
Damiend99b0522013-12-21 18:17:45 +0000295 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100296 *nodes = pn;
297 return 1;
298 } else {
Damiend99b0522013-12-21 18:17:45 +0000299 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
300 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100301 *nodes = pn;
302 return 1;
303 } else {
304 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000305 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100306 }
307 }
308}
309
Damiend99b0522013-12-21 18:17:45 +0000310void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100311}
312
Damiend99b0522013-12-21 18:17:45 +0000313void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
314 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100315 for (int i = 0; i < num_nodes; i++) {
316 compile_node(comp, pns->nodes[i]);
317 }
318}
319
Damien3ef4abb2013-10-12 16:53:13 +0100320#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200321STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000322 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100323 return false;
324 }
Damiend99b0522013-12-21 18:17:45 +0000325 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100326 return false;
327 }
328 return true;
329}
330
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200331STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000332 uint len;
333 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000334 bool has_single_quote = false;
335 bool has_double_quote = false;
336 for (int i = 0; i < len; i++) {
337 if (str[i] == '\'') {
338 has_single_quote = true;
339 } else if (str[i] == '"') {
340 has_double_quote = true;
341 }
342 }
343 if (bytes) {
344 vstr_printf(vstr, "b");
345 }
346 bool quote_single = false;
347 if (has_single_quote && !has_double_quote) {
348 vstr_printf(vstr, "\"");
349 } else {
350 quote_single = true;
351 vstr_printf(vstr, "'");
352 }
353 for (int i = 0; i < len; i++) {
354 if (str[i] == '\n') {
355 vstr_printf(vstr, "\\n");
356 } else if (str[i] == '\\') {
357 vstr_printf(vstr, "\\\\");
358 } else if (str[i] == '\'' && quote_single) {
359 vstr_printf(vstr, "\\'");
360 } else {
361 vstr_printf(vstr, "%c", str[i]);
362 }
363 }
364 if (has_single_quote && !has_double_quote) {
365 vstr_printf(vstr, "\"");
366 } else {
367 vstr_printf(vstr, "'");
368 }
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000372 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200373 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
374 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
375 return;
376 }
377
Damiend99b0522013-12-21 18:17:45 +0000378 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
379 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
380 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000381 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
382 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
383 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
384 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
385 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100386 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000387 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
388 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
389 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100390 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100391 }
392 break;
393 default: assert(0);
394 }
395}
396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200397STATIC 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 +0100398 int n = 0;
399 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000400 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100401 }
402 int total = n;
403 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000404 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100405 total += 1;
Damien3a205172013-10-12 15:01:56 +0100406 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100407 is_const = false;
408 }
409 }
410 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100411 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100412 is_const = false;
413 break;
414 }
415 }
416 if (total > 0 && is_const) {
417 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000418 vstr_t *vstr = vstr_new();
419 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000420 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000421 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100422 need_comma = true;
423 }
424 for (int i = 0; i < n; i++) {
425 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000426 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100427 }
Damien02f89412013-12-12 15:13:36 +0000428 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100429 need_comma = true;
430 }
431 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000432 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100433 } else {
Damien02f89412013-12-12 15:13:36 +0000434 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100435 }
Damien Georgeb9791222014-01-23 00:34:21 +0000436 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000437 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100438 } else {
Damiend99b0522013-12-21 18:17:45 +0000439 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100440 compile_node(comp, pn);
441 }
442 for (int i = 0; i < n; i++) {
443 compile_node(comp, pns_list->nodes[i]);
444 }
Damien Georgeb9791222014-01-23 00:34:21 +0000445 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100446 }
447}
Damien3a205172013-10-12 15:01:56 +0100448#endif
449
450// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000451void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100452#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100453 cpython_c_tuple(comp, pn, pns_list);
454#else
455 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000456 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100457 compile_node(comp, pn);
458 total += 1;
459 }
460 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000461 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100462 for (int i = 0; i < n; i++) {
463 compile_node(comp, pns_list->nodes[i]);
464 }
465 total += n;
466 }
Damien Georgeb9791222014-01-23 00:34:21 +0000467 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100468#endif
469}
Damien429d7192013-10-04 19:53:11 +0100470
Damiend99b0522013-12-21 18:17:45 +0000471void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100472 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000473 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100474}
475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200476STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000477 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200478 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200482 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 +0100483}
484
Damien3ef4abb2013-10-12 16:53:13 +0100485#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100486// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC 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 +0100488 if (node_is_const_false(pn)) {
489 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000490 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100491 }
492 return;
493 } else if (node_is_const_true(pn)) {
494 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000495 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100496 }
497 return;
Damiend99b0522013-12-21 18:17:45 +0000498 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
499 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
500 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
501 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100502 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100503 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100504 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100505 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100506 }
Damien3a205172013-10-12 15:01:56 +0100507 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100509 } else {
510 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100511 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100512 }
513 }
514 return;
Damiend99b0522013-12-21 18:17:45 +0000515 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100516 if (jump_if == false) {
517 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100518 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100519 }
520 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100521 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100522 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100523 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100524 }
Damien3a205172013-10-12 15:01:56 +0100525 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000526 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100527 }
528 return;
Damiend99b0522013-12-21 18:17:45 +0000529 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100530 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100531 return;
532 }
533 }
534
535 // nothing special, fall back to default compiling for node and jump
536 compile_node(comp, pn);
537 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000538 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100539 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000540 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100541 }
542}
Damien3a205172013-10-12 15:01:56 +0100543#endif
Damien429d7192013-10-04 19:53:11 +0100544
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200545STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100546#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100547 cpython_c_if_cond(comp, pn, jump_if, label, false);
548#else
549 if (node_is_const_false(pn)) {
550 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000551 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100552 }
553 return;
554 } else if (node_is_const_true(pn)) {
555 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000556 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100557 }
558 return;
Damiend99b0522013-12-21 18:17:45 +0000559 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
560 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
561 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
562 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100563 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100564 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100565 for (int i = 0; i < n - 1; i++) {
566 c_if_cond(comp, pns->nodes[i], true, label2);
567 }
568 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100570 } else {
571 for (int i = 0; i < n; i++) {
572 c_if_cond(comp, pns->nodes[i], true, label);
573 }
574 }
575 return;
Damiend99b0522013-12-21 18:17:45 +0000576 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100577 if (jump_if == false) {
578 for (int i = 0; i < n; i++) {
579 c_if_cond(comp, pns->nodes[i], false, label);
580 }
581 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100582 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100583 for (int i = 0; i < n - 1; i++) {
584 c_if_cond(comp, pns->nodes[i], false, label2);
585 }
586 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000587 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100588 }
589 return;
Damiend99b0522013-12-21 18:17:45 +0000590 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100591 c_if_cond(comp, pns->nodes[0], !jump_if, label);
592 return;
593 }
594 }
595
596 // nothing special, fall back to default compiling for node and jump
597 compile_node(comp, pn);
598 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000599 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100600 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000601 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100602 }
603#endif
Damien429d7192013-10-04 19:53:11 +0100604}
605
606typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000607void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100608
Damiend99b0522013-12-21 18:17:45 +0000609void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100610 if (assign_kind != ASSIGN_AUG_STORE) {
611 compile_node(comp, pns->nodes[0]);
612 }
613
Damiend99b0522013-12-21 18:17:45 +0000614 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
615 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
616 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
617 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100618 if (assign_kind != ASSIGN_AUG_STORE) {
619 for (int i = 0; i < n - 1; i++) {
620 compile_node(comp, pns1->nodes[i]);
621 }
622 }
Damiend99b0522013-12-21 18:17:45 +0000623 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
624 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100625 }
Damiend99b0522013-12-21 18:17:45 +0000626 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100627 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000628 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100629 if (assign_kind == ASSIGN_AUG_STORE) {
630 EMIT(rot_three);
631 EMIT(store_subscr);
632 } else {
633 compile_node(comp, pns1->nodes[0]);
634 if (assign_kind == ASSIGN_AUG_LOAD) {
635 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100636 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100637 } else {
638 EMIT(store_subscr);
639 }
640 }
Damiend99b0522013-12-21 18:17:45 +0000641 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
642 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100643 if (assign_kind == ASSIGN_AUG_LOAD) {
644 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000645 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100646 } else {
647 if (assign_kind == ASSIGN_AUG_STORE) {
648 EMIT(rot_two);
649 }
Damien Georgeb9791222014-01-23 00:34:21 +0000650 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100651 }
652 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100653 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100654 }
655 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100656 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100657 }
658
Damiend99b0522013-12-21 18:17:45 +0000659 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100660 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100661 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100662
663 return;
664
665cannot_assign:
666 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100667}
668
Damien George0288cf02014-04-11 11:53:00 +0000669// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
670void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
671 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
672
673 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100674 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000675 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
676 EMIT_ARG(unpack_ex, 0, num_tail);
677 have_star_index = 0;
678 }
679 for (int i = 0; i < num_tail; i++) {
680 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100681 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000682 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
683 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100684 } else {
Damien George0288cf02014-04-11 11:53:00 +0000685 compile_syntax_error(comp, nodes_tail[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100686 return;
687 }
688 }
689 }
690 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000691 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100692 }
Damien George0288cf02014-04-11 11:53:00 +0000693 if (num_head != 0) {
694 if (0 == have_star_index) {
695 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100696 } else {
Damien George0288cf02014-04-11 11:53:00 +0000697 c_assign(comp, node_head, ASSIGN_STORE);
698 }
699 }
700 for (int i = 0; i < num_tail; i++) {
701 if (num_head + i == have_star_index) {
702 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
703 } else {
704 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100705 }
706 }
707}
708
709// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000710void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100711 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000712 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100713 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000714 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
715 if (MP_PARSE_NODE_IS_ID(pn)) {
716 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100717 switch (assign_kind) {
718 case ASSIGN_STORE:
719 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000720 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100721 break;
722 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000723 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100724 break;
725 }
726 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100727 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100728 return;
729 }
730 } else {
Damiend99b0522013-12-21 18:17:45 +0000731 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
732 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100733 case PN_power:
734 // lhs is an index or attribute
735 c_assign_power(comp, pns, assign_kind);
736 break;
737
738 case PN_testlist_star_expr:
739 case PN_exprlist:
740 // lhs is a tuple
741 if (assign_kind != ASSIGN_STORE) {
742 goto bad_aug;
743 }
Damien George0288cf02014-04-11 11:53:00 +0000744 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100745 break;
746
747 case PN_atom_paren:
748 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100750 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100751 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100752 return;
Damiend99b0522013-12-21 18:17:45 +0000753 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
754 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100755 goto testlist_comp;
756 } else {
757 // parenthesis around 1 item, is just that item
758 pn = pns->nodes[0];
759 goto tail_recursion;
760 }
761 break;
762
763 case PN_atom_bracket:
764 // lhs is something in brackets
765 if (assign_kind != ASSIGN_STORE) {
766 goto bad_aug;
767 }
Damiend99b0522013-12-21 18:17:45 +0000768 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100769 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000770 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000771 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
772 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100773 goto testlist_comp;
774 } else {
775 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000776 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100777 }
778 break;
779
780 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100781 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
782 return;
Damien429d7192013-10-04 19:53:11 +0100783 }
784 return;
785
786 testlist_comp:
787 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000788 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
789 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
790 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100791 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000792 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000793 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000794 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100795 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000796 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
797 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000798 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100799 // TODO can we ever get here? can it be compiled?
800 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
801 return;
Damien429d7192013-10-04 19:53:11 +0100802 } else {
803 // sequence with 2 items
804 goto sequence_with_2_items;
805 }
806 } else {
807 // sequence with 2 items
808 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000809 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100810 }
811 return;
812 }
813 return;
814
815 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100816 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100817}
818
819// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100820// if we are not in CPython compatibility mode then:
821// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
822// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
823// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100824void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
825 assert(n_pos_defaults >= 0);
826 assert(n_kw_defaults >= 0);
827
Damien429d7192013-10-04 19:53:11 +0100828 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000829 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100830 int nfree = 0;
831 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000832 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
833 id_info_t *id = &comp->scope_cur->id_info[i];
834 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
835 for (int j = 0; j < this_scope->id_info_len; j++) {
836 id_info_t *id2 = &this_scope->id_info[j];
837 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000838#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000839 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000840#else
841 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100842 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000843#endif
Damien318aec62013-12-10 18:28:17 +0000844 nfree += 1;
845 }
846 }
Damien429d7192013-10-04 19:53:11 +0100847 }
848 }
849 }
Damien429d7192013-10-04 19:53:11 +0100850
851 // make the function/closure
852 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100853 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100854 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000855 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100856 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100857 }
858}
859
Damiend99b0522013-12-21 18:17:45 +0000860void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000861 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
863 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100864 // bare star
865 comp->have_bare_star = true;
866 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000867
868 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
869 // TODO do we need to do anything with this?
870
871 } else {
872 mp_parse_node_t pn_id;
873 mp_parse_node_t pn_colon;
874 mp_parse_node_t pn_equal;
875 if (MP_PARSE_NODE_IS_ID(pn)) {
876 // this parameter is just an id
877
878 pn_id = pn;
879 pn_colon = MP_PARSE_NODE_NULL;
880 pn_equal = MP_PARSE_NODE_NULL;
881
882 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
883 // this parameter has a colon and/or equal specifier
884
885 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
886 pn_id = pns->nodes[0];
887 pn_colon = pns->nodes[1];
888 pn_equal = pns->nodes[2];
889
890 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100891 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000892 assert(0);
893 return;
894 }
895
896 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
897 // this parameter does not have a default value
898
899 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
900 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100901 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000902 return;
903 }
904
905 } else {
906 // this parameter has a default value
907 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
908
909 if (comp->have_bare_star) {
910 comp->param_pass_num_dict_params += 1;
911 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100912#if !MICROPY_EMIT_CPYTHON
913 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
914 if (comp->param_pass_num_dict_params == 1) {
915 // first default dict param, so make the map
916 EMIT_ARG(build_map, 0);
917 }
918#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000919 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
920 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100921#if !MICROPY_EMIT_CPYTHON
922 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
923 EMIT(store_map);
924#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000925 }
926 } else {
927 comp->param_pass_num_default_params += 1;
928 if (comp->param_pass == 2) {
929 compile_node(comp, pn_equal);
930 }
931 }
932 }
933
934 // TODO pn_colon not implemented
935 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100936 }
937}
938
939// leaves function object on stack
940// returns function name
Damiend99b0522013-12-21 18:17:45 +0000941qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100942 if (comp->pass == PASS_1) {
943 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000944 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100945 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000946 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100947 }
948
949 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George02a4c052014-04-09 12:50:58 +0100950 uint old_have_bare_star = comp->have_bare_star;
951 uint old_param_pass = comp->param_pass;
952 uint old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
953 uint old_param_pass_num_default_params = comp->param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100954
955 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000956
957 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100958 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000959 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100960 comp->param_pass_num_dict_params = 0;
961 comp->param_pass_num_default_params = 0;
962 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000963
964 if (comp->had_error) {
965 return MP_QSTR_NULL;
966 }
967
968 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100969 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000970 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100971 comp->param_pass_num_dict_params = 0;
972 comp->param_pass_num_default_params = 0;
973 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
974
Damien Georgee337f1e2014-03-31 15:18:37 +0100975#if !MICROPY_EMIT_CPYTHON
976 // in Micro Python we put the default positional parameters into a tuple using the bytecode
977 if (comp->param_pass_num_default_params > 0) {
978 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
979 }
980#endif
981
Damien429d7192013-10-04 19:53:11 +0100982 // get the scope for this function
983 scope_t *fscope = (scope_t*)pns->nodes[4];
984
985 // make the function
Damien George30565092014-03-31 11:30:17 +0100986 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100987
988 // restore variables
989 comp->have_bare_star = old_have_bare_star;
990 comp->param_pass = old_param_pass;
991 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
992 comp->param_pass_num_default_params = old_param_pass_num_default_params;
993
994 // return its name (the 'f' in "def f(...):")
995 return fscope->simple_name;
996}
997
998// leaves class object on stack
999// returns class name
Damiend99b0522013-12-21 18:17:45 +00001000qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +01001001 if (comp->pass == PASS_1) {
1002 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001003 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001004 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001005 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001006 }
1007
1008 EMIT(load_build_class);
1009
1010 // scope for this class
1011 scope_t *cscope = (scope_t*)pns->nodes[3];
1012
1013 // compile the class
1014 close_over_variables_etc(comp, cscope, 0, 0);
1015
1016 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001017 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001018
1019 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001020 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1021 mp_parse_node_t parents = pns->nodes[1];
1022 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1023 parents = MP_PARSE_NODE_NULL;
1024 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001025 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001026 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001027
1028 // return its name (the 'C' in class C(...):")
1029 return cscope->simple_name;
1030}
1031
Damien6cdd3af2013-10-05 18:08:26 +01001032// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001033STATIC 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 +00001034 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001035 return false;
1036 }
1037
1038 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001039 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001040 return true;
1041 }
1042
Damiend99b0522013-12-21 18:17:45 +00001043 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001044 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001045 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001046#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001047 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001048 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001049 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001050 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001051#endif
Damien3ef4abb2013-10-12 16:53:13 +01001052#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001053 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001054 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001055#endif
Damien6cdd3af2013-10-05 18:08:26 +01001056 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001057 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001058 }
1059
1060 return true;
1061}
1062
Damiend99b0522013-12-21 18:17:45 +00001063void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001064 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001065 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001066 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1067
Damien6cdd3af2013-10-05 18:08:26 +01001068 // inherit emit options for this function/class definition
1069 uint emit_options = comp->scope_cur->emit_options;
1070
1071 // compile each decorator
1072 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001073 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001074 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1075 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001076
1077 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001078 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001079 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1080
1081 // check for built-in decorators
1082 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1083 // this was a built-in
1084 num_built_in_decorators += 1;
1085
1086 } else {
1087 // not a built-in, compile normally
1088
1089 // compile the decorator function
1090 compile_node(comp, name_nodes[0]);
1091 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001092 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001093 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001094 }
1095
1096 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001097 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001098 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001099 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001100 compile_node(comp, pns_decorator->nodes[1]);
1101 }
Damien429d7192013-10-04 19:53:11 +01001102 }
1103 }
1104
1105 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001106 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001107 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001108 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001109 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001110 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001111 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001112 } else {
1113 // shouldn't happen
1114 assert(0);
1115 }
1116
1117 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001118 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001119 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001120 }
1121
1122 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001123 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001124}
1125
Damiend99b0522013-12-21 18:17:45 +00001126void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001127 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001128 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001129 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001130}
1131
Damiend99b0522013-12-21 18:17:45 +00001132void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1133 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001134 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001135 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1136 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001137
1138 compile_node(comp, pns->nodes[0]); // base of the power node
1139
Damiend99b0522013-12-21 18:17:45 +00001140 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1141 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1142 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1143 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001144 for (int i = 0; i < n - 1; i++) {
1145 compile_node(comp, pns1->nodes[i]);
1146 }
Damiend99b0522013-12-21 18:17:45 +00001147 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1148 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001149 }
Damiend99b0522013-12-21 18:17:45 +00001150 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001151 // can't delete function calls
1152 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001153 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001154 compile_node(comp, pns1->nodes[0]);
1155 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001156 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1157 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001158 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001159 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001160 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001161 }
1162 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001163 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001164 }
1165
Damiend99b0522013-12-21 18:17:45 +00001166 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001167 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001168 }
Damiend99b0522013-12-21 18:17:45 +00001169 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1170 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1171 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1172 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001173 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1174
Damiend99b0522013-12-21 18:17:45 +00001175 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1176 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1177 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001178 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001179 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001180 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001181 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001182 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001183 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001184 c_del_stmt(comp, pns->nodes[0]);
1185 for (int i = 0; i < n; i++) {
1186 c_del_stmt(comp, pns1->nodes[i]);
1187 }
Damiend99b0522013-12-21 18:17:45 +00001188 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001189 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001190 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001191 } else {
1192 // sequence with 2 items
1193 goto sequence_with_2_items;
1194 }
1195 } else {
1196 // sequence with 2 items
1197 sequence_with_2_items:
1198 c_del_stmt(comp, pns->nodes[0]);
1199 c_del_stmt(comp, pns->nodes[1]);
1200 }
1201 } else {
1202 // tuple with 1 element
1203 c_del_stmt(comp, pn);
1204 }
1205 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001206 // TODO is there anything else to implement?
1207 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001208 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001209
1210 return;
1211
1212cannot_delete:
1213 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001214}
1215
Damiend99b0522013-12-21 18:17:45 +00001216void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001217 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1218}
1219
Damiend99b0522013-12-21 18:17:45 +00001220void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001221 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001222 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001223 }
Damien Georgecbddb272014-02-01 20:08:18 +00001224 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001225}
1226
Damiend99b0522013-12-21 18:17:45 +00001227void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001228 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001229 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001230 }
Damien Georgecbddb272014-02-01 20:08:18 +00001231 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001232}
1233
Damiend99b0522013-12-21 18:17:45 +00001234void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001235 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001236 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001237 return;
1238 }
Damiend99b0522013-12-21 18:17:45 +00001239 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001240 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001241 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001242 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001243 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001244 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1245 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 +01001246
Damien George6f355fd2014-04-10 14:11:31 +01001247 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001248 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1249 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1250 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001251 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001252 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1253 } else {
1254 compile_node(comp, pns->nodes[0]);
1255 }
1256 EMIT(return_value);
1257}
1258
Damiend99b0522013-12-21 18:17:45 +00001259void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001260 compile_node(comp, pns->nodes[0]);
1261 EMIT(pop_top);
1262}
1263
Damiend99b0522013-12-21 18:17:45 +00001264void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1265 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001266 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001267 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001268 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001269 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001270 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001271 compile_node(comp, pns->nodes[0]);
1272 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001273 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001274 } else {
1275 // raise x
1276 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001277 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001278 }
1279}
1280
Damien George635543c2014-04-10 12:56:52 +01001281// q_base holds the base of the name
1282// eg a -> q_base=a
1283// a.b.c -> q_base=a
1284void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001285 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001286 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1287 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001288 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001289 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001290 pn = pns->nodes[0];
1291 is_as = true;
1292 }
Damien George635543c2014-04-10 12:56:52 +01001293 if (MP_PARSE_NODE_IS_NULL(pn)) {
1294 // empty name (eg, from . import x)
1295 *q_base = MP_QSTR_;
1296 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1297 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001298 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001299 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001300 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001301 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001302 }
Damien George635543c2014-04-10 12:56:52 +01001303 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001304 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1305 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1306 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001307 // a name of the form a.b.c
1308 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001309 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001310 }
Damiend99b0522013-12-21 18:17:45 +00001311 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001312 int len = n - 1;
1313 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001314 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001315 }
Damien George55baff42014-01-21 21:40:13 +00001316 byte *q_ptr;
1317 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001318 for (int i = 0; i < n; i++) {
1319 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001320 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001321 }
Damien George55baff42014-01-21 21:40:13 +00001322 uint str_src_len;
1323 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001324 memcpy(str_dest, str_src, str_src_len);
1325 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001326 }
Damien George635543c2014-04-10 12:56:52 +01001327 qstr q_full = qstr_build_end(q_ptr);
1328 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001329 if (is_as) {
1330 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001331 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001332 }
1333 }
1334 } else {
Damien George635543c2014-04-10 12:56:52 +01001335 // shouldn't happen
1336 assert(0);
Damien429d7192013-10-04 19:53:11 +01001337 }
1338 } else {
Damien George635543c2014-04-10 12:56:52 +01001339 // shouldn't happen
1340 assert(0);
Damien429d7192013-10-04 19:53:11 +01001341 }
1342}
1343
Damiend99b0522013-12-21 18:17:45 +00001344void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001345 EMIT_ARG(load_const_small_int, 0); // level 0 import
1346 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1347 qstr q_base;
1348 do_import_name(comp, pn, &q_base);
1349 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001350}
1351
Damiend99b0522013-12-21 18:17:45 +00001352void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001353 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1354}
1355
Damiend99b0522013-12-21 18:17:45 +00001356void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001357 mp_parse_node_t pn_import_source = pns->nodes[0];
1358
1359 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1360 uint import_level = 0;
1361 do {
1362 mp_parse_node_t pn_rel;
1363 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)) {
1364 // This covers relative imports with dots only like "from .. import"
1365 pn_rel = pn_import_source;
1366 pn_import_source = MP_PARSE_NODE_NULL;
1367 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1368 // This covers relative imports starting with dot(s) like "from .foo import"
1369 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1370 pn_rel = pns_2b->nodes[0];
1371 pn_import_source = pns_2b->nodes[1];
1372 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1373 } else {
1374 // Not a relative import
1375 break;
1376 }
1377
1378 // get the list of . and/or ...'s
1379 mp_parse_node_t *nodes;
1380 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1381
1382 // count the total number of .'s
1383 for (int i = 0; i < n; i++) {
1384 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1385 import_level++;
1386 } else {
1387 // should be an MP_TOKEN_ELLIPSIS
1388 import_level += 3;
1389 }
1390 }
1391 } while (0);
1392
Damiend99b0522013-12-21 18:17:45 +00001393 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001394 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001395
1396 // build the "fromlist" tuple
1397#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001398 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001399#else
Damien Georgeb9791222014-01-23 00:34:21 +00001400 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1401 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001402#endif
1403
1404 // do the import
Damien George635543c2014-04-10 12:56:52 +01001405 qstr dummy_q;
1406 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001407 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001408
Damien429d7192013-10-04 19:53:11 +01001409 } else {
Damien George635543c2014-04-10 12:56:52 +01001410 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001411
1412 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001413 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001414 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001415#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001416 {
1417 vstr_t *vstr = vstr_new();
1418 vstr_printf(vstr, "(");
1419 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001420 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1421 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1422 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001423 if (i > 0) {
1424 vstr_printf(vstr, ", ");
1425 }
1426 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001427 uint len;
1428 const byte *str = qstr_data(id2, &len);
1429 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001430 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001431 }
Damien02f89412013-12-12 15:13:36 +00001432 if (n == 1) {
1433 vstr_printf(vstr, ",");
1434 }
1435 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001437 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001438 }
Damiendb4c3612013-12-10 17:27:24 +00001439#else
1440 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001441 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1442 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1443 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001444 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001445 }
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001447#endif
1448
1449 // do the import
Damien George635543c2014-04-10 12:56:52 +01001450 qstr dummy_q;
1451 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001452 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001453 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1454 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1455 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001456 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001457 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001458 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001459 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001460 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001461 }
1462 }
1463 EMIT(pop_top);
1464 }
1465}
1466
Damiend99b0522013-12-21 18:17:45 +00001467void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001468 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001469 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1470 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001471 } else {
Damiend99b0522013-12-21 18:17:45 +00001472 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1473 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001474 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001475 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001476 }
Damien429d7192013-10-04 19:53:11 +01001477 }
1478 }
1479}
1480
Damiend99b0522013-12-21 18:17:45 +00001481void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001482 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001483 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1484 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001485 } else {
Damiend99b0522013-12-21 18:17:45 +00001486 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1487 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001488 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001489 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001490 }
Damien429d7192013-10-04 19:53:11 +01001491 }
1492 }
1493}
1494
Damiend99b0522013-12-21 18:17:45 +00001495void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001496 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001497 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001498 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 +00001499 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001500 // assertion message
1501 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001502 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001503 }
Damien Georgeb9791222014-01-23 00:34:21 +00001504 EMIT_ARG(raise_varargs, 1);
1505 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001506}
1507
Damiend99b0522013-12-21 18:17:45 +00001508void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001509 // TODO proper and/or short circuiting
1510
Damien George6f355fd2014-04-10 14:11:31 +01001511 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001512
Damien George6f355fd2014-04-10 14:11:31 +01001513 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001514 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1515
1516 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001517
1518 if (
1519#if !MICROPY_EMIT_CPYTHON
1520 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1521 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1522#endif
1523 // optimisation to not jump if last instruction was return
1524 !EMIT(last_emit_was_return_value)
1525 ) {
1526 // jump over elif/else blocks
1527 EMIT_ARG(jump, l_end);
1528 }
1529
Damien Georgeb9791222014-01-23 00:34:21 +00001530 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001531
Damiend99b0522013-12-21 18:17:45 +00001532 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001533 // compile elif blocks
1534
Damiend99b0522013-12-21 18:17:45 +00001535 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001536
Damiend99b0522013-12-21 18:17:45 +00001537 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001538 // multiple elif blocks
1539
Damiend99b0522013-12-21 18:17:45 +00001540 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001541 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001542 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001543 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001544 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1545
1546 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001547 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001549 }
Damien Georgeb9791222014-01-23 00:34:21 +00001550 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001551 }
1552
1553 } else {
1554 // a single elif block
1555
Damienb05d7072013-10-05 13:37:10 +01001556 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001557 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1558
1559 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001560 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001562 }
Damien Georgeb9791222014-01-23 00:34:21 +00001563 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001564 }
1565 }
1566
1567 // compile else block
1568 compile_node(comp, pns->nodes[3]); // can be null
1569
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001571}
1572
Damien Georgecbddb272014-02-01 20:08:18 +00001573#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001574 uint old_break_label = comp->break_label; \
1575 uint old_continue_label = comp->continue_label; \
1576 uint break_label = comp_next_label(comp); \
1577 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001578 comp->break_label = break_label; \
1579 comp->continue_label = continue_label; \
1580 comp->break_continue_except_level = comp->cur_except_level;
1581
1582#define END_BREAK_CONTINUE_BLOCK \
1583 comp->break_label = old_break_label; \
1584 comp->continue_label = old_continue_label; \
1585 comp->break_continue_except_level = comp->cur_except_level;
1586
Damiend99b0522013-12-21 18:17:45 +00001587void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001588 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001589
Damience89a212013-10-15 22:25:17 +01001590 // compared to CPython, we have an optimised version of while loops
1591#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001592 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001593 EMIT_ARG(setup_loop, break_label);
1594 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001595 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1596 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001597 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001599 }
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001601 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1602 // this is a small hack to agree with CPython
1603 if (!node_is_const_true(pns->nodes[0])) {
1604 EMIT(pop_block);
1605 }
Damience89a212013-10-15 22:25:17 +01001606#else
Damien George6f355fd2014-04-10 14:11:31 +01001607 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(jump, continue_label);
1609 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001610 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001612 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1613#endif
1614
1615 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001616 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001617
1618 compile_node(comp, pns->nodes[2]); // else
1619
Damien Georgeb9791222014-01-23 00:34:21 +00001620 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001621}
1622
Damienf72fd0e2013-11-06 20:20:49 +00001623// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001624// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1625// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001626void 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 +00001627 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001628
Damien George6f355fd2014-04-10 14:11:31 +01001629 uint top_label = comp_next_label(comp);
1630 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001631
Damien George3ff2d032014-03-31 18:02:22 +01001632 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001633 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001634 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001635
Damien Georgeb9791222014-01-23 00:34:21 +00001636 EMIT_ARG(jump, entry_label);
1637 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001638
Damien George3ff2d032014-03-31 18:02:22 +01001639 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001640 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001641
1642 // store next value to var
1643 c_assign(comp, pn_var, ASSIGN_STORE);
1644
Damienf3822fc2013-11-09 20:12:03 +00001645 // compile body
1646 compile_node(comp, pn_body);
1647
Damien Georgeb9791222014-01-23 00:34:21 +00001648 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001649
Damien George3ff2d032014-03-31 18:02:22 +01001650 // compile: var + step, duplicated on stack
1651 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001652 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001653 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001654 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001655
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001657
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001658 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001659 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001660 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1661 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001662 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001663 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001664 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001665 }
Damien Georgeb9791222014-01-23 00:34:21 +00001666 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001667
Damien George3ff2d032014-03-31 18:02:22 +01001668 // discard final value of var that failed the loop condition
1669 EMIT(pop_top);
1670
Damienf72fd0e2013-11-06 20:20:49 +00001671 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001672 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001673
1674 compile_node(comp, pn_else);
1675
Damien Georgeb9791222014-01-23 00:34:21 +00001676 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001677}
1678
Damiend99b0522013-12-21 18:17:45 +00001679void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001680#if !MICROPY_EMIT_CPYTHON
1681 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1682 // this is actually slower, but uses no heap memory
1683 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001684 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 +00001685 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001686 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1687 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1688 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1689 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001690 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1691 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001692 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001693 mp_parse_node_t pn_range_start;
1694 mp_parse_node_t pn_range_end;
1695 mp_parse_node_t pn_range_step;
1696 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001697 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001698 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001699 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001700 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001701 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001702 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001703 } else if (n_args == 2) {
1704 pn_range_start = args[0];
1705 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001706 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001707 } else {
1708 pn_range_start = args[0];
1709 pn_range_end = args[1];
1710 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001711 // We need to know sign of step. This is possible only if it's constant
1712 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1713 optimize = false;
1714 }
Damienf72fd0e2013-11-06 20:20:49 +00001715 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001716 }
1717 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001718 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1719 return;
1720 }
1721 }
1722 }
1723#endif
1724
Damien Georgecbddb272014-02-01 20:08:18 +00001725 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001726
Damien George6f355fd2014-04-10 14:11:31 +01001727 uint pop_label = comp_next_label(comp);
1728 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001729
Damience89a212013-10-15 22:25:17 +01001730 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1731#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001732 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001733#endif
1734
Damien429d7192013-10-04 19:53:11 +01001735 compile_node(comp, pns->nodes[1]); // iterator
1736 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001737 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001738 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001739 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1740 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001741 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001742 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001743 }
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001745 EMIT(for_iter_end);
1746
1747 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001748 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001749
Damience89a212013-10-15 22:25:17 +01001750#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001751 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001752#endif
Damien429d7192013-10-04 19:53:11 +01001753
1754 compile_node(comp, pns->nodes[3]); // else (not tested)
1755
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(label_assign, break_label);
1757 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001758}
1759
Damiend99b0522013-12-21 18:17:45 +00001760void 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 +01001761 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001762 uint l1 = comp_next_label(comp);
1763 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001764
Damien Georgeb9791222014-01-23 00:34:21 +00001765 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001766 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001767
Damien429d7192013-10-04 19:53:11 +01001768 compile_node(comp, pn_body); // body
1769 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001770 EMIT_ARG(jump, success_label); // jump over exception handler
1771
1772 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001773 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 +00001774
Damien George6f355fd2014-04-10 14:11:31 +01001775 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001776
1777 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001778 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1779 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001780
1781 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001782 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001783
Damiend99b0522013-12-21 18:17:45 +00001784 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001785 // this is a catch all exception handler
1786 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001787 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001788 return;
1789 }
1790 } else {
1791 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001792 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1793 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1794 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1795 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001796 // handler binds the exception to a local
1797 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001798 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001799 }
1800 }
1801 EMIT(dup_top);
1802 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001803 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001804 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001805 }
1806
1807 EMIT(pop_top);
1808
1809 if (qstr_exception_local == 0) {
1810 EMIT(pop_top);
1811 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001812 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001813 }
1814
1815 EMIT(pop_top);
1816
Damien George6f355fd2014-04-10 14:11:31 +01001817 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001818 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001819 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001820 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001821 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001822 }
1823 compile_node(comp, pns_except->nodes[1]);
1824 if (qstr_exception_local != 0) {
1825 EMIT(pop_block);
1826 }
1827 EMIT(pop_except);
1828 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001829 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1830 EMIT_ARG(label_assign, l3);
1831 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1832 EMIT_ARG(store_id, qstr_exception_local);
1833 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001834
Damien George8dcc0c72014-03-27 10:55:21 +00001835 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001836 EMIT(end_finally);
1837 }
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(jump, l2);
1839 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001840 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001841 }
1842
Damien George8dcc0c72014-03-27 10:55:21 +00001843 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001844 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001845 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001846
Damien Georgeb9791222014-01-23 00:34:21 +00001847 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001848 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001850}
1851
Damiend99b0522013-12-21 18:17:45 +00001852void 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 +01001853 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001854
Damien Georgeb9791222014-01-23 00:34:21 +00001855 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001856 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001857
Damien429d7192013-10-04 19:53:11 +01001858 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001859 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001860 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001861 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001862 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001863 } else {
1864 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1865 }
1866 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1868 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001869 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001870
Damien George8dcc0c72014-03-27 10:55:21 +00001871 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001872 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001873}
1874
Damiend99b0522013-12-21 18:17:45 +00001875void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1876 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1877 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1878 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001879 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001880 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1881 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001882 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001883 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001884 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001885 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001886 // no finally
1887 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1888 } else {
1889 // have finally
Damiend99b0522013-12-21 18:17:45 +00001890 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 +01001891 }
1892 } else {
1893 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001894 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001895 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001896 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001897 }
1898 } else {
1899 // shouldn't happen
1900 assert(0);
1901 }
1902}
1903
Damiend99b0522013-12-21 18:17:45 +00001904void 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 +01001905 if (n == 0) {
1906 // no more pre-bits, compile the body of the with
1907 compile_node(comp, body);
1908 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001909 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001910 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001911 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001912 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001913 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001914 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001915 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1916 } else {
1917 // this pre-bit is just an expression
1918 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001919 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001920 EMIT(pop_top);
1921 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001922 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001923 // compile additional pre-bits and the body
1924 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1925 // finish this with block
1926 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001927 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1928 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001929 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001930 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001931 EMIT(end_finally);
1932 }
1933}
1934
Damiend99b0522013-12-21 18:17:45 +00001935void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001936 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001937 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001938 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1939 assert(n > 0);
1940
1941 // compile in a nested fashion
1942 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1943}
1944
Damiend99b0522013-12-21 18:17:45 +00001945void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1946 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001947 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1948 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001950 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001951 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001952 EMIT(pop_top);
1953
Damien429d7192013-10-04 19:53:11 +01001954 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001955 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001956 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001957 // do nothing with a lonely constant
1958 } else {
1959 compile_node(comp, pns->nodes[0]); // just an expression
1960 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1961 }
Damien429d7192013-10-04 19:53:11 +01001962 }
1963 } else {
Damiend99b0522013-12-21 18:17:45 +00001964 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1965 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001966 if (kind == PN_expr_stmt_augassign) {
1967 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1968 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001969 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001970 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001971 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001972 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1973 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1974 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1975 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1976 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1977 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1978 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1979 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1980 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1981 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1982 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1983 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1984 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001985 }
Damien George7e5fb242014-02-01 22:18:47 +00001986 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001987 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1988 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001989 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1990 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001991 // following CPython, we store left-most first
1992 if (rhs > 0) {
1993 EMIT(dup_top);
1994 }
1995 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1996 for (int i = 0; i < rhs; i++) {
1997 if (i + 1 < rhs) {
1998 EMIT(dup_top);
1999 }
Damiend99b0522013-12-21 18:17:45 +00002000 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002001 }
2002 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00002003 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2004 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2005 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2006 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002007 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002008 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2009 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002010 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2011 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2012 // can't optimise when it's a star expression on the lhs
2013 goto no_optimisation;
2014 }
Damien429d7192013-10-04 19:53:11 +01002015 compile_node(comp, pns10->nodes[0]); // rhs
2016 compile_node(comp, pns10->nodes[1]); // rhs
2017 EMIT(rot_two);
2018 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2019 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002020 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2021 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2022 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2023 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002024 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002025 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2026 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002027 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2028 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2029 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2030 // can't optimise when it's a star expression on the lhs
2031 goto no_optimisation;
2032 }
Damien429d7192013-10-04 19:53:11 +01002033 compile_node(comp, pns10->nodes[0]); // rhs
2034 compile_node(comp, pns10->nodes[1]); // rhs
2035 compile_node(comp, pns10->nodes[2]); // rhs
2036 EMIT(rot_three);
2037 EMIT(rot_two);
2038 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2039 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2040 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2041 } else {
Damien George495d7812014-04-08 17:51:47 +01002042 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002043 compile_node(comp, pns1->nodes[0]); // rhs
2044 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2045 }
2046 } else {
2047 // shouldn't happen
2048 assert(0);
2049 }
2050 }
2051}
2052
Damien Georged17926d2014-03-30 13:35:08 +01002053void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002054 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002055 compile_node(comp, pns->nodes[0]);
2056 for (int i = 1; i < num_nodes; i += 1) {
2057 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002059 }
2060}
2061
Damiend99b0522013-12-21 18:17:45 +00002062void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2063 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2064 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002065
Damien George6f355fd2014-04-10 14:11:31 +01002066 uint l_fail = comp_next_label(comp);
2067 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002068 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2069 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002070 EMIT_ARG(jump, l_end);
2071 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002072 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002073 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002074 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002075}
2076
Damiend99b0522013-12-21 18:17:45 +00002077void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002078 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002079 //mp_parse_node_t pn_params = pns->nodes[0];
2080 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002081
2082 if (comp->pass == PASS_1) {
2083 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002084 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 +01002085 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002086 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002087 }
2088
2089 // get the scope for this lambda
2090 scope_t *this_scope = (scope_t*)pns->nodes[2];
2091
2092 // make the lambda
2093 close_over_variables_etc(comp, this_scope, 0, 0);
2094}
2095
Damiend99b0522013-12-21 18:17:45 +00002096void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002097 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002098 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002099 for (int i = 0; i < n; i += 1) {
2100 compile_node(comp, pns->nodes[i]);
2101 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002102 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002103 }
2104 }
Damien Georgeb9791222014-01-23 00:34:21 +00002105 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002106}
2107
Damiend99b0522013-12-21 18:17:45 +00002108void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002109 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002110 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002111 for (int i = 0; i < n; i += 1) {
2112 compile_node(comp, pns->nodes[i]);
2113 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002114 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002115 }
2116 }
Damien Georgeb9791222014-01-23 00:34:21 +00002117 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002118}
2119
Damiend99b0522013-12-21 18:17:45 +00002120void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002121 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002122 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002123}
2124
Damiend99b0522013-12-21 18:17:45 +00002125void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002126 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002127 compile_node(comp, pns->nodes[0]);
2128 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002129 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002130 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002131 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002132 }
2133 for (int i = 1; i + 1 < num_nodes; i += 2) {
2134 compile_node(comp, pns->nodes[i + 1]);
2135 if (i + 2 < num_nodes) {
2136 EMIT(dup_top);
2137 EMIT(rot_three);
2138 }
Damien George7e5fb242014-02-01 22:18:47 +00002139 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002140 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002141 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002142 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2143 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2144 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2145 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2146 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2147 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2148 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2149 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002150 }
2151 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002152 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2153 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2154 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002155 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002156 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002157 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002158 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002159 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002160 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002161 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002162 }
2163 } else {
2164 // shouldn't happen
2165 assert(0);
2166 }
2167 } else {
2168 // shouldn't happen
2169 assert(0);
2170 }
2171 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002172 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002173 }
2174 }
2175 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002176 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002177 EMIT_ARG(jump, l_end);
2178 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002179 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002180 EMIT(rot_two);
2181 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002182 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002183 }
2184}
2185
Damiend99b0522013-12-21 18:17:45 +00002186void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002187 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002188}
2189
Damiend99b0522013-12-21 18:17:45 +00002190void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002191 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002192}
2193
Damiend99b0522013-12-21 18:17:45 +00002194void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002195 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002196}
2197
Damiend99b0522013-12-21 18:17:45 +00002198void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002199 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002200}
2201
Damiend99b0522013-12-21 18:17:45 +00002202void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2203 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002204 compile_node(comp, pns->nodes[0]);
2205 for (int i = 1; i + 1 < num_nodes; i += 2) {
2206 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002207 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002208 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002209 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002210 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002211 } else {
2212 // shouldn't happen
2213 assert(0);
2214 }
2215 }
2216}
2217
Damiend99b0522013-12-21 18:17:45 +00002218void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2219 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002220 compile_node(comp, pns->nodes[0]);
2221 for (int i = 1; i + 1 < num_nodes; i += 2) {
2222 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002223 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002224 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002225 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002226 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002227 } else {
2228 // shouldn't happen
2229 assert(0);
2230 }
2231 }
2232}
2233
Damiend99b0522013-12-21 18:17:45 +00002234void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2235 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns->nodes[0]);
2237 for (int i = 1; i + 1 < num_nodes; i += 2) {
2238 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002240 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002241 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002242 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002243 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002244 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002245 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002246 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002247 } else {
2248 // shouldn't happen
2249 assert(0);
2250 }
2251 }
2252}
2253
Damiend99b0522013-12-21 18:17:45 +00002254void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002256 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002257 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002258 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002259 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002260 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002261 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002262 } else {
2263 // shouldn't happen
2264 assert(0);
2265 }
2266}
2267
Damien George35e2a4e2014-02-05 00:51:47 +00002268void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2269 // this is to handle special super() call
2270 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2271
2272 compile_generic_all_nodes(comp, pns);
2273}
2274
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002275STATIC 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 +01002276 // function to call is on top of stack
2277
Damien George35e2a4e2014-02-05 00:51:47 +00002278#if !MICROPY_EMIT_CPYTHON
2279 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002280 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 +00002281 EMIT_ARG(load_id, MP_QSTR___class__);
2282 // get first argument to function
2283 bool found = false;
2284 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002285 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2286 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 +00002287 found = true;
2288 break;
2289 }
2290 }
2291 if (!found) {
2292 printf("TypeError: super() call cannot find self\n");
2293 return;
2294 }
Damien George922ddd62014-04-09 12:43:17 +01002295 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002296 return;
2297 }
2298#endif
2299
Damien George02a4c052014-04-09 12:50:58 +01002300 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002301 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002302 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002303 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002304
Damien Georgebbcd49a2014-02-06 20:30:16 +00002305 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002306
2307 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002308 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002309 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002310 n_positional -= 1;
2311 }
Damien George922ddd62014-04-09 12:43:17 +01002312 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002313 n_positional -= 1;
2314 }
2315
2316 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002317 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002318 } else {
Damien George922ddd62014-04-09 12:43:17 +01002319 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002320 }
2321
2322 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002323 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2327 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002328 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002329 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 +01002330 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002331 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2332 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002333 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002334 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002335 i += 1;
2336 } else {
2337 compile_node(comp, pns->nodes[i]);
2338 }
Damien George35e2a4e2014-02-05 00:51:47 +00002339 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002340 }
2341}
2342
Damiend99b0522013-12-21 18:17:45 +00002343void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002344 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002345 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002346}
2347
Damiend99b0522013-12-21 18:17:45 +00002348void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002349 // a list of strings
Damien63321742013-12-10 17:41:49 +00002350
2351 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002352 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002353 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002354 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002355 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002356 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2357 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2358 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002359 if (i == 0) {
2360 string_kind = pn_kind;
2361 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002362 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002363 return;
2364 }
Damien George55baff42014-01-21 21:40:13 +00002365 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002366 }
Damien63321742013-12-10 17:41:49 +00002367
Damien63321742013-12-10 17:41:49 +00002368 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002369 byte *q_ptr;
2370 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002371 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002372 uint s_len;
2373 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002374 memcpy(s_dest, s, s_len);
2375 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002376 }
Damien George55baff42014-01-21 21:40:13 +00002377 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002378
Damien Georgeb9791222014-01-23 00:34:21 +00002379 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002380}
2381
2382// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002383void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2384 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2385 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2386 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002387
2388 if (comp->pass == PASS_1) {
2389 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002390 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 +01002391 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002392 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002393 }
2394
2395 // get the scope for this comprehension
2396 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2397
2398 // compile the comprehension
2399 close_over_variables_etc(comp, this_scope, 0, 0);
2400
2401 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2402 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002403 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002404}
2405
Damiend99b0522013-12-21 18:17:45 +00002406void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2407 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002408 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002409 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2410 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2411 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2412 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2413 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2414 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2415 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002416 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002417 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002418 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002419 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002420 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002421 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002422 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002423 // generator expression
2424 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2425 } else {
2426 // tuple with 2 items
2427 goto tuple_with_2_items;
2428 }
2429 } else {
2430 // tuple with 2 items
2431 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002432 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002433 }
2434 } else {
2435 // parenthesis around a single item, is just that item
2436 compile_node(comp, pns->nodes[0]);
2437 }
2438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2441 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002442 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002443 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002444 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2445 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2446 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2447 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2448 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002449 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002450 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002451 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002452 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002453 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002454 // list of many items
2455 compile_node(comp, pns2->nodes[0]);
2456 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002457 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002458 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002459 // list comprehension
2460 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2461 } else {
2462 // list with 2 items
2463 goto list_with_2_items;
2464 }
2465 } else {
2466 // list with 2 items
2467 list_with_2_items:
2468 compile_node(comp, pns2->nodes[0]);
2469 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002470 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002471 }
2472 } else {
2473 // list with 1 item
2474 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002475 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002476 }
2477}
2478
Damiend99b0522013-12-21 18:17:45 +00002479void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2480 mp_parse_node_t pn = pns->nodes[0];
2481 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002482 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002483 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002484 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2485 pns = (mp_parse_node_struct_t*)pn;
2486 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002487 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002488 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002489 compile_node(comp, pn);
2490 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002491 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2492 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2493 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2494 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002495 // dict/set with multiple elements
2496
2497 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002498 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002499 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2500
2501 // first element sets whether it's a dict or set
2502 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002503 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002504 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002505 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002506 compile_node(comp, pns->nodes[0]);
2507 EMIT(store_map);
2508 is_dict = true;
2509 } else {
2510 // a set
2511 compile_node(comp, pns->nodes[0]); // 1st value of set
2512 is_dict = false;
2513 }
2514
2515 // process rest of elements
2516 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002517 mp_parse_node_t pn = nodes[i];
2518 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002519 compile_node(comp, pn);
2520 if (is_dict) {
2521 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002522 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002523 return;
2524 }
2525 EMIT(store_map);
2526 } else {
2527 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002528 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002529 return;
2530 }
2531 }
2532 }
2533
2534 // if it's a set, build it
2535 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002537 }
Damiend99b0522013-12-21 18:17:45 +00002538 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002539 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002540 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002541 // a dictionary comprehension
2542 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2543 } else {
2544 // a set comprehension
2545 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2546 }
2547 } else {
2548 // shouldn't happen
2549 assert(0);
2550 }
2551 } else {
2552 // set with one element
2553 goto set_with_one_element;
2554 }
2555 } else {
2556 // set with one element
2557 set_with_one_element:
2558 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002559 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002560 }
2561}
2562
Damiend99b0522013-12-21 18:17:45 +00002563void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002564 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002565}
2566
Damiend99b0522013-12-21 18:17:45 +00002567void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002568 // object who's index we want is on top of stack
2569 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002570 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002571}
2572
Damiend99b0522013-12-21 18:17:45 +00002573void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002574 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002576}
2577
Damiend99b0522013-12-21 18:17:45 +00002578void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2579 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2580 mp_parse_node_t pn = pns->nodes[0];
2581 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002582 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002583 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2584 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002585 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2586 pns = (mp_parse_node_struct_t*)pn;
2587 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002588 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002589 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002590 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002591 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002592 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002593 } else {
2594 // [?::x]
2595 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002596 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002597 }
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002599 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002600 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2601 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2602 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2603 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002604 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002606 } else {
2607 // [?:x:x]
2608 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002609 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002610 }
2611 } else {
2612 // [?:x]
2613 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002614 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002615 }
2616 } else {
2617 // [?:x]
2618 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002619 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002620 }
2621}
2622
Damiend99b0522013-12-21 18:17:45 +00002623void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002624 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002625 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2626 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002627}
2628
Damiend99b0522013-12-21 18:17:45 +00002629void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002630 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002631 compile_subscript_3_helper(comp, pns);
2632}
2633
Damiend99b0522013-12-21 18:17:45 +00002634void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002635 // if this is called then we are compiling a dict key:value pair
2636 compile_node(comp, pns->nodes[1]); // value
2637 compile_node(comp, pns->nodes[0]); // key
2638}
2639
Damiend99b0522013-12-21 18:17:45 +00002640void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002641 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002642 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002643 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002644}
2645
Damiend99b0522013-12-21 18:17:45 +00002646void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002647 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002648 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002649 return;
2650 }
Damien George922ddd62014-04-09 12:43:17 +01002651 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002652 compile_node(comp, pns->nodes[0]);
2653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002656 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002657 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002658 return;
2659 }
Damien George922ddd62014-04-09 12:43:17 +01002660 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002661 compile_node(comp, pns->nodes[0]);
2662}
2663
Damiend99b0522013-12-21 18:17:45 +00002664void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2665 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2666 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2667 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2668 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002669 compile_syntax_error(comp, (mp_parse_node_t)pns, "left-hand-side of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002670 return;
2671 }
Damien Georgeb9791222014-01-23 00:34:21 +00002672 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002673 compile_node(comp, pns2->nodes[0]);
2674 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002675 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002676 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2677 } else {
2678 // shouldn't happen
2679 assert(0);
2680 }
2681}
2682
Damiend99b0522013-12-21 18:17:45 +00002683void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002684 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002685 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002686 return;
2687 }
Damiend99b0522013-12-21 18:17:45 +00002688 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002690 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002691 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2692 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002693 compile_node(comp, pns->nodes[0]);
2694 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002695 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002696 EMIT(yield_from);
2697 } else {
2698 compile_node(comp, pns->nodes[0]);
2699 EMIT(yield_value);
2700 }
2701}
2702
Damiend99b0522013-12-21 18:17:45 +00002703typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002704STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002705 NULL,
2706#define nc NULL
2707#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002708#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002709#include "grammar.h"
2710#undef nc
2711#undef c
2712#undef DEF_RULE
2713};
2714
Damiend99b0522013-12-21 18:17:45 +00002715void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2716 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002717 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002718 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2719 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2720 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002721 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002722 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002723 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002724 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002725 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2726 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2727 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2728 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002729 case MP_PARSE_NODE_TOKEN:
2730 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002731 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002732 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002733 // do nothing
2734 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002735 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002736 }
2737 break;
Damien429d7192013-10-04 19:53:11 +01002738 default: assert(0);
2739 }
2740 } else {
Damiend99b0522013-12-21 18:17:45 +00002741 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002742 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002743 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002744 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002745 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002746#if MICROPY_DEBUG_PRINTERS
2747 mp_parse_node_print(pn, 0);
2748#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002749 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002750 } else {
2751 f(comp, pns);
2752 }
2753 }
2754}
2755
Damiend99b0522013-12-21 18:17:45 +00002756void 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 +01002757 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002758 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002759 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2760 if (MP_PARSE_NODE_IS_ID(pn)) {
2761 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002762 if (comp->have_bare_star) {
2763 // comes after a bare star, so doesn't count as a parameter
2764 } else {
2765 comp->scope_cur->num_params += 1;
2766 }
Damienb14de212013-10-06 00:28:28 +01002767 } else {
Damiend99b0522013-12-21 18:17:45 +00002768 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2769 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2770 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2771 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002772 //int node_index = 1; unused
2773 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002774 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002775 // this parameter has an annotation
2776 pn_annotation = pns->nodes[1];
2777 }
2778 //node_index = 2; unused
2779 }
2780 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002781 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002782 // this parameter has a default value
2783 if (comp->have_bare_star) {
2784 comp->scope_cur->num_dict_params += 1;
2785 } else {
2786 comp->scope_cur->num_default_params += 1;
2787 }
2788 }
2789 */
2790 if (comp->have_bare_star) {
2791 // comes after a bare star, so doesn't count as a parameter
2792 } else {
2793 comp->scope_cur->num_params += 1;
2794 }
Damiend99b0522013-12-21 18:17:45 +00002795 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2796 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002797 // bare star
2798 // TODO see http://www.python.org/dev/peps/pep-3102/
2799 comp->have_bare_star = true;
2800 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002801 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002802 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002803 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002804 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2805 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002806 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002807 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002808 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2809 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002810 pn_annotation = pns->nodes[1];
2811 } else {
2812 // shouldn't happen
2813 assert(0);
2814 }
Damiend99b0522013-12-21 18:17:45 +00002815 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2816 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2817 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002818 // this parameter has an annotation
2819 pn_annotation = pns->nodes[1];
2820 }
Damien George8725f8f2014-02-15 19:33:11 +00002821 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002822 } else {
Damienb14de212013-10-06 00:28:28 +01002823 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002824 assert(0);
2825 }
Damien429d7192013-10-04 19:53:11 +01002826 }
2827
2828 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002829 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002830 // TODO this parameter has an annotation
2831 }
2832 bool added;
2833 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2834 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002835 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002836 return;
2837 }
Damien429d7192013-10-04 19:53:11 +01002838 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002839 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002840 }
2841}
2842
Damiend99b0522013-12-21 18:17:45 +00002843void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002844 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2845}
2846
Damiend99b0522013-12-21 18:17:45 +00002847void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002848 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2849}
2850
Damiend99b0522013-12-21 18:17:45 +00002851void 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 +01002852 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002853 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002854 // no more nested if/for; compile inner expression
2855 compile_node(comp, pn_inner_expr);
2856 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002858 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002860 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002861 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002862 } else {
2863 EMIT(yield_value);
2864 EMIT(pop_top);
2865 }
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002867 // if condition
Damiend99b0522013-12-21 18:17:45 +00002868 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002869 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2870 pn_iter = pns_comp_if->nodes[1];
2871 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002872 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002873 // for loop
Damiend99b0522013-12-21 18:17:45 +00002874 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002875 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002876 uint l_end2 = comp_next_label(comp);
2877 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002878 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002879 EMIT_ARG(label_assign, l_top2);
2880 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002881 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2882 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 +00002883 EMIT_ARG(jump, l_top2);
2884 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002885 EMIT(for_iter_end);
2886 } else {
2887 // shouldn't happen
2888 assert(0);
2889 }
2890}
2891
Damiend99b0522013-12-21 18:17:45 +00002892void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002893 // see http://www.python.org/dev/peps/pep-0257/
2894
2895 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002896 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002897 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002898 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002899 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002900 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2901 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002902 for (int i = 0; i < num_nodes; i++) {
2903 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002904 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 +00002905 // not a newline, so this is the first statement; finish search
2906 break;
2907 }
2908 }
2909 // 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 +00002910 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002911 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002912 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002913 } else {
2914 return;
2915 }
2916
2917 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002918 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2919 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2920 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2921 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2922 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002923 compile_node(comp, pns->nodes[0]); // a doc string
2924 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002925 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002926 }
2927 }
2928 }
2929}
2930
2931void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2932 comp->pass = pass;
2933 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002934 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002936
2937 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002938 // reset maximum stack sizes in scope
2939 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002940 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002941 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002942 }
2943
Damien5ac1b2e2013-10-18 19:58:12 +01002944#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002945 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002946 scope_print_info(scope);
2947 }
Damien5ac1b2e2013-10-18 19:58:12 +01002948#endif
Damien429d7192013-10-04 19:53:11 +01002949
2950 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002951 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2952 assert(scope->kind == SCOPE_MODULE);
2953 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2954 compile_node(comp, pns->nodes[0]); // compile the expression
2955 EMIT(return_value);
2956 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002957 if (!comp->is_repl) {
2958 check_for_doc_string(comp, scope->pn);
2959 }
Damien429d7192013-10-04 19:53:11 +01002960 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002961 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002962 EMIT(return_value);
2963 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002964 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2965 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2966 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002967
2968 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002969 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002970 if (comp->pass == PASS_1) {
2971 comp->have_bare_star = false;
2972 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2973 }
2974
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002975 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002976
2977 compile_node(comp, pns->nodes[3]); // 3 is function body
2978 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002979 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002980 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002981 EMIT(return_value);
2982 }
2983 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002984 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2985 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2986 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002987
2988 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002989 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002990 if (comp->pass == PASS_1) {
2991 comp->have_bare_star = false;
2992 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2993 }
2994
2995 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002996
2997 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2998 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2999 EMIT(pop_top);
3000 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3001 }
Damien429d7192013-10-04 19:53:11 +01003002 EMIT(return_value);
3003 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3004 // a bit of a hack at the moment
3005
Damiend99b0522013-12-21 18:17:45 +00003006 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3007 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3008 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3009 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3010 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003011
Damien George55baff42014-01-21 21:40:13 +00003012 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01003013 if (comp->pass == PASS_1) {
3014 bool added;
3015 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3016 assert(added);
3017 id_info->kind = ID_INFO_KIND_LOCAL;
3018 scope->num_params = 1;
3019 }
3020
3021 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003022 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003023 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003024 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003025 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003026 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003027 }
3028
Damien George6f355fd2014-04-10 14:11:31 +01003029 uint l_end = comp_next_label(comp);
3030 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003031 EMIT_ARG(load_id, qstr_arg);
3032 EMIT_ARG(label_assign, l_top);
3033 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003034 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3035 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003036 EMIT_ARG(jump, l_top);
3037 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003038 EMIT(for_iter_end);
3039
3040 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003041 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003042 }
3043 EMIT(return_value);
3044 } else {
3045 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003046 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3047 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3048 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003049
3050 if (comp->pass == PASS_1) {
3051 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003052 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003053 assert(added);
3054 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003055 }
3056
Damien Georgeb9791222014-01-23 00:34:21 +00003057 EMIT_ARG(load_id, MP_QSTR___name__);
3058 EMIT_ARG(store_id, MP_QSTR___module__);
3059 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3060 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003061
3062 check_for_doc_string(comp, pns->nodes[2]);
3063 compile_node(comp, pns->nodes[2]); // 2 is class body
3064
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003065 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003066 assert(id != NULL);
3067 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003069 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003070#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003071 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003072#else
Damien George2bf7c092014-04-09 15:26:46 +01003073 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003074#endif
Damien429d7192013-10-04 19:53:11 +01003075 }
3076 EMIT(return_value);
3077 }
3078
Damien415eb6f2013-10-05 12:19:06 +01003079 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003080
3081 // make sure we match all the exception levels
3082 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003083}
3084
Damien George094d4502014-04-02 17:31:27 +01003085#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003086void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3087 comp->pass = pass;
3088 comp->scope_cur = scope;
3089 comp->next_label = 1;
3090
3091 if (scope->kind != SCOPE_FUNCTION) {
3092 printf("Error: inline assembler must be a function\n");
3093 return;
3094 }
3095
Damiena2f2f7d2013-10-06 00:14:13 +01003096 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003097 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003098 }
3099
Damien826005c2013-10-05 23:17:28 +01003100 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003101 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3102 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3103 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003104
Damiend99b0522013-12-21 18:17:45 +00003105 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003106
Damiena2f2f7d2013-10-06 00:14:13 +01003107 // parameters are in pns->nodes[1]
3108 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003109 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003110 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003111 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003112 }
3113
Damiend99b0522013-12-21 18:17:45 +00003114 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003115
Damiend99b0522013-12-21 18:17:45 +00003116 mp_parse_node_t pn_body = pns->nodes[3]; // body
3117 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003118 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3119
Damien Georgecbd2f742014-01-19 11:48:48 +00003120 /*
Damien826005c2013-10-05 23:17:28 +01003121 if (comp->pass == PASS_3) {
3122 //printf("----\n");
3123 scope_print_info(scope);
3124 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003125 */
Damien826005c2013-10-05 23:17:28 +01003126
3127 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003128 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3129 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3130 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3131 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3132 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3133 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3134 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3135 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3136 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3137 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3138 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3139 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3140 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003141 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3142
3143 // emit instructions
3144 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003145 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003146 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003147 return;
3148 }
Damien George6f355fd2014-04-10 14:11:31 +01003149 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003150 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003151 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003152 }
3153 } else {
3154 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003155 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003156 }
3157 }
3158 }
3159
3160 if (comp->pass > PASS_1) {
3161 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003162 }
Damien429d7192013-10-04 19:53:11 +01003163}
Damien George094d4502014-04-02 17:31:27 +01003164#endif
Damien429d7192013-10-04 19:53:11 +01003165
3166void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3167 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003168 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003169 scope->num_locals = 0;
3170 for (int i = 0; i < scope->id_info_len; i++) {
3171 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003172 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003173 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3174 continue;
3175 }
3176 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3177 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3178 }
Damien9ecbcff2013-12-11 00:41:43 +00003179 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003180 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003181 id->local_num = scope->num_locals;
3182 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003183 }
3184 }
3185
3186 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003187#if MICROPY_EMIT_CPYTHON
3188 int num_cell = 0;
3189#endif
Damien9ecbcff2013-12-11 00:41:43 +00003190 for (int i = 0; i < scope->id_info_len; i++) {
3191 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003192#if MICROPY_EMIT_CPYTHON
3193 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003194 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003195 id->local_num = num_cell;
3196 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003197 }
Damien George6baf76e2013-12-30 22:32:17 +00003198#else
3199 // in Micro Python the cells come right after the fast locals
3200 // parameters are not counted here, since they remain at the start
3201 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003202 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003203 id->local_num = scope->num_locals;
3204 scope->num_locals += 1;
3205 }
3206#endif
Damien9ecbcff2013-12-11 00:41:43 +00003207 }
Damien9ecbcff2013-12-11 00:41:43 +00003208
3209 // compute the index of free vars (freevars[idx] in CPython)
3210 // make sure they are in the order of the parent scope
3211 if (scope->parent != NULL) {
3212 int num_free = 0;
3213 for (int i = 0; i < scope->parent->id_info_len; i++) {
3214 id_info_t *id = &scope->parent->id_info[i];
3215 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3216 for (int j = 0; j < scope->id_info_len; j++) {
3217 id_info_t *id2 = &scope->id_info[j];
3218 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003219 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003220#if MICROPY_EMIT_CPYTHON
3221 // in CPython the frees are numbered after the cells
3222 id2->local_num = num_cell + num_free;
3223#else
3224 // in Micro Python the frees come first, before the params
3225 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003226#endif
3227 num_free += 1;
3228 }
3229 }
3230 }
Damien429d7192013-10-04 19:53:11 +01003231 }
Damien George6baf76e2013-12-30 22:32:17 +00003232#if !MICROPY_EMIT_CPYTHON
3233 // in Micro Python shift all other locals after the free locals
3234 if (num_free > 0) {
3235 for (int i = 0; i < scope->id_info_len; i++) {
3236 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003237 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003238 id->local_num += num_free;
3239 }
3240 }
3241 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3242 scope->num_locals += num_free;
3243 }
3244#endif
Damien429d7192013-10-04 19:53:11 +01003245 }
3246
Damien George8725f8f2014-02-15 19:33:11 +00003247 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003248
3249#if MICROPY_EMIT_CPYTHON
3250 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003251 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) {
3252 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003253 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003254 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003255 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 +00003256 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003257 }
3258 }
Damien George882b3632014-04-02 15:56:31 +01003259#endif
3260
Damien429d7192013-10-04 19:53:11 +01003261 int num_free = 0;
3262 for (int i = 0; i < scope->id_info_len; i++) {
3263 id_info_t *id = &scope->id_info[i];
3264 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3265 num_free += 1;
3266 }
3267 }
3268 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003269 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003270 }
3271}
3272
Damien George65cad122014-04-06 11:48:15 +01003273mp_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 +01003274 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003275 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003276 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003277
Damien826005c2013-10-05 23:17:28 +01003278 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003279 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003280
3281 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003282 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003283
Damien826005c2013-10-05 23:17:28 +01003284 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003285 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003286 comp->emit_method_table = &emit_pass1_method_table;
3287 comp->emit_inline_asm = NULL;
3288 comp->emit_inline_asm_method_table = NULL;
3289 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003290 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003291 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003292#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003293 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003294 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003295#endif
Damien826005c2013-10-05 23:17:28 +01003296 } else {
3297 compile_scope(comp, s, PASS_1);
3298 }
3299
3300 // update maximim number of labels needed
3301 if (comp->next_label > max_num_labels) {
3302 max_num_labels = comp->next_label;
3303 }
Damien429d7192013-10-04 19:53:11 +01003304 }
3305
Damien826005c2013-10-05 23:17:28 +01003306 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003307 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003308 compile_scope_compute_things(comp, s);
3309 }
3310
Damien826005c2013-10-05 23:17:28 +01003311 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003312 emit_pass1_free(comp->emit);
3313
Damien826005c2013-10-05 23:17:28 +01003314 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003315#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003316 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003317#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003318 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003319#endif
Damien3ef4abb2013-10-12 16:53:13 +01003320#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003321 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003322#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003323#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003324 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003325 if (false) {
3326 // dummy
3327
Damien3ef4abb2013-10-12 16:53:13 +01003328#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003329 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003330 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003331 if (emit_inline_thumb == NULL) {
3332 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3333 }
3334 comp->emit = NULL;
3335 comp->emit_method_table = NULL;
3336 comp->emit_inline_asm = emit_inline_thumb;
3337 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3338 compile_scope_inline_asm(comp, s, PASS_2);
3339 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003340#endif
3341
Damien826005c2013-10-05 23:17:28 +01003342 } else {
Damienc025ebb2013-10-12 14:30:21 +01003343
3344 // choose the emit type
3345
Damien3ef4abb2013-10-12 16:53:13 +01003346#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003347 comp->emit = emit_cpython_new(max_num_labels);
3348 comp->emit_method_table = &emit_cpython_method_table;
3349#else
Damien826005c2013-10-05 23:17:28 +01003350 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003351
3352#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003353 case MP_EMIT_OPT_NATIVE_PYTHON:
3354 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003355#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003356 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003357 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003358 }
Damien13ed3a62013-10-08 09:05:10 +01003359 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003360#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003361 if (emit_native == NULL) {
3362 emit_native = emit_native_thumb_new(max_num_labels);
3363 }
3364 comp->emit_method_table = &emit_native_thumb_method_table;
3365#endif
3366 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003367 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003368 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003369#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003370
Damien826005c2013-10-05 23:17:28 +01003371 default:
3372 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003373 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003374 }
3375 comp->emit = emit_bc;
3376 comp->emit_method_table = &emit_bc_method_table;
3377 break;
3378 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003379#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003380
3381 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003382 compile_scope(comp, s, PASS_2);
3383 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003384 }
Damien429d7192013-10-04 19:53:11 +01003385 }
3386
Damien George41d02b62014-01-24 22:42:28 +00003387 // free the emitters
3388#if !MICROPY_EMIT_CPYTHON
3389 if (emit_bc != NULL) {
3390 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003391 }
Damien George41d02b62014-01-24 22:42:28 +00003392#if MICROPY_EMIT_NATIVE
3393 if (emit_native != NULL) {
3394#if MICROPY_EMIT_X64
3395 emit_native_x64_free(emit_native);
3396#elif MICROPY_EMIT_THUMB
3397 emit_native_thumb_free(emit_native);
3398#endif
3399 }
3400#endif
3401#if MICROPY_EMIT_INLINE_THUMB
3402 if (emit_inline_thumb != NULL) {
3403 emit_inline_thumb_free(emit_inline_thumb);
3404 }
3405#endif
3406#endif // !MICROPY_EMIT_CPYTHON
3407
3408 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003409 uint unique_code_id = module_scope->unique_code_id;
3410 for (scope_t *s = module_scope; s;) {
3411 scope_t *next = s->next;
3412 scope_free(s);
3413 s = next;
3414 }
Damien5ac1b2e2013-10-18 19:58:12 +01003415
Damien George41d02b62014-01-24 22:42:28 +00003416 // free the compiler
3417 bool had_error = comp->had_error;
3418 m_del_obj(compiler_t, comp);
3419
Damien George1fb03172014-01-03 14:22:03 +00003420 if (had_error) {
3421 // TODO return a proper error message
3422 return mp_const_none;
3423 } else {
3424#if MICROPY_EMIT_CPYTHON
3425 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003426 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003427 return mp_const_true;
3428#else
3429 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003430 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georgecdd96df2014-04-06 12:58:40 +01003431 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003432#endif
3433 }
Damien429d7192013-10-04 19:53:11 +01003434}