blob: 1fc5f0722751ab0314110164c6181583365ef756 [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];
Damien429d7192013-10-04 19:53:11 +01001936 compile_node(comp, pns10->nodes[0]); // rhs
1937 compile_node(comp, pns10->nodes[1]); // rhs
1938 EMIT(rot_two);
1939 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1940 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001941 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1942 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1943 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1944 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001945 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001946 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1947 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001948 compile_node(comp, pns10->nodes[0]); // rhs
1949 compile_node(comp, pns10->nodes[1]); // rhs
1950 compile_node(comp, pns10->nodes[2]); // rhs
1951 EMIT(rot_three);
1952 EMIT(rot_two);
1953 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1954 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1955 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1956 } else {
1957 compile_node(comp, pns1->nodes[0]); // rhs
1958 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1959 }
1960 } else {
1961 // shouldn't happen
1962 assert(0);
1963 }
1964 }
1965}
1966
Damien Georged17926d2014-03-30 13:35:08 +01001967void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001968 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001969 compile_node(comp, pns->nodes[0]);
1970 for (int i = 1; i < num_nodes; i += 1) {
1971 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001972 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001973 }
1974}
1975
Damiend99b0522013-12-21 18:17:45 +00001976void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1977 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1978 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001979
1980 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001981 int l_fail = comp_next_label(comp);
1982 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001983 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1984 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001985 EMIT_ARG(jump, l_end);
1986 EMIT_ARG(label_assign, l_fail);
1987 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001988 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001989 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001990}
1991
Damiend99b0522013-12-21 18:17:45 +00001992void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001993 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001994 //mp_parse_node_t pn_params = pns->nodes[0];
1995 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001996
1997 if (comp->pass == PASS_1) {
1998 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001999 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 +01002000 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002001 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002002 }
2003
2004 // get the scope for this lambda
2005 scope_t *this_scope = (scope_t*)pns->nodes[2];
2006
2007 // make the lambda
2008 close_over_variables_etc(comp, this_scope, 0, 0);
2009}
2010
Damiend99b0522013-12-21 18:17:45 +00002011void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002012 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002013 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002014 for (int i = 0; i < n; i += 1) {
2015 compile_node(comp, pns->nodes[i]);
2016 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002017 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002018 }
2019 }
Damien Georgeb9791222014-01-23 00:34:21 +00002020 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002021}
2022
Damiend99b0522013-12-21 18:17:45 +00002023void compile_and_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_false_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_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002036 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002037 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002038}
2039
Damiend99b0522013-12-21 18:17:45 +00002040void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002041 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002042 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002043 compile_node(comp, pns->nodes[0]);
2044 bool multi = (num_nodes > 3);
2045 int l_fail = 0;
2046 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002047 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002048 }
2049 for (int i = 1; i + 1 < num_nodes; i += 2) {
2050 compile_node(comp, pns->nodes[i + 1]);
2051 if (i + 2 < num_nodes) {
2052 EMIT(dup_top);
2053 EMIT(rot_three);
2054 }
Damien George7e5fb242014-02-01 22:18:47 +00002055 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002056 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002057 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002058 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2059 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2060 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2061 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2062 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2063 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2064 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2065 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002066 }
2067 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002068 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2069 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2070 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002071 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002072 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002073 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002074 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002075 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002076 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002077 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002078 }
2079 } else {
2080 // shouldn't happen
2081 assert(0);
2082 }
2083 } else {
2084 // shouldn't happen
2085 assert(0);
2086 }
2087 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002088 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002089 }
2090 }
2091 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002092 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(jump, l_end);
2094 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002095 EMIT(rot_two);
2096 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002097 EMIT_ARG(label_assign, l_end);
2098 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002099 }
2100}
2101
Damiend99b0522013-12-21 18:17:45 +00002102void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002103 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002104}
2105
Damiend99b0522013-12-21 18:17:45 +00002106void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002107 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002108}
2109
Damiend99b0522013-12-21 18:17:45 +00002110void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002111 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002112}
2113
Damiend99b0522013-12-21 18:17:45 +00002114void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002115 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002116}
2117
Damiend99b0522013-12-21 18:17:45 +00002118void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2119 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002120 compile_node(comp, pns->nodes[0]);
2121 for (int i = 1; i + 1 < num_nodes; i += 2) {
2122 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002123 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002124 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002125 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002126 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002127 } else {
2128 // shouldn't happen
2129 assert(0);
2130 }
2131 }
2132}
2133
Damiend99b0522013-12-21 18:17:45 +00002134void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2135 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002136 compile_node(comp, pns->nodes[0]);
2137 for (int i = 1; i + 1 < num_nodes; i += 2) {
2138 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002139 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002140 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002141 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002142 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002143 } else {
2144 // shouldn't happen
2145 assert(0);
2146 }
2147 }
2148}
2149
Damiend99b0522013-12-21 18:17:45 +00002150void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2151 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002152 compile_node(comp, pns->nodes[0]);
2153 for (int i = 1; i + 1 < num_nodes; i += 2) {
2154 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002155 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002156 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002158 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002159 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002160 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002162 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002163 } else {
2164 // shouldn't happen
2165 assert(0);
2166 }
2167 }
2168}
2169
Damiend99b0522013-12-21 18:17:45 +00002170void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002171 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002172 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002173 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002174 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002175 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002176 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002177 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002178 } else {
2179 // shouldn't happen
2180 assert(0);
2181 }
2182}
2183
Damien George35e2a4e2014-02-05 00:51:47 +00002184void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2185 // this is to handle special super() call
2186 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2187
2188 compile_generic_all_nodes(comp, pns);
2189}
2190
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002191STATIC 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 +01002192 // function to call is on top of stack
2193
Damien George35e2a4e2014-02-05 00:51:47 +00002194#if !MICROPY_EMIT_CPYTHON
2195 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002196 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 +00002197 EMIT_ARG(load_id, MP_QSTR___class__);
2198 // get first argument to function
2199 bool found = false;
2200 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2201 if (comp->scope_cur->id_info[i].param) {
2202 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2203 found = true;
2204 break;
2205 }
2206 }
2207 if (!found) {
2208 printf("TypeError: super() call cannot find self\n");
2209 return;
2210 }
2211 EMIT_ARG(call_function, 2, 0, false, false);
2212 return;
2213 }
2214#endif
2215
Damien429d7192013-10-04 19:53:11 +01002216 int old_n_arg_keyword = comp->n_arg_keyword;
2217 bool old_have_star_arg = comp->have_star_arg;
2218 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2219 comp->n_arg_keyword = 0;
2220 comp->have_star_arg = false;
2221 comp->have_dbl_star_arg = false;
2222
Damien Georgebbcd49a2014-02-06 20:30:16 +00002223 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002224
2225 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002226 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002227 if (comp->have_star_arg) {
2228 n_positional -= 1;
2229 }
2230 if (comp->have_dbl_star_arg) {
2231 n_positional -= 1;
2232 }
2233
2234 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002235 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 +01002236 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002237 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 +01002238 }
2239
2240 comp->n_arg_keyword = old_n_arg_keyword;
2241 comp->have_star_arg = old_have_star_arg;
2242 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2243}
2244
Damiend99b0522013-12-21 18:17:45 +00002245void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2246 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002247 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002248 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 +01002249 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002250 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2251 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002252 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002253 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002254 i += 1;
2255 } else {
2256 compile_node(comp, pns->nodes[i]);
2257 }
Damien George35e2a4e2014-02-05 00:51:47 +00002258 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002259 }
2260}
2261
Damiend99b0522013-12-21 18:17:45 +00002262void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002263 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002264 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002265}
2266
Damiend99b0522013-12-21 18:17:45 +00002267void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002268 // a list of strings
Damien63321742013-12-10 17:41:49 +00002269
2270 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002271 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002272 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002273 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002274 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002275 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2276 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2277 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002278 if (i == 0) {
2279 string_kind = pn_kind;
2280 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002281 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002282 return;
2283 }
Damien George55baff42014-01-21 21:40:13 +00002284 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002285 }
Damien63321742013-12-10 17:41:49 +00002286
Damien63321742013-12-10 17:41:49 +00002287 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002288 byte *q_ptr;
2289 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002290 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002291 uint s_len;
2292 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002293 memcpy(s_dest, s, s_len);
2294 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002295 }
Damien George55baff42014-01-21 21:40:13 +00002296 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002297
Damien Georgeb9791222014-01-23 00:34:21 +00002298 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002299}
2300
2301// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002302void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2303 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2304 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2305 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002306
2307 if (comp->pass == PASS_1) {
2308 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002309 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 +01002310 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002311 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002312 }
2313
2314 // get the scope for this comprehension
2315 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2316
2317 // compile the comprehension
2318 close_over_variables_etc(comp, this_scope, 0, 0);
2319
2320 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2321 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002322 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002323}
2324
Damiend99b0522013-12-21 18:17:45 +00002325void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2326 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002327 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002328 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2329 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2330 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2331 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2332 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2333 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2334 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002335 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002336 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002337 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002338 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002339 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002340 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002341 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002342 // generator expression
2343 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2344 } else {
2345 // tuple with 2 items
2346 goto tuple_with_2_items;
2347 }
2348 } else {
2349 // tuple with 2 items
2350 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002351 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002352 }
2353 } else {
2354 // parenthesis around a single item, is just that item
2355 compile_node(comp, pns->nodes[0]);
2356 }
2357}
2358
Damiend99b0522013-12-21 18:17:45 +00002359void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2360 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002361 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002362 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002363 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2364 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2365 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2366 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2367 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002368 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002369 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002370 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002371 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002372 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002373 // list of many items
2374 compile_node(comp, pns2->nodes[0]);
2375 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002376 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002377 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002378 // list comprehension
2379 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2380 } else {
2381 // list with 2 items
2382 goto list_with_2_items;
2383 }
2384 } else {
2385 // list with 2 items
2386 list_with_2_items:
2387 compile_node(comp, pns2->nodes[0]);
2388 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002389 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002390 }
2391 } else {
2392 // list with 1 item
2393 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002394 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002395 }
2396}
2397
Damiend99b0522013-12-21 18:17:45 +00002398void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2399 mp_parse_node_t pn = pns->nodes[0];
2400 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002401 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002402 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002403 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2404 pns = (mp_parse_node_struct_t*)pn;
2405 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002406 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002407 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002408 compile_node(comp, pn);
2409 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002410 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2411 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2412 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2413 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002414 // dict/set with multiple elements
2415
2416 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002417 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002418 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2419
2420 // first element sets whether it's a dict or set
2421 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002422 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002423 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002424 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002425 compile_node(comp, pns->nodes[0]);
2426 EMIT(store_map);
2427 is_dict = true;
2428 } else {
2429 // a set
2430 compile_node(comp, pns->nodes[0]); // 1st value of set
2431 is_dict = false;
2432 }
2433
2434 // process rest of elements
2435 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002436 mp_parse_node_t pn = nodes[i];
2437 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002438 compile_node(comp, pn);
2439 if (is_dict) {
2440 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002441 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002442 return;
2443 }
2444 EMIT(store_map);
2445 } else {
2446 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002447 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002448 return;
2449 }
2450 }
2451 }
2452
2453 // if it's a set, build it
2454 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002455 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002456 }
Damiend99b0522013-12-21 18:17:45 +00002457 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002458 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002459 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002460 // a dictionary comprehension
2461 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2462 } else {
2463 // a set comprehension
2464 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2465 }
2466 } else {
2467 // shouldn't happen
2468 assert(0);
2469 }
2470 } else {
2471 // set with one element
2472 goto set_with_one_element;
2473 }
2474 } else {
2475 // set with one element
2476 set_with_one_element:
2477 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002478 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002479 }
2480}
2481
Damiend99b0522013-12-21 18:17:45 +00002482void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002483 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002484}
2485
Damiend99b0522013-12-21 18:17:45 +00002486void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002487 // object who's index we want is on top of stack
2488 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002489 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002490}
2491
Damiend99b0522013-12-21 18:17:45 +00002492void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002493 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002495}
2496
Damiend99b0522013-12-21 18:17:45 +00002497void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2498 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2499 mp_parse_node_t pn = pns->nodes[0];
2500 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002501 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002502 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2503 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002504 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2505 pns = (mp_parse_node_struct_t*)pn;
2506 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002508 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002509 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002510 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002511 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002512 } else {
2513 // [?::x]
2514 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002516 }
Damiend99b0522013-12-21 18:17:45 +00002517 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002518 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002519 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2520 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2521 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2522 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002523 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002524 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002525 } else {
2526 // [?:x:x]
2527 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002529 }
2530 } else {
2531 // [?:x]
2532 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002534 }
2535 } else {
2536 // [?:x]
2537 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002538 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002539 }
2540}
2541
Damiend99b0522013-12-21 18:17:45 +00002542void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002543 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002544 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2545 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002546}
2547
Damiend99b0522013-12-21 18:17:45 +00002548void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002549 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002550 compile_subscript_3_helper(comp, pns);
2551}
2552
Damiend99b0522013-12-21 18:17:45 +00002553void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002554 // if this is called then we are compiling a dict key:value pair
2555 compile_node(comp, pns->nodes[1]); // value
2556 compile_node(comp, pns->nodes[0]); // key
2557}
2558
Damiend99b0522013-12-21 18:17:45 +00002559void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002560 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002561 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002562 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002563}
2564
Damiend99b0522013-12-21 18:17:45 +00002565void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002566 if (comp->have_star_arg) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002567 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002568 return;
2569 }
2570 comp->have_star_arg = true;
2571 compile_node(comp, pns->nodes[0]);
2572}
2573
Damiend99b0522013-12-21 18:17:45 +00002574void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002575 if (comp->have_dbl_star_arg) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002576 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002577 return;
2578 }
2579 comp->have_dbl_star_arg = true;
2580 compile_node(comp, pns->nodes[0]);
2581}
2582
Damiend99b0522013-12-21 18:17:45 +00002583void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2584 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2585 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2586 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2587 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002588 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 +01002589 return;
2590 }
Damien Georgeb9791222014-01-23 00:34:21 +00002591 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002592 compile_node(comp, pns2->nodes[0]);
2593 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002594 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002595 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2596 } else {
2597 // shouldn't happen
2598 assert(0);
2599 }
2600}
2601
Damiend99b0522013-12-21 18:17:45 +00002602void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002603 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002604 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002605 return;
2606 }
Damiend99b0522013-12-21 18:17:45 +00002607 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002608 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002609 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002610 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2611 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002612 compile_node(comp, pns->nodes[0]);
2613 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002614 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002615 EMIT(yield_from);
2616 } else {
2617 compile_node(comp, pns->nodes[0]);
2618 EMIT(yield_value);
2619 }
2620}
2621
Damiend99b0522013-12-21 18:17:45 +00002622typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002623STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002624 NULL,
2625#define nc NULL
2626#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002627#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002628#include "grammar.h"
2629#undef nc
2630#undef c
2631#undef DEF_RULE
2632};
2633
Damiend99b0522013-12-21 18:17:45 +00002634void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2635 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002636 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002637 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2638 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2639 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002640 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002641 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002642 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002643 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002644 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2645 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2646 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2647 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002648 case MP_PARSE_NODE_TOKEN:
2649 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002650 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002651 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002652 // do nothing
2653 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002654 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002655 }
2656 break;
Damien429d7192013-10-04 19:53:11 +01002657 default: assert(0);
2658 }
2659 } else {
Damiend99b0522013-12-21 18:17:45 +00002660 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002661 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002662 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002663 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002664 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002665#if MICROPY_DEBUG_PRINTERS
2666 mp_parse_node_print(pn, 0);
2667#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002668 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002669 } else {
2670 f(comp, pns);
2671 }
2672 }
2673}
2674
Damiend99b0522013-12-21 18:17:45 +00002675void 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 +01002676 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002677 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002678 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2679 if (MP_PARSE_NODE_IS_ID(pn)) {
2680 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002681 if (comp->have_bare_star) {
2682 // comes after a bare star, so doesn't count as a parameter
2683 } else {
2684 comp->scope_cur->num_params += 1;
2685 }
Damienb14de212013-10-06 00:28:28 +01002686 } else {
Damiend99b0522013-12-21 18:17:45 +00002687 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2688 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2689 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2690 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002691 //int node_index = 1; unused
2692 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002693 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002694 // this parameter has an annotation
2695 pn_annotation = pns->nodes[1];
2696 }
2697 //node_index = 2; unused
2698 }
2699 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002700 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002701 // this parameter has a default value
2702 if (comp->have_bare_star) {
2703 comp->scope_cur->num_dict_params += 1;
2704 } else {
2705 comp->scope_cur->num_default_params += 1;
2706 }
2707 }
2708 */
2709 if (comp->have_bare_star) {
2710 // comes after a bare star, so doesn't count as a parameter
2711 } else {
2712 comp->scope_cur->num_params += 1;
2713 }
Damiend99b0522013-12-21 18:17:45 +00002714 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2715 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002716 // bare star
2717 // TODO see http://www.python.org/dev/peps/pep-3102/
2718 comp->have_bare_star = true;
2719 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002720 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002721 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002722 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002723 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2724 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002725 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002726 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002727 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2728 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002729 pn_annotation = pns->nodes[1];
2730 } else {
2731 // shouldn't happen
2732 assert(0);
2733 }
Damiend99b0522013-12-21 18:17:45 +00002734 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2735 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2736 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002737 // this parameter has an annotation
2738 pn_annotation = pns->nodes[1];
2739 }
Damien George8725f8f2014-02-15 19:33:11 +00002740 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002741 } else {
Damienb14de212013-10-06 00:28:28 +01002742 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002743 assert(0);
2744 }
Damien429d7192013-10-04 19:53:11 +01002745 }
2746
2747 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002748 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002749 // TODO this parameter has an annotation
2750 }
2751 bool added;
2752 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2753 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002754 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002755 return;
2756 }
2757 id_info->param = true;
2758 id_info->kind = ID_INFO_KIND_LOCAL;
2759 }
2760}
2761
Damiend99b0522013-12-21 18:17:45 +00002762void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002763 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2764}
2765
Damiend99b0522013-12-21 18:17:45 +00002766void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002767 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2768}
2769
Damiend99b0522013-12-21 18:17:45 +00002770void 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 +01002771 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002772 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002773 // no more nested if/for; compile inner expression
2774 compile_node(comp, pn_inner_expr);
2775 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002776 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002777 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002778 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002779 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002780 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002781 } else {
2782 EMIT(yield_value);
2783 EMIT(pop_top);
2784 }
Damiend99b0522013-12-21 18:17:45 +00002785 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002786 // if condition
Damiend99b0522013-12-21 18:17:45 +00002787 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002788 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2789 pn_iter = pns_comp_if->nodes[1];
2790 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002791 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002792 // for loop
Damiend99b0522013-12-21 18:17:45 +00002793 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002794 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002795 int l_end2 = comp_next_label(comp);
2796 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002797 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002798 EMIT_ARG(label_assign, l_top2);
2799 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002800 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2801 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 +00002802 EMIT_ARG(jump, l_top2);
2803 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002804 EMIT(for_iter_end);
2805 } else {
2806 // shouldn't happen
2807 assert(0);
2808 }
2809}
2810
Damiend99b0522013-12-21 18:17:45 +00002811void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002812 // see http://www.python.org/dev/peps/pep-0257/
2813
2814 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002815 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002816 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002817 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002818 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002819 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2820 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002821 for (int i = 0; i < num_nodes; i++) {
2822 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002823 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 +00002824 // not a newline, so this is the first statement; finish search
2825 break;
2826 }
2827 }
2828 // 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 +00002829 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002830 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002831 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002832 } else {
2833 return;
2834 }
2835
2836 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002837 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2838 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2839 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2840 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2841 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002842 compile_node(comp, pns->nodes[0]); // a doc string
2843 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002844 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002845 }
2846 }
2847 }
2848}
2849
2850void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2851 comp->pass = pass;
2852 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002853 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002854 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002855
2856 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002857 // reset maximum stack sizes in scope
2858 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002859 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002860 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002861 }
2862
Damien5ac1b2e2013-10-18 19:58:12 +01002863#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002864 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002865 scope_print_info(scope);
2866 }
Damien5ac1b2e2013-10-18 19:58:12 +01002867#endif
Damien429d7192013-10-04 19:53:11 +01002868
2869 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002870 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2871 assert(scope->kind == SCOPE_MODULE);
2872 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2873 compile_node(comp, pns->nodes[0]); // compile the expression
2874 EMIT(return_value);
2875 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002876 if (!comp->is_repl) {
2877 check_for_doc_string(comp, scope->pn);
2878 }
Damien429d7192013-10-04 19:53:11 +01002879 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002881 EMIT(return_value);
2882 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002883 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2884 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2885 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002886
2887 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002888 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002889 if (comp->pass == PASS_1) {
2890 comp->have_bare_star = false;
2891 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2892 }
2893
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002894 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002895
2896 compile_node(comp, pns->nodes[3]); // 3 is function body
2897 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002898 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002899 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002900 EMIT(return_value);
2901 }
2902 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002903 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2904 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2905 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002906
2907 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002908 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002909 if (comp->pass == PASS_1) {
2910 comp->have_bare_star = false;
2911 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2912 }
2913
2914 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2915 EMIT(return_value);
2916 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2917 // a bit of a hack at the moment
2918
Damiend99b0522013-12-21 18:17:45 +00002919 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2920 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2921 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2922 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2923 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002924
Damien George55baff42014-01-21 21:40:13 +00002925 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002926 if (comp->pass == PASS_1) {
2927 bool added;
2928 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2929 assert(added);
2930 id_info->kind = ID_INFO_KIND_LOCAL;
2931 scope->num_params = 1;
2932 }
2933
2934 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002936 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002937 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002938 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002939 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002940 }
2941
Damienb05d7072013-10-05 13:37:10 +01002942 int l_end = comp_next_label(comp);
2943 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(load_id, qstr_arg);
2945 EMIT_ARG(label_assign, l_top);
2946 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002947 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2948 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_ARG(jump, l_top);
2950 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002951 EMIT(for_iter_end);
2952
2953 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002954 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002955 }
2956 EMIT(return_value);
2957 } else {
2958 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002959 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2960 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2961 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002962
2963 if (comp->pass == PASS_1) {
2964 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002965 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002966 assert(added);
2967 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002968 }
2969
Damien Georgeb9791222014-01-23 00:34:21 +00002970 EMIT_ARG(load_id, MP_QSTR___name__);
2971 EMIT_ARG(store_id, MP_QSTR___module__);
2972 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2973 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002974
2975 check_for_doc_string(comp, pns->nodes[2]);
2976 compile_node(comp, pns->nodes[2]); // 2 is class body
2977
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002978 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002979 assert(id != NULL);
2980 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002981 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002982 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002983#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002984 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002985#else
Damien George35e2a4e2014-02-05 00:51:47 +00002986 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002987#endif
Damien429d7192013-10-04 19:53:11 +01002988 }
2989 EMIT(return_value);
2990 }
2991
Damien415eb6f2013-10-05 12:19:06 +01002992 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002993
2994 // make sure we match all the exception levels
2995 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002996}
2997
Damien George094d4502014-04-02 17:31:27 +01002998#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01002999void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3000 comp->pass = pass;
3001 comp->scope_cur = scope;
3002 comp->next_label = 1;
3003
3004 if (scope->kind != SCOPE_FUNCTION) {
3005 printf("Error: inline assembler must be a function\n");
3006 return;
3007 }
3008
Damiena2f2f7d2013-10-06 00:14:13 +01003009 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003010 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003011 }
3012
Damien826005c2013-10-05 23:17:28 +01003013 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003014 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3015 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3016 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003017
Damiend99b0522013-12-21 18:17:45 +00003018 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003019
Damiena2f2f7d2013-10-06 00:14:13 +01003020 // parameters are in pns->nodes[1]
3021 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003022 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003023 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003024 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003025 }
3026
Damiend99b0522013-12-21 18:17:45 +00003027 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003028
Damiend99b0522013-12-21 18:17:45 +00003029 mp_parse_node_t pn_body = pns->nodes[3]; // body
3030 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003031 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3032
Damien Georgecbd2f742014-01-19 11:48:48 +00003033 /*
Damien826005c2013-10-05 23:17:28 +01003034 if (comp->pass == PASS_3) {
3035 //printf("----\n");
3036 scope_print_info(scope);
3037 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003038 */
Damien826005c2013-10-05 23:17:28 +01003039
3040 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003041 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3042 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3043 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3044 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3045 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3046 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3047 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3048 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3049 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3050 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3051 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3052 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3053 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003054 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3055
3056 // emit instructions
3057 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003058 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003059 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003060 return;
3061 }
3062 int lab = comp_next_label(comp);
3063 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003064 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003065 }
3066 } else {
3067 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003069 }
3070 }
3071 }
3072
3073 if (comp->pass > PASS_1) {
3074 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003075 }
Damien429d7192013-10-04 19:53:11 +01003076}
Damien George094d4502014-04-02 17:31:27 +01003077#endif
Damien429d7192013-10-04 19:53:11 +01003078
3079void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3080 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003081 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003082 scope->num_locals = 0;
3083 for (int i = 0; i < scope->id_info_len; i++) {
3084 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003085 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003086 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3087 continue;
3088 }
3089 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3090 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3091 }
Damien9ecbcff2013-12-11 00:41:43 +00003092 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003093 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3094 id->local_num = scope->num_locals;
3095 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003096 }
3097 }
3098
3099 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003100#if MICROPY_EMIT_CPYTHON
3101 int num_cell = 0;
3102#endif
Damien9ecbcff2013-12-11 00:41:43 +00003103 for (int i = 0; i < scope->id_info_len; i++) {
3104 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003105#if MICROPY_EMIT_CPYTHON
3106 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003107 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003108 id->local_num = num_cell;
3109 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003110 }
Damien George6baf76e2013-12-30 22:32:17 +00003111#else
3112 // in Micro Python the cells come right after the fast locals
3113 // parameters are not counted here, since they remain at the start
3114 // of the locals, even if they are cell vars
3115 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3116 id->local_num = scope->num_locals;
3117 scope->num_locals += 1;
3118 }
3119#endif
Damien9ecbcff2013-12-11 00:41:43 +00003120 }
Damien9ecbcff2013-12-11 00:41:43 +00003121
3122 // compute the index of free vars (freevars[idx] in CPython)
3123 // make sure they are in the order of the parent scope
3124 if (scope->parent != NULL) {
3125 int num_free = 0;
3126 for (int i = 0; i < scope->parent->id_info_len; i++) {
3127 id_info_t *id = &scope->parent->id_info[i];
3128 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3129 for (int j = 0; j < scope->id_info_len; j++) {
3130 id_info_t *id2 = &scope->id_info[j];
3131 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003132 assert(!id2->param); // free vars should not be params
3133#if MICROPY_EMIT_CPYTHON
3134 // in CPython the frees are numbered after the cells
3135 id2->local_num = num_cell + num_free;
3136#else
3137 // in Micro Python the frees come first, before the params
3138 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003139#endif
3140 num_free += 1;
3141 }
3142 }
3143 }
Damien429d7192013-10-04 19:53:11 +01003144 }
Damien George6baf76e2013-12-30 22:32:17 +00003145#if !MICROPY_EMIT_CPYTHON
3146 // in Micro Python shift all other locals after the free locals
3147 if (num_free > 0) {
3148 for (int i = 0; i < scope->id_info_len; i++) {
3149 id_info_t *id = &scope->id_info[i];
3150 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3151 id->local_num += num_free;
3152 }
3153 }
3154 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3155 scope->num_locals += num_free;
3156 }
3157#endif
Damien429d7192013-10-04 19:53:11 +01003158 }
3159
Damien George8725f8f2014-02-15 19:33:11 +00003160 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003161
3162#if MICROPY_EMIT_CPYTHON
3163 // these flags computed here are for CPython compatibility only
3164 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003165 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003166 }
3167 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) {
3168 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003169 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003170
3171 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003172 // Note that we don't actually use this information at the moment (for CPython compat only)
3173 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 +00003174 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003175 }
3176 }
Damien George882b3632014-04-02 15:56:31 +01003177#endif
3178
Damien429d7192013-10-04 19:53:11 +01003179 int num_free = 0;
3180 for (int i = 0; i < scope->id_info_len; i++) {
3181 id_info_t *id = &scope->id_info[i];
3182 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3183 num_free += 1;
3184 }
3185 }
3186 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003187 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003188 }
3189}
3190
Damien George65cad122014-04-06 11:48:15 +01003191mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003192 compiler_t *comp = m_new(compiler_t, 1);
3193
Damien Georgecbd2f742014-01-19 11:48:48 +00003194 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003195 comp->is_repl = is_repl;
3196 comp->had_error = false;
3197
Damien429d7192013-10-04 19:53:11 +01003198 comp->break_label = 0;
3199 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003200 comp->break_continue_except_level = 0;
3201 comp->cur_except_level = 0;
3202
Damien George35e2a4e2014-02-05 00:51:47 +00003203 comp->func_arg_is_super = false;
3204
Damien429d7192013-10-04 19:53:11 +01003205 comp->scope_head = NULL;
3206 comp->scope_cur = NULL;
3207
Damien826005c2013-10-05 23:17:28 +01003208 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003209 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003210
3211 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003212 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003213
Damien826005c2013-10-05 23:17:28 +01003214 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003215 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003216 comp->emit_method_table = &emit_pass1_method_table;
3217 comp->emit_inline_asm = NULL;
3218 comp->emit_inline_asm_method_table = NULL;
3219 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003220 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003221 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003222#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003223 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003224 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003225#endif
Damien826005c2013-10-05 23:17:28 +01003226 } else {
3227 compile_scope(comp, s, PASS_1);
3228 }
3229
3230 // update maximim number of labels needed
3231 if (comp->next_label > max_num_labels) {
3232 max_num_labels = comp->next_label;
3233 }
Damien429d7192013-10-04 19:53:11 +01003234 }
3235
Damien826005c2013-10-05 23:17:28 +01003236 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003237 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003238 compile_scope_compute_things(comp, s);
3239 }
3240
Damien826005c2013-10-05 23:17:28 +01003241 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003242 emit_pass1_free(comp->emit);
3243
Damien826005c2013-10-05 23:17:28 +01003244 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003245#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003246 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003247#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003248 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003249#endif
Damien3ef4abb2013-10-12 16:53:13 +01003250#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003251 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003252#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003253#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003254 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003255 if (false) {
3256 // dummy
3257
Damien3ef4abb2013-10-12 16:53:13 +01003258#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003259 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003260 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003261 if (emit_inline_thumb == NULL) {
3262 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3263 }
3264 comp->emit = NULL;
3265 comp->emit_method_table = NULL;
3266 comp->emit_inline_asm = emit_inline_thumb;
3267 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3268 compile_scope_inline_asm(comp, s, PASS_2);
3269 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003270#endif
3271
Damien826005c2013-10-05 23:17:28 +01003272 } else {
Damienc025ebb2013-10-12 14:30:21 +01003273
3274 // choose the emit type
3275
Damien3ef4abb2013-10-12 16:53:13 +01003276#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003277 comp->emit = emit_cpython_new(max_num_labels);
3278 comp->emit_method_table = &emit_cpython_method_table;
3279#else
Damien826005c2013-10-05 23:17:28 +01003280 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003281
3282#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003283 case MP_EMIT_OPT_NATIVE_PYTHON:
3284 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003285#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003286 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003287 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003288 }
Damien13ed3a62013-10-08 09:05:10 +01003289 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003290#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003291 if (emit_native == NULL) {
3292 emit_native = emit_native_thumb_new(max_num_labels);
3293 }
3294 comp->emit_method_table = &emit_native_thumb_method_table;
3295#endif
3296 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003297 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003298 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003299#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003300
Damien826005c2013-10-05 23:17:28 +01003301 default:
3302 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003303 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003304 }
3305 comp->emit = emit_bc;
3306 comp->emit_method_table = &emit_bc_method_table;
3307 break;
3308 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003309#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003310
3311 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003312 compile_scope(comp, s, PASS_2);
3313 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003314 }
Damien429d7192013-10-04 19:53:11 +01003315 }
3316
Damien George41d02b62014-01-24 22:42:28 +00003317 // free the emitters
3318#if !MICROPY_EMIT_CPYTHON
3319 if (emit_bc != NULL) {
3320 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003321 }
Damien George41d02b62014-01-24 22:42:28 +00003322#if MICROPY_EMIT_NATIVE
3323 if (emit_native != NULL) {
3324#if MICROPY_EMIT_X64
3325 emit_native_x64_free(emit_native);
3326#elif MICROPY_EMIT_THUMB
3327 emit_native_thumb_free(emit_native);
3328#endif
3329 }
3330#endif
3331#if MICROPY_EMIT_INLINE_THUMB
3332 if (emit_inline_thumb != NULL) {
3333 emit_inline_thumb_free(emit_inline_thumb);
3334 }
3335#endif
3336#endif // !MICROPY_EMIT_CPYTHON
3337
3338 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003339 uint unique_code_id = module_scope->unique_code_id;
3340 for (scope_t *s = module_scope; s;) {
3341 scope_t *next = s->next;
3342 scope_free(s);
3343 s = next;
3344 }
Damien5ac1b2e2013-10-18 19:58:12 +01003345
Damien George41d02b62014-01-24 22:42:28 +00003346 // free the compiler
3347 bool had_error = comp->had_error;
3348 m_del_obj(compiler_t, comp);
3349
Damien George1fb03172014-01-03 14:22:03 +00003350 if (had_error) {
3351 // TODO return a proper error message
3352 return mp_const_none;
3353 } else {
3354#if MICROPY_EMIT_CPYTHON
3355 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003356 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003357 return mp_const_true;
3358#else
3359 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003360 // 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 +01003361 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003362#endif
3363 }
Damien429d7192013-10-04 19:53:11 +01003364}