blob: 47cd0c451025c5cc96251f76ce5af16c9483963a [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;
Damien George69b89d22014-04-11 13:38:30 +000056 uint16_t num_dict_params;
57 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010058
59 scope_t *scope_head;
60 scope_t *scope_cur;
61
Damien6cdd3af2013-10-05 18:08:26 +010062 emit_t *emit; // current emitter
63 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010064
65 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
66 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 +010067} compiler_t;
68
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010069STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000070 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010071 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
72 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
73 } else {
74 printf(" File \"%s\"\n", qstr_str(comp->source_file));
75 }
Damien Georgef41fdd02014-03-03 23:19:11 +000076 printf("SyntaxError: %s\n", msg);
77 comp->had_error = true;
78}
79
Damien George57e99eb2014-04-10 22:42:11 +010080STATIC const mp_map_elem_t mp_constants_table[] = {
81 // Extra constants as defined by a port
82 MICROPY_EXTRA_CONSTANTS
83};
84
85STATIC const mp_map_t mp_constants_map = {
86 .all_keys_are_qstrs = 1,
87 .table_is_fixed_array = 1,
88 .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
89 .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90 .table = (mp_map_elem_t*)mp_constants_table,
91};
92
Damiend99b0522013-12-21 18:17:45 +000093mp_parse_node_t fold_constants(mp_parse_node_t pn) {
94 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
95 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
96 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010097
98 // fold arguments first
99 for (int i = 0; i < n; i++) {
100 pns->nodes[i] = fold_constants(pns->nodes[i]);
101 }
102
Damiend99b0522013-12-21 18:17:45 +0000103 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100104 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000105 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 +0200106 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
107 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000108 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100109#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100110 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000111 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100112#endif
Damiend99b0522013-12-21 18:17:45 +0000113 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
114 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100115 } else {
116 // shouldn't happen
117 assert(0);
118 }
119 }
120 break;
121
122 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100123 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000124 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 +0200125 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
126 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000127 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000128 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000129 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000130 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100131 } else {
132 // shouldn't happen
133 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100134 }
Damien Georgeaf272592014-04-04 11:21:58 +0000135 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100137 }
138 }
139 break;
140
141 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000142 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 +0000143 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
144 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000145 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000146 if (!mp_small_int_mul_overflow(arg0, arg1)) {
147 arg0 *= arg1;
148 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
150 }
151 }
Damiend99b0522013-12-21 18:17:45 +0000152 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100153 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000154 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000155 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300157 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000158 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 +0300159 }
Damien429d7192013-10-04 19:53:11 +0100160 } else {
161 // shouldn't happen
162 assert(0);
163 }
164 }
165 break;
166
167 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000168 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200169 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000170 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
171 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
172 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
173 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
174 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
175 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100176 } else {
177 // shouldn't happen
178 assert(0);
179 }
180 }
181 break;
182
183 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100184 if (0) {
185#if MICROPY_EMIT_CPYTHON
186 } 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])) {
187 // int**x
188 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000189 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
190 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200191 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100192 if (power >= 0) {
193 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200194 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100195 for (; power > 0; power--) {
196 ans *= base;
197 }
Damiend99b0522013-12-21 18:17:45 +0000198 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100199 }
200 }
Damien George57e99eb2014-04-10 22:42:11 +0100201#endif
202 } 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])) {
203 // id.id
204 // look it up in constant table, see if it can be replaced with an integer
205 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
206 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
207 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
208 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
209 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
210 if (elem != NULL) {
211 mp_obj_t dest[2];
212 mp_load_method_maybe(elem->value, q_attr, dest);
213 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
214 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
215 if (MP_PARSE_FITS_SMALL_INT(val)) {
216 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
217 }
218 }
219 }
Damien429d7192013-10-04 19:53:11 +0100220 }
221 break;
222 }
223 }
224
225 return pn;
226}
227
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200228STATIC 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 +0000229STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100230
Damien George6f355fd2014-04-10 14:11:31 +0100231STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100232 return comp->next_label++;
233}
234
Damien George8dcc0c72014-03-27 10:55:21 +0000235STATIC void compile_increase_except_level(compiler_t *comp) {
236 comp->cur_except_level += 1;
237 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
238 comp->scope_cur->exc_stack_size = comp->cur_except_level;
239 }
240}
241
242STATIC void compile_decrease_except_level(compiler_t *comp) {
243 assert(comp->cur_except_level > 0);
244 comp->cur_except_level -= 1;
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC 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 +0000248 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 +0100249 scope->parent = comp->scope_cur;
250 scope->next = NULL;
251 if (comp->scope_head == NULL) {
252 comp->scope_head = scope;
253 } else {
254 scope_t *s = comp->scope_head;
255 while (s->next != NULL) {
256 s = s->next;
257 }
258 s->next = scope;
259 }
260 return scope;
261}
262
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200263STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000264 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100265 return 0;
Damiend99b0522013-12-21 18:17:45 +0000266 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100267 return 1;
268 } else {
Damiend99b0522013-12-21 18:17:45 +0000269 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
270 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100271 return 1;
272 } else {
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200278STATIC 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 +0000279 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
280 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100282 for (int i = 0; i < num_nodes; i++) {
283 f(comp, pns->nodes[i]);
284 }
Damiend99b0522013-12-21 18:17:45 +0000285 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100286 f(comp, pn);
287 }
288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000291 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100292 *nodes = NULL;
293 return 0;
Damiend99b0522013-12-21 18:17:45 +0000294 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100295 *nodes = pn;
296 return 1;
297 } else {
Damiend99b0522013-12-21 18:17:45 +0000298 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
299 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100300 *nodes = pn;
301 return 1;
302 } else {
303 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000304 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100305 }
306 }
307}
308
Damiend99b0522013-12-21 18:17:45 +0000309void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100310}
311
Damiend99b0522013-12-21 18:17:45 +0000312void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
313 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100314 for (int i = 0; i < num_nodes; i++) {
315 compile_node(comp, pns->nodes[i]);
316 }
317}
318
Damien3ef4abb2013-10-12 16:53:13 +0100319#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200320STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000321 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100322 return false;
323 }
Damiend99b0522013-12-21 18:17:45 +0000324 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100325 return false;
326 }
327 return true;
328}
329
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200330STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000331 uint len;
332 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000333 bool has_single_quote = false;
334 bool has_double_quote = false;
335 for (int i = 0; i < len; i++) {
336 if (str[i] == '\'') {
337 has_single_quote = true;
338 } else if (str[i] == '"') {
339 has_double_quote = true;
340 }
341 }
342 if (bytes) {
343 vstr_printf(vstr, "b");
344 }
345 bool quote_single = false;
346 if (has_single_quote && !has_double_quote) {
347 vstr_printf(vstr, "\"");
348 } else {
349 quote_single = true;
350 vstr_printf(vstr, "'");
351 }
352 for (int i = 0; i < len; i++) {
353 if (str[i] == '\n') {
354 vstr_printf(vstr, "\\n");
355 } else if (str[i] == '\\') {
356 vstr_printf(vstr, "\\\\");
357 } else if (str[i] == '\'' && quote_single) {
358 vstr_printf(vstr, "\\'");
359 } else {
360 vstr_printf(vstr, "%c", str[i]);
361 }
362 }
363 if (has_single_quote && !has_double_quote) {
364 vstr_printf(vstr, "\"");
365 } else {
366 vstr_printf(vstr, "'");
367 }
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000371 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200372 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
373 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
374 return;
375 }
376
Damiend99b0522013-12-21 18:17:45 +0000377 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
378 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
379 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000380 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
381 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
382 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
383 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
384 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100385 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000386 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
387 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
388 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100389 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100390 }
391 break;
392 default: assert(0);
393 }
394}
395
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200396STATIC 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 +0100397 int n = 0;
398 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000399 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100400 }
401 int total = n;
402 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000403 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 total += 1;
Damien3a205172013-10-12 15:01:56 +0100405 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 is_const = false;
407 }
408 }
409 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100410 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100411 is_const = false;
412 break;
413 }
414 }
415 if (total > 0 && is_const) {
416 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000417 vstr_t *vstr = vstr_new();
418 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000419 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000420 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100421 need_comma = true;
422 }
423 for (int i = 0; i < n; i++) {
424 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000425 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100426 }
Damien02f89412013-12-12 15:13:36 +0000427 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100428 need_comma = true;
429 }
430 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000431 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100432 } else {
Damien02f89412013-12-12 15:13:36 +0000433 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000436 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100437 } else {
Damiend99b0522013-12-21 18:17:45 +0000438 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100439 compile_node(comp, pn);
440 }
441 for (int i = 0; i < n; i++) {
442 compile_node(comp, pns_list->nodes[i]);
443 }
Damien Georgeb9791222014-01-23 00:34:21 +0000444 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100445 }
446}
Damien3a205172013-10-12 15:01:56 +0100447#endif
448
449// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000450void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100451#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100452 cpython_c_tuple(comp, pn, pns_list);
453#else
454 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000455 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100456 compile_node(comp, pn);
457 total += 1;
458 }
459 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000460 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100461 for (int i = 0; i < n; i++) {
462 compile_node(comp, pns_list->nodes[i]);
463 }
464 total += n;
465 }
Damien Georgeb9791222014-01-23 00:34:21 +0000466 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100467#endif
468}
Damien429d7192013-10-04 19:53:11 +0100469
Damiend99b0522013-12-21 18:17:45 +0000470void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100471 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000472 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200475STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000476 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200477 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200480STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200481 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 +0100482}
483
Damien3ef4abb2013-10-12 16:53:13 +0100484#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100485// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200486STATIC 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 +0100487 if (node_is_const_false(pn)) {
488 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000489 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100490 }
491 return;
492 } else if (node_is_const_true(pn)) {
493 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
498 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
499 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
500 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100501 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100502 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100503 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100504 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100505 }
Damien3a205172013-10-12 15:01:56 +0100506 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000507 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100508 } else {
509 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100510 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100511 }
512 }
513 return;
Damiend99b0522013-12-21 18:17:45 +0000514 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100515 if (jump_if == false) {
516 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100517 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100518 }
519 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100520 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100521 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100522 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100523 }
Damien3a205172013-10-12 15:01:56 +0100524 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000525 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100526 }
527 return;
Damiend99b0522013-12-21 18:17:45 +0000528 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100529 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100530 return;
531 }
532 }
533
534 // nothing special, fall back to default compiling for node and jump
535 compile_node(comp, pn);
536 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100538 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000539 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100540 }
541}
Damien3a205172013-10-12 15:01:56 +0100542#endif
Damien429d7192013-10-04 19:53:11 +0100543
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200544STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100545#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100546 cpython_c_if_cond(comp, pn, jump_if, label, false);
547#else
548 if (node_is_const_false(pn)) {
549 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000550 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100551 }
552 return;
553 } else if (node_is_const_true(pn)) {
554 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
559 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
560 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
561 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100562 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100563 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100564 for (int i = 0; i < n - 1; i++) {
565 c_if_cond(comp, pns->nodes[i], true, label2);
566 }
567 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100569 } else {
570 for (int i = 0; i < n; i++) {
571 c_if_cond(comp, pns->nodes[i], true, label);
572 }
573 }
574 return;
Damiend99b0522013-12-21 18:17:45 +0000575 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100576 if (jump_if == false) {
577 for (int i = 0; i < n; i++) {
578 c_if_cond(comp, pns->nodes[i], false, label);
579 }
580 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100581 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100582 for (int i = 0; i < n - 1; i++) {
583 c_if_cond(comp, pns->nodes[i], false, label2);
584 }
585 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000586 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100587 }
588 return;
Damiend99b0522013-12-21 18:17:45 +0000589 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100590 c_if_cond(comp, pns->nodes[0], !jump_if, label);
591 return;
592 }
593 }
594
595 // nothing special, fall back to default compiling for node and jump
596 compile_node(comp, pn);
597 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000598 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100599 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000600 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100601 }
602#endif
Damien429d7192013-10-04 19:53:11 +0100603}
604
605typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000606void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100607
Damiend99b0522013-12-21 18:17:45 +0000608void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100609 if (assign_kind != ASSIGN_AUG_STORE) {
610 compile_node(comp, pns->nodes[0]);
611 }
612
Damiend99b0522013-12-21 18:17:45 +0000613 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
614 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
615 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
616 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100617 if (assign_kind != ASSIGN_AUG_STORE) {
618 for (int i = 0; i < n - 1; i++) {
619 compile_node(comp, pns1->nodes[i]);
620 }
621 }
Damiend99b0522013-12-21 18:17:45 +0000622 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
623 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100624 }
Damiend99b0522013-12-21 18:17:45 +0000625 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100626 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000627 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100628 if (assign_kind == ASSIGN_AUG_STORE) {
629 EMIT(rot_three);
630 EMIT(store_subscr);
631 } else {
632 compile_node(comp, pns1->nodes[0]);
633 if (assign_kind == ASSIGN_AUG_LOAD) {
634 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100635 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100636 } else {
637 EMIT(store_subscr);
638 }
639 }
Damiend99b0522013-12-21 18:17:45 +0000640 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
641 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100642 if (assign_kind == ASSIGN_AUG_LOAD) {
643 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100645 } else {
646 if (assign_kind == ASSIGN_AUG_STORE) {
647 EMIT(rot_two);
648 }
Damien Georgeb9791222014-01-23 00:34:21 +0000649 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100650 }
651 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100652 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100653 }
654 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100655 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100656 }
657
Damiend99b0522013-12-21 18:17:45 +0000658 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100659 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100660 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100661
662 return;
663
664cannot_assign:
665 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100666}
667
Damien George0288cf02014-04-11 11:53:00 +0000668// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
669void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
670 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
671
672 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100673 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000674 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
675 EMIT_ARG(unpack_ex, 0, num_tail);
676 have_star_index = 0;
677 }
678 for (int i = 0; i < num_tail; i++) {
679 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100680 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000681 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
682 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100683 } else {
Damien George0288cf02014-04-11 11:53:00 +0000684 compile_syntax_error(comp, nodes_tail[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100685 return;
686 }
687 }
688 }
689 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000690 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100691 }
Damien George0288cf02014-04-11 11:53:00 +0000692 if (num_head != 0) {
693 if (0 == have_star_index) {
694 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100695 } else {
Damien George0288cf02014-04-11 11:53:00 +0000696 c_assign(comp, node_head, ASSIGN_STORE);
697 }
698 }
699 for (int i = 0; i < num_tail; i++) {
700 if (num_head + i == have_star_index) {
701 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
702 } else {
703 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100704 }
705 }
706}
707
708// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000709void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100710 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000711 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100712 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000713 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
714 if (MP_PARSE_NODE_IS_ID(pn)) {
715 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100716 switch (assign_kind) {
717 case ASSIGN_STORE:
718 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000719 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100720 break;
721 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000722 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100723 break;
724 }
725 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100726 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100727 return;
728 }
729 } else {
Damiend99b0522013-12-21 18:17:45 +0000730 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
731 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100732 case PN_power:
733 // lhs is an index or attribute
734 c_assign_power(comp, pns, assign_kind);
735 break;
736
737 case PN_testlist_star_expr:
738 case PN_exprlist:
739 // lhs is a tuple
740 if (assign_kind != ASSIGN_STORE) {
741 goto bad_aug;
742 }
Damien George0288cf02014-04-11 11:53:00 +0000743 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100744 break;
745
746 case PN_atom_paren:
747 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000748 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100749 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100750 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100751 return;
Damiend99b0522013-12-21 18:17:45 +0000752 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
753 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100754 goto testlist_comp;
755 } else {
756 // parenthesis around 1 item, is just that item
757 pn = pns->nodes[0];
758 goto tail_recursion;
759 }
760 break;
761
762 case PN_atom_bracket:
763 // lhs is something in brackets
764 if (assign_kind != ASSIGN_STORE) {
765 goto bad_aug;
766 }
Damiend99b0522013-12-21 18:17:45 +0000767 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100768 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000769 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000770 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
771 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100772 goto testlist_comp;
773 } else {
774 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000775 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100776 }
777 break;
778
779 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100780 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
781 return;
Damien429d7192013-10-04 19:53:11 +0100782 }
783 return;
784
785 testlist_comp:
786 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000787 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
788 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
789 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100790 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000791 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000792 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000793 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100794 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000795 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
796 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000797 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100798 // TODO can we ever get here? can it be compiled?
799 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
800 return;
Damien429d7192013-10-04 19:53:11 +0100801 } else {
802 // sequence with 2 items
803 goto sequence_with_2_items;
804 }
805 } else {
806 // sequence with 2 items
807 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000808 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100809 }
810 return;
811 }
812 return;
813
814 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100815 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100816}
817
818// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100819// if we are not in CPython compatibility mode then:
820// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
821// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
822// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100823void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
824 assert(n_pos_defaults >= 0);
825 assert(n_kw_defaults >= 0);
826
Damien429d7192013-10-04 19:53:11 +0100827 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000828 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100829 int nfree = 0;
830 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000831 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
832 id_info_t *id = &comp->scope_cur->id_info[i];
833 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
834 for (int j = 0; j < this_scope->id_info_len; j++) {
835 id_info_t *id2 = &this_scope->id_info[j];
836 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000837#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000838 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000839#else
840 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100841 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000842#endif
Damien318aec62013-12-10 18:28:17 +0000843 nfree += 1;
844 }
845 }
Damien429d7192013-10-04 19:53:11 +0100846 }
847 }
848 }
Damien429d7192013-10-04 19:53:11 +0100849
850 // make the function/closure
851 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100852 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100853 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000854 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100855 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100856 }
857}
858
Damiend99b0522013-12-21 18:17:45 +0000859void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000860 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000861 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
862 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100863 // bare star
864 comp->have_bare_star = true;
865 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000866
867 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
868 // TODO do we need to do anything with this?
869
870 } else {
871 mp_parse_node_t pn_id;
872 mp_parse_node_t pn_colon;
873 mp_parse_node_t pn_equal;
874 if (MP_PARSE_NODE_IS_ID(pn)) {
875 // this parameter is just an id
876
877 pn_id = pn;
878 pn_colon = MP_PARSE_NODE_NULL;
879 pn_equal = MP_PARSE_NODE_NULL;
880
881 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
882 // this parameter has a colon and/or equal specifier
883
884 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
885 pn_id = pns->nodes[0];
886 pn_colon = pns->nodes[1];
887 pn_equal = pns->nodes[2];
888
889 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100890 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000891 assert(0);
892 return;
893 }
894
895 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
896 // this parameter does not have a default value
897
898 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George69b89d22014-04-11 13:38:30 +0000899 if (!comp->have_bare_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100900 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000901 return;
902 }
903
904 } else {
905 // this parameter has a default value
906 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
907
908 if (comp->have_bare_star) {
Damien George69b89d22014-04-11 13:38:30 +0000909 comp->num_dict_params += 1;
Damien Georgee337f1e2014-03-31 15:18:37 +0100910#if !MICROPY_EMIT_CPYTHON
Damien George69b89d22014-04-11 13:38:30 +0000911 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
912 if (comp->num_dict_params == 1) {
913 // first default dict param, so make the map
914 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000915 }
Damien George69b89d22014-04-11 13:38:30 +0000916#endif
917 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
918 compile_node(comp, pn_equal);
919#if !MICROPY_EMIT_CPYTHON
920 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
921 EMIT(store_map);
922#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000923 } else {
Damien George69b89d22014-04-11 13:38:30 +0000924 comp->num_default_params += 1;
925 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000926 }
927 }
928
929 // TODO pn_colon not implemented
930 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100931 }
932}
933
934// leaves function object on stack
935// returns function name
Damiend99b0522013-12-21 18:17:45 +0000936qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100937 if (comp->pass == PASS_1) {
938 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000939 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100940 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000941 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100942 }
943
944 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George02a4c052014-04-09 12:50:58 +0100945 uint old_have_bare_star = comp->have_bare_star;
Damien George69b89d22014-04-11 13:38:30 +0000946 uint old_num_dict_params = comp->num_dict_params;
947 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +0100948
949 // compile default parameters
950 comp->have_bare_star = false;
Damien George69b89d22014-04-11 13:38:30 +0000951 comp->num_dict_params = 0;
952 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +0100953 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000954
955 if (comp->had_error) {
956 return MP_QSTR_NULL;
957 }
958
Damien Georgee337f1e2014-03-31 15:18:37 +0100959#if !MICROPY_EMIT_CPYTHON
960 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George69b89d22014-04-11 13:38:30 +0000961 if (comp->num_default_params > 0) {
962 EMIT_ARG(build_tuple, comp->num_default_params);
Damien Georgee337f1e2014-03-31 15:18:37 +0100963 }
964#endif
965
Damien429d7192013-10-04 19:53:11 +0100966 // get the scope for this function
967 scope_t *fscope = (scope_t*)pns->nodes[4];
968
969 // make the function
Damien George69b89d22014-04-11 13:38:30 +0000970 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100971
972 // restore variables
973 comp->have_bare_star = old_have_bare_star;
Damien George69b89d22014-04-11 13:38:30 +0000974 comp->num_dict_params = old_num_dict_params;
975 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100976
977 // return its name (the 'f' in "def f(...):")
978 return fscope->simple_name;
979}
980
981// leaves class object on stack
982// returns class name
Damiend99b0522013-12-21 18:17:45 +0000983qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100984 if (comp->pass == PASS_1) {
985 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000986 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100987 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000988 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100989 }
990
991 EMIT(load_build_class);
992
993 // scope for this class
994 scope_t *cscope = (scope_t*)pns->nodes[3];
995
996 // compile the class
997 close_over_variables_etc(comp, cscope, 0, 0);
998
999 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001000 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001001
1002 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001003 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1004 mp_parse_node_t parents = pns->nodes[1];
1005 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1006 parents = MP_PARSE_NODE_NULL;
1007 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001008 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001009 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001010
1011 // return its name (the 'C' in class C(...):")
1012 return cscope->simple_name;
1013}
1014
Damien6cdd3af2013-10-05 18:08:26 +01001015// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001016STATIC 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 +00001017 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001018 return false;
1019 }
1020
1021 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001022 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001023 return true;
1024 }
1025
Damiend99b0522013-12-21 18:17:45 +00001026 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001027 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001028 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001029#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001030 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001031 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001032 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001033 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001034#endif
Damien3ef4abb2013-10-12 16:53:13 +01001035#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001036 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001037 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001038#endif
Damien6cdd3af2013-10-05 18:08:26 +01001039 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001040 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001041 }
1042
1043 return true;
1044}
1045
Damiend99b0522013-12-21 18:17:45 +00001046void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001047 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001048 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001049 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1050
Damien6cdd3af2013-10-05 18:08:26 +01001051 // inherit emit options for this function/class definition
1052 uint emit_options = comp->scope_cur->emit_options;
1053
1054 // compile each decorator
1055 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001056 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001057 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1058 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001059
1060 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001061 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001062 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1063
1064 // check for built-in decorators
1065 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1066 // this was a built-in
1067 num_built_in_decorators += 1;
1068
1069 } else {
1070 // not a built-in, compile normally
1071
1072 // compile the decorator function
1073 compile_node(comp, name_nodes[0]);
1074 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001075 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001076 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001077 }
1078
1079 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001080 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001081 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001082 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001083 compile_node(comp, pns_decorator->nodes[1]);
1084 }
Damien429d7192013-10-04 19:53:11 +01001085 }
1086 }
1087
1088 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001089 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001090 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001091 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001092 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001093 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001094 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001095 } else {
1096 // shouldn't happen
1097 assert(0);
1098 }
1099
1100 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001101 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001102 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001103 }
1104
1105 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001106 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001107}
1108
Damiend99b0522013-12-21 18:17:45 +00001109void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001110 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001111 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001112 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001113}
1114
Damiend99b0522013-12-21 18:17:45 +00001115void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1116 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001117 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001118 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1119 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001120
1121 compile_node(comp, pns->nodes[0]); // base of the power node
1122
Damiend99b0522013-12-21 18:17:45 +00001123 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1124 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1125 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1126 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001127 for (int i = 0; i < n - 1; i++) {
1128 compile_node(comp, pns1->nodes[i]);
1129 }
Damiend99b0522013-12-21 18:17:45 +00001130 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1131 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001132 }
Damiend99b0522013-12-21 18:17:45 +00001133 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001134 // can't delete function calls
1135 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001136 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001137 compile_node(comp, pns1->nodes[0]);
1138 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001139 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1140 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001141 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001142 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001143 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001144 }
1145 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001146 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001147 }
1148
Damiend99b0522013-12-21 18:17:45 +00001149 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001150 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001151 }
Damiend99b0522013-12-21 18:17:45 +00001152 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1153 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1154 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1155 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001156 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1157
Damiend99b0522013-12-21 18:17:45 +00001158 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1159 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1160 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001161 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001162 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001163 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001164 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001165 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001166 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001167 c_del_stmt(comp, pns->nodes[0]);
1168 for (int i = 0; i < n; i++) {
1169 c_del_stmt(comp, pns1->nodes[i]);
1170 }
Damiend99b0522013-12-21 18:17:45 +00001171 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001172 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001173 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001174 } else {
1175 // sequence with 2 items
1176 goto sequence_with_2_items;
1177 }
1178 } else {
1179 // sequence with 2 items
1180 sequence_with_2_items:
1181 c_del_stmt(comp, pns->nodes[0]);
1182 c_del_stmt(comp, pns->nodes[1]);
1183 }
1184 } else {
1185 // tuple with 1 element
1186 c_del_stmt(comp, pn);
1187 }
1188 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001189 // TODO is there anything else to implement?
1190 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001191 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001192
1193 return;
1194
1195cannot_delete:
1196 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001197}
1198
Damiend99b0522013-12-21 18:17:45 +00001199void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001200 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1201}
1202
Damiend99b0522013-12-21 18:17:45 +00001203void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001204 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001205 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001206 }
Damien Georgecbddb272014-02-01 20:08:18 +00001207 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001208}
1209
Damiend99b0522013-12-21 18:17:45 +00001210void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001211 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001212 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001213 }
Damien Georgecbddb272014-02-01 20:08:18 +00001214 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001215}
1216
Damiend99b0522013-12-21 18:17:45 +00001217void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001218 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001219 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001220 return;
1221 }
Damiend99b0522013-12-21 18:17:45 +00001222 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001223 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001224 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001225 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001226 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001227 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1228 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 +01001229
Damien George6f355fd2014-04-10 14:11:31 +01001230 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001231 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1232 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1233 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001234 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001235 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1236 } else {
1237 compile_node(comp, pns->nodes[0]);
1238 }
1239 EMIT(return_value);
1240}
1241
Damiend99b0522013-12-21 18:17:45 +00001242void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001243 compile_node(comp, pns->nodes[0]);
1244 EMIT(pop_top);
1245}
1246
Damiend99b0522013-12-21 18:17:45 +00001247void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1248 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001249 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001250 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001251 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001252 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001253 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001254 compile_node(comp, pns->nodes[0]);
1255 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001256 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001257 } else {
1258 // raise x
1259 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001260 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001261 }
1262}
1263
Damien George635543c2014-04-10 12:56:52 +01001264// q_base holds the base of the name
1265// eg a -> q_base=a
1266// a.b.c -> q_base=a
1267void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001268 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001269 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1270 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001271 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001272 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001273 pn = pns->nodes[0];
1274 is_as = true;
1275 }
Damien George635543c2014-04-10 12:56:52 +01001276 if (MP_PARSE_NODE_IS_NULL(pn)) {
1277 // empty name (eg, from . import x)
1278 *q_base = MP_QSTR_;
1279 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1280 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001281 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001282 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001283 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001284 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001285 }
Damien George635543c2014-04-10 12:56:52 +01001286 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001287 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1288 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1289 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001290 // a name of the form a.b.c
1291 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001292 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001293 }
Damiend99b0522013-12-21 18:17:45 +00001294 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001295 int len = n - 1;
1296 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001297 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001298 }
Damien George55baff42014-01-21 21:40:13 +00001299 byte *q_ptr;
1300 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001301 for (int i = 0; i < n; i++) {
1302 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001303 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001304 }
Damien George55baff42014-01-21 21:40:13 +00001305 uint str_src_len;
1306 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001307 memcpy(str_dest, str_src, str_src_len);
1308 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001309 }
Damien George635543c2014-04-10 12:56:52 +01001310 qstr q_full = qstr_build_end(q_ptr);
1311 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001312 if (is_as) {
1313 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001314 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001315 }
1316 }
1317 } else {
Damien George635543c2014-04-10 12:56:52 +01001318 // shouldn't happen
1319 assert(0);
Damien429d7192013-10-04 19:53:11 +01001320 }
1321 } else {
Damien George635543c2014-04-10 12:56:52 +01001322 // shouldn't happen
1323 assert(0);
Damien429d7192013-10-04 19:53:11 +01001324 }
1325}
1326
Damiend99b0522013-12-21 18:17:45 +00001327void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001328 EMIT_ARG(load_const_small_int, 0); // level 0 import
1329 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1330 qstr q_base;
1331 do_import_name(comp, pn, &q_base);
1332 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001333}
1334
Damiend99b0522013-12-21 18:17:45 +00001335void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001336 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1337}
1338
Damiend99b0522013-12-21 18:17:45 +00001339void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001340 mp_parse_node_t pn_import_source = pns->nodes[0];
1341
1342 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1343 uint import_level = 0;
1344 do {
1345 mp_parse_node_t pn_rel;
1346 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)) {
1347 // This covers relative imports with dots only like "from .. import"
1348 pn_rel = pn_import_source;
1349 pn_import_source = MP_PARSE_NODE_NULL;
1350 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1351 // This covers relative imports starting with dot(s) like "from .foo import"
1352 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1353 pn_rel = pns_2b->nodes[0];
1354 pn_import_source = pns_2b->nodes[1];
1355 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1356 } else {
1357 // Not a relative import
1358 break;
1359 }
1360
1361 // get the list of . and/or ...'s
1362 mp_parse_node_t *nodes;
1363 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1364
1365 // count the total number of .'s
1366 for (int i = 0; i < n; i++) {
1367 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1368 import_level++;
1369 } else {
1370 // should be an MP_TOKEN_ELLIPSIS
1371 import_level += 3;
1372 }
1373 }
1374 } while (0);
1375
Damiend99b0522013-12-21 18:17:45 +00001376 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001377 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001378
1379 // build the "fromlist" tuple
1380#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001381 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001382#else
Damien Georgeb9791222014-01-23 00:34:21 +00001383 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1384 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001385#endif
1386
1387 // do the import
Damien George635543c2014-04-10 12:56:52 +01001388 qstr dummy_q;
1389 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001390 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001391
Damien429d7192013-10-04 19:53:11 +01001392 } else {
Damien George635543c2014-04-10 12:56:52 +01001393 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001394
1395 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001396 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001397 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001398#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001399 {
1400 vstr_t *vstr = vstr_new();
1401 vstr_printf(vstr, "(");
1402 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001403 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1404 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1405 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001406 if (i > 0) {
1407 vstr_printf(vstr, ", ");
1408 }
1409 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001410 uint len;
1411 const byte *str = qstr_data(id2, &len);
1412 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001413 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001414 }
Damien02f89412013-12-12 15:13:36 +00001415 if (n == 1) {
1416 vstr_printf(vstr, ",");
1417 }
1418 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001419 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001420 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001421 }
Damiendb4c3612013-12-10 17:27:24 +00001422#else
1423 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001424 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1425 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1426 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001428 }
Damien Georgeb9791222014-01-23 00:34:21 +00001429 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001430#endif
1431
1432 // do the import
Damien George635543c2014-04-10 12:56:52 +01001433 qstr dummy_q;
1434 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001435 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001436 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1437 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1438 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001439 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001440 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001441 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001442 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001443 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001444 }
1445 }
1446 EMIT(pop_top);
1447 }
1448}
1449
Damiend99b0522013-12-21 18:17:45 +00001450void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001451 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001452 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1453 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001454 } else {
Damiend99b0522013-12-21 18:17:45 +00001455 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1456 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001457 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001458 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001459 }
Damien429d7192013-10-04 19:53:11 +01001460 }
1461 }
1462}
1463
Damiend99b0522013-12-21 18:17:45 +00001464void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001465 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001466 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1467 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001468 } else {
Damiend99b0522013-12-21 18:17:45 +00001469 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1470 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001471 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001472 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001473 }
Damien429d7192013-10-04 19:53:11 +01001474 }
1475 }
1476}
1477
Damiend99b0522013-12-21 18:17:45 +00001478void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001479 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001480 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001481 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 +00001482 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001483 // assertion message
1484 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001485 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001486 }
Damien Georgeb9791222014-01-23 00:34:21 +00001487 EMIT_ARG(raise_varargs, 1);
1488 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001489}
1490
Damiend99b0522013-12-21 18:17:45 +00001491void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001492 // TODO proper and/or short circuiting
1493
Damien George6f355fd2014-04-10 14:11:31 +01001494 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001495
Damien George6f355fd2014-04-10 14:11:31 +01001496 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001497 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1498
1499 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001500
1501 if (
1502#if !MICROPY_EMIT_CPYTHON
1503 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1504 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1505#endif
1506 // optimisation to not jump if last instruction was return
1507 !EMIT(last_emit_was_return_value)
1508 ) {
1509 // jump over elif/else blocks
1510 EMIT_ARG(jump, l_end);
1511 }
1512
Damien Georgeb9791222014-01-23 00:34:21 +00001513 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001514
Damiend99b0522013-12-21 18:17:45 +00001515 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001516 // compile elif blocks
1517
Damiend99b0522013-12-21 18:17:45 +00001518 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001519
Damiend99b0522013-12-21 18:17:45 +00001520 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001521 // multiple elif blocks
1522
Damiend99b0522013-12-21 18:17:45 +00001523 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001524 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001525 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001526 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001527 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1528
1529 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001530 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001531 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001532 }
Damien Georgeb9791222014-01-23 00:34:21 +00001533 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001534 }
1535
1536 } else {
1537 // a single elif block
1538
Damienb05d7072013-10-05 13:37:10 +01001539 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001540 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1541
1542 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001543 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001545 }
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001547 }
1548 }
1549
1550 // compile else block
1551 compile_node(comp, pns->nodes[3]); // can be null
1552
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001554}
1555
Damien Georgecbddb272014-02-01 20:08:18 +00001556#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001557 uint old_break_label = comp->break_label; \
1558 uint old_continue_label = comp->continue_label; \
1559 uint break_label = comp_next_label(comp); \
1560 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001561 comp->break_label = break_label; \
1562 comp->continue_label = continue_label; \
1563 comp->break_continue_except_level = comp->cur_except_level;
1564
1565#define END_BREAK_CONTINUE_BLOCK \
1566 comp->break_label = old_break_label; \
1567 comp->continue_label = old_continue_label; \
1568 comp->break_continue_except_level = comp->cur_except_level;
1569
Damiend99b0522013-12-21 18:17:45 +00001570void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001571 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001572
Damience89a212013-10-15 22:25:17 +01001573 // compared to CPython, we have an optimised version of while loops
1574#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001575 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(setup_loop, break_label);
1577 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001578 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1579 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001580 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001582 }
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001584 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1585 // this is a small hack to agree with CPython
1586 if (!node_is_const_true(pns->nodes[0])) {
1587 EMIT(pop_block);
1588 }
Damience89a212013-10-15 22:25:17 +01001589#else
Damien George6f355fd2014-04-10 14:11:31 +01001590 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001591 EMIT_ARG(jump, continue_label);
1592 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001593 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001595 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1596#endif
1597
1598 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001599 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001600
1601 compile_node(comp, pns->nodes[2]); // else
1602
Damien Georgeb9791222014-01-23 00:34:21 +00001603 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001604}
1605
Damienf72fd0e2013-11-06 20:20:49 +00001606// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001607// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1608// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001609void 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 +00001610 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001611
Damien George6f355fd2014-04-10 14:11:31 +01001612 uint top_label = comp_next_label(comp);
1613 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001614
Damien George3ff2d032014-03-31 18:02:22 +01001615 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001616 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001617 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001618
Damien Georgeb9791222014-01-23 00:34:21 +00001619 EMIT_ARG(jump, entry_label);
1620 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001621
Damien George3ff2d032014-03-31 18:02:22 +01001622 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001623 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001624
1625 // store next value to var
1626 c_assign(comp, pn_var, ASSIGN_STORE);
1627
Damienf3822fc2013-11-09 20:12:03 +00001628 // compile body
1629 compile_node(comp, pn_body);
1630
Damien Georgeb9791222014-01-23 00:34:21 +00001631 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001632
Damien George3ff2d032014-03-31 18:02:22 +01001633 // compile: var + step, duplicated on stack
1634 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001635 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001636 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001637 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001638
Damien Georgeb9791222014-01-23 00:34:21 +00001639 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001640
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001641 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001642 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001643 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1644 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001645 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001646 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001647 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001648 }
Damien Georgeb9791222014-01-23 00:34:21 +00001649 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001650
Damien George3ff2d032014-03-31 18:02:22 +01001651 // discard final value of var that failed the loop condition
1652 EMIT(pop_top);
1653
Damienf72fd0e2013-11-06 20:20:49 +00001654 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001655 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001656
1657 compile_node(comp, pn_else);
1658
Damien Georgeb9791222014-01-23 00:34:21 +00001659 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001660}
1661
Damiend99b0522013-12-21 18:17:45 +00001662void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001663#if !MICROPY_EMIT_CPYTHON
1664 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1665 // this is actually slower, but uses no heap memory
1666 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001667 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 +00001668 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001669 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1670 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1671 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1672 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001673 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1674 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001675 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001676 mp_parse_node_t pn_range_start;
1677 mp_parse_node_t pn_range_end;
1678 mp_parse_node_t pn_range_step;
1679 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001680 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001681 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001682 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001683 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001684 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001685 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001686 } else if (n_args == 2) {
1687 pn_range_start = args[0];
1688 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001689 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001690 } else {
1691 pn_range_start = args[0];
1692 pn_range_end = args[1];
1693 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001694 // We need to know sign of step. This is possible only if it's constant
1695 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1696 optimize = false;
1697 }
Damienf72fd0e2013-11-06 20:20:49 +00001698 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001699 }
1700 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001701 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1702 return;
1703 }
1704 }
1705 }
1706#endif
1707
Damien Georgecbddb272014-02-01 20:08:18 +00001708 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001709
Damien George6f355fd2014-04-10 14:11:31 +01001710 uint pop_label = comp_next_label(comp);
1711 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001712
Damience89a212013-10-15 22:25:17 +01001713 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1714#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001715 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001716#endif
1717
Damien429d7192013-10-04 19:53:11 +01001718 compile_node(comp, pns->nodes[1]); // iterator
1719 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001720 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001721 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001722 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1723 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001724 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001725 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001726 }
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001728 EMIT(for_iter_end);
1729
1730 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001731 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001732
Damience89a212013-10-15 22:25:17 +01001733#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001734 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001735#endif
Damien429d7192013-10-04 19:53:11 +01001736
1737 compile_node(comp, pns->nodes[3]); // else (not tested)
1738
Damien Georgeb9791222014-01-23 00:34:21 +00001739 EMIT_ARG(label_assign, break_label);
1740 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001741}
1742
Damiend99b0522013-12-21 18:17:45 +00001743void 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 +01001744 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001745 uint l1 = comp_next_label(comp);
1746 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001747
Damien Georgeb9791222014-01-23 00:34:21 +00001748 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001749 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001750
Damien429d7192013-10-04 19:53:11 +01001751 compile_node(comp, pn_body); // body
1752 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001753 EMIT_ARG(jump, success_label); // jump over exception handler
1754
1755 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001756 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 +00001757
Damien George6f355fd2014-04-10 14:11:31 +01001758 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001759
1760 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001761 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1762 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001763
1764 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001765 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001766
Damiend99b0522013-12-21 18:17:45 +00001767 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001768 // this is a catch all exception handler
1769 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001770 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001771 return;
1772 }
1773 } else {
1774 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001775 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1776 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1777 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1778 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001779 // handler binds the exception to a local
1780 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001781 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001782 }
1783 }
1784 EMIT(dup_top);
1785 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001786 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001788 }
1789
1790 EMIT(pop_top);
1791
1792 if (qstr_exception_local == 0) {
1793 EMIT(pop_top);
1794 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001795 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001796 }
1797
1798 EMIT(pop_top);
1799
Damien George6f355fd2014-04-10 14:11:31 +01001800 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001801 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001802 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001803 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001804 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001805 }
1806 compile_node(comp, pns_except->nodes[1]);
1807 if (qstr_exception_local != 0) {
1808 EMIT(pop_block);
1809 }
1810 EMIT(pop_except);
1811 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001812 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1813 EMIT_ARG(label_assign, l3);
1814 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1815 EMIT_ARG(store_id, qstr_exception_local);
1816 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001817
Damien George8dcc0c72014-03-27 10:55:21 +00001818 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001819 EMIT(end_finally);
1820 }
Damien Georgeb9791222014-01-23 00:34:21 +00001821 EMIT_ARG(jump, l2);
1822 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001823 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001824 }
1825
Damien George8dcc0c72014-03-27 10:55:21 +00001826 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001827 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001828 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001829
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001831 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001832 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001833}
1834
Damiend99b0522013-12-21 18:17:45 +00001835void 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 +01001836 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001837
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001839 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001840
Damien429d7192013-10-04 19:53:11 +01001841 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001842 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001843 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001844 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001845 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001846 } else {
1847 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1848 }
1849 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001850 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1851 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001852 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001853
Damien George8dcc0c72014-03-27 10:55:21 +00001854 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001855 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001856}
1857
Damiend99b0522013-12-21 18:17:45 +00001858void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1859 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1860 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1861 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001862 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001863 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1864 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001865 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001866 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001867 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001868 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001869 // no finally
1870 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1871 } else {
1872 // have finally
Damiend99b0522013-12-21 18:17:45 +00001873 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 +01001874 }
1875 } else {
1876 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001877 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001878 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001879 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001880 }
1881 } else {
1882 // shouldn't happen
1883 assert(0);
1884 }
1885}
1886
Damiend99b0522013-12-21 18:17:45 +00001887void 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 +01001888 if (n == 0) {
1889 // no more pre-bits, compile the body of the with
1890 compile_node(comp, body);
1891 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001892 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001893 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001894 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001895 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001896 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001897 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001898 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1899 } else {
1900 // this pre-bit is just an expression
1901 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001902 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001903 EMIT(pop_top);
1904 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001905 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001906 // compile additional pre-bits and the body
1907 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1908 // finish this with block
1909 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001910 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1911 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001912 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001913 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001914 EMIT(end_finally);
1915 }
1916}
1917
Damiend99b0522013-12-21 18:17:45 +00001918void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001919 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001920 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001921 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1922 assert(n > 0);
1923
1924 // compile in a nested fashion
1925 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1926}
1927
Damiend99b0522013-12-21 18:17:45 +00001928void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1929 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001930 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1931 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001932 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001933 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001934 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001935 EMIT(pop_top);
1936
Damien429d7192013-10-04 19:53:11 +01001937 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001938 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001939 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001940 // do nothing with a lonely constant
1941 } else {
1942 compile_node(comp, pns->nodes[0]); // just an expression
1943 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1944 }
Damien429d7192013-10-04 19:53:11 +01001945 }
1946 } else {
Damiend99b0522013-12-21 18:17:45 +00001947 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1948 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001949 if (kind == PN_expr_stmt_augassign) {
1950 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1951 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001952 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001953 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001954 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001955 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1956 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1957 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1958 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1959 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1960 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1961 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1962 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1963 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1964 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1965 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1966 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1967 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001968 }
Damien George7e5fb242014-02-01 22:18:47 +00001969 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001970 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1971 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001972 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1973 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001974 // following CPython, we store left-most first
1975 if (rhs > 0) {
1976 EMIT(dup_top);
1977 }
1978 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1979 for (int i = 0; i < rhs; i++) {
1980 if (i + 1 < rhs) {
1981 EMIT(dup_top);
1982 }
Damiend99b0522013-12-21 18:17:45 +00001983 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001984 }
1985 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001986 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1987 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1988 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1989 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001990 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001991 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1992 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001993 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1994 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1995 // can't optimise when it's a star expression on the lhs
1996 goto no_optimisation;
1997 }
Damien429d7192013-10-04 19:53:11 +01001998 compile_node(comp, pns10->nodes[0]); // rhs
1999 compile_node(comp, pns10->nodes[1]); // rhs
2000 EMIT(rot_two);
2001 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2002 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002003 } else 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]) == 3
2006 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002007 // optimisation for a, b, c = d, e, f; 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 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2013 // can't optimise when it's a star expression on the lhs
2014 goto no_optimisation;
2015 }
Damien429d7192013-10-04 19:53:11 +01002016 compile_node(comp, pns10->nodes[0]); // rhs
2017 compile_node(comp, pns10->nodes[1]); // rhs
2018 compile_node(comp, pns10->nodes[2]); // rhs
2019 EMIT(rot_three);
2020 EMIT(rot_two);
2021 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2022 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2023 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2024 } else {
Damien George495d7812014-04-08 17:51:47 +01002025 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002026 compile_node(comp, pns1->nodes[0]); // rhs
2027 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2028 }
2029 } else {
2030 // shouldn't happen
2031 assert(0);
2032 }
2033 }
2034}
2035
Damien Georged17926d2014-03-30 13:35:08 +01002036void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002037 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002038 compile_node(comp, pns->nodes[0]);
2039 for (int i = 1; i < num_nodes; i += 1) {
2040 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002042 }
2043}
2044
Damiend99b0522013-12-21 18:17:45 +00002045void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2046 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2047 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002048
Damien George6f355fd2014-04-10 14:11:31 +01002049 uint l_fail = comp_next_label(comp);
2050 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002051 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2052 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002053 EMIT_ARG(jump, l_end);
2054 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002055 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002056 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002058}
2059
Damiend99b0522013-12-21 18:17:45 +00002060void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002061 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002062 //mp_parse_node_t pn_params = pns->nodes[0];
2063 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002064
2065 if (comp->pass == PASS_1) {
2066 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002067 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 +01002068 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002069 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002070 }
2071
2072 // get the scope for this lambda
2073 scope_t *this_scope = (scope_t*)pns->nodes[2];
2074
2075 // make the lambda
2076 close_over_variables_etc(comp, this_scope, 0, 0);
2077}
2078
Damiend99b0522013-12-21 18:17:45 +00002079void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002080 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002081 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002082 for (int i = 0; i < n; i += 1) {
2083 compile_node(comp, pns->nodes[i]);
2084 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002085 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002086 }
2087 }
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002089}
2090
Damiend99b0522013-12-21 18:17:45 +00002091void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002092 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002093 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002094 for (int i = 0; i < n; i += 1) {
2095 compile_node(comp, pns->nodes[i]);
2096 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002097 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002098 }
2099 }
Damien Georgeb9791222014-01-23 00:34:21 +00002100 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002101}
2102
Damiend99b0522013-12-21 18:17:45 +00002103void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002104 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002105 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002106}
2107
Damiend99b0522013-12-21 18:17:45 +00002108void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002109 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002110 compile_node(comp, pns->nodes[0]);
2111 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002112 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002113 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002114 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002115 }
2116 for (int i = 1; i + 1 < num_nodes; i += 2) {
2117 compile_node(comp, pns->nodes[i + 1]);
2118 if (i + 2 < num_nodes) {
2119 EMIT(dup_top);
2120 EMIT(rot_three);
2121 }
Damien George7e5fb242014-02-01 22:18:47 +00002122 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002124 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2126 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2127 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2128 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2129 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2130 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2131 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2132 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002133 }
2134 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002135 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2136 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2137 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002138 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002139 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002140 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002141 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002142 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002143 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002144 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002145 }
2146 } else {
2147 // shouldn't happen
2148 assert(0);
2149 }
2150 } else {
2151 // shouldn't happen
2152 assert(0);
2153 }
2154 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002155 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002156 }
2157 }
2158 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002159 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002160 EMIT_ARG(jump, l_end);
2161 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002162 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002163 EMIT(rot_two);
2164 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002165 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002166 }
2167}
2168
Damiend99b0522013-12-21 18:17:45 +00002169void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002170 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002171}
2172
Damiend99b0522013-12-21 18:17:45 +00002173void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002175}
2176
Damiend99b0522013-12-21 18:17:45 +00002177void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002178 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002179}
2180
Damiend99b0522013-12-21 18:17:45 +00002181void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002182 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002183}
2184
Damiend99b0522013-12-21 18:17:45 +00002185void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2186 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002187 compile_node(comp, pns->nodes[0]);
2188 for (int i = 1; i + 1 < num_nodes; i += 2) {
2189 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002190 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002191 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002192 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002193 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002194 } else {
2195 // shouldn't happen
2196 assert(0);
2197 }
2198 }
2199}
2200
Damiend99b0522013-12-21 18:17:45 +00002201void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2202 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002203 compile_node(comp, pns->nodes[0]);
2204 for (int i = 1; i + 1 < num_nodes; i += 2) {
2205 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002206 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002207 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002208 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002209 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002210 } else {
2211 // shouldn't happen
2212 assert(0);
2213 }
2214 }
2215}
2216
Damiend99b0522013-12-21 18:17:45 +00002217void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2218 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002219 compile_node(comp, pns->nodes[0]);
2220 for (int i = 1; i + 1 < num_nodes; i += 2) {
2221 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002222 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002223 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002224 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002225 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002226 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002227 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002228 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002229 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002230 } else {
2231 // shouldn't happen
2232 assert(0);
2233 }
2234 }
2235}
2236
Damiend99b0522013-12-21 18:17:45 +00002237void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002238 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002239 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002240 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002241 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002242 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002243 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002244 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002245 } else {
2246 // shouldn't happen
2247 assert(0);
2248 }
2249}
2250
Damien George35e2a4e2014-02-05 00:51:47 +00002251void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2252 // this is to handle special super() call
2253 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2254
2255 compile_generic_all_nodes(comp, pns);
2256}
2257
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002258STATIC 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 +01002259 // function to call is on top of stack
2260
Damien George35e2a4e2014-02-05 00:51:47 +00002261#if !MICROPY_EMIT_CPYTHON
2262 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002263 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 +00002264 EMIT_ARG(load_id, MP_QSTR___class__);
2265 // get first argument to function
2266 bool found = false;
2267 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002268 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2269 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 +00002270 found = true;
2271 break;
2272 }
2273 }
2274 if (!found) {
2275 printf("TypeError: super() call cannot find self\n");
2276 return;
2277 }
Damien George922ddd62014-04-09 12:43:17 +01002278 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002279 return;
2280 }
2281#endif
2282
Damien George02a4c052014-04-09 12:50:58 +01002283 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002284 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002285 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002286 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002287
Damien Georgebbcd49a2014-02-06 20:30:16 +00002288 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002289
2290 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002291 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002292 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002293 n_positional -= 1;
2294 }
Damien George922ddd62014-04-09 12:43:17 +01002295 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002296 n_positional -= 1;
2297 }
2298
2299 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002300 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002301 } else {
Damien George922ddd62014-04-09 12:43:17 +01002302 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002303 }
2304
2305 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002306 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002307}
2308
Damiend99b0522013-12-21 18:17:45 +00002309void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2310 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002311 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002312 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 +01002313 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002314 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2315 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002316 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002317 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002318 i += 1;
2319 } else {
2320 compile_node(comp, pns->nodes[i]);
2321 }
Damien George35e2a4e2014-02-05 00:51:47 +00002322 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002323 }
2324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002327 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002328 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002329}
2330
Damiend99b0522013-12-21 18:17:45 +00002331void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002332 // a list of strings
Damien63321742013-12-10 17:41:49 +00002333
2334 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002335 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002336 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002337 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002338 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002339 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2340 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2341 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002342 if (i == 0) {
2343 string_kind = pn_kind;
2344 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002345 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002346 return;
2347 }
Damien George55baff42014-01-21 21:40:13 +00002348 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002349 }
Damien63321742013-12-10 17:41:49 +00002350
Damien63321742013-12-10 17:41:49 +00002351 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002352 byte *q_ptr;
2353 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002354 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002355 uint s_len;
2356 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002357 memcpy(s_dest, s, s_len);
2358 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002359 }
Damien George55baff42014-01-21 21:40:13 +00002360 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002361
Damien Georgeb9791222014-01-23 00:34:21 +00002362 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002363}
2364
2365// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002366void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2367 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2368 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2369 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002370
2371 if (comp->pass == PASS_1) {
2372 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002373 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 +01002374 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002375 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002376 }
2377
2378 // get the scope for this comprehension
2379 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2380
2381 // compile the comprehension
2382 close_over_variables_etc(comp, this_scope, 0, 0);
2383
2384 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2385 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002386 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002387}
2388
Damiend99b0522013-12-21 18:17:45 +00002389void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2390 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002391 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002392 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2393 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2394 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2395 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2396 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2397 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2398 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002399 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002400 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002401 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002403 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002404 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002405 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002406 // generator expression
2407 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2408 } else {
2409 // tuple with 2 items
2410 goto tuple_with_2_items;
2411 }
2412 } else {
2413 // tuple with 2 items
2414 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002415 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002416 }
2417 } else {
2418 // parenthesis around a single item, is just that item
2419 compile_node(comp, pns->nodes[0]);
2420 }
2421}
2422
Damiend99b0522013-12-21 18:17:45 +00002423void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2424 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002425 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002426 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002427 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2428 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2429 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2430 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2431 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002432 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002433 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002434 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002435 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002436 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002437 // list of many items
2438 compile_node(comp, pns2->nodes[0]);
2439 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002440 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002441 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002442 // list comprehension
2443 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2444 } else {
2445 // list with 2 items
2446 goto list_with_2_items;
2447 }
2448 } else {
2449 // list with 2 items
2450 list_with_2_items:
2451 compile_node(comp, pns2->nodes[0]);
2452 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002453 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002454 }
2455 } else {
2456 // list with 1 item
2457 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002458 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002459 }
2460}
2461
Damiend99b0522013-12-21 18:17:45 +00002462void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2463 mp_parse_node_t pn = pns->nodes[0];
2464 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002465 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002466 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002467 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2468 pns = (mp_parse_node_struct_t*)pn;
2469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002470 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002471 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002472 compile_node(comp, pn);
2473 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002474 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2475 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2476 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2477 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002478 // dict/set with multiple elements
2479
2480 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002481 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002482 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2483
2484 // first element sets whether it's a dict or set
2485 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002486 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002487 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002488 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002489 compile_node(comp, pns->nodes[0]);
2490 EMIT(store_map);
2491 is_dict = true;
2492 } else {
2493 // a set
2494 compile_node(comp, pns->nodes[0]); // 1st value of set
2495 is_dict = false;
2496 }
2497
2498 // process rest of elements
2499 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002500 mp_parse_node_t pn = nodes[i];
2501 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002502 compile_node(comp, pn);
2503 if (is_dict) {
2504 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002505 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002506 return;
2507 }
2508 EMIT(store_map);
2509 } else {
2510 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002511 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002512 return;
2513 }
2514 }
2515 }
2516
2517 // if it's a set, build it
2518 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002519 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002520 }
Damiend99b0522013-12-21 18:17:45 +00002521 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002522 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002523 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002524 // a dictionary comprehension
2525 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2526 } else {
2527 // a set comprehension
2528 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2529 }
2530 } else {
2531 // shouldn't happen
2532 assert(0);
2533 }
2534 } else {
2535 // set with one element
2536 goto set_with_one_element;
2537 }
2538 } else {
2539 // set with one element
2540 set_with_one_element:
2541 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002543 }
2544}
2545
Damiend99b0522013-12-21 18:17:45 +00002546void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002547 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002548}
2549
Damiend99b0522013-12-21 18:17:45 +00002550void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002551 // object who's index we want is on top of stack
2552 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002553 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002554}
2555
Damiend99b0522013-12-21 18:17:45 +00002556void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002557 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002558 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002559}
2560
Damiend99b0522013-12-21 18:17:45 +00002561void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2562 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2563 mp_parse_node_t pn = pns->nodes[0];
2564 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002565 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2567 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002568 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2569 pns = (mp_parse_node_struct_t*)pn;
2570 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002572 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002573 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002574 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002575 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002576 } else {
2577 // [?::x]
2578 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002580 }
Damiend99b0522013-12-21 18:17:45 +00002581 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002582 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002583 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2584 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2585 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2586 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002587 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002588 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002589 } else {
2590 // [?:x:x]
2591 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002592 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002593 }
2594 } else {
2595 // [?:x]
2596 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002597 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002598 }
2599 } else {
2600 // [?:x]
2601 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002602 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002603 }
2604}
2605
Damiend99b0522013-12-21 18:17:45 +00002606void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002607 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002608 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2609 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002610}
2611
Damiend99b0522013-12-21 18:17:45 +00002612void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002613 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002614 compile_subscript_3_helper(comp, pns);
2615}
2616
Damiend99b0522013-12-21 18:17:45 +00002617void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002618 // if this is called then we are compiling a dict key:value pair
2619 compile_node(comp, pns->nodes[1]); // value
2620 compile_node(comp, pns->nodes[0]); // key
2621}
2622
Damiend99b0522013-12-21 18:17:45 +00002623void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002624 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002625 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002626 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002627}
2628
Damiend99b0522013-12-21 18:17:45 +00002629void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002630 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002631 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002632 return;
2633 }
Damien George922ddd62014-04-09 12:43:17 +01002634 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002635 compile_node(comp, pns->nodes[0]);
2636}
2637
Damiend99b0522013-12-21 18:17:45 +00002638void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002639 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002640 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002641 return;
2642 }
Damien George922ddd62014-04-09 12:43:17 +01002643 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002644 compile_node(comp, pns->nodes[0]);
2645}
2646
Damiend99b0522013-12-21 18:17:45 +00002647void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2648 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2649 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2650 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2651 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002652 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 +01002653 return;
2654 }
Damien Georgeb9791222014-01-23 00:34:21 +00002655 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002656 compile_node(comp, pns2->nodes[0]);
2657 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002658 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002659 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2660 } else {
2661 // shouldn't happen
2662 assert(0);
2663 }
2664}
2665
Damiend99b0522013-12-21 18:17:45 +00002666void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002667 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002668 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002669 return;
2670 }
Damiend99b0522013-12-21 18:17:45 +00002671 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002672 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002673 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002674 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2675 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002676 compile_node(comp, pns->nodes[0]);
2677 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002678 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002679 EMIT(yield_from);
2680 } else {
2681 compile_node(comp, pns->nodes[0]);
2682 EMIT(yield_value);
2683 }
2684}
2685
Damiend99b0522013-12-21 18:17:45 +00002686typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002687STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002688 NULL,
2689#define nc NULL
2690#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002691#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002692#include "grammar.h"
2693#undef nc
2694#undef c
2695#undef DEF_RULE
2696};
2697
Damiend99b0522013-12-21 18:17:45 +00002698void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2699 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002700 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002701 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2702 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2703 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002704 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002705 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002706 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002707 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002708 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2709 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2710 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2711 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002712 case MP_PARSE_NODE_TOKEN:
2713 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002714 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002715 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002716 // do nothing
2717 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002719 }
2720 break;
Damien429d7192013-10-04 19:53:11 +01002721 default: assert(0);
2722 }
2723 } else {
Damiend99b0522013-12-21 18:17:45 +00002724 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002725 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002726 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002727 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002728 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002729#if MICROPY_DEBUG_PRINTERS
2730 mp_parse_node_print(pn, 0);
2731#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002732 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002733 } else {
2734 f(comp, pns);
2735 }
2736 }
2737}
2738
Damiend99b0522013-12-21 18:17:45 +00002739void 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 +01002740 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002741 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002742 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2743 if (MP_PARSE_NODE_IS_ID(pn)) {
2744 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002745 if (comp->have_bare_star) {
2746 // comes after a bare star, so doesn't count as a parameter
2747 } else {
2748 comp->scope_cur->num_params += 1;
2749 }
Damienb14de212013-10-06 00:28:28 +01002750 } else {
Damiend99b0522013-12-21 18:17:45 +00002751 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2752 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2753 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2754 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002755 //int node_index = 1; unused
2756 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002757 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002758 // this parameter has an annotation
2759 pn_annotation = pns->nodes[1];
2760 }
2761 //node_index = 2; unused
2762 }
2763 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002764 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002765 // this parameter has a default value
2766 if (comp->have_bare_star) {
2767 comp->scope_cur->num_dict_params += 1;
2768 } else {
2769 comp->scope_cur->num_default_params += 1;
2770 }
2771 }
2772 */
2773 if (comp->have_bare_star) {
2774 // comes after a bare star, so doesn't count as a parameter
2775 } else {
2776 comp->scope_cur->num_params += 1;
2777 }
Damiend99b0522013-12-21 18:17:45 +00002778 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2779 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002780 // bare star
2781 // TODO see http://www.python.org/dev/peps/pep-3102/
2782 comp->have_bare_star = true;
2783 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002784 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002785 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002786 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002787 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2788 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002789 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002790 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002791 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2792 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002793 pn_annotation = pns->nodes[1];
2794 } else {
2795 // shouldn't happen
2796 assert(0);
2797 }
Damiend99b0522013-12-21 18:17:45 +00002798 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2799 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2800 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002801 // this parameter has an annotation
2802 pn_annotation = pns->nodes[1];
2803 }
Damien George8725f8f2014-02-15 19:33:11 +00002804 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002805 } else {
Damienb14de212013-10-06 00:28:28 +01002806 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002807 assert(0);
2808 }
Damien429d7192013-10-04 19:53:11 +01002809 }
2810
2811 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002812 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002813 // TODO this parameter has an annotation
2814 }
2815 bool added;
2816 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2817 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002818 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002819 return;
2820 }
Damien429d7192013-10-04 19:53:11 +01002821 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002822 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002823 }
2824}
2825
Damiend99b0522013-12-21 18:17:45 +00002826void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002827 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2828}
2829
Damiend99b0522013-12-21 18:17:45 +00002830void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002831 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2832}
2833
Damiend99b0522013-12-21 18:17:45 +00002834void 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 +01002835 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002836 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002837 // no more nested if/for; compile inner expression
2838 compile_node(comp, pn_inner_expr);
2839 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002840 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002841 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002843 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002844 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002845 } else {
2846 EMIT(yield_value);
2847 EMIT(pop_top);
2848 }
Damiend99b0522013-12-21 18:17:45 +00002849 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002850 // if condition
Damiend99b0522013-12-21 18:17:45 +00002851 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002852 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2853 pn_iter = pns_comp_if->nodes[1];
2854 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002855 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002856 // for loop
Damiend99b0522013-12-21 18:17:45 +00002857 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002858 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002859 uint l_end2 = comp_next_label(comp);
2860 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002861 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002862 EMIT_ARG(label_assign, l_top2);
2863 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002864 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2865 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 +00002866 EMIT_ARG(jump, l_top2);
2867 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002868 EMIT(for_iter_end);
2869 } else {
2870 // shouldn't happen
2871 assert(0);
2872 }
2873}
2874
Damiend99b0522013-12-21 18:17:45 +00002875void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002876 // see http://www.python.org/dev/peps/pep-0257/
2877
2878 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002879 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002880 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002881 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002882 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002883 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2884 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002885 for (int i = 0; i < num_nodes; i++) {
2886 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002887 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 +00002888 // not a newline, so this is the first statement; finish search
2889 break;
2890 }
2891 }
2892 // 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 +00002893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002894 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002895 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002896 } else {
2897 return;
2898 }
2899
2900 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002901 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2902 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2903 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2904 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2905 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002906 compile_node(comp, pns->nodes[0]); // a doc string
2907 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002908 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002909 }
2910 }
2911 }
2912}
2913
2914void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2915 comp->pass = pass;
2916 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002917 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002918 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002919
2920 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002921 // reset maximum stack sizes in scope
2922 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002923 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002924 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002925 }
2926
Damien5ac1b2e2013-10-18 19:58:12 +01002927#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002928 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002929 scope_print_info(scope);
2930 }
Damien5ac1b2e2013-10-18 19:58:12 +01002931#endif
Damien429d7192013-10-04 19:53:11 +01002932
2933 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002934 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2935 assert(scope->kind == SCOPE_MODULE);
2936 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2937 compile_node(comp, pns->nodes[0]); // compile the expression
2938 EMIT(return_value);
2939 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002940 if (!comp->is_repl) {
2941 check_for_doc_string(comp, scope->pn);
2942 }
Damien429d7192013-10-04 19:53:11 +01002943 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002945 EMIT(return_value);
2946 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002947 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2948 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2949 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002950
2951 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002952 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002953 if (comp->pass == PASS_1) {
2954 comp->have_bare_star = false;
2955 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2956 }
2957
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002958 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002959
2960 compile_node(comp, pns->nodes[3]); // 3 is function body
2961 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002962 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002963 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002964 EMIT(return_value);
2965 }
2966 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002967 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2968 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2969 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002970
2971 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002972 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002973 if (comp->pass == PASS_1) {
2974 comp->have_bare_star = false;
2975 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2976 }
2977
2978 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00002979
2980 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
2981 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
2982 EMIT(pop_top);
2983 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2984 }
Damien429d7192013-10-04 19:53:11 +01002985 EMIT(return_value);
2986 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2987 // a bit of a hack at the moment
2988
Damiend99b0522013-12-21 18:17:45 +00002989 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2990 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2991 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2992 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2993 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002994
Damien George55baff42014-01-21 21:40:13 +00002995 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002996 if (comp->pass == PASS_1) {
2997 bool added;
2998 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2999 assert(added);
3000 id_info->kind = ID_INFO_KIND_LOCAL;
3001 scope->num_params = 1;
3002 }
3003
3004 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003005 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003006 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003007 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003008 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003009 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003010 }
3011
Damien George6f355fd2014-04-10 14:11:31 +01003012 uint l_end = comp_next_label(comp);
3013 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003014 EMIT_ARG(load_id, qstr_arg);
3015 EMIT_ARG(label_assign, l_top);
3016 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003017 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3018 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003019 EMIT_ARG(jump, l_top);
3020 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003021 EMIT(for_iter_end);
3022
3023 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003024 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003025 }
3026 EMIT(return_value);
3027 } else {
3028 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003029 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3030 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3031 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003032
3033 if (comp->pass == PASS_1) {
3034 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003035 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003036 assert(added);
3037 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003038 }
3039
Damien Georgeb9791222014-01-23 00:34:21 +00003040 EMIT_ARG(load_id, MP_QSTR___name__);
3041 EMIT_ARG(store_id, MP_QSTR___module__);
3042 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3043 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003044
3045 check_for_doc_string(comp, pns->nodes[2]);
3046 compile_node(comp, pns->nodes[2]); // 2 is class body
3047
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003048 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003049 assert(id != NULL);
3050 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003051 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003052 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003053#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003054 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003055#else
Damien George2bf7c092014-04-09 15:26:46 +01003056 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003057#endif
Damien429d7192013-10-04 19:53:11 +01003058 }
3059 EMIT(return_value);
3060 }
3061
Damien415eb6f2013-10-05 12:19:06 +01003062 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003063
3064 // make sure we match all the exception levels
3065 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003066}
3067
Damien George094d4502014-04-02 17:31:27 +01003068#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003069void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3070 comp->pass = pass;
3071 comp->scope_cur = scope;
3072 comp->next_label = 1;
3073
3074 if (scope->kind != SCOPE_FUNCTION) {
3075 printf("Error: inline assembler must be a function\n");
3076 return;
3077 }
3078
Damiena2f2f7d2013-10-06 00:14:13 +01003079 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003080 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003081 }
3082
Damien826005c2013-10-05 23:17:28 +01003083 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003084 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3085 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3086 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003087
Damiend99b0522013-12-21 18:17:45 +00003088 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003089
Damiena2f2f7d2013-10-06 00:14:13 +01003090 // parameters are in pns->nodes[1]
3091 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003092 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003093 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003094 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003095 }
3096
Damiend99b0522013-12-21 18:17:45 +00003097 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003098
Damiend99b0522013-12-21 18:17:45 +00003099 mp_parse_node_t pn_body = pns->nodes[3]; // body
3100 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003101 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3102
Damien Georgecbd2f742014-01-19 11:48:48 +00003103 /*
Damien826005c2013-10-05 23:17:28 +01003104 if (comp->pass == PASS_3) {
3105 //printf("----\n");
3106 scope_print_info(scope);
3107 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003108 */
Damien826005c2013-10-05 23:17:28 +01003109
3110 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003111 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3112 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3113 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3114 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3115 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3116 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3117 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3118 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3119 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3120 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3121 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3122 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3123 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003124 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3125
3126 // emit instructions
3127 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003128 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003129 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003130 return;
3131 }
Damien George6f355fd2014-04-10 14:11:31 +01003132 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003133 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003134 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003135 }
3136 } else {
3137 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003138 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003139 }
3140 }
3141 }
3142
3143 if (comp->pass > PASS_1) {
3144 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003145 }
Damien429d7192013-10-04 19:53:11 +01003146}
Damien George094d4502014-04-02 17:31:27 +01003147#endif
Damien429d7192013-10-04 19:53:11 +01003148
3149void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3150 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003151 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003152 scope->num_locals = 0;
3153 for (int i = 0; i < scope->id_info_len; i++) {
3154 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003155 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003156 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3157 continue;
3158 }
3159 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3160 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3161 }
Damien9ecbcff2013-12-11 00:41:43 +00003162 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003163 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003164 id->local_num = scope->num_locals;
3165 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003166 }
3167 }
3168
3169 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003170#if MICROPY_EMIT_CPYTHON
3171 int num_cell = 0;
3172#endif
Damien9ecbcff2013-12-11 00:41:43 +00003173 for (int i = 0; i < scope->id_info_len; i++) {
3174 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003175#if MICROPY_EMIT_CPYTHON
3176 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003177 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003178 id->local_num = num_cell;
3179 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003180 }
Damien George6baf76e2013-12-30 22:32:17 +00003181#else
3182 // in Micro Python the cells come right after the fast locals
3183 // parameters are not counted here, since they remain at the start
3184 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003185 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003186 id->local_num = scope->num_locals;
3187 scope->num_locals += 1;
3188 }
3189#endif
Damien9ecbcff2013-12-11 00:41:43 +00003190 }
Damien9ecbcff2013-12-11 00:41:43 +00003191
3192 // compute the index of free vars (freevars[idx] in CPython)
3193 // make sure they are in the order of the parent scope
3194 if (scope->parent != NULL) {
3195 int num_free = 0;
3196 for (int i = 0; i < scope->parent->id_info_len; i++) {
3197 id_info_t *id = &scope->parent->id_info[i];
3198 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3199 for (int j = 0; j < scope->id_info_len; j++) {
3200 id_info_t *id2 = &scope->id_info[j];
3201 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003202 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003203#if MICROPY_EMIT_CPYTHON
3204 // in CPython the frees are numbered after the cells
3205 id2->local_num = num_cell + num_free;
3206#else
3207 // in Micro Python the frees come first, before the params
3208 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003209#endif
3210 num_free += 1;
3211 }
3212 }
3213 }
Damien429d7192013-10-04 19:53:11 +01003214 }
Damien George6baf76e2013-12-30 22:32:17 +00003215#if !MICROPY_EMIT_CPYTHON
3216 // in Micro Python shift all other locals after the free locals
3217 if (num_free > 0) {
3218 for (int i = 0; i < scope->id_info_len; i++) {
3219 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003220 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003221 id->local_num += num_free;
3222 }
3223 }
3224 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3225 scope->num_locals += num_free;
3226 }
3227#endif
Damien429d7192013-10-04 19:53:11 +01003228 }
3229
Damien George8725f8f2014-02-15 19:33:11 +00003230 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003231
3232#if MICROPY_EMIT_CPYTHON
3233 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003234 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) {
3235 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003236 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003237 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003238 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 +00003239 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003240 }
3241 }
Damien George882b3632014-04-02 15:56:31 +01003242#endif
3243
Damien429d7192013-10-04 19:53:11 +01003244 int num_free = 0;
3245 for (int i = 0; i < scope->id_info_len; i++) {
3246 id_info_t *id = &scope->id_info[i];
3247 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3248 num_free += 1;
3249 }
3250 }
3251 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003252 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003253 }
3254}
3255
Damien George65cad122014-04-06 11:48:15 +01003256mp_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 +01003257 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003258 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003259 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003260
Damien826005c2013-10-05 23:17:28 +01003261 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003262 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003263
3264 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003265 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003266
Damien826005c2013-10-05 23:17:28 +01003267 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003268 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003269 comp->emit_method_table = &emit_pass1_method_table;
3270 comp->emit_inline_asm = NULL;
3271 comp->emit_inline_asm_method_table = NULL;
3272 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003273 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003274 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003275#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003276 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003277 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003278#endif
Damien826005c2013-10-05 23:17:28 +01003279 } else {
3280 compile_scope(comp, s, PASS_1);
3281 }
3282
3283 // update maximim number of labels needed
3284 if (comp->next_label > max_num_labels) {
3285 max_num_labels = comp->next_label;
3286 }
Damien429d7192013-10-04 19:53:11 +01003287 }
3288
Damien826005c2013-10-05 23:17:28 +01003289 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003290 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003291 compile_scope_compute_things(comp, s);
3292 }
3293
Damien826005c2013-10-05 23:17:28 +01003294 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003295 emit_pass1_free(comp->emit);
3296
Damien826005c2013-10-05 23:17:28 +01003297 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003298#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003299 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003300#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003301 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003302#endif
Damien3ef4abb2013-10-12 16:53:13 +01003303#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003304 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003305#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003306#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003307 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003308 if (false) {
3309 // dummy
3310
Damien3ef4abb2013-10-12 16:53:13 +01003311#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003312 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003313 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003314 if (emit_inline_thumb == NULL) {
3315 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3316 }
3317 comp->emit = NULL;
3318 comp->emit_method_table = NULL;
3319 comp->emit_inline_asm = emit_inline_thumb;
3320 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3321 compile_scope_inline_asm(comp, s, PASS_2);
3322 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003323#endif
3324
Damien826005c2013-10-05 23:17:28 +01003325 } else {
Damienc025ebb2013-10-12 14:30:21 +01003326
3327 // choose the emit type
3328
Damien3ef4abb2013-10-12 16:53:13 +01003329#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003330 comp->emit = emit_cpython_new(max_num_labels);
3331 comp->emit_method_table = &emit_cpython_method_table;
3332#else
Damien826005c2013-10-05 23:17:28 +01003333 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003334
3335#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003336 case MP_EMIT_OPT_NATIVE_PYTHON:
3337 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003338#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003339 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003340 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003341 }
Damien13ed3a62013-10-08 09:05:10 +01003342 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003343#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003344 if (emit_native == NULL) {
3345 emit_native = emit_native_thumb_new(max_num_labels);
3346 }
3347 comp->emit_method_table = &emit_native_thumb_method_table;
3348#endif
3349 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003350 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003351 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003352#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003353
Damien826005c2013-10-05 23:17:28 +01003354 default:
3355 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003356 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003357 }
3358 comp->emit = emit_bc;
3359 comp->emit_method_table = &emit_bc_method_table;
3360 break;
3361 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003362#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003363
3364 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003365 compile_scope(comp, s, PASS_2);
3366 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003367 }
Damien429d7192013-10-04 19:53:11 +01003368 }
3369
Damien George41d02b62014-01-24 22:42:28 +00003370 // free the emitters
3371#if !MICROPY_EMIT_CPYTHON
3372 if (emit_bc != NULL) {
3373 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003374 }
Damien George41d02b62014-01-24 22:42:28 +00003375#if MICROPY_EMIT_NATIVE
3376 if (emit_native != NULL) {
3377#if MICROPY_EMIT_X64
3378 emit_native_x64_free(emit_native);
3379#elif MICROPY_EMIT_THUMB
3380 emit_native_thumb_free(emit_native);
3381#endif
3382 }
3383#endif
3384#if MICROPY_EMIT_INLINE_THUMB
3385 if (emit_inline_thumb != NULL) {
3386 emit_inline_thumb_free(emit_inline_thumb);
3387 }
3388#endif
3389#endif // !MICROPY_EMIT_CPYTHON
3390
3391 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003392 uint unique_code_id = module_scope->unique_code_id;
3393 for (scope_t *s = module_scope; s;) {
3394 scope_t *next = s->next;
3395 scope_free(s);
3396 s = next;
3397 }
Damien5ac1b2e2013-10-18 19:58:12 +01003398
Damien George41d02b62014-01-24 22:42:28 +00003399 // free the compiler
3400 bool had_error = comp->had_error;
3401 m_del_obj(compiler_t, comp);
3402
Damien George1fb03172014-01-03 14:22:03 +00003403 if (had_error) {
3404 // TODO return a proper error message
3405 return mp_const_none;
3406 } else {
3407#if MICROPY_EMIT_CPYTHON
3408 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003409 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003410 return mp_const_true;
3411#else
3412 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003413 // 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 +01003414 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003415#endif
3416 }
Damien429d7192013-10-04 19:53:11 +01003417}