blob: 3ea6dd3a6145003d860dc9b7cdb6a34cd1de676b [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
39typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000040 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010041 uint8_t is_repl;
42 uint8_t pass; // holds enum type pass_kind_t
43 uint8_t had_error; // try to keep compiler clean from nlr
44 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010045
Damienb05d7072013-10-05 13:37:10 +010046 int next_label;
Damienb05d7072013-10-05 13:37:10 +010047
Damien429d7192013-10-04 19:53:11 +010048 int break_label;
49 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000050 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010051 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010052
Damien George02a4c052014-04-09 12:50:58 +010053 uint16_t n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +010054 uint8_t star_flags;
Damien George02a4c052014-04-09 12:50:58 +010055 uint8_t have_bare_star;
56 uint8_t param_pass;
57 uint16_t param_pass_num_dict_params;
58 uint16_t param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +010059
60 scope_t *scope_head;
61 scope_t *scope_cur;
62
Damien6cdd3af2013-10-05 18:08:26 +010063 emit_t *emit; // current emitter
64 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010065
66 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
67 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010068} compiler_t;
69
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010070STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000071 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010072 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
73 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
74 } else {
75 printf(" File \"%s\"\n", qstr_str(comp->source_file));
76 }
Damien Georgef41fdd02014-03-03 23:19:11 +000077 printf("SyntaxError: %s\n", msg);
78 comp->had_error = true;
79}
80
Damiend99b0522013-12-21 18:17:45 +000081mp_parse_node_t fold_constants(mp_parse_node_t pn) {
82 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
83 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
84 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010085
86 // fold arguments first
87 for (int i = 0; i < n; i++) {
88 pns->nodes[i] = fold_constants(pns->nodes[i]);
89 }
90
Damiend99b0522013-12-21 18:17:45 +000091 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010092 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000093 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 +020094 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
95 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000096 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010097#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010098 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000099 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100100#endif
Damiend99b0522013-12-21 18:17:45 +0000101 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100103 } else {
104 // shouldn't happen
105 assert(0);
106 }
107 }
108 break;
109
110 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100111 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000112 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 +0200113 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
114 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000115 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000116 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000117 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000118 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100119 } else {
120 // shouldn't happen
121 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100122 }
Damien Georgeaf272592014-04-04 11:21:58 +0000123 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
124 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100125 }
126 }
127 break;
128
129 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000130 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgeaf272592014-04-04 11:21:58 +0000131 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
132 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000133 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000134 if (!mp_small_int_mul_overflow(arg0, arg1)) {
135 arg0 *= arg1;
136 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
137 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
138 }
139 }
Damiend99b0522013-12-21 18:17:45 +0000140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100141 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000143 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000144 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300145 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000146 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 +0300147 }
Damien429d7192013-10-04 19:53:11 +0100148 } else {
149 // shouldn't happen
150 assert(0);
151 }
152 }
153 break;
154
155 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000156 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200157 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000158 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
159 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
160 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
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_TILDE)) {
163 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100164 } else {
165 // shouldn't happen
166 assert(0);
167 }
168 }
169 break;
170
Damien3ef4abb2013-10-12 16:53:13 +0100171#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100172 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100173 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000174 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])) {
175 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
176 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200177 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100178 if (power >= 0) {
179 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200180 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100181 for (; power > 0; power--) {
182 ans *= base;
183 }
Damiend99b0522013-12-21 18:17:45 +0000184 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100185 }
186 }
187 }
188 break;
Damien0efb3a12013-10-12 16:16:56 +0100189#endif
Damien429d7192013-10-04 19:53:11 +0100190 }
191 }
192
193 return pn;
194}
195
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200196STATIC 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 +0000197STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100198
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200199STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100200 return comp->next_label++;
201}
202
Damien George8dcc0c72014-03-27 10:55:21 +0000203STATIC void compile_increase_except_level(compiler_t *comp) {
204 comp->cur_except_level += 1;
205 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
206 comp->scope_cur->exc_stack_size = comp->cur_except_level;
207 }
208}
209
210STATIC void compile_decrease_except_level(compiler_t *comp) {
211 assert(comp->cur_except_level > 0);
212 comp->cur_except_level -= 1;
213}
214
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200215STATIC 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 +0000216 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 +0100217 scope->parent = comp->scope_cur;
218 scope->next = NULL;
219 if (comp->scope_head == NULL) {
220 comp->scope_head = scope;
221 } else {
222 scope_t *s = comp->scope_head;
223 while (s->next != NULL) {
224 s = s->next;
225 }
226 s->next = scope;
227 }
228 return scope;
229}
230
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200231STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000232 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100233 return 0;
Damiend99b0522013-12-21 18:17:45 +0000234 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100235 return 1;
236 } else {
Damiend99b0522013-12-21 18:17:45 +0000237 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
238 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100239 return 1;
240 } else {
Damiend99b0522013-12-21 18:17:45 +0000241 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100242 }
243 }
244}
245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC 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 +0000247 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
249 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100250 for (int i = 0; i < num_nodes; i++) {
251 f(comp, pns->nodes[i]);
252 }
Damiend99b0522013-12-21 18:17:45 +0000253 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100254 f(comp, pn);
255 }
256}
257
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200258STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000259 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100260 *nodes = NULL;
261 return 0;
Damiend99b0522013-12-21 18:17:45 +0000262 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100263 *nodes = pn;
264 return 1;
265 } else {
Damiend99b0522013-12-21 18:17:45 +0000266 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
267 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100268 *nodes = pn;
269 return 1;
270 } else {
271 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000272 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100273 }
274 }
275}
276
Damiend99b0522013-12-21 18:17:45 +0000277void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100278}
279
Damiend99b0522013-12-21 18:17:45 +0000280void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100282 for (int i = 0; i < num_nodes; i++) {
283 compile_node(comp, pns->nodes[i]);
284 }
285}
286
Damien3ef4abb2013-10-12 16:53:13 +0100287#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200288STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000289 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100290 return false;
291 }
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 return false;
294 }
295 return true;
296}
297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000299 uint len;
300 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000301 bool has_single_quote = false;
302 bool has_double_quote = false;
303 for (int i = 0; i < len; i++) {
304 if (str[i] == '\'') {
305 has_single_quote = true;
306 } else if (str[i] == '"') {
307 has_double_quote = true;
308 }
309 }
310 if (bytes) {
311 vstr_printf(vstr, "b");
312 }
313 bool quote_single = false;
314 if (has_single_quote && !has_double_quote) {
315 vstr_printf(vstr, "\"");
316 } else {
317 quote_single = true;
318 vstr_printf(vstr, "'");
319 }
320 for (int i = 0; i < len; i++) {
321 if (str[i] == '\n') {
322 vstr_printf(vstr, "\\n");
323 } else if (str[i] == '\\') {
324 vstr_printf(vstr, "\\\\");
325 } else if (str[i] == '\'' && quote_single) {
326 vstr_printf(vstr, "\\'");
327 } else {
328 vstr_printf(vstr, "%c", str[i]);
329 }
330 }
331 if (has_single_quote && !has_double_quote) {
332 vstr_printf(vstr, "\"");
333 } else {
334 vstr_printf(vstr, "'");
335 }
336}
337
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200338STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000339 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200340 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
341 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
342 return;
343 }
344
Damiend99b0522013-12-21 18:17:45 +0000345 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
346 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
347 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000348 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
349 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
351 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
352 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100353 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000354 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
355 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
356 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100357 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100358 }
359 break;
360 default: assert(0);
361 }
362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC 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 +0100365 int n = 0;
366 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000367 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100368 }
369 int total = n;
370 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000371 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100372 total += 1;
Damien3a205172013-10-12 15:01:56 +0100373 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100374 is_const = false;
375 }
376 }
377 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100378 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100379 is_const = false;
380 break;
381 }
382 }
383 if (total > 0 && is_const) {
384 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000385 vstr_t *vstr = vstr_new();
386 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000387 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000388 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100389 need_comma = true;
390 }
391 for (int i = 0; i < n; i++) {
392 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000393 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100394 }
Damien02f89412013-12-12 15:13:36 +0000395 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100396 need_comma = true;
397 }
398 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000399 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100400 } else {
Damien02f89412013-12-12 15:13:36 +0000401 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100402 }
Damien Georgeb9791222014-01-23 00:34:21 +0000403 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000404 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100405 } else {
Damiend99b0522013-12-21 18:17:45 +0000406 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100407 compile_node(comp, pn);
408 }
409 for (int i = 0; i < n; i++) {
410 compile_node(comp, pns_list->nodes[i]);
411 }
Damien Georgeb9791222014-01-23 00:34:21 +0000412 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100413 }
414}
Damien3a205172013-10-12 15:01:56 +0100415#endif
416
417// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000418void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100419#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100420 cpython_c_tuple(comp, pn, pns_list);
421#else
422 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000423 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100424 compile_node(comp, pn);
425 total += 1;
426 }
427 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000428 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100429 for (int i = 0; i < n; i++) {
430 compile_node(comp, pns_list->nodes[i]);
431 }
432 total += n;
433 }
Damien Georgeb9791222014-01-23 00:34:21 +0000434 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100435#endif
436}
Damien429d7192013-10-04 19:53:11 +0100437
Damiend99b0522013-12-21 18:17:45 +0000438void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100439 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000440 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200443STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000444 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200445 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100446}
447
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200448STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200449 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 +0100450}
451
Damien3ef4abb2013-10-12 16:53:13 +0100452#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100453// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200454STATIC 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 +0100455 if (node_is_const_false(pn)) {
456 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000457 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100458 }
459 return;
460 } else if (node_is_const_true(pn)) {
461 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000462 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100463 }
464 return;
Damiend99b0522013-12-21 18:17:45 +0000465 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
466 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
467 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
468 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100469 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100470 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100471 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100472 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100473 }
Damien3a205172013-10-12 15:01:56 +0100474 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000475 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100476 } else {
477 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100478 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100479 }
480 }
481 return;
Damiend99b0522013-12-21 18:17:45 +0000482 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100483 if (jump_if == false) {
484 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100485 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100486 }
487 } else {
Damienb05d7072013-10-05 13:37:10 +0100488 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100489 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100490 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100491 }
Damien3a205172013-10-12 15:01:56 +0100492 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000493 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100494 }
495 return;
Damiend99b0522013-12-21 18:17:45 +0000496 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100497 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100498 return;
499 }
500 }
501
502 // nothing special, fall back to default compiling for node and jump
503 compile_node(comp, pn);
504 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000505 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100506 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000507 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100508 }
509}
Damien3a205172013-10-12 15:01:56 +0100510#endif
Damien429d7192013-10-04 19:53:11 +0100511
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200512STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100513#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100514 cpython_c_if_cond(comp, pn, jump_if, label, false);
515#else
516 if (node_is_const_false(pn)) {
517 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000518 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100519 }
520 return;
521 } else if (node_is_const_true(pn)) {
522 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000523 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100524 }
525 return;
Damiend99b0522013-12-21 18:17:45 +0000526 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
527 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
528 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
529 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100530 if (jump_if == false) {
531 int label2 = comp_next_label(comp);
532 for (int i = 0; i < n - 1; i++) {
533 c_if_cond(comp, pns->nodes[i], true, label2);
534 }
535 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000536 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100537 } else {
538 for (int i = 0; i < n; i++) {
539 c_if_cond(comp, pns->nodes[i], true, label);
540 }
541 }
542 return;
Damiend99b0522013-12-21 18:17:45 +0000543 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100544 if (jump_if == false) {
545 for (int i = 0; i < n; i++) {
546 c_if_cond(comp, pns->nodes[i], false, label);
547 }
548 } else {
549 int label2 = comp_next_label(comp);
550 for (int i = 0; i < n - 1; i++) {
551 c_if_cond(comp, pns->nodes[i], false, label2);
552 }
553 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000554 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100555 }
556 return;
Damiend99b0522013-12-21 18:17:45 +0000557 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100558 c_if_cond(comp, pns->nodes[0], !jump_if, label);
559 return;
560 }
561 }
562
563 // nothing special, fall back to default compiling for node and jump
564 compile_node(comp, pn);
565 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000566 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100567 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100569 }
570#endif
Damien429d7192013-10-04 19:53:11 +0100571}
572
573typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000574void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100575
Damiend99b0522013-12-21 18:17:45 +0000576void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100577 if (assign_kind != ASSIGN_AUG_STORE) {
578 compile_node(comp, pns->nodes[0]);
579 }
580
Damiend99b0522013-12-21 18:17:45 +0000581 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
582 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
583 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
584 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100585 if (assign_kind != ASSIGN_AUG_STORE) {
586 for (int i = 0; i < n - 1; i++) {
587 compile_node(comp, pns1->nodes[i]);
588 }
589 }
Damiend99b0522013-12-21 18:17:45 +0000590 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
591 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100592 }
Damiend99b0522013-12-21 18:17:45 +0000593 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100594 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000595 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100596 if (assign_kind == ASSIGN_AUG_STORE) {
597 EMIT(rot_three);
598 EMIT(store_subscr);
599 } else {
600 compile_node(comp, pns1->nodes[0]);
601 if (assign_kind == ASSIGN_AUG_LOAD) {
602 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100603 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100604 } else {
605 EMIT(store_subscr);
606 }
607 }
Damiend99b0522013-12-21 18:17:45 +0000608 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
609 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100610 if (assign_kind == ASSIGN_AUG_LOAD) {
611 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000612 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100613 } else {
614 if (assign_kind == ASSIGN_AUG_STORE) {
615 EMIT(rot_two);
616 }
Damien Georgeb9791222014-01-23 00:34:21 +0000617 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100618 }
619 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100620 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100621 }
622 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100623 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100624 }
625
Damiend99b0522013-12-21 18:17:45 +0000626 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100627 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100628 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100629
630 return;
631
632cannot_assign:
633 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100634}
635
Damiend99b0522013-12-21 18:17:45 +0000636void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(n >= 0);
638 int have_star_index = -1;
639 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000640 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100643 have_star_index = i;
644 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100645 compile_syntax_error(comp, nodes[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100646 return;
647 }
648 }
649 }
650 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 for (int i = 0; i < n; i++) {
654 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000655 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100656 } else {
657 c_assign(comp, nodes[i], ASSIGN_STORE);
658 }
659 }
660}
661
662// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000663void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100664 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000665 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100666 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000667 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
668 if (MP_PARSE_NODE_IS_ID(pn)) {
669 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100670 switch (assign_kind) {
671 case ASSIGN_STORE:
672 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100674 break;
675 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 }
679 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100680 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100681 return;
682 }
683 } else {
Damiend99b0522013-12-21 18:17:45 +0000684 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
685 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100686 case PN_power:
687 // lhs is an index or attribute
688 c_assign_power(comp, pns, assign_kind);
689 break;
690
691 case PN_testlist_star_expr:
692 case PN_exprlist:
693 // lhs is a tuple
694 if (assign_kind != ASSIGN_STORE) {
695 goto bad_aug;
696 }
Damiend99b0522013-12-21 18:17:45 +0000697 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100698 break;
699
700 case PN_atom_paren:
701 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100703 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100704 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // parenthesis around 1 item, is just that item
711 pn = pns->nodes[0];
712 goto tail_recursion;
713 }
714 break;
715
716 case PN_atom_bracket:
717 // lhs is something in brackets
718 if (assign_kind != ASSIGN_STORE) {
719 goto bad_aug;
720 }
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100722 // empty list, assignment allowed
723 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
725 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100726 goto testlist_comp;
727 } else {
728 // brackets around 1 item
729 c_assign_tuple(comp, 1, &pns->nodes[0]);
730 }
731 break;
732
733 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100734 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
735 return;
Damien429d7192013-10-04 19:53:11 +0100736 }
737 return;
738
739 testlist_comp:
740 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000741 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
742 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
743 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100746 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100748 // sequence of many items
749 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000750 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100752 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
753 for (int i = 0; i < n; i++) {
754 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
755 }
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100757 // TODO can we ever get here? can it be compiled?
758 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
759 return;
Damien429d7192013-10-04 19:53:11 +0100760 } else {
761 // sequence with 2 items
762 goto sequence_with_2_items;
763 }
764 } else {
765 // sequence with 2 items
766 sequence_with_2_items:
767 c_assign_tuple(comp, 2, pns->nodes);
768 }
769 return;
770 }
771 return;
772
773 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100774 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100775}
776
777// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100778// if we are not in CPython compatibility mode then:
779// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
780// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
781// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100782void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
783 assert(n_pos_defaults >= 0);
784 assert(n_kw_defaults >= 0);
785
Damien429d7192013-10-04 19:53:11 +0100786 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000787 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100788 int nfree = 0;
789 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000790 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
791 id_info_t *id = &comp->scope_cur->id_info[i];
792 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
793 for (int j = 0; j < this_scope->id_info_len; j++) {
794 id_info_t *id2 = &this_scope->id_info[j];
795 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000796#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000797 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000798#else
799 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100800 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000801#endif
Damien318aec62013-12-10 18:28:17 +0000802 nfree += 1;
803 }
804 }
Damien429d7192013-10-04 19:53:11 +0100805 }
806 }
807 }
Damien429d7192013-10-04 19:53:11 +0100808
809 // make the function/closure
810 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100811 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100812 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000813 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100814 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100815 }
816}
817
Damiend99b0522013-12-21 18:17:45 +0000818void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000819 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000820 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
821 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100822 // bare star
823 comp->have_bare_star = true;
824 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000825
826 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
827 // TODO do we need to do anything with this?
828
829 } else {
830 mp_parse_node_t pn_id;
831 mp_parse_node_t pn_colon;
832 mp_parse_node_t pn_equal;
833 if (MP_PARSE_NODE_IS_ID(pn)) {
834 // this parameter is just an id
835
836 pn_id = pn;
837 pn_colon = MP_PARSE_NODE_NULL;
838 pn_equal = MP_PARSE_NODE_NULL;
839
840 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
841 // this parameter has a colon and/or equal specifier
842
843 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
844 pn_id = pns->nodes[0];
845 pn_colon = pns->nodes[1];
846 pn_equal = pns->nodes[2];
847
848 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100849 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000850 assert(0);
851 return;
852 }
853
854 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
855 // this parameter does not have a default value
856
857 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
858 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100859 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000860 return;
861 }
862
863 } else {
864 // this parameter has a default value
865 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
866
867 if (comp->have_bare_star) {
868 comp->param_pass_num_dict_params += 1;
869 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100870#if !MICROPY_EMIT_CPYTHON
871 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
872 if (comp->param_pass_num_dict_params == 1) {
873 // first default dict param, so make the map
874 EMIT_ARG(build_map, 0);
875 }
876#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000877 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
878 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100879#if !MICROPY_EMIT_CPYTHON
880 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
881 EMIT(store_map);
882#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000883 }
884 } else {
885 comp->param_pass_num_default_params += 1;
886 if (comp->param_pass == 2) {
887 compile_node(comp, pn_equal);
888 }
889 }
890 }
891
892 // TODO pn_colon not implemented
893 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100894 }
895}
896
897// leaves function object on stack
898// returns function name
Damiend99b0522013-12-21 18:17:45 +0000899qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100900 if (comp->pass == PASS_1) {
901 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000902 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100903 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000904 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100905 }
906
907 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George02a4c052014-04-09 12:50:58 +0100908 uint old_have_bare_star = comp->have_bare_star;
909 uint old_param_pass = comp->param_pass;
910 uint old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
911 uint old_param_pass_num_default_params = comp->param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100912
913 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000914
915 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100916 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000917 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100918 comp->param_pass_num_dict_params = 0;
919 comp->param_pass_num_default_params = 0;
920 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000921
922 if (comp->had_error) {
923 return MP_QSTR_NULL;
924 }
925
926 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100927 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000928 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100929 comp->param_pass_num_dict_params = 0;
930 comp->param_pass_num_default_params = 0;
931 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
932
Damien Georgee337f1e2014-03-31 15:18:37 +0100933#if !MICROPY_EMIT_CPYTHON
934 // in Micro Python we put the default positional parameters into a tuple using the bytecode
935 if (comp->param_pass_num_default_params > 0) {
936 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
937 }
938#endif
939
Damien429d7192013-10-04 19:53:11 +0100940 // get the scope for this function
941 scope_t *fscope = (scope_t*)pns->nodes[4];
942
943 // make the function
Damien George30565092014-03-31 11:30:17 +0100944 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100945
946 // restore variables
947 comp->have_bare_star = old_have_bare_star;
948 comp->param_pass = old_param_pass;
949 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
950 comp->param_pass_num_default_params = old_param_pass_num_default_params;
951
952 // return its name (the 'f' in "def f(...):")
953 return fscope->simple_name;
954}
955
956// leaves class object on stack
957// returns class name
Damiend99b0522013-12-21 18:17:45 +0000958qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100959 if (comp->pass == PASS_1) {
960 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000961 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100962 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000963 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100964 }
965
966 EMIT(load_build_class);
967
968 // scope for this class
969 scope_t *cscope = (scope_t*)pns->nodes[3];
970
971 // compile the class
972 close_over_variables_etc(comp, cscope, 0, 0);
973
974 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000975 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100976
977 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100978 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
979 mp_parse_node_t parents = pns->nodes[1];
980 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
981 parents = MP_PARSE_NODE_NULL;
982 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000983 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100984 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100985
986 // return its name (the 'C' in class C(...):")
987 return cscope->simple_name;
988}
989
Damien6cdd3af2013-10-05 18:08:26 +0100990// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200991STATIC 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 +0000992 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100993 return false;
994 }
995
996 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100997 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100998 return true;
999 }
1000
Damiend99b0522013-12-21 18:17:45 +00001001 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001002 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001003 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001004#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001005 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001006 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001007 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001008 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001009#endif
Damien3ef4abb2013-10-12 16:53:13 +01001010#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001011 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001012 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001013#endif
Damien6cdd3af2013-10-05 18:08:26 +01001014 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001015 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001016 }
1017
1018 return true;
1019}
1020
Damiend99b0522013-12-21 18:17:45 +00001021void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001022 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001023 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001024 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1025
Damien6cdd3af2013-10-05 18:08:26 +01001026 // inherit emit options for this function/class definition
1027 uint emit_options = comp->scope_cur->emit_options;
1028
1029 // compile each decorator
1030 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001031 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001032 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1033 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001034
1035 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001036 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001037 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1038
1039 // check for built-in decorators
1040 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1041 // this was a built-in
1042 num_built_in_decorators += 1;
1043
1044 } else {
1045 // not a built-in, compile normally
1046
1047 // compile the decorator function
1048 compile_node(comp, name_nodes[0]);
1049 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001050 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001051 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001052 }
1053
1054 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001055 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001056 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001057 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001058 compile_node(comp, pns_decorator->nodes[1]);
1059 }
Damien429d7192013-10-04 19:53:11 +01001060 }
1061 }
1062
1063 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001064 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001065 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001066 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001067 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001069 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001070 } else {
1071 // shouldn't happen
1072 assert(0);
1073 }
1074
1075 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001076 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001077 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001078 }
1079
1080 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001081 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001082}
1083
Damiend99b0522013-12-21 18:17:45 +00001084void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001085 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001086 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001087 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001088}
1089
Damiend99b0522013-12-21 18:17:45 +00001090void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1091 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001092 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001093 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1094 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001095
1096 compile_node(comp, pns->nodes[0]); // base of the power node
1097
Damiend99b0522013-12-21 18:17:45 +00001098 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1099 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1100 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1101 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001102 for (int i = 0; i < n - 1; i++) {
1103 compile_node(comp, pns1->nodes[i]);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1106 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001107 }
Damiend99b0522013-12-21 18:17:45 +00001108 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001109 // can't delete function calls
1110 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001111 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001112 compile_node(comp, pns1->nodes[0]);
1113 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001114 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1115 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001116 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001117 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001118 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001119 }
1120 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001121 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001122 }
1123
Damiend99b0522013-12-21 18:17:45 +00001124 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001125 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001126 }
Damiend99b0522013-12-21 18:17:45 +00001127 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1128 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1129 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1130 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001131 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1132
Damiend99b0522013-12-21 18:17:45 +00001133 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1134 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1135 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001136 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001137 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001138 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001139 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001140 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001141 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001142 c_del_stmt(comp, pns->nodes[0]);
1143 for (int i = 0; i < n; i++) {
1144 c_del_stmt(comp, pns1->nodes[i]);
1145 }
Damiend99b0522013-12-21 18:17:45 +00001146 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001147 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001148 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001149 } else {
1150 // sequence with 2 items
1151 goto sequence_with_2_items;
1152 }
1153 } else {
1154 // sequence with 2 items
1155 sequence_with_2_items:
1156 c_del_stmt(comp, pns->nodes[0]);
1157 c_del_stmt(comp, pns->nodes[1]);
1158 }
1159 } else {
1160 // tuple with 1 element
1161 c_del_stmt(comp, pn);
1162 }
1163 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001164 // TODO is there anything else to implement?
1165 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001166 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001167
1168 return;
1169
1170cannot_delete:
1171 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001172}
1173
Damiend99b0522013-12-21 18:17:45 +00001174void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001175 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1176}
1177
Damiend99b0522013-12-21 18:17:45 +00001178void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001179 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001180 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001181 }
Damien Georgecbddb272014-02-01 20:08:18 +00001182 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001183}
1184
Damiend99b0522013-12-21 18:17:45 +00001185void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001186 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001187 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001188 }
Damien Georgecbddb272014-02-01 20:08:18 +00001189 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001190}
1191
Damiend99b0522013-12-21 18:17:45 +00001192void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001193 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001194 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001195 return;
1196 }
Damiend99b0522013-12-21 18:17:45 +00001197 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001198 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001199 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001200 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001201 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001202 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1203 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 +01001204
Damienb05d7072013-10-05 13:37:10 +01001205 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001206 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1207 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1208 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001209 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001210 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1211 } else {
1212 compile_node(comp, pns->nodes[0]);
1213 }
1214 EMIT(return_value);
1215}
1216
Damiend99b0522013-12-21 18:17:45 +00001217void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001218 compile_node(comp, pns->nodes[0]);
1219 EMIT(pop_top);
1220}
1221
Damiend99b0522013-12-21 18:17:45 +00001222void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1223 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001224 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001225 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001226 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001227 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001228 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001229 compile_node(comp, pns->nodes[0]);
1230 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001231 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001232 } else {
1233 // raise x
1234 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001235 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001236 }
1237}
1238
1239// q1 holds the base, q2 the full name
1240// eg a -> q1=q2=a
1241// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001242void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001243 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001244 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1245 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001246 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001247 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001248 pn = pns->nodes[0];
1249 is_as = true;
1250 }
Damiend99b0522013-12-21 18:17:45 +00001251 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001252 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001253 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001254 if (!is_as) {
1255 *q1 = *q2;
1256 }
Damien Georgeb9791222014-01-23 00:34:21 +00001257 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001258 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1259 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1260 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001261 // a name of the form a.b.c
1262 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001263 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001264 }
Damiend99b0522013-12-21 18:17:45 +00001265 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001266 int len = n - 1;
1267 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001268 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001269 }
Damien George55baff42014-01-21 21:40:13 +00001270 byte *q_ptr;
1271 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001272 for (int i = 0; i < n; i++) {
1273 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001274 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001275 }
Damien George55baff42014-01-21 21:40:13 +00001276 uint str_src_len;
1277 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001278 memcpy(str_dest, str_src, str_src_len);
1279 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001280 }
Damien George55baff42014-01-21 21:40:13 +00001281 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001282 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001283 if (is_as) {
1284 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001285 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001286 }
1287 }
1288 } else {
1289 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001290 // This covers relative imports starting with dot(s) like "from .foo import"
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001291 compile_syntax_error(comp, pn, "Relative imports not implemented");
1292 return;
Damien429d7192013-10-04 19:53:11 +01001293 }
1294 } else {
1295 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001296 // This covers relative imports with dots only like "from .. import"
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001297 compile_syntax_error(comp, pn, "Relative imports not implemented");
1298 return;
Damien429d7192013-10-04 19:53:11 +01001299 }
1300}
1301
Damiend99b0522013-12-21 18:17:45 +00001302void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001303 EMIT_ARG(load_const_small_int, 0); // ??
1304 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001305 qstr q1, q2;
1306 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001307 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001308}
1309
Damiend99b0522013-12-21 18:17:45 +00001310void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001311 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1312}
1313
Damiend99b0522013-12-21 18:17:45 +00001314void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1315 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001316 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001317
1318 // build the "fromlist" tuple
1319#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001320 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001321#else
Damien Georgeb9791222014-01-23 00:34:21 +00001322 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1323 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001324#endif
1325
1326 // do the import
Damien429d7192013-10-04 19:53:11 +01001327 qstr dummy_q, id1;
1328 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1329 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001330
Damien429d7192013-10-04 19:53:11 +01001331 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001332 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001333
1334 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001335 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001336 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001337#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001338 {
1339 vstr_t *vstr = vstr_new();
1340 vstr_printf(vstr, "(");
1341 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001342 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1343 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1344 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001345 if (i > 0) {
1346 vstr_printf(vstr, ", ");
1347 }
1348 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001349 uint len;
1350 const byte *str = qstr_data(id2, &len);
1351 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001352 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001353 }
Damien02f89412013-12-12 15:13:36 +00001354 if (n == 1) {
1355 vstr_printf(vstr, ",");
1356 }
1357 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001358 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001359 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001360 }
Damiendb4c3612013-12-10 17:27:24 +00001361#else
1362 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001363 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1364 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1365 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001367 }
Damien Georgeb9791222014-01-23 00:34:21 +00001368 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001369#endif
1370
1371 // do the import
Damien429d7192013-10-04 19:53:11 +01001372 qstr dummy_q, id1;
1373 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1374 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001375 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1376 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1377 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001379 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001380 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001381 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001382 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001383 }
1384 }
1385 EMIT(pop_top);
1386 }
1387}
1388
Damiend99b0522013-12-21 18:17:45 +00001389void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001390 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001391 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1392 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001393 } else {
Damiend99b0522013-12-21 18:17:45 +00001394 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1395 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001396 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001397 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001398 }
Damien429d7192013-10-04 19:53:11 +01001399 }
1400 }
1401}
1402
Damiend99b0522013-12-21 18:17:45 +00001403void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001404 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001405 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1406 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001407 } else {
Damiend99b0522013-12-21 18:17:45 +00001408 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1409 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001410 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001411 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001412 }
Damien429d7192013-10-04 19:53:11 +01001413 }
1414 }
1415}
1416
Damiend99b0522013-12-21 18:17:45 +00001417void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001418 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001419 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001420 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 +00001421 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001422 // assertion message
1423 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001424 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001425 }
Damien Georgeb9791222014-01-23 00:34:21 +00001426 EMIT_ARG(raise_varargs, 1);
1427 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001428}
1429
Damiend99b0522013-12-21 18:17:45 +00001430void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001431 // TODO proper and/or short circuiting
1432
Damienb05d7072013-10-05 13:37:10 +01001433 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001434
Damienb05d7072013-10-05 13:37:10 +01001435 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001436 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1437
1438 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001439
1440 if (
1441#if !MICROPY_EMIT_CPYTHON
1442 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1443 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1444#endif
1445 // optimisation to not jump if last instruction was return
1446 !EMIT(last_emit_was_return_value)
1447 ) {
1448 // jump over elif/else blocks
1449 EMIT_ARG(jump, l_end);
1450 }
1451
Damien Georgeb9791222014-01-23 00:34:21 +00001452 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001453
Damiend99b0522013-12-21 18:17:45 +00001454 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001455 // compile elif blocks
1456
Damiend99b0522013-12-21 18:17:45 +00001457 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001458
Damiend99b0522013-12-21 18:17:45 +00001459 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001460 // multiple elif blocks
1461
Damiend99b0522013-12-21 18:17:45 +00001462 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001463 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001464 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001465 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001466 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1467
1468 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001469 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001470 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001471 }
Damien Georgeb9791222014-01-23 00:34:21 +00001472 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001473 }
1474
1475 } else {
1476 // a single elif block
1477
Damienb05d7072013-10-05 13:37:10 +01001478 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001479 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1480
1481 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001482 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001484 }
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001486 }
1487 }
1488
1489 // compile else block
1490 compile_node(comp, pns->nodes[3]); // can be null
1491
Damien Georgeb9791222014-01-23 00:34:21 +00001492 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001493}
1494
Damien Georgecbddb272014-02-01 20:08:18 +00001495#define START_BREAK_CONTINUE_BLOCK \
1496 int old_break_label = comp->break_label; \
1497 int old_continue_label = comp->continue_label; \
1498 int break_label = comp_next_label(comp); \
1499 int continue_label = comp_next_label(comp); \
1500 comp->break_label = break_label; \
1501 comp->continue_label = continue_label; \
1502 comp->break_continue_except_level = comp->cur_except_level;
1503
1504#define END_BREAK_CONTINUE_BLOCK \
1505 comp->break_label = old_break_label; \
1506 comp->continue_label = old_continue_label; \
1507 comp->break_continue_except_level = comp->cur_except_level;
1508
Damiend99b0522013-12-21 18:17:45 +00001509void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001510 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001511
Damience89a212013-10-15 22:25:17 +01001512 // compared to CPython, we have an optimised version of while loops
1513#if MICROPY_EMIT_CPYTHON
1514 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001515 EMIT_ARG(setup_loop, break_label);
1516 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001517 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1518 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001519 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001520 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001521 }
Damien Georgeb9791222014-01-23 00:34:21 +00001522 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001523 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1524 // this is a small hack to agree with CPython
1525 if (!node_is_const_true(pns->nodes[0])) {
1526 EMIT(pop_block);
1527 }
Damience89a212013-10-15 22:25:17 +01001528#else
1529 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001530 EMIT_ARG(jump, continue_label);
1531 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001532 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001533 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001534 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1535#endif
1536
1537 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001538 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001539
1540 compile_node(comp, pns->nodes[2]); // else
1541
Damien Georgeb9791222014-01-23 00:34:21 +00001542 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001543}
1544
Damienf72fd0e2013-11-06 20:20:49 +00001545// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001546// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1547// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001548void 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 +00001549 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001550
1551 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001552 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001553
Damien George3ff2d032014-03-31 18:02:22 +01001554 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001555 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001556 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001557
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(jump, entry_label);
1559 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001560
Damien George3ff2d032014-03-31 18:02:22 +01001561 // at this point we actually have 1 less element on the stack
1562 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1563
1564 // store next value to var
1565 c_assign(comp, pn_var, ASSIGN_STORE);
1566
Damienf3822fc2013-11-09 20:12:03 +00001567 // compile body
1568 compile_node(comp, pn_body);
1569
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001571
Damien George3ff2d032014-03-31 18:02:22 +01001572 // compile: var + step, duplicated on stack
1573 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001574 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001575 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001576 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001577
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001579
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001580 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001581 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001582 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1583 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001584 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001585 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001586 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001587 }
Damien Georgeb9791222014-01-23 00:34:21 +00001588 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001589
Damien George3ff2d032014-03-31 18:02:22 +01001590 // discard final value of var that failed the loop condition
1591 EMIT(pop_top);
1592
Damienf72fd0e2013-11-06 20:20:49 +00001593 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001594 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001595
1596 compile_node(comp, pn_else);
1597
Damien Georgeb9791222014-01-23 00:34:21 +00001598 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001599}
1600
Damiend99b0522013-12-21 18:17:45 +00001601void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001602#if !MICROPY_EMIT_CPYTHON
1603 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1604 // this is actually slower, but uses no heap memory
1605 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001606 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 +00001607 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001608 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1609 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1610 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1611 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001612 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1613 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001614 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001615 mp_parse_node_t pn_range_start;
1616 mp_parse_node_t pn_range_end;
1617 mp_parse_node_t pn_range_step;
1618 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001619 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001620 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001621 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001622 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001623 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001624 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001625 } else if (n_args == 2) {
1626 pn_range_start = args[0];
1627 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001628 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001629 } else {
1630 pn_range_start = args[0];
1631 pn_range_end = args[1];
1632 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001633 // We need to know sign of step. This is possible only if it's constant
1634 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1635 optimize = false;
1636 }
Damienf72fd0e2013-11-06 20:20:49 +00001637 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001638 }
1639 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001640 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1641 return;
1642 }
1643 }
1644 }
1645#endif
1646
Damien Georgecbddb272014-02-01 20:08:18 +00001647 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001648
Damienb05d7072013-10-05 13:37:10 +01001649 int pop_label = comp_next_label(comp);
1650 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001651
Damience89a212013-10-15 22:25:17 +01001652 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1653#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001655#endif
1656
Damien429d7192013-10-04 19:53:11 +01001657 compile_node(comp, pns->nodes[1]); // iterator
1658 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001659 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001660 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001661 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1662 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001663 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001664 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001665 }
Damien Georgeb9791222014-01-23 00:34:21 +00001666 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001667 EMIT(for_iter_end);
1668
1669 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001670 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001671
Damience89a212013-10-15 22:25:17 +01001672#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001673 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001674#endif
Damien429d7192013-10-04 19:53:11 +01001675
1676 compile_node(comp, pns->nodes[3]); // else (not tested)
1677
Damien Georgeb9791222014-01-23 00:34:21 +00001678 EMIT_ARG(label_assign, break_label);
1679 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001680}
1681
Damiend99b0522013-12-21 18:17:45 +00001682void 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 +01001683 // this function is a bit of a hack at the moment
1684 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1685
1686 // setup code
1687 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001688 int l1 = comp_next_label(comp);
1689 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001690
Damien Georgeb9791222014-01-23 00:34:21 +00001691 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001692 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001693
Damien429d7192013-10-04 19:53:11 +01001694 compile_node(comp, pn_body); // body
1695 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001696 EMIT_ARG(jump, success_label);
1697 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001698 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001699
1700 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001701 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1702 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001703
1704 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001705 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001706
Damiend99b0522013-12-21 18:17:45 +00001707 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001708 // this is a catch all exception handler
1709 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001710 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001711 return;
1712 }
1713 } else {
1714 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001715 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1716 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1717 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1718 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001719 // handler binds the exception to a local
1720 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001721 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001722 }
1723 }
1724 EMIT(dup_top);
1725 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001726 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001728 }
1729
1730 EMIT(pop_top);
1731
1732 if (qstr_exception_local == 0) {
1733 EMIT(pop_top);
1734 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001736 }
1737
1738 EMIT(pop_top);
1739
Damiene2880aa2013-12-20 14:22:59 +00001740 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001741 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001742 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001744 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001745 }
1746 compile_node(comp, pns_except->nodes[1]);
1747 if (qstr_exception_local != 0) {
1748 EMIT(pop_block);
1749 }
1750 EMIT(pop_except);
1751 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001752 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1753 EMIT_ARG(label_assign, l3);
1754 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1755 EMIT_ARG(store_id, qstr_exception_local);
1756 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001757
Damien George8dcc0c72014-03-27 10:55:21 +00001758 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001759 EMIT(end_finally);
1760 }
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(jump, l2);
1762 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001763 }
1764
Damien George8dcc0c72014-03-27 10:55:21 +00001765 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001766 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001767
Damien Georgeb9791222014-01-23 00:34:21 +00001768 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001769 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(label_assign, l2);
1771 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001772}
1773
Damiend99b0522013-12-21 18:17:45 +00001774void 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 +01001775 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1776 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001777 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001778
Damien Georgeb9791222014-01-23 00:34:21 +00001779 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001780 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001781
Damien429d7192013-10-04 19:53:11 +01001782 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001783 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001784 compile_node(comp, pn_body);
1785 } else {
1786 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1787 }
1788 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1790 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001791 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001792
Damien George8dcc0c72014-03-27 10:55:21 +00001793 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001794 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001795
Damien Georgeb9791222014-01-23 00:34:21 +00001796 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001797}
1798
Damiend99b0522013-12-21 18:17:45 +00001799void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1800 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1801 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1802 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001803 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001804 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1805 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001806 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001807 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001808 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001809 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001810 // no finally
1811 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1812 } else {
1813 // have finally
Damiend99b0522013-12-21 18:17:45 +00001814 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 +01001815 }
1816 } else {
1817 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001818 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001819 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001820 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001821 }
1822 } else {
1823 // shouldn't happen
1824 assert(0);
1825 }
1826}
1827
Damiend99b0522013-12-21 18:17:45 +00001828void 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 +01001829 if (n == 0) {
1830 // no more pre-bits, compile the body of the with
1831 compile_node(comp, body);
1832 } else {
Damienb05d7072013-10-05 13:37:10 +01001833 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001834 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001835 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001836 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001837 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001839 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1840 } else {
1841 // this pre-bit is just an expression
1842 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001843 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001844 EMIT(pop_top);
1845 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001846 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001847 // compile additional pre-bits and the body
1848 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1849 // finish this with block
1850 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001851 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1852 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001853 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001854 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001855 EMIT(end_finally);
1856 }
1857}
1858
Damiend99b0522013-12-21 18:17:45 +00001859void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001860 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001861 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001862 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1863 assert(n > 0);
1864
1865 // compile in a nested fashion
1866 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1867}
1868
Damiend99b0522013-12-21 18:17:45 +00001869void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1870 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001871 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1872 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001873 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001874 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001875 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001876 EMIT(pop_top);
1877
Damien429d7192013-10-04 19:53:11 +01001878 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001879 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001880 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001881 // do nothing with a lonely constant
1882 } else {
1883 compile_node(comp, pns->nodes[0]); // just an expression
1884 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1885 }
Damien429d7192013-10-04 19:53:11 +01001886 }
1887 } else {
Damiend99b0522013-12-21 18:17:45 +00001888 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1889 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001890 if (kind == PN_expr_stmt_augassign) {
1891 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1892 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001893 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001894 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001895 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001896 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1897 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1898 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1899 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1900 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1901 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1902 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1903 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1904 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1905 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1906 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1907 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1908 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001909 }
Damien George7e5fb242014-02-01 22:18:47 +00001910 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001911 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1912 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001913 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1914 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001915 // following CPython, we store left-most first
1916 if (rhs > 0) {
1917 EMIT(dup_top);
1918 }
1919 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1920 for (int i = 0; i < rhs; i++) {
1921 if (i + 1 < rhs) {
1922 EMIT(dup_top);
1923 }
Damiend99b0522013-12-21 18:17:45 +00001924 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001925 }
1926 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001927 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1928 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1929 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1930 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001931 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001932 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1933 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001934 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1935 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1936 // can't optimise when it's a star expression on the lhs
1937 goto no_optimisation;
1938 }
Damien429d7192013-10-04 19:53:11 +01001939 compile_node(comp, pns10->nodes[0]); // rhs
1940 compile_node(comp, pns10->nodes[1]); // rhs
1941 EMIT(rot_two);
1942 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1943 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001944 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1945 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1946 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1947 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001948 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001949 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1950 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001951 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1952 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1953 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1954 // can't optimise when it's a star expression on the lhs
1955 goto no_optimisation;
1956 }
Damien429d7192013-10-04 19:53:11 +01001957 compile_node(comp, pns10->nodes[0]); // rhs
1958 compile_node(comp, pns10->nodes[1]); // rhs
1959 compile_node(comp, pns10->nodes[2]); // rhs
1960 EMIT(rot_three);
1961 EMIT(rot_two);
1962 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1963 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1964 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1965 } else {
Damien George495d7812014-04-08 17:51:47 +01001966 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01001967 compile_node(comp, pns1->nodes[0]); // rhs
1968 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1969 }
1970 } else {
1971 // shouldn't happen
1972 assert(0);
1973 }
1974 }
1975}
1976
Damien Georged17926d2014-03-30 13:35:08 +01001977void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001978 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001979 compile_node(comp, pns->nodes[0]);
1980 for (int i = 1; i < num_nodes; i += 1) {
1981 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001983 }
1984}
1985
Damiend99b0522013-12-21 18:17:45 +00001986void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1987 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1988 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001989
1990 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001991 int l_fail = comp_next_label(comp);
1992 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001993 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1994 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001995 EMIT_ARG(jump, l_end);
1996 EMIT_ARG(label_assign, l_fail);
1997 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001998 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001999 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002000}
2001
Damiend99b0522013-12-21 18:17:45 +00002002void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002003 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002004 //mp_parse_node_t pn_params = pns->nodes[0];
2005 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002006
2007 if (comp->pass == PASS_1) {
2008 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002009 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 +01002010 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002011 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002012 }
2013
2014 // get the scope for this lambda
2015 scope_t *this_scope = (scope_t*)pns->nodes[2];
2016
2017 // make the lambda
2018 close_over_variables_etc(comp, this_scope, 0, 0);
2019}
2020
Damiend99b0522013-12-21 18:17:45 +00002021void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002022 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002023 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002024 for (int i = 0; i < n; i += 1) {
2025 compile_node(comp, pns->nodes[i]);
2026 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002027 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002028 }
2029 }
Damien Georgeb9791222014-01-23 00:34:21 +00002030 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002031}
2032
Damiend99b0522013-12-21 18:17:45 +00002033void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002034 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002035 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002036 for (int i = 0; i < n; i += 1) {
2037 compile_node(comp, pns->nodes[i]);
2038 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002040 }
2041 }
Damien Georgeb9791222014-01-23 00:34:21 +00002042 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002043}
2044
Damiend99b0522013-12-21 18:17:45 +00002045void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002046 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002047 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002048}
2049
Damiend99b0522013-12-21 18:17:45 +00002050void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002051 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002052 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002053 compile_node(comp, pns->nodes[0]);
2054 bool multi = (num_nodes > 3);
2055 int l_fail = 0;
2056 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002057 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002058 }
2059 for (int i = 1; i + 1 < num_nodes; i += 2) {
2060 compile_node(comp, pns->nodes[i + 1]);
2061 if (i + 2 < num_nodes) {
2062 EMIT(dup_top);
2063 EMIT(rot_three);
2064 }
Damien George7e5fb242014-02-01 22:18:47 +00002065 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002066 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002067 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002068 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2069 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2070 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2071 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2072 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2073 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2074 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2075 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002076 }
2077 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002078 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2079 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2080 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002081 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002082 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002083 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002084 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002085 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002086 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002087 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002088 }
2089 } else {
2090 // shouldn't happen
2091 assert(0);
2092 }
2093 } else {
2094 // shouldn't happen
2095 assert(0);
2096 }
2097 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002098 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002099 }
2100 }
2101 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002102 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002103 EMIT_ARG(jump, l_end);
2104 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002105 EMIT(rot_two);
2106 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002107 EMIT_ARG(label_assign, l_end);
2108 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002109 }
2110}
2111
Damiend99b0522013-12-21 18:17:45 +00002112void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002113 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002114}
2115
Damiend99b0522013-12-21 18:17:45 +00002116void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002118}
2119
Damiend99b0522013-12-21 18:17:45 +00002120void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002121 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002122}
2123
Damiend99b0522013-12-21 18:17:45 +00002124void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002126}
2127
Damiend99b0522013-12-21 18:17:45 +00002128void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2129 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002130 compile_node(comp, pns->nodes[0]);
2131 for (int i = 1; i + 1 < num_nodes; i += 2) {
2132 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002133 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002134 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002135 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002137 } else {
2138 // shouldn't happen
2139 assert(0);
2140 }
2141 }
2142}
2143
Damiend99b0522013-12-21 18:17:45 +00002144void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2145 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002146 compile_node(comp, pns->nodes[0]);
2147 for (int i = 1; i + 1 < num_nodes; i += 2) {
2148 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002149 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002150 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002151 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002152 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002153 } else {
2154 // shouldn't happen
2155 assert(0);
2156 }
2157 }
2158}
2159
Damiend99b0522013-12-21 18:17:45 +00002160void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2161 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002162 compile_node(comp, pns->nodes[0]);
2163 for (int i = 1; i + 1 < num_nodes; i += 2) {
2164 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002165 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002166 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002167 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002168 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002169 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002171 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002172 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002173 } else {
2174 // shouldn't happen
2175 assert(0);
2176 }
2177 }
2178}
2179
Damiend99b0522013-12-21 18:17:45 +00002180void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002181 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002182 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002183 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002184 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002185 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002186 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002187 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002188 } else {
2189 // shouldn't happen
2190 assert(0);
2191 }
2192}
2193
Damien George35e2a4e2014-02-05 00:51:47 +00002194void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2195 // this is to handle special super() call
2196 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2197
2198 compile_generic_all_nodes(comp, pns);
2199}
2200
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002201STATIC 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 +01002202 // function to call is on top of stack
2203
Damien George35e2a4e2014-02-05 00:51:47 +00002204#if !MICROPY_EMIT_CPYTHON
2205 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002206 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 +00002207 EMIT_ARG(load_id, MP_QSTR___class__);
2208 // get first argument to function
2209 bool found = false;
2210 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002211 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2212 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002213 found = true;
2214 break;
2215 }
2216 }
2217 if (!found) {
2218 printf("TypeError: super() call cannot find self\n");
2219 return;
2220 }
Damien George922ddd62014-04-09 12:43:17 +01002221 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002222 return;
2223 }
2224#endif
2225
Damien George02a4c052014-04-09 12:50:58 +01002226 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002227 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002228 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002229 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002230
Damien Georgebbcd49a2014-02-06 20:30:16 +00002231 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002232
2233 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002234 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002235 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002236 n_positional -= 1;
2237 }
Damien George922ddd62014-04-09 12:43:17 +01002238 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002239 n_positional -= 1;
2240 }
2241
2242 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002243 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002244 } else {
Damien George922ddd62014-04-09 12:43:17 +01002245 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002246 }
2247
2248 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002249 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002250}
2251
Damiend99b0522013-12-21 18:17:45 +00002252void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2253 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002254 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002255 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 +01002256 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002257 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2258 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002260 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002261 i += 1;
2262 } else {
2263 compile_node(comp, pns->nodes[i]);
2264 }
Damien George35e2a4e2014-02-05 00:51:47 +00002265 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002266 }
2267}
2268
Damiend99b0522013-12-21 18:17:45 +00002269void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002270 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002271 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002272}
2273
Damiend99b0522013-12-21 18:17:45 +00002274void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002275 // a list of strings
Damien63321742013-12-10 17:41:49 +00002276
2277 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002278 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002279 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002280 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002281 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002282 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2283 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2284 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002285 if (i == 0) {
2286 string_kind = pn_kind;
2287 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002288 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002289 return;
2290 }
Damien George55baff42014-01-21 21:40:13 +00002291 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002292 }
Damien63321742013-12-10 17:41:49 +00002293
Damien63321742013-12-10 17:41:49 +00002294 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002295 byte *q_ptr;
2296 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002297 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002298 uint s_len;
2299 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002300 memcpy(s_dest, s, s_len);
2301 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002302 }
Damien George55baff42014-01-21 21:40:13 +00002303 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002304
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002306}
2307
2308// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002309void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2310 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2311 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2312 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002313
2314 if (comp->pass == PASS_1) {
2315 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002316 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 +01002317 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002318 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002319 }
2320
2321 // get the scope for this comprehension
2322 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2323
2324 // compile the comprehension
2325 close_over_variables_etc(comp, this_scope, 0, 0);
2326
2327 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2328 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002329 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002330}
2331
Damiend99b0522013-12-21 18:17:45 +00002332void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2333 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002334 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002335 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2336 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2337 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2338 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2339 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2340 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2341 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002342 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002343 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002344 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002345 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002346 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002347 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002348 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002349 // generator expression
2350 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2351 } else {
2352 // tuple with 2 items
2353 goto tuple_with_2_items;
2354 }
2355 } else {
2356 // tuple with 2 items
2357 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002358 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002359 }
2360 } else {
2361 // parenthesis around a single item, is just that item
2362 compile_node(comp, pns->nodes[0]);
2363 }
2364}
2365
Damiend99b0522013-12-21 18:17:45 +00002366void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2367 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002368 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002369 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002370 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2371 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2372 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2373 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2374 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002375 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002376 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002377 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002378 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002379 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002380 // list of many items
2381 compile_node(comp, pns2->nodes[0]);
2382 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002383 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002384 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002385 // list comprehension
2386 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2387 } else {
2388 // list with 2 items
2389 goto list_with_2_items;
2390 }
2391 } else {
2392 // list with 2 items
2393 list_with_2_items:
2394 compile_node(comp, pns2->nodes[0]);
2395 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002396 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002397 }
2398 } else {
2399 // list with 1 item
2400 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002401 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002402 }
2403}
2404
Damiend99b0522013-12-21 18:17:45 +00002405void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2406 mp_parse_node_t pn = pns->nodes[0];
2407 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002408 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002409 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002410 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2411 pns = (mp_parse_node_struct_t*)pn;
2412 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002413 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002414 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002415 compile_node(comp, pn);
2416 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002417 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2418 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2419 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2420 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002421 // dict/set with multiple elements
2422
2423 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002424 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002425 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2426
2427 // first element sets whether it's a dict or set
2428 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002429 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002430 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002431 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002432 compile_node(comp, pns->nodes[0]);
2433 EMIT(store_map);
2434 is_dict = true;
2435 } else {
2436 // a set
2437 compile_node(comp, pns->nodes[0]); // 1st value of set
2438 is_dict = false;
2439 }
2440
2441 // process rest of elements
2442 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002443 mp_parse_node_t pn = nodes[i];
2444 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002445 compile_node(comp, pn);
2446 if (is_dict) {
2447 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002448 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002449 return;
2450 }
2451 EMIT(store_map);
2452 } else {
2453 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002454 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002455 return;
2456 }
2457 }
2458 }
2459
2460 // if it's a set, build it
2461 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002462 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002463 }
Damiend99b0522013-12-21 18:17:45 +00002464 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002465 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002466 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002467 // a dictionary comprehension
2468 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2469 } else {
2470 // a set comprehension
2471 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2472 }
2473 } else {
2474 // shouldn't happen
2475 assert(0);
2476 }
2477 } else {
2478 // set with one element
2479 goto set_with_one_element;
2480 }
2481 } else {
2482 // set with one element
2483 set_with_one_element:
2484 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002485 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002486 }
2487}
2488
Damiend99b0522013-12-21 18:17:45 +00002489void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002490 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002491}
2492
Damiend99b0522013-12-21 18:17:45 +00002493void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002494 // object who's index we want is on top of stack
2495 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002496 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002497}
2498
Damiend99b0522013-12-21 18:17:45 +00002499void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002500 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2505 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2506 mp_parse_node_t pn = pns->nodes[0];
2507 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002508 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002509 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2510 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002511 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2512 pns = (mp_parse_node_struct_t*)pn;
2513 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002514 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002515 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002516 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002517 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002518 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002519 } else {
2520 // [?::x]
2521 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002522 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002523 }
Damiend99b0522013-12-21 18:17:45 +00002524 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002525 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002526 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2527 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2528 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2529 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002530 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002531 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002532 } else {
2533 // [?:x:x]
2534 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002535 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002536 }
2537 } else {
2538 // [?:x]
2539 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002540 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002541 }
2542 } else {
2543 // [?:x]
2544 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002545 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002546 }
2547}
2548
Damiend99b0522013-12-21 18:17:45 +00002549void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002550 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002551 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2552 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002553}
2554
Damiend99b0522013-12-21 18:17:45 +00002555void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002556 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002557 compile_subscript_3_helper(comp, pns);
2558}
2559
Damiend99b0522013-12-21 18:17:45 +00002560void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002561 // if this is called then we are compiling a dict key:value pair
2562 compile_node(comp, pns->nodes[1]); // value
2563 compile_node(comp, pns->nodes[0]); // key
2564}
2565
Damiend99b0522013-12-21 18:17:45 +00002566void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002567 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002568 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002569 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002570}
2571
Damiend99b0522013-12-21 18:17:45 +00002572void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002573 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002574 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002575 return;
2576 }
Damien George922ddd62014-04-09 12:43:17 +01002577 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002578 compile_node(comp, pns->nodes[0]);
2579}
2580
Damiend99b0522013-12-21 18:17:45 +00002581void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002582 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002583 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002584 return;
2585 }
Damien George922ddd62014-04-09 12:43:17 +01002586 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002587 compile_node(comp, pns->nodes[0]);
2588}
2589
Damiend99b0522013-12-21 18:17:45 +00002590void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2591 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2592 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2593 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2594 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002595 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 +01002596 return;
2597 }
Damien Georgeb9791222014-01-23 00:34:21 +00002598 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002599 compile_node(comp, pns2->nodes[0]);
2600 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002602 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2603 } else {
2604 // shouldn't happen
2605 assert(0);
2606 }
2607}
2608
Damiend99b0522013-12-21 18:17:45 +00002609void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002610 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002611 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002612 return;
2613 }
Damiend99b0522013-12-21 18:17:45 +00002614 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002615 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002616 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002617 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2618 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002619 compile_node(comp, pns->nodes[0]);
2620 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002621 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002622 EMIT(yield_from);
2623 } else {
2624 compile_node(comp, pns->nodes[0]);
2625 EMIT(yield_value);
2626 }
2627}
2628
Damiend99b0522013-12-21 18:17:45 +00002629typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002630STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002631 NULL,
2632#define nc NULL
2633#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002634#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002635#include "grammar.h"
2636#undef nc
2637#undef c
2638#undef DEF_RULE
2639};
2640
Damiend99b0522013-12-21 18:17:45 +00002641void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2642 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002643 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002644 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2645 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2646 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002647 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002648 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002649 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002650 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002651 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2652 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2653 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2654 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002655 case MP_PARSE_NODE_TOKEN:
2656 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002657 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002658 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002659 // do nothing
2660 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002661 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002662 }
2663 break;
Damien429d7192013-10-04 19:53:11 +01002664 default: assert(0);
2665 }
2666 } else {
Damiend99b0522013-12-21 18:17:45 +00002667 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002668 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002669 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002670 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002671 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002672#if MICROPY_DEBUG_PRINTERS
2673 mp_parse_node_print(pn, 0);
2674#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002675 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002676 } else {
2677 f(comp, pns);
2678 }
2679 }
2680}
2681
Damiend99b0522013-12-21 18:17:45 +00002682void 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 +01002683 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002684 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002685 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2686 if (MP_PARSE_NODE_IS_ID(pn)) {
2687 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002688 if (comp->have_bare_star) {
2689 // comes after a bare star, so doesn't count as a parameter
2690 } else {
2691 comp->scope_cur->num_params += 1;
2692 }
Damienb14de212013-10-06 00:28:28 +01002693 } else {
Damiend99b0522013-12-21 18:17:45 +00002694 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2695 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2696 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2697 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002698 //int node_index = 1; unused
2699 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002700 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002701 // this parameter has an annotation
2702 pn_annotation = pns->nodes[1];
2703 }
2704 //node_index = 2; unused
2705 }
2706 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002707 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002708 // this parameter has a default value
2709 if (comp->have_bare_star) {
2710 comp->scope_cur->num_dict_params += 1;
2711 } else {
2712 comp->scope_cur->num_default_params += 1;
2713 }
2714 }
2715 */
2716 if (comp->have_bare_star) {
2717 // comes after a bare star, so doesn't count as a parameter
2718 } else {
2719 comp->scope_cur->num_params += 1;
2720 }
Damiend99b0522013-12-21 18:17:45 +00002721 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2722 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002723 // bare star
2724 // TODO see http://www.python.org/dev/peps/pep-3102/
2725 comp->have_bare_star = true;
2726 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002727 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002728 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002729 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002730 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2731 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002732 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002733 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002734 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2735 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002736 pn_annotation = pns->nodes[1];
2737 } else {
2738 // shouldn't happen
2739 assert(0);
2740 }
Damiend99b0522013-12-21 18:17:45 +00002741 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2742 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2743 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002744 // this parameter has an annotation
2745 pn_annotation = pns->nodes[1];
2746 }
Damien George8725f8f2014-02-15 19:33:11 +00002747 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002748 } else {
Damienb14de212013-10-06 00:28:28 +01002749 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002750 assert(0);
2751 }
Damien429d7192013-10-04 19:53:11 +01002752 }
2753
2754 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002755 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002756 // TODO this parameter has an annotation
2757 }
2758 bool added;
2759 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2760 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002761 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002762 return;
2763 }
Damien429d7192013-10-04 19:53:11 +01002764 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002765 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002766 }
2767}
2768
Damiend99b0522013-12-21 18:17:45 +00002769void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002770 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2771}
2772
Damiend99b0522013-12-21 18:17:45 +00002773void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002774 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2775}
2776
Damiend99b0522013-12-21 18:17:45 +00002777void 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 +01002778 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002779 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002780 // no more nested if/for; compile inner expression
2781 compile_node(comp, pn_inner_expr);
2782 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002783 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002784 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002785 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002786 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002787 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002788 } else {
2789 EMIT(yield_value);
2790 EMIT(pop_top);
2791 }
Damiend99b0522013-12-21 18:17:45 +00002792 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002793 // if condition
Damiend99b0522013-12-21 18:17:45 +00002794 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002795 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2796 pn_iter = pns_comp_if->nodes[1];
2797 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002798 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002799 // for loop
Damiend99b0522013-12-21 18:17:45 +00002800 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002801 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002802 int l_end2 = comp_next_label(comp);
2803 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002804 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002805 EMIT_ARG(label_assign, l_top2);
2806 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002807 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2808 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 +00002809 EMIT_ARG(jump, l_top2);
2810 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002811 EMIT(for_iter_end);
2812 } else {
2813 // shouldn't happen
2814 assert(0);
2815 }
2816}
2817
Damiend99b0522013-12-21 18:17:45 +00002818void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002819 // see http://www.python.org/dev/peps/pep-0257/
2820
2821 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002822 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002823 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002824 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002825 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002826 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2827 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002828 for (int i = 0; i < num_nodes; i++) {
2829 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002830 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 +00002831 // not a newline, so this is the first statement; finish search
2832 break;
2833 }
2834 }
2835 // 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 +00002836 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002837 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002838 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002839 } else {
2840 return;
2841 }
2842
2843 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002844 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2845 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2846 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2847 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2848 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002849 compile_node(comp, pns->nodes[0]); // a doc string
2850 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002852 }
2853 }
2854 }
2855}
2856
2857void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2858 comp->pass = pass;
2859 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002860 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002861 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002862
2863 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002864 // reset maximum stack sizes in scope
2865 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002866 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002867 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002868 }
2869
Damien5ac1b2e2013-10-18 19:58:12 +01002870#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002871 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002872 scope_print_info(scope);
2873 }
Damien5ac1b2e2013-10-18 19:58:12 +01002874#endif
Damien429d7192013-10-04 19:53:11 +01002875
2876 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002877 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2878 assert(scope->kind == SCOPE_MODULE);
2879 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2880 compile_node(comp, pns->nodes[0]); // compile the expression
2881 EMIT(return_value);
2882 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002883 if (!comp->is_repl) {
2884 check_for_doc_string(comp, scope->pn);
2885 }
Damien429d7192013-10-04 19:53:11 +01002886 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002888 EMIT(return_value);
2889 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002890 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2891 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2892 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002893
2894 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002895 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002896 if (comp->pass == PASS_1) {
2897 comp->have_bare_star = false;
2898 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2899 }
2900
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002901 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002902
2903 compile_node(comp, pns->nodes[3]); // 3 is function body
2904 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002905 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002906 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002907 EMIT(return_value);
2908 }
2909 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002910 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2911 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2912 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002913
2914 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002915 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002916 if (comp->pass == PASS_1) {
2917 comp->have_bare_star = false;
2918 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2919 }
2920
2921 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2922 EMIT(return_value);
2923 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2924 // a bit of a hack at the moment
2925
Damiend99b0522013-12-21 18:17:45 +00002926 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2927 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2928 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2929 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2930 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002931
Damien George55baff42014-01-21 21:40:13 +00002932 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002933 if (comp->pass == PASS_1) {
2934 bool added;
2935 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2936 assert(added);
2937 id_info->kind = ID_INFO_KIND_LOCAL;
2938 scope->num_params = 1;
2939 }
2940
2941 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002942 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002943 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002945 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002946 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002947 }
2948
Damienb05d7072013-10-05 13:37:10 +01002949 int l_end = comp_next_label(comp);
2950 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002951 EMIT_ARG(load_id, qstr_arg);
2952 EMIT_ARG(label_assign, l_top);
2953 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002954 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2955 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002956 EMIT_ARG(jump, l_top);
2957 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002958 EMIT(for_iter_end);
2959
2960 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002961 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002962 }
2963 EMIT(return_value);
2964 } else {
2965 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002966 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2967 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2968 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002969
2970 if (comp->pass == PASS_1) {
2971 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002972 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002973 assert(added);
2974 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002975 }
2976
Damien Georgeb9791222014-01-23 00:34:21 +00002977 EMIT_ARG(load_id, MP_QSTR___name__);
2978 EMIT_ARG(store_id, MP_QSTR___module__);
2979 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2980 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002981
2982 check_for_doc_string(comp, pns->nodes[2]);
2983 compile_node(comp, pns->nodes[2]); // 2 is class body
2984
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002985 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002986 assert(id != NULL);
2987 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002988 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002989 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002990#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002992#else
Damien George2bf7c092014-04-09 15:26:46 +01002993 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002994#endif
Damien429d7192013-10-04 19:53:11 +01002995 }
2996 EMIT(return_value);
2997 }
2998
Damien415eb6f2013-10-05 12:19:06 +01002999 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003000
3001 // make sure we match all the exception levels
3002 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003003}
3004
Damien George094d4502014-04-02 17:31:27 +01003005#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003006void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3007 comp->pass = pass;
3008 comp->scope_cur = scope;
3009 comp->next_label = 1;
3010
3011 if (scope->kind != SCOPE_FUNCTION) {
3012 printf("Error: inline assembler must be a function\n");
3013 return;
3014 }
3015
Damiena2f2f7d2013-10-06 00:14:13 +01003016 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003017 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003018 }
3019
Damien826005c2013-10-05 23:17:28 +01003020 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003021 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3022 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3023 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003024
Damiend99b0522013-12-21 18:17:45 +00003025 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003026
Damiena2f2f7d2013-10-06 00:14:13 +01003027 // parameters are in pns->nodes[1]
3028 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003029 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003030 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003031 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003032 }
3033
Damiend99b0522013-12-21 18:17:45 +00003034 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003035
Damiend99b0522013-12-21 18:17:45 +00003036 mp_parse_node_t pn_body = pns->nodes[3]; // body
3037 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003038 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3039
Damien Georgecbd2f742014-01-19 11:48:48 +00003040 /*
Damien826005c2013-10-05 23:17:28 +01003041 if (comp->pass == PASS_3) {
3042 //printf("----\n");
3043 scope_print_info(scope);
3044 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003045 */
Damien826005c2013-10-05 23:17:28 +01003046
3047 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003048 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3049 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3050 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3051 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3052 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3053 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3054 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3055 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3056 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3057 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3058 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3059 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3060 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003061 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3062
3063 // emit instructions
3064 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003065 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003066 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003067 return;
3068 }
3069 int lab = comp_next_label(comp);
3070 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003071 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003072 }
3073 } else {
3074 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003075 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003076 }
3077 }
3078 }
3079
3080 if (comp->pass > PASS_1) {
3081 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003082 }
Damien429d7192013-10-04 19:53:11 +01003083}
Damien George094d4502014-04-02 17:31:27 +01003084#endif
Damien429d7192013-10-04 19:53:11 +01003085
3086void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3087 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003088 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003089 scope->num_locals = 0;
3090 for (int i = 0; i < scope->id_info_len; i++) {
3091 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003092 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003093 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3094 continue;
3095 }
3096 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3097 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3098 }
Damien9ecbcff2013-12-11 00:41:43 +00003099 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003100 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003101 id->local_num = scope->num_locals;
3102 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003103 }
3104 }
3105
3106 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003107#if MICROPY_EMIT_CPYTHON
3108 int num_cell = 0;
3109#endif
Damien9ecbcff2013-12-11 00:41:43 +00003110 for (int i = 0; i < scope->id_info_len; i++) {
3111 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003112#if MICROPY_EMIT_CPYTHON
3113 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003114 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003115 id->local_num = num_cell;
3116 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003117 }
Damien George6baf76e2013-12-30 22:32:17 +00003118#else
3119 // in Micro Python the cells come right after the fast locals
3120 // parameters are not counted here, since they remain at the start
3121 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003122 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003123 id->local_num = scope->num_locals;
3124 scope->num_locals += 1;
3125 }
3126#endif
Damien9ecbcff2013-12-11 00:41:43 +00003127 }
Damien9ecbcff2013-12-11 00:41:43 +00003128
3129 // compute the index of free vars (freevars[idx] in CPython)
3130 // make sure they are in the order of the parent scope
3131 if (scope->parent != NULL) {
3132 int num_free = 0;
3133 for (int i = 0; i < scope->parent->id_info_len; i++) {
3134 id_info_t *id = &scope->parent->id_info[i];
3135 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3136 for (int j = 0; j < scope->id_info_len; j++) {
3137 id_info_t *id2 = &scope->id_info[j];
3138 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003139 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003140#if MICROPY_EMIT_CPYTHON
3141 // in CPython the frees are numbered after the cells
3142 id2->local_num = num_cell + num_free;
3143#else
3144 // in Micro Python the frees come first, before the params
3145 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003146#endif
3147 num_free += 1;
3148 }
3149 }
3150 }
Damien429d7192013-10-04 19:53:11 +01003151 }
Damien George6baf76e2013-12-30 22:32:17 +00003152#if !MICROPY_EMIT_CPYTHON
3153 // in Micro Python shift all other locals after the free locals
3154 if (num_free > 0) {
3155 for (int i = 0; i < scope->id_info_len; i++) {
3156 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003157 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003158 id->local_num += num_free;
3159 }
3160 }
3161 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3162 scope->num_locals += num_free;
3163 }
3164#endif
Damien429d7192013-10-04 19:53:11 +01003165 }
3166
Damien George8725f8f2014-02-15 19:33:11 +00003167 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003168
3169#if MICROPY_EMIT_CPYTHON
3170 // these flags computed here are for CPython compatibility only
3171 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003172 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003173 }
3174 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) {
3175 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003176 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003177
3178 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003179 // Note that we don't actually use this information at the moment (for CPython compat only)
3180 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 +00003181 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003182 }
3183 }
Damien George882b3632014-04-02 15:56:31 +01003184#endif
3185
Damien429d7192013-10-04 19:53:11 +01003186 int num_free = 0;
3187 for (int i = 0; i < scope->id_info_len; i++) {
3188 id_info_t *id = &scope->id_info[i];
3189 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3190 num_free += 1;
3191 }
3192 }
3193 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003194 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003195 }
3196}
3197
Damien George65cad122014-04-06 11:48:15 +01003198mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien Georgeb140bff2014-04-09 12:54:21 +01003199 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003200 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003201 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003202
Damien826005c2013-10-05 23:17:28 +01003203 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003204 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003205
3206 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003207 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003208
Damien826005c2013-10-05 23:17:28 +01003209 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003210 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003211 comp->emit_method_table = &emit_pass1_method_table;
3212 comp->emit_inline_asm = NULL;
3213 comp->emit_inline_asm_method_table = NULL;
3214 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003215 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003216 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003217#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003218 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003219 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003220#endif
Damien826005c2013-10-05 23:17:28 +01003221 } else {
3222 compile_scope(comp, s, PASS_1);
3223 }
3224
3225 // update maximim number of labels needed
3226 if (comp->next_label > max_num_labels) {
3227 max_num_labels = comp->next_label;
3228 }
Damien429d7192013-10-04 19:53:11 +01003229 }
3230
Damien826005c2013-10-05 23:17:28 +01003231 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003232 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003233 compile_scope_compute_things(comp, s);
3234 }
3235
Damien826005c2013-10-05 23:17:28 +01003236 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003237 emit_pass1_free(comp->emit);
3238
Damien826005c2013-10-05 23:17:28 +01003239 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003240#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003241 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003242#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003243 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003244#endif
Damien3ef4abb2013-10-12 16:53:13 +01003245#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003246 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003247#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003248#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003249 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003250 if (false) {
3251 // dummy
3252
Damien3ef4abb2013-10-12 16:53:13 +01003253#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003254 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003255 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003256 if (emit_inline_thumb == NULL) {
3257 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3258 }
3259 comp->emit = NULL;
3260 comp->emit_method_table = NULL;
3261 comp->emit_inline_asm = emit_inline_thumb;
3262 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3263 compile_scope_inline_asm(comp, s, PASS_2);
3264 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003265#endif
3266
Damien826005c2013-10-05 23:17:28 +01003267 } else {
Damienc025ebb2013-10-12 14:30:21 +01003268
3269 // choose the emit type
3270
Damien3ef4abb2013-10-12 16:53:13 +01003271#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003272 comp->emit = emit_cpython_new(max_num_labels);
3273 comp->emit_method_table = &emit_cpython_method_table;
3274#else
Damien826005c2013-10-05 23:17:28 +01003275 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003276
3277#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003278 case MP_EMIT_OPT_NATIVE_PYTHON:
3279 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003280#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003281 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003282 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003283 }
Damien13ed3a62013-10-08 09:05:10 +01003284 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003285#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003286 if (emit_native == NULL) {
3287 emit_native = emit_native_thumb_new(max_num_labels);
3288 }
3289 comp->emit_method_table = &emit_native_thumb_method_table;
3290#endif
3291 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003292 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003293 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003294#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003295
Damien826005c2013-10-05 23:17:28 +01003296 default:
3297 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003298 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003299 }
3300 comp->emit = emit_bc;
3301 comp->emit_method_table = &emit_bc_method_table;
3302 break;
3303 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003304#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003305
3306 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003307 compile_scope(comp, s, PASS_2);
3308 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003309 }
Damien429d7192013-10-04 19:53:11 +01003310 }
3311
Damien George41d02b62014-01-24 22:42:28 +00003312 // free the emitters
3313#if !MICROPY_EMIT_CPYTHON
3314 if (emit_bc != NULL) {
3315 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003316 }
Damien George41d02b62014-01-24 22:42:28 +00003317#if MICROPY_EMIT_NATIVE
3318 if (emit_native != NULL) {
3319#if MICROPY_EMIT_X64
3320 emit_native_x64_free(emit_native);
3321#elif MICROPY_EMIT_THUMB
3322 emit_native_thumb_free(emit_native);
3323#endif
3324 }
3325#endif
3326#if MICROPY_EMIT_INLINE_THUMB
3327 if (emit_inline_thumb != NULL) {
3328 emit_inline_thumb_free(emit_inline_thumb);
3329 }
3330#endif
3331#endif // !MICROPY_EMIT_CPYTHON
3332
3333 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003334 uint unique_code_id = module_scope->unique_code_id;
3335 for (scope_t *s = module_scope; s;) {
3336 scope_t *next = s->next;
3337 scope_free(s);
3338 s = next;
3339 }
Damien5ac1b2e2013-10-18 19:58:12 +01003340
Damien George41d02b62014-01-24 22:42:28 +00003341 // free the compiler
3342 bool had_error = comp->had_error;
3343 m_del_obj(compiler_t, comp);
3344
Damien George1fb03172014-01-03 14:22:03 +00003345 if (had_error) {
3346 // TODO return a proper error message
3347 return mp_const_none;
3348 } else {
3349#if MICROPY_EMIT_CPYTHON
3350 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003351 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003352 return mp_const_true;
3353#else
3354 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003355 // 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 +01003356 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003357#endif
3358 }
Damien429d7192013-10-04 19:53:11 +01003359}