blob: ebc144b341c54e227852e36abef68970785bf5fb [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
39typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000040 qstr source_file;
Damien George78035b92014-04-09 12:27:39 +010041 uint8_t is_repl;
42 uint8_t pass; // holds enum type pass_kind_t
43 uint8_t had_error; // try to keep compiler clean from nlr
44 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien429d7192013-10-04 19:53:11 +010045
Damien George6f355fd2014-04-10 14:11:31 +010046 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010047
Damien George6f355fd2014-04-10 14:11:31 +010048 uint break_label;
49 uint continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000050 int break_continue_except_level;
Damien George78035b92014-04-09 12:27:39 +010051 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010052
Damien George02a4c052014-04-09 12:50:58 +010053 uint16_t n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +010054 uint8_t star_flags;
Damien George02a4c052014-04-09 12:50:58 +010055 uint8_t have_bare_star;
56 uint8_t param_pass;
57 uint16_t param_pass_num_dict_params;
58 uint16_t param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +010059
60 scope_t *scope_head;
61 scope_t *scope_cur;
62
Damien6cdd3af2013-10-05 18:08:26 +010063 emit_t *emit; // current emitter
64 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010065
66 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
67 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010068} compiler_t;
69
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010070STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgef41fdd02014-03-03 23:19:11 +000071 // TODO store the error message to a variable in compiler_t instead of printing it
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010072 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
73 printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
74 } else {
75 printf(" File \"%s\"\n", qstr_str(comp->source_file));
76 }
Damien Georgef41fdd02014-03-03 23:19:11 +000077 printf("SyntaxError: %s\n", msg);
78 comp->had_error = true;
79}
80
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
Damien George6f355fd2014-04-10 14:11:31 +0100199STATIC uint 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) {
Damien George6f355fd2014-04-10 14:11:31 +0100470 uint 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 {
Damien George6f355fd2014-04-10 14:11:31 +0100488 uint 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) {
Damien George6f355fd2014-04-10 14:11:31 +0100531 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100532 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 {
Damien George6f355fd2014-04-10 14:11:31 +0100549 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100550 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
Damien George6f355fd2014-04-10 14:11:31 +01001205 uint 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
Damien George635543c2014-04-10 12:56:52 +01001239// q_base holds the base of the name
1240// eg a -> q_base=a
1241// a.b.c -> q_base=a
1242void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
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
Damien George635543c2014-04-10 12:56:52 +01001247 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001248 pn = pns->nodes[0];
1249 is_as = true;
1250 }
Damien George635543c2014-04-10 12:56:52 +01001251 if (MP_PARSE_NODE_IS_NULL(pn)) {
1252 // empty name (eg, from . import x)
1253 *q_base = MP_QSTR_;
1254 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1255 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001256 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001257 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001258 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001259 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001260 }
Damien George635543c2014-04-10 12:56:52 +01001261 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001262 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1263 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1264 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001265 // a name of the form a.b.c
1266 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001267 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001268 }
Damiend99b0522013-12-21 18:17:45 +00001269 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001270 int len = n - 1;
1271 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001272 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001273 }
Damien George55baff42014-01-21 21:40:13 +00001274 byte *q_ptr;
1275 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001276 for (int i = 0; i < n; i++) {
1277 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001278 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001279 }
Damien George55baff42014-01-21 21:40:13 +00001280 uint str_src_len;
1281 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001282 memcpy(str_dest, str_src, str_src_len);
1283 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001284 }
Damien George635543c2014-04-10 12:56:52 +01001285 qstr q_full = qstr_build_end(q_ptr);
1286 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001287 if (is_as) {
1288 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001289 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001290 }
1291 }
1292 } else {
Damien George635543c2014-04-10 12:56:52 +01001293 // shouldn't happen
1294 assert(0);
Damien429d7192013-10-04 19:53:11 +01001295 }
1296 } else {
Damien George635543c2014-04-10 12:56:52 +01001297 // shouldn't happen
1298 assert(0);
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 George635543c2014-04-10 12:56:52 +01001303 EMIT_ARG(load_const_small_int, 0); // level 0 import
1304 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1305 qstr q_base;
1306 do_import_name(comp, pn, &q_base);
1307 EMIT_ARG(store_id, q_base);
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) {
Damien George635543c2014-04-10 12:56:52 +01001315 mp_parse_node_t pn_import_source = pns->nodes[0];
1316
1317 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1318 uint import_level = 0;
1319 do {
1320 mp_parse_node_t pn_rel;
1321 if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) {
1322 // This covers relative imports with dots only like "from .. import"
1323 pn_rel = pn_import_source;
1324 pn_import_source = MP_PARSE_NODE_NULL;
1325 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1326 // This covers relative imports starting with dot(s) like "from .foo import"
1327 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1328 pn_rel = pns_2b->nodes[0];
1329 pn_import_source = pns_2b->nodes[1];
1330 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1331 } else {
1332 // Not a relative import
1333 break;
1334 }
1335
1336 // get the list of . and/or ...'s
1337 mp_parse_node_t *nodes;
1338 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1339
1340 // count the total number of .'s
1341 for (int i = 0; i < n; i++) {
1342 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1343 import_level++;
1344 } else {
1345 // should be an MP_TOKEN_ELLIPSIS
1346 import_level += 3;
1347 }
1348 }
1349 } while (0);
1350
Damiend99b0522013-12-21 18:17:45 +00001351 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001352 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001353
1354 // build the "fromlist" tuple
1355#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001357#else
Damien Georgeb9791222014-01-23 00:34:21 +00001358 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1359 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001360#endif
1361
1362 // do the import
Damien George635543c2014-04-10 12:56:52 +01001363 qstr dummy_q;
1364 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001365 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001366
Damien429d7192013-10-04 19:53:11 +01001367 } else {
Damien George635543c2014-04-10 12:56:52 +01001368 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001369
1370 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001371 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001372 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001373#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001374 {
1375 vstr_t *vstr = vstr_new();
1376 vstr_printf(vstr, "(");
1377 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001378 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1379 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1380 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001381 if (i > 0) {
1382 vstr_printf(vstr, ", ");
1383 }
1384 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001385 uint len;
1386 const byte *str = qstr_data(id2, &len);
1387 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001388 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001389 }
Damien02f89412013-12-12 15:13:36 +00001390 if (n == 1) {
1391 vstr_printf(vstr, ",");
1392 }
1393 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001394 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001395 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001396 }
Damiendb4c3612013-12-10 17:27:24 +00001397#else
1398 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001399 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1400 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1401 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001402 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001403 }
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001405#endif
1406
1407 // do the import
Damien George635543c2014-04-10 12:56:52 +01001408 qstr dummy_q;
1409 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001410 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001411 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1412 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1413 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001414 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001415 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001417 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001419 }
1420 }
1421 EMIT(pop_top);
1422 }
1423}
1424
Damiend99b0522013-12-21 18:17:45 +00001425void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001426 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001427 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1428 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001429 } else {
Damiend99b0522013-12-21 18:17:45 +00001430 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1431 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001432 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001433 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001434 }
Damien429d7192013-10-04 19:53:11 +01001435 }
1436 }
1437}
1438
Damiend99b0522013-12-21 18:17:45 +00001439void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001440 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001441 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1442 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001443 } else {
Damiend99b0522013-12-21 18:17:45 +00001444 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1445 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001446 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001447 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001448 }
Damien429d7192013-10-04 19:53:11 +01001449 }
1450 }
1451}
1452
Damiend99b0522013-12-21 18:17:45 +00001453void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001454 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001455 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001456 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 +00001457 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001458 // assertion message
1459 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001460 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001461 }
Damien Georgeb9791222014-01-23 00:34:21 +00001462 EMIT_ARG(raise_varargs, 1);
1463 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001464}
1465
Damiend99b0522013-12-21 18:17:45 +00001466void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001467 // TODO proper and/or short circuiting
1468
Damien George6f355fd2014-04-10 14:11:31 +01001469 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001470
Damien George6f355fd2014-04-10 14:11:31 +01001471 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001472 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1473
1474 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001475
1476 if (
1477#if !MICROPY_EMIT_CPYTHON
1478 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1479 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1480#endif
1481 // optimisation to not jump if last instruction was return
1482 !EMIT(last_emit_was_return_value)
1483 ) {
1484 // jump over elif/else blocks
1485 EMIT_ARG(jump, l_end);
1486 }
1487
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001489
Damiend99b0522013-12-21 18:17:45 +00001490 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001491 // compile elif blocks
1492
Damiend99b0522013-12-21 18:17:45 +00001493 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001494
Damiend99b0522013-12-21 18:17:45 +00001495 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001496 // multiple elif blocks
1497
Damiend99b0522013-12-21 18:17:45 +00001498 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001499 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001500 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001501 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001502 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1503
1504 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001505 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001507 }
Damien Georgeb9791222014-01-23 00:34:21 +00001508 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001509 }
1510
1511 } else {
1512 // a single elif block
1513
Damienb05d7072013-10-05 13:37:10 +01001514 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001515 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1516
1517 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001518 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001519 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001520 }
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001522 }
1523 }
1524
1525 // compile else block
1526 compile_node(comp, pns->nodes[3]); // can be null
1527
Damien Georgeb9791222014-01-23 00:34:21 +00001528 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001529}
1530
Damien Georgecbddb272014-02-01 20:08:18 +00001531#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001532 uint old_break_label = comp->break_label; \
1533 uint old_continue_label = comp->continue_label; \
1534 uint break_label = comp_next_label(comp); \
1535 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001536 comp->break_label = break_label; \
1537 comp->continue_label = continue_label; \
1538 comp->break_continue_except_level = comp->cur_except_level;
1539
1540#define END_BREAK_CONTINUE_BLOCK \
1541 comp->break_label = old_break_label; \
1542 comp->continue_label = old_continue_label; \
1543 comp->break_continue_except_level = comp->cur_except_level;
1544
Damiend99b0522013-12-21 18:17:45 +00001545void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001546 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001547
Damience89a212013-10-15 22:25:17 +01001548 // compared to CPython, we have an optimised version of while loops
1549#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001550 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001551 EMIT_ARG(setup_loop, break_label);
1552 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001553 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1554 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001555 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001557 }
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001559 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1560 // this is a small hack to agree with CPython
1561 if (!node_is_const_true(pns->nodes[0])) {
1562 EMIT(pop_block);
1563 }
Damience89a212013-10-15 22:25:17 +01001564#else
Damien George6f355fd2014-04-10 14:11:31 +01001565 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001566 EMIT_ARG(jump, continue_label);
1567 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001568 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001569 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001570 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1571#endif
1572
1573 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001574 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001575
1576 compile_node(comp, pns->nodes[2]); // else
1577
Damien Georgeb9791222014-01-23 00:34:21 +00001578 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001579}
1580
Damienf72fd0e2013-11-06 20:20:49 +00001581// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001582// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1583// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001584void 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 +00001585 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001586
Damien George6f355fd2014-04-10 14:11:31 +01001587 uint top_label = comp_next_label(comp);
1588 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001589
Damien George3ff2d032014-03-31 18:02:22 +01001590 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001591 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001592 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001593
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(jump, entry_label);
1595 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001596
Damien George3ff2d032014-03-31 18:02:22 +01001597 // at this point we actually have 1 less element on the stack
1598 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1599
1600 // store next value to var
1601 c_assign(comp, pn_var, ASSIGN_STORE);
1602
Damienf3822fc2013-11-09 20:12:03 +00001603 // compile body
1604 compile_node(comp, pn_body);
1605
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001607
Damien George3ff2d032014-03-31 18:02:22 +01001608 // compile: var + step, duplicated on stack
1609 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001610 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001611 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001612 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001613
Damien Georgeb9791222014-01-23 00:34:21 +00001614 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001615
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001616 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001617 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001618 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1619 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001620 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001621 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001622 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001623 }
Damien Georgeb9791222014-01-23 00:34:21 +00001624 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001625
Damien George3ff2d032014-03-31 18:02:22 +01001626 // discard final value of var that failed the loop condition
1627 EMIT(pop_top);
1628
Damienf72fd0e2013-11-06 20:20:49 +00001629 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001630 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001631
1632 compile_node(comp, pn_else);
1633
Damien Georgeb9791222014-01-23 00:34:21 +00001634 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001635}
1636
Damiend99b0522013-12-21 18:17:45 +00001637void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001638#if !MICROPY_EMIT_CPYTHON
1639 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1640 // this is actually slower, but uses no heap memory
1641 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001642 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 +00001643 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001644 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1645 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1646 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1647 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001648 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1649 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001650 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001651 mp_parse_node_t pn_range_start;
1652 mp_parse_node_t pn_range_end;
1653 mp_parse_node_t pn_range_step;
1654 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001655 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001656 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001657 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001658 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001659 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001660 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001661 } else if (n_args == 2) {
1662 pn_range_start = args[0];
1663 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001664 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001665 } else {
1666 pn_range_start = args[0];
1667 pn_range_end = args[1];
1668 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001669 // We need to know sign of step. This is possible only if it's constant
1670 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1671 optimize = false;
1672 }
Damienf72fd0e2013-11-06 20:20:49 +00001673 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001674 }
1675 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001676 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1677 return;
1678 }
1679 }
1680 }
1681#endif
1682
Damien Georgecbddb272014-02-01 20:08:18 +00001683 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001684
Damien George6f355fd2014-04-10 14:11:31 +01001685 uint pop_label = comp_next_label(comp);
1686 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001687
Damience89a212013-10-15 22:25:17 +01001688 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1689#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001691#endif
1692
Damien429d7192013-10-04 19:53:11 +01001693 compile_node(comp, pns->nodes[1]); // iterator
1694 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001695 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001696 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001697 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1698 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001699 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001700 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001701 }
Damien Georgeb9791222014-01-23 00:34:21 +00001702 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001703 EMIT(for_iter_end);
1704
1705 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001706 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001707
Damience89a212013-10-15 22:25:17 +01001708#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001709 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001710#endif
Damien429d7192013-10-04 19:53:11 +01001711
1712 compile_node(comp, pns->nodes[3]); // else (not tested)
1713
Damien Georgeb9791222014-01-23 00:34:21 +00001714 EMIT_ARG(label_assign, break_label);
1715 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001716}
1717
Damiend99b0522013-12-21 18:17:45 +00001718void 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 +01001719 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001720 uint l1 = comp_next_label(comp);
1721 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001722
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001724 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001725
Damien429d7192013-10-04 19:53:11 +01001726 compile_node(comp, pn_body); // body
1727 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001728 EMIT_ARG(jump, success_label); // jump over exception handler
1729
1730 EMIT_ARG(label_assign, l1); // start of exception handler
1731 EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
1732
Damien George6f355fd2014-04-10 14:11:31 +01001733 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001734
1735 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001736 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1737 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001738
1739 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001740 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001741
Damiend99b0522013-12-21 18:17:45 +00001742 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001743 // this is a catch all exception handler
1744 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001745 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001746 return;
1747 }
1748 } else {
1749 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001750 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1751 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1752 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1753 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001754 // handler binds the exception to a local
1755 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001756 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001757 }
1758 }
1759 EMIT(dup_top);
1760 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001761 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001762 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001763 }
1764
1765 EMIT(pop_top);
1766
1767 if (qstr_exception_local == 0) {
1768 EMIT(pop_top);
1769 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001771 }
1772
1773 EMIT(pop_top);
1774
Damien George6f355fd2014-04-10 14:11:31 +01001775 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001776 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001777 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001779 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001780 }
1781 compile_node(comp, pns_except->nodes[1]);
1782 if (qstr_exception_local != 0) {
1783 EMIT(pop_block);
1784 }
1785 EMIT(pop_except);
1786 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1788 EMIT_ARG(label_assign, l3);
1789 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1790 EMIT_ARG(store_id, qstr_exception_local);
1791 EMIT_ARG(delete_id, qstr_exception_local);
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);
1795 }
Damien Georgeb9791222014-01-23 00:34:21 +00001796 EMIT_ARG(jump, l2);
1797 EMIT_ARG(label_assign, end_finally_label);
Damien George069a35e2014-04-10 17:22:19 +00001798 EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001799 }
1800
Damien George8dcc0c72014-03-27 10:55:21 +00001801 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001802 EMIT(end_finally);
Damien George069a35e2014-04-10 17:22:19 +00001803 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001804
Damien Georgeb9791222014-01-23 00:34:21 +00001805 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001806 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001807 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001808}
1809
Damiend99b0522013-12-21 18:17:45 +00001810void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
Damien George6f355fd2014-04-10 14:11:31 +01001811 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001812
Damien Georgeb9791222014-01-23 00:34:21 +00001813 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001814 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001815
Damien429d7192013-10-04 19:53:11 +01001816 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001817 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien George069a35e2014-04-10 17:22:19 +00001818 EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001819 compile_node(comp, pn_body);
Damien George069a35e2014-04-10 17:22:19 +00001820 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 3);
Damien429d7192013-10-04 19:53:11 +01001821 } else {
1822 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1823 }
1824 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001825 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1826 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001827 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001828
Damien George8dcc0c72014-03-27 10:55:21 +00001829 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001830 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001831}
1832
Damiend99b0522013-12-21 18:17:45 +00001833void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1834 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1835 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1836 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001837 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001838 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1839 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001840 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001841 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001842 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001843 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001844 // no finally
1845 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1846 } else {
1847 // have finally
Damiend99b0522013-12-21 18:17:45 +00001848 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 +01001849 }
1850 } else {
1851 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001852 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001853 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001854 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001855 }
1856 } else {
1857 // shouldn't happen
1858 assert(0);
1859 }
1860}
1861
Damiend99b0522013-12-21 18:17:45 +00001862void 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 +01001863 if (n == 0) {
1864 // no more pre-bits, compile the body of the with
1865 compile_node(comp, body);
1866 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001867 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001868 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001869 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001870 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001871 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001872 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001873 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1874 } else {
1875 // this pre-bit is just an expression
1876 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001877 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001878 EMIT(pop_top);
1879 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001880 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001881 // compile additional pre-bits and the body
1882 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1883 // finish this with block
1884 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001885 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1886 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001887 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001888 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001889 EMIT(end_finally);
1890 }
1891}
1892
Damiend99b0522013-12-21 18:17:45 +00001893void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001894 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001895 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001896 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1897 assert(n > 0);
1898
1899 // compile in a nested fashion
1900 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1901}
1902
Damiend99b0522013-12-21 18:17:45 +00001903void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1904 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001905 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1906 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001907 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001908 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001909 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001910 EMIT(pop_top);
1911
Damien429d7192013-10-04 19:53:11 +01001912 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001913 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001914 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001915 // do nothing with a lonely constant
1916 } else {
1917 compile_node(comp, pns->nodes[0]); // just an expression
1918 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1919 }
Damien429d7192013-10-04 19:53:11 +01001920 }
1921 } else {
Damiend99b0522013-12-21 18:17:45 +00001922 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1923 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001924 if (kind == PN_expr_stmt_augassign) {
1925 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1926 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001927 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001928 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001929 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001930 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1931 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1932 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1933 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1934 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1935 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1936 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1937 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1938 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1939 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1940 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1941 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1942 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001943 }
Damien George7e5fb242014-02-01 22:18:47 +00001944 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001945 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1946 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001947 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1948 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001949 // following CPython, we store left-most first
1950 if (rhs > 0) {
1951 EMIT(dup_top);
1952 }
1953 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1954 for (int i = 0; i < rhs; i++) {
1955 if (i + 1 < rhs) {
1956 EMIT(dup_top);
1957 }
Damiend99b0522013-12-21 18:17:45 +00001958 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001959 }
1960 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001961 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1962 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1963 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1964 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001965 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001966 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1967 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001968 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1969 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1970 // can't optimise when it's a star expression on the lhs
1971 goto no_optimisation;
1972 }
Damien429d7192013-10-04 19:53:11 +01001973 compile_node(comp, pns10->nodes[0]); // rhs
1974 compile_node(comp, pns10->nodes[1]); // rhs
1975 EMIT(rot_two);
1976 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1977 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001978 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1979 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1980 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1981 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001982 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001983 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1984 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001985 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1986 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1987 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1988 // can't optimise when it's a star expression on the lhs
1989 goto no_optimisation;
1990 }
Damien429d7192013-10-04 19:53:11 +01001991 compile_node(comp, pns10->nodes[0]); // rhs
1992 compile_node(comp, pns10->nodes[1]); // rhs
1993 compile_node(comp, pns10->nodes[2]); // rhs
1994 EMIT(rot_three);
1995 EMIT(rot_two);
1996 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1997 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1998 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1999 } else {
Damien George495d7812014-04-08 17:51:47 +01002000 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002001 compile_node(comp, pns1->nodes[0]); // rhs
2002 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2003 }
2004 } else {
2005 // shouldn't happen
2006 assert(0);
2007 }
2008 }
2009}
2010
Damien Georged17926d2014-03-30 13:35:08 +01002011void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002012 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002013 compile_node(comp, pns->nodes[0]);
2014 for (int i = 1; i < num_nodes; i += 1) {
2015 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002016 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002017 }
2018}
2019
Damiend99b0522013-12-21 18:17:45 +00002020void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2021 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2022 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002023
Damien George6f355fd2014-04-10 14:11:31 +01002024 uint l_fail = comp_next_label(comp);
2025 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002026 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2027 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002028 EMIT_ARG(jump, l_end);
2029 EMIT_ARG(label_assign, l_fail);
Damien George069a35e2014-04-10 17:22:19 +00002030 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002031 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002032 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002033}
2034
Damiend99b0522013-12-21 18:17:45 +00002035void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002036 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002037 //mp_parse_node_t pn_params = pns->nodes[0];
2038 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002039
2040 if (comp->pass == PASS_1) {
2041 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002042 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 +01002043 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002044 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002045 }
2046
2047 // get the scope for this lambda
2048 scope_t *this_scope = (scope_t*)pns->nodes[2];
2049
2050 // make the lambda
2051 close_over_variables_etc(comp, this_scope, 0, 0);
2052}
2053
Damiend99b0522013-12-21 18:17:45 +00002054void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002055 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002056 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002057 for (int i = 0; i < n; i += 1) {
2058 compile_node(comp, pns->nodes[i]);
2059 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002060 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002061 }
2062 }
Damien Georgeb9791222014-01-23 00:34:21 +00002063 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002064}
2065
Damiend99b0522013-12-21 18:17:45 +00002066void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002067 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002068 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002069 for (int i = 0; i < n; i += 1) {
2070 compile_node(comp, pns->nodes[i]);
2071 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002072 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002073 }
2074 }
Damien Georgeb9791222014-01-23 00:34:21 +00002075 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002076}
2077
Damiend99b0522013-12-21 18:17:45 +00002078void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002079 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002080 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002081}
2082
Damiend99b0522013-12-21 18:17:45 +00002083void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002084 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002085 compile_node(comp, pns->nodes[0]);
2086 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002087 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002088 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002089 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002090 }
2091 for (int i = 1; i + 1 < num_nodes; i += 2) {
2092 compile_node(comp, pns->nodes[i + 1]);
2093 if (i + 2 < num_nodes) {
2094 EMIT(dup_top);
2095 EMIT(rot_three);
2096 }
Damien George7e5fb242014-02-01 22:18:47 +00002097 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002098 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002099 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2101 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2102 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2103 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2104 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2105 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2106 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2107 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002108 }
2109 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002110 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2111 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2112 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002113 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002114 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002115 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002116 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002118 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002119 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002120 }
2121 } else {
2122 // shouldn't happen
2123 assert(0);
2124 }
2125 } else {
2126 // shouldn't happen
2127 assert(0);
2128 }
2129 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002130 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002131 }
2132 }
2133 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002134 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002135 EMIT_ARG(jump, l_end);
2136 EMIT_ARG(label_assign, l_fail);
Damien George069a35e2014-04-10 17:22:19 +00002137 EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 1);
Damien429d7192013-10-04 19:53:11 +01002138 EMIT(rot_two);
2139 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002140 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002141 }
2142}
2143
Damiend99b0522013-12-21 18:17:45 +00002144void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002145 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002146}
2147
Damiend99b0522013-12-21 18:17:45 +00002148void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002150}
2151
Damiend99b0522013-12-21 18:17:45 +00002152void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002153 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002154}
2155
Damiend99b0522013-12-21 18:17:45 +00002156void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002157 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002158}
2159
Damiend99b0522013-12-21 18:17:45 +00002160void compile_shift_expr(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_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002166 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002167 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002168 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002169 } else {
2170 // shouldn't happen
2171 assert(0);
2172 }
2173 }
2174}
2175
Damiend99b0522013-12-21 18:17:45 +00002176void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2177 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002178 compile_node(comp, pns->nodes[0]);
2179 for (int i = 1; i + 1 < num_nodes; i += 2) {
2180 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002181 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002182 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002183 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002184 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002185 } else {
2186 // shouldn't happen
2187 assert(0);
2188 }
2189 }
2190}
2191
Damiend99b0522013-12-21 18:17:45 +00002192void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2193 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002194 compile_node(comp, pns->nodes[0]);
2195 for (int i = 1; i + 1 < num_nodes; i += 2) {
2196 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002197 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002198 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002199 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002200 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002201 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002202 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002203 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002204 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002205 } else {
2206 // shouldn't happen
2207 assert(0);
2208 }
2209 }
2210}
2211
Damiend99b0522013-12-21 18:17:45 +00002212void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002213 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002214 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002215 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002216 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002217 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002218 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002219 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002220 } else {
2221 // shouldn't happen
2222 assert(0);
2223 }
2224}
2225
Damien George35e2a4e2014-02-05 00:51:47 +00002226void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2227 // this is to handle special super() call
2228 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2229
2230 compile_generic_all_nodes(comp, pns);
2231}
2232
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002233STATIC 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 +01002234 // function to call is on top of stack
2235
Damien George35e2a4e2014-02-05 00:51:47 +00002236#if !MICROPY_EMIT_CPYTHON
2237 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002238 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 +00002239 EMIT_ARG(load_id, MP_QSTR___class__);
2240 // get first argument to function
2241 bool found = false;
2242 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002243 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2244 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 +00002245 found = true;
2246 break;
2247 }
2248 }
2249 if (!found) {
2250 printf("TypeError: super() call cannot find self\n");
2251 return;
2252 }
Damien George922ddd62014-04-09 12:43:17 +01002253 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002254 return;
2255 }
2256#endif
2257
Damien George02a4c052014-04-09 12:50:58 +01002258 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002259 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002260 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002261 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002262
Damien Georgebbcd49a2014-02-06 20:30:16 +00002263 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002264
2265 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002266 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002267 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002268 n_positional -= 1;
2269 }
Damien George922ddd62014-04-09 12:43:17 +01002270 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002271 n_positional -= 1;
2272 }
2273
2274 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002275 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002276 } else {
Damien George922ddd62014-04-09 12:43:17 +01002277 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002278 }
2279
2280 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002281 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002282}
2283
Damiend99b0522013-12-21 18:17:45 +00002284void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2285 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002286 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002287 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 +01002288 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002289 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2290 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002291 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002292 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002293 i += 1;
2294 } else {
2295 compile_node(comp, pns->nodes[i]);
2296 }
Damien George35e2a4e2014-02-05 00:51:47 +00002297 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002298 }
2299}
2300
Damiend99b0522013-12-21 18:17:45 +00002301void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002302 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002303 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002304}
2305
Damiend99b0522013-12-21 18:17:45 +00002306void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002307 // a list of strings
Damien63321742013-12-10 17:41:49 +00002308
2309 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002310 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002311 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002312 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002313 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002314 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2315 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2316 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002317 if (i == 0) {
2318 string_kind = pn_kind;
2319 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002320 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002321 return;
2322 }
Damien George55baff42014-01-21 21:40:13 +00002323 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002324 }
Damien63321742013-12-10 17:41:49 +00002325
Damien63321742013-12-10 17:41:49 +00002326 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002327 byte *q_ptr;
2328 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002329 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002330 uint s_len;
2331 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002332 memcpy(s_dest, s, s_len);
2333 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002334 }
Damien George55baff42014-01-21 21:40:13 +00002335 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002336
Damien Georgeb9791222014-01-23 00:34:21 +00002337 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002338}
2339
2340// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002341void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2342 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2343 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2344 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002345
2346 if (comp->pass == PASS_1) {
2347 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002348 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 +01002349 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002350 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002351 }
2352
2353 // get the scope for this comprehension
2354 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2355
2356 // compile the comprehension
2357 close_over_variables_etc(comp, this_scope, 0, 0);
2358
2359 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2360 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002361 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002362}
2363
Damiend99b0522013-12-21 18:17:45 +00002364void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2365 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002366 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002367 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2368 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2369 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2370 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2371 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2372 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2373 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002374 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002375 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002376 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002377 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002378 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002379 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002380 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002381 // generator expression
2382 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2383 } else {
2384 // tuple with 2 items
2385 goto tuple_with_2_items;
2386 }
2387 } else {
2388 // tuple with 2 items
2389 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002390 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002391 }
2392 } else {
2393 // parenthesis around a single item, is just that item
2394 compile_node(comp, pns->nodes[0]);
2395 }
2396}
2397
Damiend99b0522013-12-21 18:17:45 +00002398void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2399 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002400 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002401 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2403 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2404 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2405 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2406 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002407 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002408 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002409 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002410 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002411 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002412 // list of many items
2413 compile_node(comp, pns2->nodes[0]);
2414 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002415 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002416 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002417 // list comprehension
2418 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2419 } else {
2420 // list with 2 items
2421 goto list_with_2_items;
2422 }
2423 } else {
2424 // list with 2 items
2425 list_with_2_items:
2426 compile_node(comp, pns2->nodes[0]);
2427 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002428 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002429 }
2430 } else {
2431 // list with 1 item
2432 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002433 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002434 }
2435}
2436
Damiend99b0522013-12-21 18:17:45 +00002437void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2438 mp_parse_node_t pn = pns->nodes[0];
2439 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002440 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002441 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002442 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2443 pns = (mp_parse_node_struct_t*)pn;
2444 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002445 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002446 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002447 compile_node(comp, pn);
2448 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2450 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2451 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2452 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002453 // dict/set with multiple elements
2454
2455 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002456 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002457 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2458
2459 // first element sets whether it's a dict or set
2460 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002461 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002462 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002463 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002464 compile_node(comp, pns->nodes[0]);
2465 EMIT(store_map);
2466 is_dict = true;
2467 } else {
2468 // a set
2469 compile_node(comp, pns->nodes[0]); // 1st value of set
2470 is_dict = false;
2471 }
2472
2473 // process rest of elements
2474 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002475 mp_parse_node_t pn = nodes[i];
2476 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002477 compile_node(comp, pn);
2478 if (is_dict) {
2479 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002480 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002481 return;
2482 }
2483 EMIT(store_map);
2484 } else {
2485 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002486 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002487 return;
2488 }
2489 }
2490 }
2491
2492 // if it's a set, build it
2493 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002495 }
Damiend99b0522013-12-21 18:17:45 +00002496 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002497 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002498 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002499 // a dictionary comprehension
2500 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2501 } else {
2502 // a set comprehension
2503 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2504 }
2505 } else {
2506 // shouldn't happen
2507 assert(0);
2508 }
2509 } else {
2510 // set with one element
2511 goto set_with_one_element;
2512 }
2513 } else {
2514 // set with one element
2515 set_with_one_element:
2516 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002517 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002518 }
2519}
2520
Damiend99b0522013-12-21 18:17:45 +00002521void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002522 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002526 // object who's index we want is on top of stack
2527 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002528 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002529}
2530
Damiend99b0522013-12-21 18:17:45 +00002531void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002532 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002533 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002534}
2535
Damiend99b0522013-12-21 18:17:45 +00002536void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2537 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2538 mp_parse_node_t pn = pns->nodes[0];
2539 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002540 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2542 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002543 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2544 pns = (mp_parse_node_struct_t*)pn;
2545 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002546 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002547 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002548 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002549 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002551 } else {
2552 // [?::x]
2553 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002555 }
Damiend99b0522013-12-21 18:17:45 +00002556 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002557 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002558 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2559 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2560 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2561 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002562 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002563 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002564 } else {
2565 // [?:x:x]
2566 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002567 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002568 }
2569 } else {
2570 // [?:x]
2571 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002572 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002573 }
2574 } else {
2575 // [?:x]
2576 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002577 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002578 }
2579}
2580
Damiend99b0522013-12-21 18:17:45 +00002581void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002582 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002583 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2584 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002585}
2586
Damiend99b0522013-12-21 18:17:45 +00002587void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002588 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002589 compile_subscript_3_helper(comp, pns);
2590}
2591
Damiend99b0522013-12-21 18:17:45 +00002592void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002593 // if this is called then we are compiling a dict key:value pair
2594 compile_node(comp, pns->nodes[1]); // value
2595 compile_node(comp, pns->nodes[0]); // key
2596}
2597
Damiend99b0522013-12-21 18:17:45 +00002598void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002599 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002600 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002601 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002602}
2603
Damiend99b0522013-12-21 18:17:45 +00002604void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002605 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002606 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002607 return;
2608 }
Damien George922ddd62014-04-09 12:43:17 +01002609 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002610 compile_node(comp, pns->nodes[0]);
2611}
2612
Damiend99b0522013-12-21 18:17:45 +00002613void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002614 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002615 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002616 return;
2617 }
Damien George922ddd62014-04-09 12:43:17 +01002618 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002619 compile_node(comp, pns->nodes[0]);
2620}
2621
Damiend99b0522013-12-21 18:17:45 +00002622void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2623 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2624 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2625 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2626 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002627 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 +01002628 return;
2629 }
Damien Georgeb9791222014-01-23 00:34:21 +00002630 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002631 compile_node(comp, pns2->nodes[0]);
2632 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002633 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002634 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2635 } else {
2636 // shouldn't happen
2637 assert(0);
2638 }
2639}
2640
Damiend99b0522013-12-21 18:17:45 +00002641void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002642 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002643 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002644 return;
2645 }
Damiend99b0522013-12-21 18:17:45 +00002646 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002647 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002648 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002649 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2650 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002651 compile_node(comp, pns->nodes[0]);
2652 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002653 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002654 EMIT(yield_from);
2655 } else {
2656 compile_node(comp, pns->nodes[0]);
2657 EMIT(yield_value);
2658 }
2659}
2660
Damiend99b0522013-12-21 18:17:45 +00002661typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002662STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002663 NULL,
2664#define nc NULL
2665#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002666#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002667#include "grammar.h"
2668#undef nc
2669#undef c
2670#undef DEF_RULE
2671};
2672
Damiend99b0522013-12-21 18:17:45 +00002673void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2674 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002675 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002676 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2677 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2678 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002679 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002680 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002681 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002682 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002683 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2684 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2685 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2686 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002687 case MP_PARSE_NODE_TOKEN:
2688 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002689 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002690 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002691 // do nothing
2692 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002693 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002694 }
2695 break;
Damien429d7192013-10-04 19:53:11 +01002696 default: assert(0);
2697 }
2698 } else {
Damiend99b0522013-12-21 18:17:45 +00002699 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002700 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002701 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002702 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002703 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002704#if MICROPY_DEBUG_PRINTERS
2705 mp_parse_node_print(pn, 0);
2706#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002707 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002708 } else {
2709 f(comp, pns);
2710 }
2711 }
2712}
2713
Damiend99b0522013-12-21 18:17:45 +00002714void 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 +01002715 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002716 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002717 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2718 if (MP_PARSE_NODE_IS_ID(pn)) {
2719 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002720 if (comp->have_bare_star) {
2721 // comes after a bare star, so doesn't count as a parameter
2722 } else {
2723 comp->scope_cur->num_params += 1;
2724 }
Damienb14de212013-10-06 00:28:28 +01002725 } else {
Damiend99b0522013-12-21 18:17:45 +00002726 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2727 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2728 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2729 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002730 //int node_index = 1; unused
2731 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002732 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002733 // this parameter has an annotation
2734 pn_annotation = pns->nodes[1];
2735 }
2736 //node_index = 2; unused
2737 }
2738 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002739 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002740 // this parameter has a default value
2741 if (comp->have_bare_star) {
2742 comp->scope_cur->num_dict_params += 1;
2743 } else {
2744 comp->scope_cur->num_default_params += 1;
2745 }
2746 }
2747 */
2748 if (comp->have_bare_star) {
2749 // comes after a bare star, so doesn't count as a parameter
2750 } else {
2751 comp->scope_cur->num_params += 1;
2752 }
Damiend99b0522013-12-21 18:17:45 +00002753 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2754 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002755 // bare star
2756 // TODO see http://www.python.org/dev/peps/pep-3102/
2757 comp->have_bare_star = true;
2758 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002759 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002760 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002761 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002762 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2763 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002764 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002765 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002766 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2767 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002768 pn_annotation = pns->nodes[1];
2769 } else {
2770 // shouldn't happen
2771 assert(0);
2772 }
Damiend99b0522013-12-21 18:17:45 +00002773 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2774 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2775 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002776 // this parameter has an annotation
2777 pn_annotation = pns->nodes[1];
2778 }
Damien George8725f8f2014-02-15 19:33:11 +00002779 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002780 } else {
Damienb14de212013-10-06 00:28:28 +01002781 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002782 assert(0);
2783 }
Damien429d7192013-10-04 19:53:11 +01002784 }
2785
2786 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002787 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002788 // TODO this parameter has an annotation
2789 }
2790 bool added;
2791 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2792 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002793 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002794 return;
2795 }
Damien429d7192013-10-04 19:53:11 +01002796 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002797 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002798 }
2799}
2800
Damiend99b0522013-12-21 18:17:45 +00002801void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002802 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2803}
2804
Damiend99b0522013-12-21 18:17:45 +00002805void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002806 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2807}
2808
Damiend99b0522013-12-21 18:17:45 +00002809void 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 +01002810 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002811 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002812 // no more nested if/for; compile inner expression
2813 compile_node(comp, pn_inner_expr);
2814 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002815 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002816 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002817 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002818 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002820 } else {
2821 EMIT(yield_value);
2822 EMIT(pop_top);
2823 }
Damiend99b0522013-12-21 18:17:45 +00002824 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002825 // if condition
Damiend99b0522013-12-21 18:17:45 +00002826 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002827 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2828 pn_iter = pns_comp_if->nodes[1];
2829 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002830 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002831 // for loop
Damiend99b0522013-12-21 18:17:45 +00002832 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002833 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002834 uint l_end2 = comp_next_label(comp);
2835 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002836 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(label_assign, l_top2);
2838 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002839 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2840 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 +00002841 EMIT_ARG(jump, l_top2);
2842 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002843 EMIT(for_iter_end);
2844 } else {
2845 // shouldn't happen
2846 assert(0);
2847 }
2848}
2849
Damiend99b0522013-12-21 18:17:45 +00002850void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002851 // see http://www.python.org/dev/peps/pep-0257/
2852
2853 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002854 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002855 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002856 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002857 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002858 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2859 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002860 for (int i = 0; i < num_nodes; i++) {
2861 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002862 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 +00002863 // not a newline, so this is the first statement; finish search
2864 break;
2865 }
2866 }
2867 // 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 +00002868 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002869 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002870 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002871 } else {
2872 return;
2873 }
2874
2875 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002876 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2877 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2878 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2879 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2880 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002881 compile_node(comp, pns->nodes[0]); // a doc string
2882 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002883 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002884 }
2885 }
2886 }
2887}
2888
2889void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2890 comp->pass = pass;
2891 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002892 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002894
2895 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002896 // reset maximum stack sizes in scope
2897 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002898 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002899 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002900 }
2901
Damien5ac1b2e2013-10-18 19:58:12 +01002902#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002903 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002904 scope_print_info(scope);
2905 }
Damien5ac1b2e2013-10-18 19:58:12 +01002906#endif
Damien429d7192013-10-04 19:53:11 +01002907
2908 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002909 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2910 assert(scope->kind == SCOPE_MODULE);
2911 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2912 compile_node(comp, pns->nodes[0]); // compile the expression
2913 EMIT(return_value);
2914 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002915 if (!comp->is_repl) {
2916 check_for_doc_string(comp, scope->pn);
2917 }
Damien429d7192013-10-04 19:53:11 +01002918 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002919 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002920 EMIT(return_value);
2921 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002922 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2923 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2924 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002925
2926 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002927 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002928 if (comp->pass == PASS_1) {
2929 comp->have_bare_star = false;
2930 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2931 }
2932
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002933 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002934
2935 compile_node(comp, pns->nodes[3]); // 3 is function body
2936 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002937 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002938 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002939 EMIT(return_value);
2940 }
2941 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002942 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2943 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2944 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002945
2946 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002947 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002948 if (comp->pass == PASS_1) {
2949 comp->have_bare_star = false;
2950 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2951 }
2952
2953 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2954 EMIT(return_value);
2955 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2956 // a bit of a hack at the moment
2957
Damiend99b0522013-12-21 18:17:45 +00002958 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2959 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2960 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2961 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2962 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002963
Damien George55baff42014-01-21 21:40:13 +00002964 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002965 if (comp->pass == PASS_1) {
2966 bool added;
2967 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2968 assert(added);
2969 id_info->kind = ID_INFO_KIND_LOCAL;
2970 scope->num_params = 1;
2971 }
2972
2973 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002975 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002976 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002977 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002978 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002979 }
2980
Damien George6f355fd2014-04-10 14:11:31 +01002981 uint l_end = comp_next_label(comp);
2982 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002983 EMIT_ARG(load_id, qstr_arg);
2984 EMIT_ARG(label_assign, l_top);
2985 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002986 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2987 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002988 EMIT_ARG(jump, l_top);
2989 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002990 EMIT(for_iter_end);
2991
2992 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002993 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002994 }
2995 EMIT(return_value);
2996 } else {
2997 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002998 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2999 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3000 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003001
3002 if (comp->pass == PASS_1) {
3003 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003004 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003005 assert(added);
3006 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003007 }
3008
Damien Georgeb9791222014-01-23 00:34:21 +00003009 EMIT_ARG(load_id, MP_QSTR___name__);
3010 EMIT_ARG(store_id, MP_QSTR___module__);
3011 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3012 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003013
3014 check_for_doc_string(comp, pns->nodes[2]);
3015 compile_node(comp, pns->nodes[2]); // 2 is class body
3016
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003017 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003018 assert(id != NULL);
3019 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003020 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003021 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003022#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003023 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003024#else
Damien George2bf7c092014-04-09 15:26:46 +01003025 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003026#endif
Damien429d7192013-10-04 19:53:11 +01003027 }
3028 EMIT(return_value);
3029 }
3030
Damien415eb6f2013-10-05 12:19:06 +01003031 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003032
3033 // make sure we match all the exception levels
3034 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003035}
3036
Damien George094d4502014-04-02 17:31:27 +01003037#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003038void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3039 comp->pass = pass;
3040 comp->scope_cur = scope;
3041 comp->next_label = 1;
3042
3043 if (scope->kind != SCOPE_FUNCTION) {
3044 printf("Error: inline assembler must be a function\n");
3045 return;
3046 }
3047
Damiena2f2f7d2013-10-06 00:14:13 +01003048 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003049 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003050 }
3051
Damien826005c2013-10-05 23:17:28 +01003052 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003053 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3054 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3055 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003056
Damiend99b0522013-12-21 18:17:45 +00003057 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003058
Damiena2f2f7d2013-10-06 00:14:13 +01003059 // parameters are in pns->nodes[1]
3060 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003061 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003062 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003063 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003064 }
3065
Damiend99b0522013-12-21 18:17:45 +00003066 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003067
Damiend99b0522013-12-21 18:17:45 +00003068 mp_parse_node_t pn_body = pns->nodes[3]; // body
3069 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003070 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3071
Damien Georgecbd2f742014-01-19 11:48:48 +00003072 /*
Damien826005c2013-10-05 23:17:28 +01003073 if (comp->pass == PASS_3) {
3074 //printf("----\n");
3075 scope_print_info(scope);
3076 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003077 */
Damien826005c2013-10-05 23:17:28 +01003078
3079 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003080 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3081 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3082 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3083 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3084 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3085 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3086 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3087 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3088 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3089 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3090 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3091 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3092 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003093 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3094
3095 // emit instructions
3096 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003097 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003098 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003099 return;
3100 }
Damien George6f355fd2014-04-10 14:11:31 +01003101 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003102 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003103 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003104 }
3105 } else {
3106 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003107 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003108 }
3109 }
3110 }
3111
3112 if (comp->pass > PASS_1) {
3113 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003114 }
Damien429d7192013-10-04 19:53:11 +01003115}
Damien George094d4502014-04-02 17:31:27 +01003116#endif
Damien429d7192013-10-04 19:53:11 +01003117
3118void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3119 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003120 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003121 scope->num_locals = 0;
3122 for (int i = 0; i < scope->id_info_len; i++) {
3123 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003124 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003125 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3126 continue;
3127 }
3128 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3129 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3130 }
Damien9ecbcff2013-12-11 00:41:43 +00003131 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003132 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003133 id->local_num = scope->num_locals;
3134 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003135 }
3136 }
3137
3138 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003139#if MICROPY_EMIT_CPYTHON
3140 int num_cell = 0;
3141#endif
Damien9ecbcff2013-12-11 00:41:43 +00003142 for (int i = 0; i < scope->id_info_len; i++) {
3143 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003144#if MICROPY_EMIT_CPYTHON
3145 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003146 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003147 id->local_num = num_cell;
3148 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003149 }
Damien George6baf76e2013-12-30 22:32:17 +00003150#else
3151 // in Micro Python the cells come right after the fast locals
3152 // parameters are not counted here, since they remain at the start
3153 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003154 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003155 id->local_num = scope->num_locals;
3156 scope->num_locals += 1;
3157 }
3158#endif
Damien9ecbcff2013-12-11 00:41:43 +00003159 }
Damien9ecbcff2013-12-11 00:41:43 +00003160
3161 // compute the index of free vars (freevars[idx] in CPython)
3162 // make sure they are in the order of the parent scope
3163 if (scope->parent != NULL) {
3164 int num_free = 0;
3165 for (int i = 0; i < scope->parent->id_info_len; i++) {
3166 id_info_t *id = &scope->parent->id_info[i];
3167 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3168 for (int j = 0; j < scope->id_info_len; j++) {
3169 id_info_t *id2 = &scope->id_info[j];
3170 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003171 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003172#if MICROPY_EMIT_CPYTHON
3173 // in CPython the frees are numbered after the cells
3174 id2->local_num = num_cell + num_free;
3175#else
3176 // in Micro Python the frees come first, before the params
3177 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003178#endif
3179 num_free += 1;
3180 }
3181 }
3182 }
Damien429d7192013-10-04 19:53:11 +01003183 }
Damien George6baf76e2013-12-30 22:32:17 +00003184#if !MICROPY_EMIT_CPYTHON
3185 // in Micro Python shift all other locals after the free locals
3186 if (num_free > 0) {
3187 for (int i = 0; i < scope->id_info_len; i++) {
3188 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003189 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003190 id->local_num += num_free;
3191 }
3192 }
3193 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3194 scope->num_locals += num_free;
3195 }
3196#endif
Damien429d7192013-10-04 19:53:11 +01003197 }
3198
Damien George8725f8f2014-02-15 19:33:11 +00003199 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003200
3201#if MICROPY_EMIT_CPYTHON
3202 // these flags computed here are for CPython compatibility only
3203 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003204 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003205 }
3206 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) {
3207 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003208 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003209
3210 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003211 // Note that we don't actually use this information at the moment (for CPython compat only)
3212 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 +00003213 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003214 }
3215 }
Damien George882b3632014-04-02 15:56:31 +01003216#endif
3217
Damien429d7192013-10-04 19:53:11 +01003218 int num_free = 0;
3219 for (int i = 0; i < scope->id_info_len; i++) {
3220 id_info_t *id = &scope->id_info[i];
3221 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3222 num_free += 1;
3223 }
3224 }
3225 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003226 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003227 }
3228}
3229
Damien George65cad122014-04-06 11:48:15 +01003230mp_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 +01003231 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003232 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003233 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003234
Damien826005c2013-10-05 23:17:28 +01003235 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003236 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003237
3238 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003239 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003240
Damien826005c2013-10-05 23:17:28 +01003241 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003242 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003243 comp->emit_method_table = &emit_pass1_method_table;
3244 comp->emit_inline_asm = NULL;
3245 comp->emit_inline_asm_method_table = NULL;
3246 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003247 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003248 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003249#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003250 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003251 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003252#endif
Damien826005c2013-10-05 23:17:28 +01003253 } else {
3254 compile_scope(comp, s, PASS_1);
3255 }
3256
3257 // update maximim number of labels needed
3258 if (comp->next_label > max_num_labels) {
3259 max_num_labels = comp->next_label;
3260 }
Damien429d7192013-10-04 19:53:11 +01003261 }
3262
Damien826005c2013-10-05 23:17:28 +01003263 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003264 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003265 compile_scope_compute_things(comp, s);
3266 }
3267
Damien826005c2013-10-05 23:17:28 +01003268 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003269 emit_pass1_free(comp->emit);
3270
Damien826005c2013-10-05 23:17:28 +01003271 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003272#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003273 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003274#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003275 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003276#endif
Damien3ef4abb2013-10-12 16:53:13 +01003277#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003278 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003279#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003280#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003281 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003282 if (false) {
3283 // dummy
3284
Damien3ef4abb2013-10-12 16:53:13 +01003285#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003286 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003287 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003288 if (emit_inline_thumb == NULL) {
3289 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3290 }
3291 comp->emit = NULL;
3292 comp->emit_method_table = NULL;
3293 comp->emit_inline_asm = emit_inline_thumb;
3294 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3295 compile_scope_inline_asm(comp, s, PASS_2);
3296 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003297#endif
3298
Damien826005c2013-10-05 23:17:28 +01003299 } else {
Damienc025ebb2013-10-12 14:30:21 +01003300
3301 // choose the emit type
3302
Damien3ef4abb2013-10-12 16:53:13 +01003303#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003304 comp->emit = emit_cpython_new(max_num_labels);
3305 comp->emit_method_table = &emit_cpython_method_table;
3306#else
Damien826005c2013-10-05 23:17:28 +01003307 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003308
3309#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003310 case MP_EMIT_OPT_NATIVE_PYTHON:
3311 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003312#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003313 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003314 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003315 }
Damien13ed3a62013-10-08 09:05:10 +01003316 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003317#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003318 if (emit_native == NULL) {
3319 emit_native = emit_native_thumb_new(max_num_labels);
3320 }
3321 comp->emit_method_table = &emit_native_thumb_method_table;
3322#endif
3323 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003324 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003325 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003326#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003327
Damien826005c2013-10-05 23:17:28 +01003328 default:
3329 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003330 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003331 }
3332 comp->emit = emit_bc;
3333 comp->emit_method_table = &emit_bc_method_table;
3334 break;
3335 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003336#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003337
3338 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003339 compile_scope(comp, s, PASS_2);
3340 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003341 }
Damien429d7192013-10-04 19:53:11 +01003342 }
3343
Damien George41d02b62014-01-24 22:42:28 +00003344 // free the emitters
3345#if !MICROPY_EMIT_CPYTHON
3346 if (emit_bc != NULL) {
3347 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003348 }
Damien George41d02b62014-01-24 22:42:28 +00003349#if MICROPY_EMIT_NATIVE
3350 if (emit_native != NULL) {
3351#if MICROPY_EMIT_X64
3352 emit_native_x64_free(emit_native);
3353#elif MICROPY_EMIT_THUMB
3354 emit_native_thumb_free(emit_native);
3355#endif
3356 }
3357#endif
3358#if MICROPY_EMIT_INLINE_THUMB
3359 if (emit_inline_thumb != NULL) {
3360 emit_inline_thumb_free(emit_inline_thumb);
3361 }
3362#endif
3363#endif // !MICROPY_EMIT_CPYTHON
3364
3365 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003366 uint unique_code_id = module_scope->unique_code_id;
3367 for (scope_t *s = module_scope; s;) {
3368 scope_t *next = s->next;
3369 scope_free(s);
3370 s = next;
3371 }
Damien5ac1b2e2013-10-18 19:58:12 +01003372
Damien George41d02b62014-01-24 22:42:28 +00003373 // free the compiler
3374 bool had_error = comp->had_error;
3375 m_del_obj(compiler_t, comp);
3376
Damien George1fb03172014-01-03 14:22:03 +00003377 if (had_error) {
3378 // TODO return a proper error message
3379 return mp_const_none;
3380 } else {
3381#if MICROPY_EMIT_CPYTHON
3382 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003383 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003384 return mp_const_true;
3385#else
3386 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003387 // 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 +01003388 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003389#endif
3390 }
Damien429d7192013-10-04 19:53:11 +01003391}