blob: 95fe1d759fd2d06f1616c672577297b179c6e222 [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;
Damien5ac1b2e2013-10-18 19:58:12 +010041 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010042 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010043 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010044
Damienb05d7072013-10-05 13:37:10 +010045 int next_label;
Damienb05d7072013-10-05 13:37:10 +010046
Damien429d7192013-10-04 19:53:11 +010047 int break_label;
48 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000049 int break_continue_except_level;
50 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010051
52 int n_arg_keyword;
53 bool have_star_arg;
54 bool have_dbl_star_arg;
55 bool have_bare_star;
56 int param_pass;
57 int param_pass_num_dict_params;
58 int param_pass_num_default_params;
59
Damien George35e2a4e2014-02-05 00:51:47 +000060 bool func_arg_is_super; // used to compile special case of super() function call
61
Damien429d7192013-10-04 19:53:11 +010062 scope_t *scope_head;
63 scope_t *scope_cur;
64
Damien6cdd3af2013-10-05 18:08:26 +010065 emit_t *emit; // current emitter
66 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010067
68 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
69 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 +010070} compiler_t;
71
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010072STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000073 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010074 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
75 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
76 } else {
77 printf(" File \"%s\"\n", qstr_str(comp->source_file));
78 }
Damien Georgef41fdd02014-03-03 23:19:11 +000079 printf("SyntaxError: %s\n", msg);
80 comp->had_error = true;
81}
82
Damiend99b0522013-12-21 18:17:45 +000083mp_parse_node_t fold_constants(mp_parse_node_t pn) {
84 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
85 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
86 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010087
88 // fold arguments first
89 for (int i = 0; i < n; i++) {
90 pns->nodes[i] = fold_constants(pns->nodes[i]);
91 }
92
Damiend99b0522013-12-21 18:17:45 +000093 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010094 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000095 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 +020096 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
97 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000098 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010099#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100100 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000101 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100102#endif
Damiend99b0522013-12-21 18:17:45 +0000103 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
104 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100105 } else {
106 // shouldn't happen
107 assert(0);
108 }
109 }
110 break;
111
112 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100113 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000114 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 +0200115 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
116 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000117 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000118 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000119 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000120 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100121 } else {
122 // shouldn't happen
123 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100124 }
Damien Georgeaf272592014-04-04 11:21:58 +0000125 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
126 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100127 }
128 }
129 break;
130
131 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000132 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgeaf272592014-04-04 11:21:58 +0000133 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
134 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000136 if (!mp_small_int_mul_overflow(arg0, arg1)) {
137 arg0 *= arg1;
138 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
139 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
140 }
141 }
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100143 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000144 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000145 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000146 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300147 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000148 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 +0300149 }
Damien429d7192013-10-04 19:53:11 +0100150 } else {
151 // shouldn't happen
152 assert(0);
153 }
154 }
155 break;
156
157 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000158 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200159 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000160 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
161 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
162 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
163 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
164 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
165 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100166 } else {
167 // shouldn't happen
168 assert(0);
169 }
170 }
171 break;
172
Damien3ef4abb2013-10-12 16:53:13 +0100173#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100174 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100175 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000176 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])) {
177 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
178 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200179 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100180 if (power >= 0) {
181 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200182 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100183 for (; power > 0; power--) {
184 ans *= base;
185 }
Damiend99b0522013-12-21 18:17:45 +0000186 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100187 }
188 }
189 }
190 break;
Damien0efb3a12013-10-12 16:16:56 +0100191#endif
Damien429d7192013-10-04 19:53:11 +0100192 }
193 }
194
195 return pn;
196}
197
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200198STATIC 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 +0000199STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100200
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200201STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100202 return comp->next_label++;
203}
204
Damien George8dcc0c72014-03-27 10:55:21 +0000205STATIC void compile_increase_except_level(compiler_t *comp) {
206 comp->cur_except_level += 1;
207 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
208 comp->scope_cur->exc_stack_size = comp->cur_except_level;
209 }
210}
211
212STATIC void compile_decrease_except_level(compiler_t *comp) {
213 assert(comp->cur_except_level > 0);
214 comp->cur_except_level -= 1;
215}
216
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200217STATIC 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 +0000218 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 +0100219 scope->parent = comp->scope_cur;
220 scope->next = NULL;
221 if (comp->scope_head == NULL) {
222 comp->scope_head = scope;
223 } else {
224 scope_t *s = comp->scope_head;
225 while (s->next != NULL) {
226 s = s->next;
227 }
228 s->next = scope;
229 }
230 return scope;
231}
232
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200233STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000234 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100235 return 0;
Damiend99b0522013-12-21 18:17:45 +0000236 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100237 return 1;
238 } else {
Damiend99b0522013-12-21 18:17:45 +0000239 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
240 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100241 return 1;
242 } else {
Damiend99b0522013-12-21 18:17:45 +0000243 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100244 }
245 }
246}
247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200248STATIC 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 +0000249 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
250 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
251 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100252 for (int i = 0; i < num_nodes; i++) {
253 f(comp, pns->nodes[i]);
254 }
Damiend99b0522013-12-21 18:17:45 +0000255 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100256 f(comp, pn);
257 }
258}
259
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200260STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000261 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100262 *nodes = NULL;
263 return 0;
Damiend99b0522013-12-21 18:17:45 +0000264 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100265 *nodes = pn;
266 return 1;
267 } else {
Damiend99b0522013-12-21 18:17:45 +0000268 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
269 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100270 *nodes = pn;
271 return 1;
272 } else {
273 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000274 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100275 }
276 }
277}
278
Damiend99b0522013-12-21 18:17:45 +0000279void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100280}
281
Damiend99b0522013-12-21 18:17:45 +0000282void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
283 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100284 for (int i = 0; i < num_nodes; i++) {
285 compile_node(comp, pns->nodes[i]);
286 }
287}
288
Damien3ef4abb2013-10-12 16:53:13 +0100289#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000291 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100292 return false;
293 }
Damiend99b0522013-12-21 18:17:45 +0000294 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100295 return false;
296 }
297 return true;
298}
299
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200300STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000301 uint len;
302 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000303 bool has_single_quote = false;
304 bool has_double_quote = false;
305 for (int i = 0; i < len; i++) {
306 if (str[i] == '\'') {
307 has_single_quote = true;
308 } else if (str[i] == '"') {
309 has_double_quote = true;
310 }
311 }
312 if (bytes) {
313 vstr_printf(vstr, "b");
314 }
315 bool quote_single = false;
316 if (has_single_quote && !has_double_quote) {
317 vstr_printf(vstr, "\"");
318 } else {
319 quote_single = true;
320 vstr_printf(vstr, "'");
321 }
322 for (int i = 0; i < len; i++) {
323 if (str[i] == '\n') {
324 vstr_printf(vstr, "\\n");
325 } else if (str[i] == '\\') {
326 vstr_printf(vstr, "\\\\");
327 } else if (str[i] == '\'' && quote_single) {
328 vstr_printf(vstr, "\\'");
329 } else {
330 vstr_printf(vstr, "%c", str[i]);
331 }
332 }
333 if (has_single_quote && !has_double_quote) {
334 vstr_printf(vstr, "\"");
335 } else {
336 vstr_printf(vstr, "'");
337 }
338}
339
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200340STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000341 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200342 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
343 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
344 return;
345 }
346
Damiend99b0522013-12-21 18:17:45 +0000347 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
348 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
349 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000350 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
351 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
352 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
353 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
354 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100355 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000356 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
357 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
358 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100359 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100360 }
361 break;
362 default: assert(0);
363 }
364}
365
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200366STATIC 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 +0100367 int n = 0;
368 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000369 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100370 }
371 int total = n;
372 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000373 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100374 total += 1;
Damien3a205172013-10-12 15:01:56 +0100375 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100376 is_const = false;
377 }
378 }
379 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100380 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100381 is_const = false;
382 break;
383 }
384 }
385 if (total > 0 && is_const) {
386 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000387 vstr_t *vstr = vstr_new();
388 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000389 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000390 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100391 need_comma = true;
392 }
393 for (int i = 0; i < n; i++) {
394 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000395 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100396 }
Damien02f89412013-12-12 15:13:36 +0000397 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100398 need_comma = true;
399 }
400 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000401 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100402 } else {
Damien02f89412013-12-12 15:13:36 +0000403 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100404 }
Damien Georgeb9791222014-01-23 00:34:21 +0000405 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000406 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100407 } else {
Damiend99b0522013-12-21 18:17:45 +0000408 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100409 compile_node(comp, pn);
410 }
411 for (int i = 0; i < n; i++) {
412 compile_node(comp, pns_list->nodes[i]);
413 }
Damien Georgeb9791222014-01-23 00:34:21 +0000414 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100415 }
416}
Damien3a205172013-10-12 15:01:56 +0100417#endif
418
419// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000420void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100421#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100422 cpython_c_tuple(comp, pn, pns_list);
423#else
424 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000425 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100426 compile_node(comp, pn);
427 total += 1;
428 }
429 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000430 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100431 for (int i = 0; i < n; i++) {
432 compile_node(comp, pns_list->nodes[i]);
433 }
434 total += n;
435 }
Damien Georgeb9791222014-01-23 00:34:21 +0000436 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100437#endif
438}
Damien429d7192013-10-04 19:53:11 +0100439
Damiend99b0522013-12-21 18:17:45 +0000440void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100441 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000442 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100443}
444
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200445STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000446 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200447 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100448}
449
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200450STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200451 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 +0100452}
453
Damien3ef4abb2013-10-12 16:53:13 +0100454#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100455// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200456STATIC 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 +0100457 if (node_is_const_false(pn)) {
458 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000459 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100460 }
461 return;
462 } else if (node_is_const_true(pn)) {
463 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000464 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100465 }
466 return;
Damiend99b0522013-12-21 18:17:45 +0000467 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
468 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
469 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
470 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100471 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100472 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100473 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100474 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100475 }
Damien3a205172013-10-12 15:01:56 +0100476 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000477 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100478 } else {
479 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100480 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100481 }
482 }
483 return;
Damiend99b0522013-12-21 18:17:45 +0000484 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100485 if (jump_if == false) {
486 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100487 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100488 }
489 } else {
Damienb05d7072013-10-05 13:37:10 +0100490 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100491 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100492 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100493 }
Damien3a205172013-10-12 15:01:56 +0100494 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000495 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100496 }
497 return;
Damiend99b0522013-12-21 18:17:45 +0000498 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100499 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100500 return;
501 }
502 }
503
504 // nothing special, fall back to default compiling for node and jump
505 compile_node(comp, pn);
506 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000507 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100508 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000509 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100510 }
511}
Damien3a205172013-10-12 15:01:56 +0100512#endif
Damien429d7192013-10-04 19:53:11 +0100513
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200514STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100515#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100516 cpython_c_if_cond(comp, pn, jump_if, label, false);
517#else
518 if (node_is_const_false(pn)) {
519 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000520 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100521 }
522 return;
523 } else if (node_is_const_true(pn)) {
524 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000525 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100526 }
527 return;
Damiend99b0522013-12-21 18:17:45 +0000528 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
529 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
530 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
531 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100532 if (jump_if == false) {
533 int label2 = comp_next_label(comp);
534 for (int i = 0; i < n - 1; i++) {
535 c_if_cond(comp, pns->nodes[i], true, label2);
536 }
537 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000538 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100539 } else {
540 for (int i = 0; i < n; i++) {
541 c_if_cond(comp, pns->nodes[i], true, label);
542 }
543 }
544 return;
Damiend99b0522013-12-21 18:17:45 +0000545 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100546 if (jump_if == false) {
547 for (int i = 0; i < n; i++) {
548 c_if_cond(comp, pns->nodes[i], false, label);
549 }
550 } else {
551 int label2 = comp_next_label(comp);
552 for (int i = 0; i < n - 1; i++) {
553 c_if_cond(comp, pns->nodes[i], false, label2);
554 }
555 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000556 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100557 }
558 return;
Damiend99b0522013-12-21 18:17:45 +0000559 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100560 c_if_cond(comp, pns->nodes[0], !jump_if, label);
561 return;
562 }
563 }
564
565 // nothing special, fall back to default compiling for node and jump
566 compile_node(comp, pn);
567 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100569 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000570 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100571 }
572#endif
Damien429d7192013-10-04 19:53:11 +0100573}
574
575typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000576void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100577
Damiend99b0522013-12-21 18:17:45 +0000578void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100579 if (assign_kind != ASSIGN_AUG_STORE) {
580 compile_node(comp, pns->nodes[0]);
581 }
582
Damiend99b0522013-12-21 18:17:45 +0000583 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
584 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
585 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
586 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100587 if (assign_kind != ASSIGN_AUG_STORE) {
588 for (int i = 0; i < n - 1; i++) {
589 compile_node(comp, pns1->nodes[i]);
590 }
591 }
Damiend99b0522013-12-21 18:17:45 +0000592 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
593 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100594 }
Damiend99b0522013-12-21 18:17:45 +0000595 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100596 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_three);
600 EMIT(store_subscr);
601 } else {
602 compile_node(comp, pns1->nodes[0]);
603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100605 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 EMIT(store_subscr);
608 }
609 }
Damiend99b0522013-12-21 18:17:45 +0000610 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
611 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100612 if (assign_kind == ASSIGN_AUG_LOAD) {
613 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 if (assign_kind == ASSIGN_AUG_STORE) {
617 EMIT(rot_two);
618 }
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100620 }
621 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100622 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100623 }
624 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100625 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100626 }
627
Damiend99b0522013-12-21 18:17:45 +0000628 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100629 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100630 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100631
632 return;
633
634cannot_assign:
635 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100636}
637
Damiend99b0522013-12-21 18:17:45 +0000638void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100639 assert(n >= 0);
640 int have_star_index = -1;
641 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000642 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100643 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100645 have_star_index = i;
646 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100647 compile_syntax_error(comp, nodes[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100648 return;
649 }
650 }
651 }
652 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100654 }
655 for (int i = 0; i < n; i++) {
656 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000657 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100658 } else {
659 c_assign(comp, nodes[i], ASSIGN_STORE);
660 }
661 }
662}
663
664// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000665void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100666 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000667 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100668 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000669 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
670 if (MP_PARSE_NODE_IS_ID(pn)) {
671 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100672 switch (assign_kind) {
673 case ASSIGN_STORE:
674 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100676 break;
677 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000678 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100679 break;
680 }
681 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100682 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100683 return;
684 }
685 } else {
Damiend99b0522013-12-21 18:17:45 +0000686 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
687 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100688 case PN_power:
689 // lhs is an index or attribute
690 c_assign_power(comp, pns, assign_kind);
691 break;
692
693 case PN_testlist_star_expr:
694 case PN_exprlist:
695 // lhs is a tuple
696 if (assign_kind != ASSIGN_STORE) {
697 goto bad_aug;
698 }
Damiend99b0522013-12-21 18:17:45 +0000699 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100700 break;
701
702 case PN_atom_paren:
703 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000704 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100705 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100706 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
709 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100710 goto testlist_comp;
711 } else {
712 // parenthesis around 1 item, is just that item
713 pn = pns->nodes[0];
714 goto tail_recursion;
715 }
716 break;
717
718 case PN_atom_bracket:
719 // lhs is something in brackets
720 if (assign_kind != ASSIGN_STORE) {
721 goto bad_aug;
722 }
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100724 // empty list, assignment allowed
725 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000726 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
727 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100728 goto testlist_comp;
729 } else {
730 // brackets around 1 item
731 c_assign_tuple(comp, 1, &pns->nodes[0]);
732 }
733 break;
734
735 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100736 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
737 return;
Damien429d7192013-10-04 19:53:11 +0100738 }
739 return;
740
741 testlist_comp:
742 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000743 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
744 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
745 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100746 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000747 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100748 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100750 // sequence of many items
751 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000752 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000753 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100754 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
755 for (int i = 0; i < n; i++) {
756 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
757 }
Damiend99b0522013-12-21 18:17:45 +0000758 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100759 // TODO can we ever get here? can it be compiled?
760 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
761 return;
Damien429d7192013-10-04 19:53:11 +0100762 } else {
763 // sequence with 2 items
764 goto sequence_with_2_items;
765 }
766 } else {
767 // sequence with 2 items
768 sequence_with_2_items:
769 c_assign_tuple(comp, 2, pns->nodes);
770 }
771 return;
772 }
773 return;
774
775 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100776 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100777}
778
779// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100780// if we are not in CPython compatibility mode then:
781// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
782// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
783// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100784void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
785 assert(n_pos_defaults >= 0);
786 assert(n_kw_defaults >= 0);
787
Damien429d7192013-10-04 19:53:11 +0100788 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000789 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100790 int nfree = 0;
791 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000792 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
793 id_info_t *id = &comp->scope_cur->id_info[i];
794 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
795 for (int j = 0; j < this_scope->id_info_len; j++) {
796 id_info_t *id2 = &this_scope->id_info[j];
797 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000798#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#else
801 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000802 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000803#endif
Damien318aec62013-12-10 18:28:17 +0000804 nfree += 1;
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807 }
808 }
809 }
Damien429d7192013-10-04 19:53:11 +0100810
811 // make the function/closure
812 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100813 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100814 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000815 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100816 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100817 }
818}
819
Damiend99b0522013-12-21 18:17:45 +0000820void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000821 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000822 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
823 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100824 // bare star
825 comp->have_bare_star = true;
826 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000827
828 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
829 // TODO do we need to do anything with this?
830
831 } else {
832 mp_parse_node_t pn_id;
833 mp_parse_node_t pn_colon;
834 mp_parse_node_t pn_equal;
835 if (MP_PARSE_NODE_IS_ID(pn)) {
836 // this parameter is just an id
837
838 pn_id = pn;
839 pn_colon = MP_PARSE_NODE_NULL;
840 pn_equal = MP_PARSE_NODE_NULL;
841
842 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
843 // this parameter has a colon and/or equal specifier
844
845 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
846 pn_id = pns->nodes[0];
847 pn_colon = pns->nodes[1];
848 pn_equal = pns->nodes[2];
849
850 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100851 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000852 assert(0);
853 return;
854 }
855
856 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
857 // this parameter does not have a default value
858
859 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
860 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100861 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000862 return;
863 }
864
865 } else {
866 // this parameter has a default value
867 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
868
869 if (comp->have_bare_star) {
870 comp->param_pass_num_dict_params += 1;
871 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100872#if !MICROPY_EMIT_CPYTHON
873 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
874 if (comp->param_pass_num_dict_params == 1) {
875 // first default dict param, so make the map
876 EMIT_ARG(build_map, 0);
877 }
878#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000879 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
880 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100881#if !MICROPY_EMIT_CPYTHON
882 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
883 EMIT(store_map);
884#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000885 }
886 } else {
887 comp->param_pass_num_default_params += 1;
888 if (comp->param_pass == 2) {
889 compile_node(comp, pn_equal);
890 }
891 }
892 }
893
894 // TODO pn_colon not implemented
895 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100896 }
897}
898
899// leaves function object on stack
900// returns function name
Damiend99b0522013-12-21 18:17:45 +0000901qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100902 if (comp->pass == PASS_1) {
903 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000904 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100905 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000906 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100907 }
908
909 // save variables (probably don't need to do this, since we can't have nested definitions..?)
910 bool old_have_bare_star = comp->have_bare_star;
911 int old_param_pass = comp->param_pass;
912 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
913 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
914
915 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000916
917 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100918 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000919 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100920 comp->param_pass_num_dict_params = 0;
921 comp->param_pass_num_default_params = 0;
922 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000923
924 if (comp->had_error) {
925 return MP_QSTR_NULL;
926 }
927
928 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100929 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000930 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100931 comp->param_pass_num_dict_params = 0;
932 comp->param_pass_num_default_params = 0;
933 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
934
Damien Georgee337f1e2014-03-31 15:18:37 +0100935#if !MICROPY_EMIT_CPYTHON
936 // in Micro Python we put the default positional parameters into a tuple using the bytecode
937 if (comp->param_pass_num_default_params > 0) {
938 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
939 }
940#endif
941
Damien429d7192013-10-04 19:53:11 +0100942 // get the scope for this function
943 scope_t *fscope = (scope_t*)pns->nodes[4];
944
945 // make the function
Damien George30565092014-03-31 11:30:17 +0100946 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100947
948 // restore variables
949 comp->have_bare_star = old_have_bare_star;
950 comp->param_pass = old_param_pass;
951 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
952 comp->param_pass_num_default_params = old_param_pass_num_default_params;
953
954 // return its name (the 'f' in "def f(...):")
955 return fscope->simple_name;
956}
957
958// leaves class object on stack
959// returns class name
Damiend99b0522013-12-21 18:17:45 +0000960qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100961 if (comp->pass == PASS_1) {
962 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000963 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100964 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000965 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100966 }
967
968 EMIT(load_build_class);
969
970 // scope for this class
971 scope_t *cscope = (scope_t*)pns->nodes[3];
972
973 // compile the class
974 close_over_variables_etc(comp, cscope, 0, 0);
975
976 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000977 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100978
979 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100980 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
981 mp_parse_node_t parents = pns->nodes[1];
982 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
983 parents = MP_PARSE_NODE_NULL;
984 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000985 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100986 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100987
988 // return its name (the 'C' in class C(...):")
989 return cscope->simple_name;
990}
991
Damien6cdd3af2013-10-05 18:08:26 +0100992// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200993STATIC 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 +0000994 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100995 return false;
996 }
997
998 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100999 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001000 return true;
1001 }
1002
Damiend99b0522013-12-21 18:17:45 +00001003 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001004 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001005 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001006#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001007 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001008 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001009 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001010 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001011#endif
Damien3ef4abb2013-10-12 16:53:13 +01001012#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001013 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001014 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001015#endif
Damien6cdd3af2013-10-05 18:08:26 +01001016 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001017 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001018 }
1019
1020 return true;
1021}
1022
Damiend99b0522013-12-21 18:17:45 +00001023void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001024 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001025 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001026 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1027
Damien6cdd3af2013-10-05 18:08:26 +01001028 // inherit emit options for this function/class definition
1029 uint emit_options = comp->scope_cur->emit_options;
1030
1031 // compile each decorator
1032 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001033 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001034 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1035 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001036
1037 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001038 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001039 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1040
1041 // check for built-in decorators
1042 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1043 // this was a built-in
1044 num_built_in_decorators += 1;
1045
1046 } else {
1047 // not a built-in, compile normally
1048
1049 // compile the decorator function
1050 compile_node(comp, name_nodes[0]);
1051 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001052 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001053 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001054 }
1055
1056 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001057 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001058 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001059 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001060 compile_node(comp, pns_decorator->nodes[1]);
1061 }
Damien429d7192013-10-04 19:53:11 +01001062 }
1063 }
1064
1065 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001066 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001067 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001068 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001069 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001070 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001071 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001072 } else {
1073 // shouldn't happen
1074 assert(0);
1075 }
1076
1077 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001078 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001079 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001080 }
1081
1082 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001083 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001084}
1085
Damiend99b0522013-12-21 18:17:45 +00001086void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001087 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001088 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001089 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001090}
1091
Damiend99b0522013-12-21 18:17:45 +00001092void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1093 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001094 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001095 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1096 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001097
1098 compile_node(comp, pns->nodes[0]); // base of the power node
1099
Damiend99b0522013-12-21 18:17:45 +00001100 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1101 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1102 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1103 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001104 for (int i = 0; i < n - 1; i++) {
1105 compile_node(comp, pns1->nodes[i]);
1106 }
Damiend99b0522013-12-21 18:17:45 +00001107 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1108 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001109 }
Damiend99b0522013-12-21 18:17:45 +00001110 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001111 // can't delete function calls
1112 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001113 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001114 compile_node(comp, pns1->nodes[0]);
1115 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001116 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1117 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001118 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001119 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001120 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001121 }
1122 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001123 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001124 }
1125
Damiend99b0522013-12-21 18:17:45 +00001126 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001127 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001128 }
Damiend99b0522013-12-21 18:17:45 +00001129 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1130 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1131 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1132 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001133 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1134
Damiend99b0522013-12-21 18:17:45 +00001135 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1136 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1137 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001138 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001139 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001140 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001141 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001142 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001143 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001144 c_del_stmt(comp, pns->nodes[0]);
1145 for (int i = 0; i < n; i++) {
1146 c_del_stmt(comp, pns1->nodes[i]);
1147 }
Damiend99b0522013-12-21 18:17:45 +00001148 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001149 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001150 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001151 } else {
1152 // sequence with 2 items
1153 goto sequence_with_2_items;
1154 }
1155 } else {
1156 // sequence with 2 items
1157 sequence_with_2_items:
1158 c_del_stmt(comp, pns->nodes[0]);
1159 c_del_stmt(comp, pns->nodes[1]);
1160 }
1161 } else {
1162 // tuple with 1 element
1163 c_del_stmt(comp, pn);
1164 }
1165 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001166 // TODO is there anything else to implement?
1167 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001168 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001169
1170 return;
1171
1172cannot_delete:
1173 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001174}
1175
Damiend99b0522013-12-21 18:17:45 +00001176void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001177 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1178}
1179
Damiend99b0522013-12-21 18:17:45 +00001180void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001181 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001182 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001183 }
Damien Georgecbddb272014-02-01 20:08:18 +00001184 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001185}
1186
Damiend99b0522013-12-21 18:17:45 +00001187void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001188 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001189 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001190 }
Damien Georgecbddb272014-02-01 20:08:18 +00001191 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001192}
1193
Damiend99b0522013-12-21 18:17:45 +00001194void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001195 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001196 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001197 return;
1198 }
Damiend99b0522013-12-21 18:17:45 +00001199 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001200 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001201 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001202 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001203 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001204 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1205 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 +01001206
Damienb05d7072013-10-05 13:37:10 +01001207 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001208 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1209 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1210 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001211 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001212 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1213 } else {
1214 compile_node(comp, pns->nodes[0]);
1215 }
1216 EMIT(return_value);
1217}
1218
Damiend99b0522013-12-21 18:17:45 +00001219void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001220 compile_node(comp, pns->nodes[0]);
1221 EMIT(pop_top);
1222}
1223
Damiend99b0522013-12-21 18:17:45 +00001224void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1225 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001226 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001227 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001228 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001229 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001230 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001231 compile_node(comp, pns->nodes[0]);
1232 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001234 } else {
1235 // raise x
1236 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001237 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001238 }
1239}
1240
1241// q1 holds the base, q2 the full name
1242// eg a -> q1=q2=a
1243// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001244void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001245 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001246 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1247 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001248 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001249 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001250 pn = pns->nodes[0];
1251 is_as = true;
1252 }
Damiend99b0522013-12-21 18:17:45 +00001253 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001254 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001255 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001256 if (!is_as) {
1257 *q1 = *q2;
1258 }
Damien Georgeb9791222014-01-23 00:34:21 +00001259 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001260 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1261 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1262 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001263 // a name of the form a.b.c
1264 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001265 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001266 }
Damiend99b0522013-12-21 18:17:45 +00001267 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001268 int len = n - 1;
1269 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001270 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001271 }
Damien George55baff42014-01-21 21:40:13 +00001272 byte *q_ptr;
1273 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001274 for (int i = 0; i < n; i++) {
1275 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001276 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001277 }
Damien George55baff42014-01-21 21:40:13 +00001278 uint str_src_len;
1279 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001280 memcpy(str_dest, str_src, str_src_len);
1281 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001282 }
Damien George55baff42014-01-21 21:40:13 +00001283 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001284 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001285 if (is_as) {
1286 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001287 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001288 }
1289 }
1290 } else {
1291 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001292 // This covers relative imports starting with dot(s) like "from .foo import"
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001293 compile_syntax_error(comp, pn, "Relative imports not implemented");
1294 return;
Damien429d7192013-10-04 19:53:11 +01001295 }
1296 } else {
1297 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001298 // This covers relative imports with dots only like "from .. import"
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001299 compile_syntax_error(comp, pn, "Relative imports not implemented");
1300 return;
Damien429d7192013-10-04 19:53:11 +01001301 }
1302}
1303
Damiend99b0522013-12-21 18:17:45 +00001304void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001305 EMIT_ARG(load_const_small_int, 0); // ??
1306 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001307 qstr q1, q2;
1308 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001309 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001310}
1311
Damiend99b0522013-12-21 18:17:45 +00001312void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001313 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1314}
1315
Damiend99b0522013-12-21 18:17:45 +00001316void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1317 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001319
1320 // build the "fromlist" tuple
1321#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001322 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001323#else
Damien Georgeb9791222014-01-23 00:34:21 +00001324 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1325 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001326#endif
1327
1328 // do the import
Damien429d7192013-10-04 19:53:11 +01001329 qstr dummy_q, id1;
1330 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1331 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001332
Damien429d7192013-10-04 19:53:11 +01001333 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001334 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001335
1336 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001337 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001338 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001339#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001340 {
1341 vstr_t *vstr = vstr_new();
1342 vstr_printf(vstr, "(");
1343 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001344 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1345 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1346 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001347 if (i > 0) {
1348 vstr_printf(vstr, ", ");
1349 }
1350 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001351 uint len;
1352 const byte *str = qstr_data(id2, &len);
1353 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001354 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001355 }
Damien02f89412013-12-12 15:13:36 +00001356 if (n == 1) {
1357 vstr_printf(vstr, ",");
1358 }
1359 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001360 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001361 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001362 }
Damiendb4c3612013-12-10 17:27:24 +00001363#else
1364 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001365 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1366 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1367 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001368 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001369 }
Damien Georgeb9791222014-01-23 00:34:21 +00001370 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001371#endif
1372
1373 // do the import
Damien429d7192013-10-04 19:53:11 +01001374 qstr dummy_q, id1;
1375 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1376 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001377 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1378 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1379 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001380 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001381 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001383 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001384 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001385 }
1386 }
1387 EMIT(pop_top);
1388 }
1389}
1390
Damiend99b0522013-12-21 18:17:45 +00001391void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001392 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001393 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1394 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001395 } else {
Damiend99b0522013-12-21 18:17:45 +00001396 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1397 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001398 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001399 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001400 }
Damien429d7192013-10-04 19:53:11 +01001401 }
1402 }
1403}
1404
Damiend99b0522013-12-21 18:17:45 +00001405void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001406 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001407 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1408 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001409 } else {
Damiend99b0522013-12-21 18:17:45 +00001410 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1411 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001412 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001413 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001414 }
Damien429d7192013-10-04 19:53:11 +01001415 }
1416 }
1417}
1418
Damiend99b0522013-12-21 18:17:45 +00001419void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001420 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001421 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001422 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 +00001423 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001424 // assertion message
1425 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001426 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001427 }
Damien Georgeb9791222014-01-23 00:34:21 +00001428 EMIT_ARG(raise_varargs, 1);
1429 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001430}
1431
Damiend99b0522013-12-21 18:17:45 +00001432void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001433 // TODO proper and/or short circuiting
1434
Damienb05d7072013-10-05 13:37:10 +01001435 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001436
Damienb05d7072013-10-05 13:37:10 +01001437 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001438 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1439
1440 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001441
1442 if (
1443#if !MICROPY_EMIT_CPYTHON
1444 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1445 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1446#endif
1447 // optimisation to not jump if last instruction was return
1448 !EMIT(last_emit_was_return_value)
1449 ) {
1450 // jump over elif/else blocks
1451 EMIT_ARG(jump, l_end);
1452 }
1453
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001455
Damiend99b0522013-12-21 18:17:45 +00001456 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001457 // compile elif blocks
1458
Damiend99b0522013-12-21 18:17:45 +00001459 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001460
Damiend99b0522013-12-21 18:17:45 +00001461 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001462 // multiple elif blocks
1463
Damiend99b0522013-12-21 18:17:45 +00001464 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001465 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001466 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001467 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001468 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1469
1470 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001471 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001472 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001473 }
Damien Georgeb9791222014-01-23 00:34:21 +00001474 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001475 }
1476
1477 } else {
1478 // a single elif block
1479
Damienb05d7072013-10-05 13:37:10 +01001480 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001481 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1482
1483 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001484 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001486 }
Damien Georgeb9791222014-01-23 00:34:21 +00001487 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001488 }
1489 }
1490
1491 // compile else block
1492 compile_node(comp, pns->nodes[3]); // can be null
1493
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001495}
1496
Damien Georgecbddb272014-02-01 20:08:18 +00001497#define START_BREAK_CONTINUE_BLOCK \
1498 int old_break_label = comp->break_label; \
1499 int old_continue_label = comp->continue_label; \
1500 int break_label = comp_next_label(comp); \
1501 int continue_label = comp_next_label(comp); \
1502 comp->break_label = break_label; \
1503 comp->continue_label = continue_label; \
1504 comp->break_continue_except_level = comp->cur_except_level;
1505
1506#define END_BREAK_CONTINUE_BLOCK \
1507 comp->break_label = old_break_label; \
1508 comp->continue_label = old_continue_label; \
1509 comp->break_continue_except_level = comp->cur_except_level;
1510
Damiend99b0522013-12-21 18:17:45 +00001511void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001512 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001513
Damience89a212013-10-15 22:25:17 +01001514 // compared to CPython, we have an optimised version of while loops
1515#if MICROPY_EMIT_CPYTHON
1516 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001517 EMIT_ARG(setup_loop, break_label);
1518 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001519 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1520 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001521 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001522 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001523 }
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001525 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1526 // this is a small hack to agree with CPython
1527 if (!node_is_const_true(pns->nodes[0])) {
1528 EMIT(pop_block);
1529 }
Damience89a212013-10-15 22:25:17 +01001530#else
1531 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001532 EMIT_ARG(jump, continue_label);
1533 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001534 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001535 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001536 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1537#endif
1538
1539 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001540 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001541
1542 compile_node(comp, pns->nodes[2]); // else
1543
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001545}
1546
Damienf72fd0e2013-11-06 20:20:49 +00001547// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001548// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1549// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001550void 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 +00001551 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001552
1553 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001554 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001555
Damien George3ff2d032014-03-31 18:02:22 +01001556 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001557 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001558 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001559
Damien Georgeb9791222014-01-23 00:34:21 +00001560 EMIT_ARG(jump, entry_label);
1561 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001562
Damien George3ff2d032014-03-31 18:02:22 +01001563 // at this point we actually have 1 less element on the stack
1564 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1565
1566 // store next value to var
1567 c_assign(comp, pn_var, ASSIGN_STORE);
1568
Damienf3822fc2013-11-09 20:12:03 +00001569 // compile body
1570 compile_node(comp, pn_body);
1571
Damien Georgeb9791222014-01-23 00:34:21 +00001572 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001573
Damien George3ff2d032014-03-31 18:02:22 +01001574 // compile: var + step, duplicated on stack
1575 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001576 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001577 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001578 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001579
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001581
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001582 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001583 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001584 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1585 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001586 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001587 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001588 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001589 }
Damien Georgeb9791222014-01-23 00:34:21 +00001590 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001591
Damien George3ff2d032014-03-31 18:02:22 +01001592 // discard final value of var that failed the loop condition
1593 EMIT(pop_top);
1594
Damienf72fd0e2013-11-06 20:20:49 +00001595 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001596 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001597
1598 compile_node(comp, pn_else);
1599
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001601}
1602
Damiend99b0522013-12-21 18:17:45 +00001603void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001604#if !MICROPY_EMIT_CPYTHON
1605 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1606 // this is actually slower, but uses no heap memory
1607 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001608 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 +00001609 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001610 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1611 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1612 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1613 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001614 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1615 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001616 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001617 mp_parse_node_t pn_range_start;
1618 mp_parse_node_t pn_range_end;
1619 mp_parse_node_t pn_range_step;
1620 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001621 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001622 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001623 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001624 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001625 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001626 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001627 } else if (n_args == 2) {
1628 pn_range_start = args[0];
1629 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001630 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001631 } else {
1632 pn_range_start = args[0];
1633 pn_range_end = args[1];
1634 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001635 // We need to know sign of step. This is possible only if it's constant
1636 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1637 optimize = false;
1638 }
Damienf72fd0e2013-11-06 20:20:49 +00001639 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001640 }
1641 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001642 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1643 return;
1644 }
1645 }
1646 }
1647#endif
1648
Damien Georgecbddb272014-02-01 20:08:18 +00001649 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001650
Damienb05d7072013-10-05 13:37:10 +01001651 int pop_label = comp_next_label(comp);
1652 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001653
Damience89a212013-10-15 22:25:17 +01001654 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1655#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001657#endif
1658
Damien429d7192013-10-04 19:53:11 +01001659 compile_node(comp, pns->nodes[1]); // iterator
1660 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001661 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001662 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001663 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1664 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001665 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001666 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001667 }
Damien Georgeb9791222014-01-23 00:34:21 +00001668 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001669 EMIT(for_iter_end);
1670
1671 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001672 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001673
Damience89a212013-10-15 22:25:17 +01001674#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001675 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001676#endif
Damien429d7192013-10-04 19:53:11 +01001677
1678 compile_node(comp, pns->nodes[3]); // else (not tested)
1679
Damien Georgeb9791222014-01-23 00:34:21 +00001680 EMIT_ARG(label_assign, break_label);
1681 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001682}
1683
Damiend99b0522013-12-21 18:17:45 +00001684void 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 +01001685 // this function is a bit of a hack at the moment
1686 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1687
1688 // setup code
1689 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001690 int l1 = comp_next_label(comp);
1691 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001692
Damien Georgeb9791222014-01-23 00:34:21 +00001693 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001694 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001695
Damien429d7192013-10-04 19:53:11 +01001696 compile_node(comp, pn_body); // body
1697 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(jump, success_label);
1699 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001700 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001701
1702 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001703 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1704 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001705
1706 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001707 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001708
Damiend99b0522013-12-21 18:17:45 +00001709 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001710 // this is a catch all exception handler
1711 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001712 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001713 return;
1714 }
1715 } else {
1716 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001717 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1718 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1719 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1720 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001721 // handler binds the exception to a local
1722 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001723 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001724 }
1725 }
1726 EMIT(dup_top);
1727 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001728 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001729 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001730 }
1731
1732 EMIT(pop_top);
1733
1734 if (qstr_exception_local == 0) {
1735 EMIT(pop_top);
1736 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001737 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001738 }
1739
1740 EMIT(pop_top);
1741
Damiene2880aa2013-12-20 14:22:59 +00001742 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001743 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001744 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001745 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001746 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001747 }
1748 compile_node(comp, pns_except->nodes[1]);
1749 if (qstr_exception_local != 0) {
1750 EMIT(pop_block);
1751 }
1752 EMIT(pop_except);
1753 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1755 EMIT_ARG(label_assign, l3);
1756 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1757 EMIT_ARG(store_id, qstr_exception_local);
1758 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001759
Damien George8dcc0c72014-03-27 10:55:21 +00001760 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001761 EMIT(end_finally);
1762 }
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(jump, l2);
1764 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001765 }
1766
Damien George8dcc0c72014-03-27 10:55:21 +00001767 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001768 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001769
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001771 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001772 EMIT_ARG(label_assign, l2);
1773 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001774}
1775
Damiend99b0522013-12-21 18:17:45 +00001776void 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) {
Damien429d7192013-10-04 19:53:11 +01001777 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1778 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001779 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001780
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001782 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001783
Damien429d7192013-10-04 19:53:11 +01001784 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001785 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001786 compile_node(comp, pn_body);
1787 } else {
1788 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1789 }
1790 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001791 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1792 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001793 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001794
Damien George8dcc0c72014-03-27 10:55:21 +00001795 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001796 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001797
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001799}
1800
Damiend99b0522013-12-21 18:17:45 +00001801void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1802 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1803 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1804 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001805 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001806 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1807 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001808 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001809 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001810 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001811 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001812 // no finally
1813 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1814 } else {
1815 // have finally
Damiend99b0522013-12-21 18:17:45 +00001816 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 +01001817 }
1818 } else {
1819 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001820 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001821 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001822 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001823 }
1824 } else {
1825 // shouldn't happen
1826 assert(0);
1827 }
1828}
1829
Damiend99b0522013-12-21 18:17:45 +00001830void 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 +01001831 if (n == 0) {
1832 // no more pre-bits, compile the body of the with
1833 compile_node(comp, body);
1834 } else {
Damienb05d7072013-10-05 13:37:10 +01001835 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001836 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001837 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001839 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001840 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001841 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1842 } else {
1843 // this pre-bit is just an expression
1844 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001845 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001846 EMIT(pop_top);
1847 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001848 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001849 // compile additional pre-bits and the body
1850 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1851 // finish this with block
1852 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001853 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1854 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001855 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001856 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001857 EMIT(end_finally);
1858 }
1859}
1860
Damiend99b0522013-12-21 18:17:45 +00001861void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001862 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001863 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001864 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1865 assert(n > 0);
1866
1867 // compile in a nested fashion
1868 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1869}
1870
Damiend99b0522013-12-21 18:17:45 +00001871void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1872 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001873 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1874 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001875 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001876 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001877 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001878 EMIT(pop_top);
1879
Damien429d7192013-10-04 19:53:11 +01001880 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001881 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001882 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001883 // do nothing with a lonely constant
1884 } else {
1885 compile_node(comp, pns->nodes[0]); // just an expression
1886 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1887 }
Damien429d7192013-10-04 19:53:11 +01001888 }
1889 } else {
Damiend99b0522013-12-21 18:17:45 +00001890 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1891 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001892 if (kind == PN_expr_stmt_augassign) {
1893 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1894 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001895 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001896 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001897 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001898 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1899 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1900 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1901 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1902 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1903 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1904 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1905 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1906 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1907 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1908 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1909 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1910 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001911 }
Damien George7e5fb242014-02-01 22:18:47 +00001912 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001913 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1914 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001915 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1916 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001917 // following CPython, we store left-most first
1918 if (rhs > 0) {
1919 EMIT(dup_top);
1920 }
1921 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1922 for (int i = 0; i < rhs; i++) {
1923 if (i + 1 < rhs) {
1924 EMIT(dup_top);
1925 }
Damiend99b0522013-12-21 18:17:45 +00001926 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001927 }
1928 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001929 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1930 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1931 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1932 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001933 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001934 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1935 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001936 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1937 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1938 // can't optimise when it's a star expression on the lhs
1939 goto no_optimisation;
1940 }
Damien429d7192013-10-04 19:53:11 +01001941 compile_node(comp, pns10->nodes[0]); // rhs
1942 compile_node(comp, pns10->nodes[1]); // rhs
1943 EMIT(rot_two);
1944 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1945 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001946 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1947 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1948 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1949 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001950 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001951 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1952 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001953 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1954 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1955 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1956 // can't optimise when it's a star expression on the lhs
1957 goto no_optimisation;
1958 }
Damien429d7192013-10-04 19:53:11 +01001959 compile_node(comp, pns10->nodes[0]); // rhs
1960 compile_node(comp, pns10->nodes[1]); // rhs
1961 compile_node(comp, pns10->nodes[2]); // rhs
1962 EMIT(rot_three);
1963 EMIT(rot_two);
1964 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1965 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1966 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1967 } else {
Damien George495d7812014-04-08 17:51:47 +01001968 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01001969 compile_node(comp, pns1->nodes[0]); // rhs
1970 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1971 }
1972 } else {
1973 // shouldn't happen
1974 assert(0);
1975 }
1976 }
1977}
1978
Damien Georged17926d2014-03-30 13:35:08 +01001979void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001980 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001981 compile_node(comp, pns->nodes[0]);
1982 for (int i = 1; i < num_nodes; i += 1) {
1983 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001984 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001985 }
1986}
1987
Damiend99b0522013-12-21 18:17:45 +00001988void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1989 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1990 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001991
1992 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001993 int l_fail = comp_next_label(comp);
1994 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001995 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1996 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001997 EMIT_ARG(jump, l_end);
1998 EMIT_ARG(label_assign, l_fail);
1999 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01002000 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002001 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002002}
2003
Damiend99b0522013-12-21 18:17:45 +00002004void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002005 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002006 //mp_parse_node_t pn_params = pns->nodes[0];
2007 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002008
2009 if (comp->pass == PASS_1) {
2010 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002011 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 +01002012 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002013 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002014 }
2015
2016 // get the scope for this lambda
2017 scope_t *this_scope = (scope_t*)pns->nodes[2];
2018
2019 // make the lambda
2020 close_over_variables_etc(comp, this_scope, 0, 0);
2021}
2022
Damiend99b0522013-12-21 18:17:45 +00002023void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002024 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002025 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002026 for (int i = 0; i < n; i += 1) {
2027 compile_node(comp, pns->nodes[i]);
2028 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002029 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002030 }
2031 }
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002033}
2034
Damiend99b0522013-12-21 18:17:45 +00002035void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002036 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002037 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002038 for (int i = 0; i < n; i += 1) {
2039 compile_node(comp, pns->nodes[i]);
2040 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002042 }
2043 }
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002045}
2046
Damiend99b0522013-12-21 18:17:45 +00002047void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002048 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002049 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002050}
2051
Damiend99b0522013-12-21 18:17:45 +00002052void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002053 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002054 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002055 compile_node(comp, pns->nodes[0]);
2056 bool multi = (num_nodes > 3);
2057 int l_fail = 0;
2058 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002059 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002060 }
2061 for (int i = 1; i + 1 < num_nodes; i += 2) {
2062 compile_node(comp, pns->nodes[i + 1]);
2063 if (i + 2 < num_nodes) {
2064 EMIT(dup_top);
2065 EMIT(rot_three);
2066 }
Damien George7e5fb242014-02-01 22:18:47 +00002067 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002068 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002069 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002070 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2071 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2072 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2073 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2074 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2075 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2076 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2077 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002078 }
2079 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002080 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2081 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2082 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002083 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002084 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002085 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002086 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002087 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002088 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002089 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 } else {
2092 // shouldn't happen
2093 assert(0);
2094 }
2095 } else {
2096 // shouldn't happen
2097 assert(0);
2098 }
2099 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002100 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002101 }
2102 }
2103 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002104 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002105 EMIT_ARG(jump, l_end);
2106 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002107 EMIT(rot_two);
2108 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002109 EMIT_ARG(label_assign, l_end);
2110 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002111 }
2112}
2113
Damiend99b0522013-12-21 18:17:45 +00002114void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002115 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002116}
2117
Damiend99b0522013-12-21 18:17:45 +00002118void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002119 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002120}
2121
Damiend99b0522013-12-21 18:17:45 +00002122void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002124}
2125
Damiend99b0522013-12-21 18:17:45 +00002126void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002127 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002128}
2129
Damiend99b0522013-12-21 18:17:45 +00002130void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2131 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002132 compile_node(comp, pns->nodes[0]);
2133 for (int i = 1; i + 1 < num_nodes; i += 2) {
2134 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002138 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002139 } else {
2140 // shouldn't happen
2141 assert(0);
2142 }
2143 }
2144}
2145
Damiend99b0522013-12-21 18:17:45 +00002146void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2147 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002148 compile_node(comp, pns->nodes[0]);
2149 for (int i = 1; i + 1 < num_nodes; i += 2) {
2150 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002151 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002152 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002153 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002154 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002155 } else {
2156 // shouldn't happen
2157 assert(0);
2158 }
2159 }
2160}
2161
Damiend99b0522013-12-21 18:17:45 +00002162void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2163 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002164 compile_node(comp, pns->nodes[0]);
2165 for (int i = 1; i + 1 < num_nodes; i += 2) {
2166 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002167 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002168 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002169 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002171 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002172 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002173 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002175 } else {
2176 // shouldn't happen
2177 assert(0);
2178 }
2179 }
2180}
2181
Damiend99b0522013-12-21 18:17:45 +00002182void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002183 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002184 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002185 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002186 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002187 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002188 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002189 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002190 } else {
2191 // shouldn't happen
2192 assert(0);
2193 }
2194}
2195
Damien George35e2a4e2014-02-05 00:51:47 +00002196void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2197 // this is to handle special super() call
2198 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2199
2200 compile_generic_all_nodes(comp, pns);
2201}
2202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002203STATIC 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 +01002204 // function to call is on top of stack
2205
Damien George35e2a4e2014-02-05 00:51:47 +00002206#if !MICROPY_EMIT_CPYTHON
2207 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002208 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 +00002209 EMIT_ARG(load_id, MP_QSTR___class__);
2210 // get first argument to function
2211 bool found = false;
2212 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2213 if (comp->scope_cur->id_info[i].param) {
2214 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2215 found = true;
2216 break;
2217 }
2218 }
2219 if (!found) {
2220 printf("TypeError: super() call cannot find self\n");
2221 return;
2222 }
2223 EMIT_ARG(call_function, 2, 0, false, false);
2224 return;
2225 }
2226#endif
2227
Damien429d7192013-10-04 19:53:11 +01002228 int old_n_arg_keyword = comp->n_arg_keyword;
2229 bool old_have_star_arg = comp->have_star_arg;
2230 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2231 comp->n_arg_keyword = 0;
2232 comp->have_star_arg = false;
2233 comp->have_dbl_star_arg = false;
2234
Damien Georgebbcd49a2014-02-06 20:30:16 +00002235 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002236
2237 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002238 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002239 if (comp->have_star_arg) {
2240 n_positional -= 1;
2241 }
2242 if (comp->have_dbl_star_arg) {
2243 n_positional -= 1;
2244 }
2245
2246 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002247 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002248 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002249 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002250 }
2251
2252 comp->n_arg_keyword = old_n_arg_keyword;
2253 comp->have_star_arg = old_have_star_arg;
2254 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2255}
2256
Damiend99b0522013-12-21 18:17:45 +00002257void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2258 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002259 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002260 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 +01002261 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002262 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2263 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002264 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002265 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002266 i += 1;
2267 } else {
2268 compile_node(comp, pns->nodes[i]);
2269 }
Damien George35e2a4e2014-02-05 00:51:47 +00002270 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002271 }
2272}
2273
Damiend99b0522013-12-21 18:17:45 +00002274void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002275 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002276 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002277}
2278
Damiend99b0522013-12-21 18:17:45 +00002279void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002280 // a list of strings
Damien63321742013-12-10 17:41:49 +00002281
2282 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002283 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002284 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002285 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002286 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002287 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2288 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2289 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002290 if (i == 0) {
2291 string_kind = pn_kind;
2292 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002293 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002294 return;
2295 }
Damien George55baff42014-01-21 21:40:13 +00002296 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002297 }
Damien63321742013-12-10 17:41:49 +00002298
Damien63321742013-12-10 17:41:49 +00002299 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002300 byte *q_ptr;
2301 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002302 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002303 uint s_len;
2304 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002305 memcpy(s_dest, s, s_len);
2306 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002307 }
Damien George55baff42014-01-21 21:40:13 +00002308 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002309
Damien Georgeb9791222014-01-23 00:34:21 +00002310 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002311}
2312
2313// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002314void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2315 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2316 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2317 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002318
2319 if (comp->pass == PASS_1) {
2320 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002321 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 +01002322 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002323 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002324 }
2325
2326 // get the scope for this comprehension
2327 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2328
2329 // compile the comprehension
2330 close_over_variables_etc(comp, this_scope, 0, 0);
2331
2332 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2333 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002334 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002335}
2336
Damiend99b0522013-12-21 18:17:45 +00002337void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2338 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002339 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002340 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2341 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2342 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2343 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2344 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2345 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2346 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002347 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002348 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002349 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002350 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002351 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002352 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002353 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002354 // generator expression
2355 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2356 } else {
2357 // tuple with 2 items
2358 goto tuple_with_2_items;
2359 }
2360 } else {
2361 // tuple with 2 items
2362 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002363 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002364 }
2365 } else {
2366 // parenthesis around a single item, is just that item
2367 compile_node(comp, pns->nodes[0]);
2368 }
2369}
2370
Damiend99b0522013-12-21 18:17:45 +00002371void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2372 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002373 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002374 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002375 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2376 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2377 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2378 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2379 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002380 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002381 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002382 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002383 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002384 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002385 // list of many items
2386 compile_node(comp, pns2->nodes[0]);
2387 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002388 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002389 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002390 // list comprehension
2391 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2392 } else {
2393 // list with 2 items
2394 goto list_with_2_items;
2395 }
2396 } else {
2397 // list with 2 items
2398 list_with_2_items:
2399 compile_node(comp, pns2->nodes[0]);
2400 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002401 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002402 }
2403 } else {
2404 // list with 1 item
2405 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002406 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002407 }
2408}
2409
Damiend99b0522013-12-21 18:17:45 +00002410void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2411 mp_parse_node_t pn = pns->nodes[0];
2412 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002413 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002414 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002415 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2416 pns = (mp_parse_node_struct_t*)pn;
2417 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002418 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002419 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002420 compile_node(comp, pn);
2421 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002422 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2423 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2424 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2425 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002426 // dict/set with multiple elements
2427
2428 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002429 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002430 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2431
2432 // first element sets whether it's a dict or set
2433 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002434 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002435 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002436 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002437 compile_node(comp, pns->nodes[0]);
2438 EMIT(store_map);
2439 is_dict = true;
2440 } else {
2441 // a set
2442 compile_node(comp, pns->nodes[0]); // 1st value of set
2443 is_dict = false;
2444 }
2445
2446 // process rest of elements
2447 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002448 mp_parse_node_t pn = nodes[i];
2449 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002450 compile_node(comp, pn);
2451 if (is_dict) {
2452 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002453 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002454 return;
2455 }
2456 EMIT(store_map);
2457 } else {
2458 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002459 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002460 return;
2461 }
2462 }
2463 }
2464
2465 // if it's a set, build it
2466 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002467 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002468 }
Damiend99b0522013-12-21 18:17:45 +00002469 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002470 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002471 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002472 // a dictionary comprehension
2473 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2474 } else {
2475 // a set comprehension
2476 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2477 }
2478 } else {
2479 // shouldn't happen
2480 assert(0);
2481 }
2482 } else {
2483 // set with one element
2484 goto set_with_one_element;
2485 }
2486 } else {
2487 // set with one element
2488 set_with_one_element:
2489 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002490 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002491 }
2492}
2493
Damiend99b0522013-12-21 18:17:45 +00002494void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002495 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002496}
2497
Damiend99b0522013-12-21 18:17:45 +00002498void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002499 // object who's index we want is on top of stack
2500 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002501 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002505 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002506 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002507}
2508
Damiend99b0522013-12-21 18:17:45 +00002509void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2510 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2511 mp_parse_node_t pn = pns->nodes[0];
2512 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002513 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002514 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2515 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002516 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2517 pns = (mp_parse_node_struct_t*)pn;
2518 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002519 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002520 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002521 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002522 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002523 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002524 } else {
2525 // [?::x]
2526 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002527 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002528 }
Damiend99b0522013-12-21 18:17:45 +00002529 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002530 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002531 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2532 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2533 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2534 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002535 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002537 } else {
2538 // [?:x:x]
2539 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002540 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002541 }
2542 } else {
2543 // [?:x]
2544 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002545 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002546 }
2547 } else {
2548 // [?:x]
2549 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002551 }
2552}
2553
Damiend99b0522013-12-21 18:17:45 +00002554void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002555 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002556 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2557 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002558}
2559
Damiend99b0522013-12-21 18:17:45 +00002560void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002562 compile_subscript_3_helper(comp, pns);
2563}
2564
Damiend99b0522013-12-21 18:17:45 +00002565void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002566 // if this is called then we are compiling a dict key:value pair
2567 compile_node(comp, pns->nodes[1]); // value
2568 compile_node(comp, pns->nodes[0]); // key
2569}
2570
Damiend99b0522013-12-21 18:17:45 +00002571void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002572 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002573 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002575}
2576
Damiend99b0522013-12-21 18:17:45 +00002577void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002578 if (comp->have_star_arg) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002579 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002580 return;
2581 }
2582 comp->have_star_arg = true;
2583 compile_node(comp, pns->nodes[0]);
2584}
2585
Damiend99b0522013-12-21 18:17:45 +00002586void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002587 if (comp->have_dbl_star_arg) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002588 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002589 return;
2590 }
2591 comp->have_dbl_star_arg = true;
2592 compile_node(comp, pns->nodes[0]);
2593}
2594
Damiend99b0522013-12-21 18:17:45 +00002595void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2596 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2597 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2598 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2599 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002600 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 +01002601 return;
2602 }
Damien Georgeb9791222014-01-23 00:34:21 +00002603 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002604 compile_node(comp, pns2->nodes[0]);
2605 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002606 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002607 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2608 } else {
2609 // shouldn't happen
2610 assert(0);
2611 }
2612}
2613
Damiend99b0522013-12-21 18:17:45 +00002614void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002615 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002616 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002617 return;
2618 }
Damiend99b0522013-12-21 18:17:45 +00002619 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002620 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002621 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002622 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2623 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002624 compile_node(comp, pns->nodes[0]);
2625 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002626 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002627 EMIT(yield_from);
2628 } else {
2629 compile_node(comp, pns->nodes[0]);
2630 EMIT(yield_value);
2631 }
2632}
2633
Damiend99b0522013-12-21 18:17:45 +00002634typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002635STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002636 NULL,
2637#define nc NULL
2638#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002639#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002640#include "grammar.h"
2641#undef nc
2642#undef c
2643#undef DEF_RULE
2644};
2645
Damiend99b0522013-12-21 18:17:45 +00002646void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2647 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002648 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002649 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2650 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2651 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002652 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002653 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002654 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002655 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002656 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2657 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2658 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2659 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002660 case MP_PARSE_NODE_TOKEN:
2661 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002662 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002663 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002664 // do nothing
2665 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002666 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002667 }
2668 break;
Damien429d7192013-10-04 19:53:11 +01002669 default: assert(0);
2670 }
2671 } else {
Damiend99b0522013-12-21 18:17:45 +00002672 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002673 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002674 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002675 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002676 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002677#if MICROPY_DEBUG_PRINTERS
2678 mp_parse_node_print(pn, 0);
2679#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002680 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002681 } else {
2682 f(comp, pns);
2683 }
2684 }
2685}
2686
Damiend99b0522013-12-21 18:17:45 +00002687void 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 +01002688 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002689 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002690 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2691 if (MP_PARSE_NODE_IS_ID(pn)) {
2692 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002693 if (comp->have_bare_star) {
2694 // comes after a bare star, so doesn't count as a parameter
2695 } else {
2696 comp->scope_cur->num_params += 1;
2697 }
Damienb14de212013-10-06 00:28:28 +01002698 } else {
Damiend99b0522013-12-21 18:17:45 +00002699 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2700 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2701 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2702 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002703 //int node_index = 1; unused
2704 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002705 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002706 // this parameter has an annotation
2707 pn_annotation = pns->nodes[1];
2708 }
2709 //node_index = 2; unused
2710 }
2711 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002712 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002713 // this parameter has a default value
2714 if (comp->have_bare_star) {
2715 comp->scope_cur->num_dict_params += 1;
2716 } else {
2717 comp->scope_cur->num_default_params += 1;
2718 }
2719 }
2720 */
2721 if (comp->have_bare_star) {
2722 // comes after a bare star, so doesn't count as a parameter
2723 } else {
2724 comp->scope_cur->num_params += 1;
2725 }
Damiend99b0522013-12-21 18:17:45 +00002726 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2727 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002728 // bare star
2729 // TODO see http://www.python.org/dev/peps/pep-3102/
2730 comp->have_bare_star = true;
2731 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002732 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002733 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002734 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002735 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2736 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002737 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002738 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002739 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2740 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002741 pn_annotation = pns->nodes[1];
2742 } else {
2743 // shouldn't happen
2744 assert(0);
2745 }
Damiend99b0522013-12-21 18:17:45 +00002746 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2747 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2748 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002749 // this parameter has an annotation
2750 pn_annotation = pns->nodes[1];
2751 }
Damien George8725f8f2014-02-15 19:33:11 +00002752 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002753 } else {
Damienb14de212013-10-06 00:28:28 +01002754 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002755 assert(0);
2756 }
Damien429d7192013-10-04 19:53:11 +01002757 }
2758
2759 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002760 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002761 // TODO this parameter has an annotation
2762 }
2763 bool added;
2764 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2765 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002766 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002767 return;
2768 }
2769 id_info->param = true;
2770 id_info->kind = ID_INFO_KIND_LOCAL;
2771 }
2772}
2773
Damiend99b0522013-12-21 18:17:45 +00002774void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002775 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2776}
2777
Damiend99b0522013-12-21 18:17:45 +00002778void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002779 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2780}
2781
Damiend99b0522013-12-21 18:17:45 +00002782void 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 +01002783 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002784 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002785 // no more nested if/for; compile inner expression
2786 compile_node(comp, pn_inner_expr);
2787 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002788 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002789 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002790 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002791 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002792 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002793 } else {
2794 EMIT(yield_value);
2795 EMIT(pop_top);
2796 }
Damiend99b0522013-12-21 18:17:45 +00002797 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002798 // if condition
Damiend99b0522013-12-21 18:17:45 +00002799 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002800 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2801 pn_iter = pns_comp_if->nodes[1];
2802 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002803 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002804 // for loop
Damiend99b0522013-12-21 18:17:45 +00002805 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002806 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002807 int l_end2 = comp_next_label(comp);
2808 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002809 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(label_assign, l_top2);
2811 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002812 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2813 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 +00002814 EMIT_ARG(jump, l_top2);
2815 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002816 EMIT(for_iter_end);
2817 } else {
2818 // shouldn't happen
2819 assert(0);
2820 }
2821}
2822
Damiend99b0522013-12-21 18:17:45 +00002823void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002824 // see http://www.python.org/dev/peps/pep-0257/
2825
2826 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002827 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002828 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002829 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002830 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002831 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2832 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002833 for (int i = 0; i < num_nodes; i++) {
2834 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002835 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 +00002836 // not a newline, so this is the first statement; finish search
2837 break;
2838 }
2839 }
2840 // 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 +00002841 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002842 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002843 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002844 } else {
2845 return;
2846 }
2847
2848 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002849 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2850 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2851 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2852 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2853 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002854 compile_node(comp, pns->nodes[0]); // a doc string
2855 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002857 }
2858 }
2859 }
2860}
2861
2862void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2863 comp->pass = pass;
2864 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002865 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002866 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002867
2868 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002869 // reset maximum stack sizes in scope
2870 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002871 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002872 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002873 }
2874
Damien5ac1b2e2013-10-18 19:58:12 +01002875#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002876 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002877 scope_print_info(scope);
2878 }
Damien5ac1b2e2013-10-18 19:58:12 +01002879#endif
Damien429d7192013-10-04 19:53:11 +01002880
2881 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002882 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2883 assert(scope->kind == SCOPE_MODULE);
2884 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2885 compile_node(comp, pns->nodes[0]); // compile the expression
2886 EMIT(return_value);
2887 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002888 if (!comp->is_repl) {
2889 check_for_doc_string(comp, scope->pn);
2890 }
Damien429d7192013-10-04 19:53:11 +01002891 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002892 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002893 EMIT(return_value);
2894 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002895 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2896 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2897 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002898
2899 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002900 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002901 if (comp->pass == PASS_1) {
2902 comp->have_bare_star = false;
2903 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2904 }
2905
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002906 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002907
2908 compile_node(comp, pns->nodes[3]); // 3 is function body
2909 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002910 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002911 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002912 EMIT(return_value);
2913 }
2914 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002915 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2916 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2917 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002918
2919 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002920 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002921 if (comp->pass == PASS_1) {
2922 comp->have_bare_star = false;
2923 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2924 }
2925
2926 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2927 EMIT(return_value);
2928 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2929 // a bit of a hack at the moment
2930
Damiend99b0522013-12-21 18:17:45 +00002931 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2932 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2933 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2934 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2935 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002936
Damien George55baff42014-01-21 21:40:13 +00002937 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002938 if (comp->pass == PASS_1) {
2939 bool added;
2940 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2941 assert(added);
2942 id_info->kind = ID_INFO_KIND_LOCAL;
2943 scope->num_params = 1;
2944 }
2945
2946 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002947 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002948 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002950 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002951 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002952 }
2953
Damienb05d7072013-10-05 13:37:10 +01002954 int l_end = comp_next_label(comp);
2955 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002956 EMIT_ARG(load_id, qstr_arg);
2957 EMIT_ARG(label_assign, l_top);
2958 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002959 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2960 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002961 EMIT_ARG(jump, l_top);
2962 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002963 EMIT(for_iter_end);
2964
2965 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002966 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002967 }
2968 EMIT(return_value);
2969 } else {
2970 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002971 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2972 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2973 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002974
2975 if (comp->pass == PASS_1) {
2976 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002977 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002978 assert(added);
2979 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002980 }
2981
Damien Georgeb9791222014-01-23 00:34:21 +00002982 EMIT_ARG(load_id, MP_QSTR___name__);
2983 EMIT_ARG(store_id, MP_QSTR___module__);
2984 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2985 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002986
2987 check_for_doc_string(comp, pns->nodes[2]);
2988 compile_node(comp, pns->nodes[2]); // 2 is class body
2989
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002990 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002991 assert(id != NULL);
2992 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002993 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002994 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002995#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002996 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002997#else
Damien George35e2a4e2014-02-05 00:51:47 +00002998 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002999#endif
Damien429d7192013-10-04 19:53:11 +01003000 }
3001 EMIT(return_value);
3002 }
3003
Damien415eb6f2013-10-05 12:19:06 +01003004 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003005
3006 // make sure we match all the exception levels
3007 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003008}
3009
Damien George094d4502014-04-02 17:31:27 +01003010#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003011void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3012 comp->pass = pass;
3013 comp->scope_cur = scope;
3014 comp->next_label = 1;
3015
3016 if (scope->kind != SCOPE_FUNCTION) {
3017 printf("Error: inline assembler must be a function\n");
3018 return;
3019 }
3020
Damiena2f2f7d2013-10-06 00:14:13 +01003021 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003022 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003023 }
3024
Damien826005c2013-10-05 23:17:28 +01003025 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003026 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3027 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3028 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003029
Damiend99b0522013-12-21 18:17:45 +00003030 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003031
Damiena2f2f7d2013-10-06 00:14:13 +01003032 // parameters are in pns->nodes[1]
3033 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003034 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003035 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003036 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003037 }
3038
Damiend99b0522013-12-21 18:17:45 +00003039 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003040
Damiend99b0522013-12-21 18:17:45 +00003041 mp_parse_node_t pn_body = pns->nodes[3]; // body
3042 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003043 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3044
Damien Georgecbd2f742014-01-19 11:48:48 +00003045 /*
Damien826005c2013-10-05 23:17:28 +01003046 if (comp->pass == PASS_3) {
3047 //printf("----\n");
3048 scope_print_info(scope);
3049 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003050 */
Damien826005c2013-10-05 23:17:28 +01003051
3052 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003053 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3054 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3055 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3056 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3057 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3058 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3059 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3060 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3061 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3062 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3063 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3064 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3065 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003066 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3067
3068 // emit instructions
3069 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003070 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003071 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003072 return;
3073 }
3074 int lab = comp_next_label(comp);
3075 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003076 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003077 }
3078 } else {
3079 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003080 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003081 }
3082 }
3083 }
3084
3085 if (comp->pass > PASS_1) {
3086 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003087 }
Damien429d7192013-10-04 19:53:11 +01003088}
Damien George094d4502014-04-02 17:31:27 +01003089#endif
Damien429d7192013-10-04 19:53:11 +01003090
3091void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3092 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003093 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003094 scope->num_locals = 0;
3095 for (int i = 0; i < scope->id_info_len; i++) {
3096 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003097 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003098 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3099 continue;
3100 }
3101 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3102 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3103 }
Damien9ecbcff2013-12-11 00:41:43 +00003104 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003105 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3106 id->local_num = scope->num_locals;
3107 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003108 }
3109 }
3110
3111 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003112#if MICROPY_EMIT_CPYTHON
3113 int num_cell = 0;
3114#endif
Damien9ecbcff2013-12-11 00:41:43 +00003115 for (int i = 0; i < scope->id_info_len; i++) {
3116 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003117#if MICROPY_EMIT_CPYTHON
3118 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003119 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003120 id->local_num = num_cell;
3121 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003122 }
Damien George6baf76e2013-12-30 22:32:17 +00003123#else
3124 // in Micro Python the cells come right after the fast locals
3125 // parameters are not counted here, since they remain at the start
3126 // of the locals, even if they are cell vars
3127 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3128 id->local_num = scope->num_locals;
3129 scope->num_locals += 1;
3130 }
3131#endif
Damien9ecbcff2013-12-11 00:41:43 +00003132 }
Damien9ecbcff2013-12-11 00:41:43 +00003133
3134 // compute the index of free vars (freevars[idx] in CPython)
3135 // make sure they are in the order of the parent scope
3136 if (scope->parent != NULL) {
3137 int num_free = 0;
3138 for (int i = 0; i < scope->parent->id_info_len; i++) {
3139 id_info_t *id = &scope->parent->id_info[i];
3140 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3141 for (int j = 0; j < scope->id_info_len; j++) {
3142 id_info_t *id2 = &scope->id_info[j];
3143 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003144 assert(!id2->param); // free vars should not be params
3145#if MICROPY_EMIT_CPYTHON
3146 // in CPython the frees are numbered after the cells
3147 id2->local_num = num_cell + num_free;
3148#else
3149 // in Micro Python the frees come first, before the params
3150 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003151#endif
3152 num_free += 1;
3153 }
3154 }
3155 }
Damien429d7192013-10-04 19:53:11 +01003156 }
Damien George6baf76e2013-12-30 22:32:17 +00003157#if !MICROPY_EMIT_CPYTHON
3158 // in Micro Python shift all other locals after the free locals
3159 if (num_free > 0) {
3160 for (int i = 0; i < scope->id_info_len; i++) {
3161 id_info_t *id = &scope->id_info[i];
3162 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3163 id->local_num += num_free;
3164 }
3165 }
3166 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3167 scope->num_locals += num_free;
3168 }
3169#endif
Damien429d7192013-10-04 19:53:11 +01003170 }
3171
Damien George8725f8f2014-02-15 19:33:11 +00003172 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003173
3174#if MICROPY_EMIT_CPYTHON
3175 // these flags computed here are for CPython compatibility only
3176 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003177 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003178 }
3179 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) {
3180 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003181 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003182
3183 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003184 // Note that we don't actually use this information at the moment (for CPython compat only)
3185 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 +00003186 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003187 }
3188 }
Damien George882b3632014-04-02 15:56:31 +01003189#endif
3190
Damien429d7192013-10-04 19:53:11 +01003191 int num_free = 0;
3192 for (int i = 0; i < scope->id_info_len; i++) {
3193 id_info_t *id = &scope->id_info[i];
3194 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3195 num_free += 1;
3196 }
3197 }
3198 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003199 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003200 }
3201}
3202
Damien George65cad122014-04-06 11:48:15 +01003203mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003204 compiler_t *comp = m_new(compiler_t, 1);
3205
Damien Georgecbd2f742014-01-19 11:48:48 +00003206 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003207 comp->is_repl = is_repl;
3208 comp->had_error = false;
3209
Damien429d7192013-10-04 19:53:11 +01003210 comp->break_label = 0;
3211 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003212 comp->break_continue_except_level = 0;
3213 comp->cur_except_level = 0;
3214
Damien George35e2a4e2014-02-05 00:51:47 +00003215 comp->func_arg_is_super = false;
3216
Damien429d7192013-10-04 19:53:11 +01003217 comp->scope_head = NULL;
3218 comp->scope_cur = NULL;
3219
Damien826005c2013-10-05 23:17:28 +01003220 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003221 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003222
3223 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003224 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003225
Damien826005c2013-10-05 23:17:28 +01003226 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003227 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003228 comp->emit_method_table = &emit_pass1_method_table;
3229 comp->emit_inline_asm = NULL;
3230 comp->emit_inline_asm_method_table = NULL;
3231 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003232 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003233 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003234#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003235 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003236 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003237#endif
Damien826005c2013-10-05 23:17:28 +01003238 } else {
3239 compile_scope(comp, s, PASS_1);
3240 }
3241
3242 // update maximim number of labels needed
3243 if (comp->next_label > max_num_labels) {
3244 max_num_labels = comp->next_label;
3245 }
Damien429d7192013-10-04 19:53:11 +01003246 }
3247
Damien826005c2013-10-05 23:17:28 +01003248 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003249 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003250 compile_scope_compute_things(comp, s);
3251 }
3252
Damien826005c2013-10-05 23:17:28 +01003253 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003254 emit_pass1_free(comp->emit);
3255
Damien826005c2013-10-05 23:17:28 +01003256 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003257#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003258 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003259#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003260 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003261#endif
Damien3ef4abb2013-10-12 16:53:13 +01003262#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003263 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003264#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003265#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003266 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003267 if (false) {
3268 // dummy
3269
Damien3ef4abb2013-10-12 16:53:13 +01003270#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003271 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003272 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003273 if (emit_inline_thumb == NULL) {
3274 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3275 }
3276 comp->emit = NULL;
3277 comp->emit_method_table = NULL;
3278 comp->emit_inline_asm = emit_inline_thumb;
3279 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3280 compile_scope_inline_asm(comp, s, PASS_2);
3281 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003282#endif
3283
Damien826005c2013-10-05 23:17:28 +01003284 } else {
Damienc025ebb2013-10-12 14:30:21 +01003285
3286 // choose the emit type
3287
Damien3ef4abb2013-10-12 16:53:13 +01003288#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003289 comp->emit = emit_cpython_new(max_num_labels);
3290 comp->emit_method_table = &emit_cpython_method_table;
3291#else
Damien826005c2013-10-05 23:17:28 +01003292 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003293
3294#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003295 case MP_EMIT_OPT_NATIVE_PYTHON:
3296 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003297#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003298 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003299 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003300 }
Damien13ed3a62013-10-08 09:05:10 +01003301 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003302#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003303 if (emit_native == NULL) {
3304 emit_native = emit_native_thumb_new(max_num_labels);
3305 }
3306 comp->emit_method_table = &emit_native_thumb_method_table;
3307#endif
3308 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003309 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003310 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003311#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003312
Damien826005c2013-10-05 23:17:28 +01003313 default:
3314 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003315 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003316 }
3317 comp->emit = emit_bc;
3318 comp->emit_method_table = &emit_bc_method_table;
3319 break;
3320 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003321#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003322
3323 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003324 compile_scope(comp, s, PASS_2);
3325 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003326 }
Damien429d7192013-10-04 19:53:11 +01003327 }
3328
Damien George41d02b62014-01-24 22:42:28 +00003329 // free the emitters
3330#if !MICROPY_EMIT_CPYTHON
3331 if (emit_bc != NULL) {
3332 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003333 }
Damien George41d02b62014-01-24 22:42:28 +00003334#if MICROPY_EMIT_NATIVE
3335 if (emit_native != NULL) {
3336#if MICROPY_EMIT_X64
3337 emit_native_x64_free(emit_native);
3338#elif MICROPY_EMIT_THUMB
3339 emit_native_thumb_free(emit_native);
3340#endif
3341 }
3342#endif
3343#if MICROPY_EMIT_INLINE_THUMB
3344 if (emit_inline_thumb != NULL) {
3345 emit_inline_thumb_free(emit_inline_thumb);
3346 }
3347#endif
3348#endif // !MICROPY_EMIT_CPYTHON
3349
3350 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003351 uint unique_code_id = module_scope->unique_code_id;
3352 for (scope_t *s = module_scope; s;) {
3353 scope_t *next = s->next;
3354 scope_free(s);
3355 s = next;
3356 }
Damien5ac1b2e2013-10-18 19:58:12 +01003357
Damien George41d02b62014-01-24 22:42:28 +00003358 // free the compiler
3359 bool had_error = comp->had_error;
3360 m_del_obj(compiler_t, comp);
3361
Damien George1fb03172014-01-03 14:22:03 +00003362 if (had_error) {
3363 // TODO return a proper error message
3364 return mp_const_none;
3365 } else {
3366#if MICROPY_EMIT_CPYTHON
3367 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003368 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003369 return mp_const_true;
3370#else
3371 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003372 // 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 +01003373 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003374#endif
3375 }
Damien429d7192013-10-04 19:53:11 +01003376}