blob: 9a5534a211c42e8e3b38714ebfbf567b611b2fa1 [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 // this function is a bit of a hack at the moment
1720 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1721
1722 // setup code
1723 int stack_size = EMIT(get_stack_size);
Damien George6f355fd2014-04-10 14:11:31 +01001724 uint l1 = comp_next_label(comp);
1725 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001726
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001728 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001729
Damien429d7192013-10-04 19:53:11 +01001730 compile_node(comp, pn_body); // body
1731 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001732 EMIT_ARG(jump, success_label);
1733 EMIT_ARG(label_assign, l1);
Damien George6f355fd2014-04-10 14:11:31 +01001734 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001735
1736 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001737 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1738 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001739
1740 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001741 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001742
Damiend99b0522013-12-21 18:17:45 +00001743 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001744 // this is a catch all exception handler
1745 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001746 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001747 return;
1748 }
1749 } else {
1750 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001751 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1752 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1753 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1754 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001755 // handler binds the exception to a local
1756 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001757 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001758 }
1759 }
1760 EMIT(dup_top);
1761 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001762 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001763 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001764 }
1765
1766 EMIT(pop_top);
1767
1768 if (qstr_exception_local == 0) {
1769 EMIT(pop_top);
1770 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001772 }
1773
1774 EMIT(pop_top);
1775
Damien George6f355fd2014-04-10 14:11:31 +01001776 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001777 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001778 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001779 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001780 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001781 }
1782 compile_node(comp, pns_except->nodes[1]);
1783 if (qstr_exception_local != 0) {
1784 EMIT(pop_block);
1785 }
1786 EMIT(pop_except);
1787 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001788 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1789 EMIT_ARG(label_assign, l3);
1790 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1791 EMIT_ARG(store_id, qstr_exception_local);
1792 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001793
Damien George8dcc0c72014-03-27 10:55:21 +00001794 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001795 EMIT(end_finally);
1796 }
Damien Georgeb9791222014-01-23 00:34:21 +00001797 EMIT_ARG(jump, l2);
1798 EMIT_ARG(label_assign, end_finally_label);
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 Georgecbddb272014-02-01 20:08:18 +00001803
Damien Georgeb9791222014-01-23 00:34:21 +00001804 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001805 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001806 EMIT_ARG(label_assign, l2);
1807 EMIT_ARG(set_stack_size, stack_size);
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) {
Damien429d7192013-10-04 19:53:11 +01001811 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1812 int stack_size = EMIT(get_stack_size);
Damien George6f355fd2014-04-10 14:11:31 +01001813 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001814
Damien Georgeb9791222014-01-23 00:34:21 +00001815 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001816 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001817
Damien429d7192013-10-04 19:53:11 +01001818 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001819 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001820 compile_node(comp, pn_body);
1821 } 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);
Damien Georgecbddb272014-02-01 20:08:18 +00001831
Damien Georgeb9791222014-01-23 00:34:21 +00001832 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001833}
1834
Damiend99b0522013-12-21 18:17:45 +00001835void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1836 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1837 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1838 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001839 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001840 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1841 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001842 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001843 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001844 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001845 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001846 // no finally
1847 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1848 } else {
1849 // have finally
Damiend99b0522013-12-21 18:17:45 +00001850 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 +01001851 }
1852 } else {
1853 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001854 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001855 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001856 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001857 }
1858 } else {
1859 // shouldn't happen
1860 assert(0);
1861 }
1862}
1863
Damiend99b0522013-12-21 18:17:45 +00001864void 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 +01001865 if (n == 0) {
1866 // no more pre-bits, compile the body of the with
1867 compile_node(comp, body);
1868 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001869 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001870 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001871 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001872 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001873 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001874 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001875 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1876 } else {
1877 // this pre-bit is just an expression
1878 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001879 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001880 EMIT(pop_top);
1881 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001882 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001883 // compile additional pre-bits and the body
1884 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1885 // finish this with block
1886 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001887 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1888 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001889 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001890 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001891 EMIT(end_finally);
1892 }
1893}
1894
Damiend99b0522013-12-21 18:17:45 +00001895void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001896 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001897 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001898 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1899 assert(n > 0);
1900
1901 // compile in a nested fashion
1902 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1903}
1904
Damiend99b0522013-12-21 18:17:45 +00001905void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1906 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001907 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1908 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001909 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001910 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001911 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001912 EMIT(pop_top);
1913
Damien429d7192013-10-04 19:53:11 +01001914 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001915 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001916 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001917 // do nothing with a lonely constant
1918 } else {
1919 compile_node(comp, pns->nodes[0]); // just an expression
1920 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1921 }
Damien429d7192013-10-04 19:53:11 +01001922 }
1923 } else {
Damiend99b0522013-12-21 18:17:45 +00001924 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1925 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001926 if (kind == PN_expr_stmt_augassign) {
1927 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1928 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001929 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001930 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001931 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001932 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1933 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1934 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1935 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1936 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1937 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1938 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1939 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1940 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1941 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1942 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1943 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1944 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001945 }
Damien George7e5fb242014-02-01 22:18:47 +00001946 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001947 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1948 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001949 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1950 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001951 // following CPython, we store left-most first
1952 if (rhs > 0) {
1953 EMIT(dup_top);
1954 }
1955 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1956 for (int i = 0; i < rhs; i++) {
1957 if (i + 1 < rhs) {
1958 EMIT(dup_top);
1959 }
Damiend99b0522013-12-21 18:17:45 +00001960 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001961 }
1962 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001963 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1964 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1965 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1966 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001967 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001968 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1969 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001970 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1971 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1972 // can't optimise when it's a star expression on the lhs
1973 goto no_optimisation;
1974 }
Damien429d7192013-10-04 19:53:11 +01001975 compile_node(comp, pns10->nodes[0]); // rhs
1976 compile_node(comp, pns10->nodes[1]); // rhs
1977 EMIT(rot_two);
1978 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1979 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001980 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1981 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1982 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1983 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001984 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001985 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1986 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001987 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1988 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1989 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1990 // can't optimise when it's a star expression on the lhs
1991 goto no_optimisation;
1992 }
Damien429d7192013-10-04 19:53:11 +01001993 compile_node(comp, pns10->nodes[0]); // rhs
1994 compile_node(comp, pns10->nodes[1]); // rhs
1995 compile_node(comp, pns10->nodes[2]); // rhs
1996 EMIT(rot_three);
1997 EMIT(rot_two);
1998 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1999 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2000 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2001 } else {
Damien George495d7812014-04-08 17:51:47 +01002002 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002003 compile_node(comp, pns1->nodes[0]); // rhs
2004 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2005 }
2006 } else {
2007 // shouldn't happen
2008 assert(0);
2009 }
2010 }
2011}
2012
Damien Georged17926d2014-03-30 13:35:08 +01002013void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002014 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002015 compile_node(comp, pns->nodes[0]);
2016 for (int i = 1; i < num_nodes; i += 1) {
2017 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002018 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002019 }
2020}
2021
Damiend99b0522013-12-21 18:17:45 +00002022void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2023 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2024 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002025
2026 int stack_size = EMIT(get_stack_size);
Damien George6f355fd2014-04-10 14:11:31 +01002027 uint l_fail = comp_next_label(comp);
2028 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002029 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2030 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002031 EMIT_ARG(jump, l_end);
2032 EMIT_ARG(label_assign, l_fail);
2033 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01002034 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002035 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002036}
2037
Damiend99b0522013-12-21 18:17:45 +00002038void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002039 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002040 //mp_parse_node_t pn_params = pns->nodes[0];
2041 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002042
2043 if (comp->pass == PASS_1) {
2044 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002045 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 +01002046 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002047 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002048 }
2049
2050 // get the scope for this lambda
2051 scope_t *this_scope = (scope_t*)pns->nodes[2];
2052
2053 // make the lambda
2054 close_over_variables_etc(comp, this_scope, 0, 0);
2055}
2056
Damiend99b0522013-12-21 18:17:45 +00002057void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002058 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002059 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002060 for (int i = 0; i < n; i += 1) {
2061 compile_node(comp, pns->nodes[i]);
2062 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002063 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002064 }
2065 }
Damien Georgeb9791222014-01-23 00:34:21 +00002066 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002067}
2068
Damiend99b0522013-12-21 18:17:45 +00002069void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002070 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002071 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002072 for (int i = 0; i < n; i += 1) {
2073 compile_node(comp, pns->nodes[i]);
2074 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002075 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002076 }
2077 }
Damien Georgeb9791222014-01-23 00:34:21 +00002078 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002079}
2080
Damiend99b0522013-12-21 18:17:45 +00002081void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002082 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002083 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002084}
2085
Damiend99b0522013-12-21 18:17:45 +00002086void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002087 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002088 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pns->nodes[0]);
2090 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002091 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002092 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002093 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002094 }
2095 for (int i = 1; i + 1 < num_nodes; i += 2) {
2096 compile_node(comp, pns->nodes[i + 1]);
2097 if (i + 2 < num_nodes) {
2098 EMIT(dup_top);
2099 EMIT(rot_three);
2100 }
Damien George7e5fb242014-02-01 22:18:47 +00002101 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002102 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002103 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002104 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2105 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2106 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2107 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2108 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2109 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2110 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2111 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002112 }
2113 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002114 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2115 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2116 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002117 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002118 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002119 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002120 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002121 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002122 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002123 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002124 }
2125 } else {
2126 // shouldn't happen
2127 assert(0);
2128 }
2129 } else {
2130 // shouldn't happen
2131 assert(0);
2132 }
2133 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002134 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002135 }
2136 }
2137 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002138 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002139 EMIT_ARG(jump, l_end);
2140 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002141 EMIT(rot_two);
2142 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002143 EMIT_ARG(label_assign, l_end);
2144 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002145 }
2146}
2147
Damiend99b0522013-12-21 18:17:45 +00002148void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002149 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002150}
2151
Damiend99b0522013-12-21 18:17:45 +00002152void compile_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_OR);
Damien429d7192013-10-04 19:53:11 +01002154}
2155
Damiend99b0522013-12-21 18:17:45 +00002156void compile_xor_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_XOR);
Damien429d7192013-10-04 19:53:11 +01002158}
2159
Damiend99b0522013-12-21 18:17:45 +00002160void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002161 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002162}
2163
Damiend99b0522013-12-21 18:17:45 +00002164void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2165 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002166 compile_node(comp, pns->nodes[0]);
2167 for (int i = 1; i + 1 < num_nodes; i += 2) {
2168 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002169 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002171 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002172 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002173 } else {
2174 // shouldn't happen
2175 assert(0);
2176 }
2177 }
2178}
2179
Damiend99b0522013-12-21 18:17:45 +00002180void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2181 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002182 compile_node(comp, pns->nodes[0]);
2183 for (int i = 1; i + 1 < num_nodes; i += 2) {
2184 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002185 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002186 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002187 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002188 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002189 } else {
2190 // shouldn't happen
2191 assert(0);
2192 }
2193 }
2194}
2195
Damiend99b0522013-12-21 18:17:45 +00002196void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2197 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002198 compile_node(comp, pns->nodes[0]);
2199 for (int i = 1; i + 1 < num_nodes; i += 2) {
2200 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002201 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002202 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002203 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002204 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002205 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002206 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002207 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002208 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002209 } else {
2210 // shouldn't happen
2211 assert(0);
2212 }
2213 }
2214}
2215
Damiend99b0522013-12-21 18:17:45 +00002216void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002217 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002218 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002219 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002220 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002221 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002222 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002223 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002224 } else {
2225 // shouldn't happen
2226 assert(0);
2227 }
2228}
2229
Damien George35e2a4e2014-02-05 00:51:47 +00002230void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2231 // this is to handle special super() call
2232 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2233
2234 compile_generic_all_nodes(comp, pns);
2235}
2236
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002237STATIC 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 +01002238 // function to call is on top of stack
2239
Damien George35e2a4e2014-02-05 00:51:47 +00002240#if !MICROPY_EMIT_CPYTHON
2241 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002242 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 +00002243 EMIT_ARG(load_id, MP_QSTR___class__);
2244 // get first argument to function
2245 bool found = false;
2246 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002247 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2248 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 +00002249 found = true;
2250 break;
2251 }
2252 }
2253 if (!found) {
2254 printf("TypeError: super() call cannot find self\n");
2255 return;
2256 }
Damien George922ddd62014-04-09 12:43:17 +01002257 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002258 return;
2259 }
2260#endif
2261
Damien George02a4c052014-04-09 12:50:58 +01002262 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002263 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002264 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002265 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002266
Damien Georgebbcd49a2014-02-06 20:30:16 +00002267 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002268
2269 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002270 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002271 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002272 n_positional -= 1;
2273 }
Damien George922ddd62014-04-09 12:43:17 +01002274 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002275 n_positional -= 1;
2276 }
2277
2278 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002279 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002280 } else {
Damien George922ddd62014-04-09 12:43:17 +01002281 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002282 }
2283
2284 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002285 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002286}
2287
Damiend99b0522013-12-21 18:17:45 +00002288void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2289 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002290 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002291 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 +01002292 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002293 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2294 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002295 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002296 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002297 i += 1;
2298 } else {
2299 compile_node(comp, pns->nodes[i]);
2300 }
Damien George35e2a4e2014-02-05 00:51:47 +00002301 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002302 }
2303}
2304
Damiend99b0522013-12-21 18:17:45 +00002305void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002306 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002307 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002308}
2309
Damiend99b0522013-12-21 18:17:45 +00002310void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002311 // a list of strings
Damien63321742013-12-10 17:41:49 +00002312
2313 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002314 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002315 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002316 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002317 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002318 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2319 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2320 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002321 if (i == 0) {
2322 string_kind = pn_kind;
2323 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002324 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002325 return;
2326 }
Damien George55baff42014-01-21 21:40:13 +00002327 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002328 }
Damien63321742013-12-10 17:41:49 +00002329
Damien63321742013-12-10 17:41:49 +00002330 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002331 byte *q_ptr;
2332 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002333 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002334 uint s_len;
2335 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002336 memcpy(s_dest, s, s_len);
2337 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002338 }
Damien George55baff42014-01-21 21:40:13 +00002339 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002340
Damien Georgeb9791222014-01-23 00:34:21 +00002341 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002342}
2343
2344// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002345void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2346 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2347 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2348 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002349
2350 if (comp->pass == PASS_1) {
2351 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002352 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 +01002353 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002354 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002355 }
2356
2357 // get the scope for this comprehension
2358 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2359
2360 // compile the comprehension
2361 close_over_variables_etc(comp, this_scope, 0, 0);
2362
2363 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2364 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002365 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002366}
2367
Damiend99b0522013-12-21 18:17:45 +00002368void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2369 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002370 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002371 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2372 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2373 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2374 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2375 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2376 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2377 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002378 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002379 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002380 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002381 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002382 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002383 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002384 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002385 // generator expression
2386 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2387 } else {
2388 // tuple with 2 items
2389 goto tuple_with_2_items;
2390 }
2391 } else {
2392 // tuple with 2 items
2393 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002394 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002395 }
2396 } else {
2397 // parenthesis around a single item, is just that item
2398 compile_node(comp, pns->nodes[0]);
2399 }
2400}
2401
Damiend99b0522013-12-21 18:17:45 +00002402void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2403 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002404 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002405 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002406 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2407 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2408 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2409 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2410 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002411 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002412 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002413 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002414 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002415 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002416 // list of many items
2417 compile_node(comp, pns2->nodes[0]);
2418 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002419 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002421 // list comprehension
2422 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2423 } else {
2424 // list with 2 items
2425 goto list_with_2_items;
2426 }
2427 } else {
2428 // list with 2 items
2429 list_with_2_items:
2430 compile_node(comp, pns2->nodes[0]);
2431 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002432 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002433 }
2434 } else {
2435 // list with 1 item
2436 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002437 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002438 }
2439}
2440
Damiend99b0522013-12-21 18:17:45 +00002441void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2442 mp_parse_node_t pn = pns->nodes[0];
2443 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002444 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002445 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2447 pns = (mp_parse_node_struct_t*)pn;
2448 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002449 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002450 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002451 compile_node(comp, pn);
2452 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002453 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2454 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2455 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2456 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002457 // dict/set with multiple elements
2458
2459 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002460 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002461 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2462
2463 // first element sets whether it's a dict or set
2464 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002465 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002466 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002467 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002468 compile_node(comp, pns->nodes[0]);
2469 EMIT(store_map);
2470 is_dict = true;
2471 } else {
2472 // a set
2473 compile_node(comp, pns->nodes[0]); // 1st value of set
2474 is_dict = false;
2475 }
2476
2477 // process rest of elements
2478 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002479 mp_parse_node_t pn = nodes[i];
2480 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002481 compile_node(comp, pn);
2482 if (is_dict) {
2483 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002484 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002485 return;
2486 }
2487 EMIT(store_map);
2488 } else {
2489 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002490 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002491 return;
2492 }
2493 }
2494 }
2495
2496 // if it's a set, build it
2497 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002498 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002499 }
Damiend99b0522013-12-21 18:17:45 +00002500 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002501 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002502 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002503 // a dictionary comprehension
2504 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2505 } else {
2506 // a set comprehension
2507 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2508 }
2509 } else {
2510 // shouldn't happen
2511 assert(0);
2512 }
2513 } else {
2514 // set with one element
2515 goto set_with_one_element;
2516 }
2517 } else {
2518 // set with one element
2519 set_with_one_element:
2520 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002521 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002522 }
2523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002526 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002527}
2528
Damiend99b0522013-12-21 18:17:45 +00002529void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002530 // object who's index we want is on top of stack
2531 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002532 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002533}
2534
Damiend99b0522013-12-21 18:17:45 +00002535void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002536 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002538}
2539
Damiend99b0522013-12-21 18:17:45 +00002540void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2541 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2542 mp_parse_node_t pn = pns->nodes[0];
2543 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002544 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002545 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2546 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002547 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2548 pns = (mp_parse_node_struct_t*)pn;
2549 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002551 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002552 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002553 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002555 } else {
2556 // [?::x]
2557 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002558 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002559 }
Damiend99b0522013-12-21 18:17:45 +00002560 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002561 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002562 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2563 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2564 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2565 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002566 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002567 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002568 } else {
2569 // [?:x:x]
2570 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002572 }
2573 } else {
2574 // [?:x]
2575 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002576 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002577 }
2578 } else {
2579 // [?:x]
2580 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002581 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002582 }
2583}
2584
Damiend99b0522013-12-21 18:17:45 +00002585void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002586 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002587 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2588 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002589}
2590
Damiend99b0522013-12-21 18:17:45 +00002591void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002592 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002593 compile_subscript_3_helper(comp, pns);
2594}
2595
Damiend99b0522013-12-21 18:17:45 +00002596void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002597 // if this is called then we are compiling a dict key:value pair
2598 compile_node(comp, pns->nodes[1]); // value
2599 compile_node(comp, pns->nodes[0]); // key
2600}
2601
Damiend99b0522013-12-21 18:17:45 +00002602void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002603 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002604 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002606}
2607
Damiend99b0522013-12-21 18:17:45 +00002608void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002609 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002610 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002611 return;
2612 }
Damien George922ddd62014-04-09 12:43:17 +01002613 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002614 compile_node(comp, pns->nodes[0]);
2615}
2616
Damiend99b0522013-12-21 18:17:45 +00002617void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002618 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002619 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002620 return;
2621 }
Damien George922ddd62014-04-09 12:43:17 +01002622 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002623 compile_node(comp, pns->nodes[0]);
2624}
2625
Damiend99b0522013-12-21 18:17:45 +00002626void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2627 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2628 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2629 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2630 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002631 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 +01002632 return;
2633 }
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002635 compile_node(comp, pns2->nodes[0]);
2636 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002637 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002638 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2639 } else {
2640 // shouldn't happen
2641 assert(0);
2642 }
2643}
2644
Damiend99b0522013-12-21 18:17:45 +00002645void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002646 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002647 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002648 return;
2649 }
Damiend99b0522013-12-21 18:17:45 +00002650 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002651 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002652 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002653 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2654 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002655 compile_node(comp, pns->nodes[0]);
2656 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002657 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002658 EMIT(yield_from);
2659 } else {
2660 compile_node(comp, pns->nodes[0]);
2661 EMIT(yield_value);
2662 }
2663}
2664
Damiend99b0522013-12-21 18:17:45 +00002665typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002666STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002667 NULL,
2668#define nc NULL
2669#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002670#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002671#include "grammar.h"
2672#undef nc
2673#undef c
2674#undef DEF_RULE
2675};
2676
Damiend99b0522013-12-21 18:17:45 +00002677void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2678 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002679 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002680 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2681 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2682 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002684 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002685 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002686 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002687 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2688 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2689 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2690 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002691 case MP_PARSE_NODE_TOKEN:
2692 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002693 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002694 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002695 // do nothing
2696 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002697 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002698 }
2699 break;
Damien429d7192013-10-04 19:53:11 +01002700 default: assert(0);
2701 }
2702 } else {
Damiend99b0522013-12-21 18:17:45 +00002703 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002705 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002706 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002707 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002708#if MICROPY_DEBUG_PRINTERS
2709 mp_parse_node_print(pn, 0);
2710#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002711 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002712 } else {
2713 f(comp, pns);
2714 }
2715 }
2716}
2717
Damiend99b0522013-12-21 18:17:45 +00002718void 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 +01002719 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002720 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002721 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2722 if (MP_PARSE_NODE_IS_ID(pn)) {
2723 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002724 if (comp->have_bare_star) {
2725 // comes after a bare star, so doesn't count as a parameter
2726 } else {
2727 comp->scope_cur->num_params += 1;
2728 }
Damienb14de212013-10-06 00:28:28 +01002729 } else {
Damiend99b0522013-12-21 18:17:45 +00002730 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2731 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2732 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2733 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002734 //int node_index = 1; unused
2735 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002736 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002737 // this parameter has an annotation
2738 pn_annotation = pns->nodes[1];
2739 }
2740 //node_index = 2; unused
2741 }
2742 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002743 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002744 // this parameter has a default value
2745 if (comp->have_bare_star) {
2746 comp->scope_cur->num_dict_params += 1;
2747 } else {
2748 comp->scope_cur->num_default_params += 1;
2749 }
2750 }
2751 */
2752 if (comp->have_bare_star) {
2753 // comes after a bare star, so doesn't count as a parameter
2754 } else {
2755 comp->scope_cur->num_params += 1;
2756 }
Damiend99b0522013-12-21 18:17:45 +00002757 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2758 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002759 // bare star
2760 // TODO see http://www.python.org/dev/peps/pep-3102/
2761 comp->have_bare_star = true;
2762 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002763 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002764 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002765 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002766 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2767 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002768 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002769 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002770 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2771 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002772 pn_annotation = pns->nodes[1];
2773 } else {
2774 // shouldn't happen
2775 assert(0);
2776 }
Damiend99b0522013-12-21 18:17:45 +00002777 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2778 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2779 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002780 // this parameter has an annotation
2781 pn_annotation = pns->nodes[1];
2782 }
Damien George8725f8f2014-02-15 19:33:11 +00002783 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002784 } else {
Damienb14de212013-10-06 00:28:28 +01002785 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002786 assert(0);
2787 }
Damien429d7192013-10-04 19:53:11 +01002788 }
2789
2790 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002791 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002792 // TODO this parameter has an annotation
2793 }
2794 bool added;
2795 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2796 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002797 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002798 return;
2799 }
Damien429d7192013-10-04 19:53:11 +01002800 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002801 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002802 }
2803}
2804
Damiend99b0522013-12-21 18:17:45 +00002805void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002806 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2807}
2808
Damiend99b0522013-12-21 18:17:45 +00002809void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002810 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2811}
2812
Damiend99b0522013-12-21 18:17:45 +00002813void 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 +01002814 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002815 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002816 // no more nested if/for; compile inner expression
2817 compile_node(comp, pn_inner_expr);
2818 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002820 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002821 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002822 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002824 } else {
2825 EMIT(yield_value);
2826 EMIT(pop_top);
2827 }
Damiend99b0522013-12-21 18:17:45 +00002828 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002829 // if condition
Damiend99b0522013-12-21 18:17:45 +00002830 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002831 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2832 pn_iter = pns_comp_if->nodes[1];
2833 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002834 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002835 // for loop
Damiend99b0522013-12-21 18:17:45 +00002836 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002837 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002838 uint l_end2 = comp_next_label(comp);
2839 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002840 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002841 EMIT_ARG(label_assign, l_top2);
2842 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002843 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2844 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 +00002845 EMIT_ARG(jump, l_top2);
2846 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002847 EMIT(for_iter_end);
2848 } else {
2849 // shouldn't happen
2850 assert(0);
2851 }
2852}
2853
Damiend99b0522013-12-21 18:17:45 +00002854void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002855 // see http://www.python.org/dev/peps/pep-0257/
2856
2857 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002858 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002859 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002860 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002861 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2863 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002864 for (int i = 0; i < num_nodes; i++) {
2865 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002866 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 +00002867 // not a newline, so this is the first statement; finish search
2868 break;
2869 }
2870 }
2871 // 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 +00002872 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002873 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002874 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002875 } else {
2876 return;
2877 }
2878
2879 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002880 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2881 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2882 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2883 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2884 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002885 compile_node(comp, pns->nodes[0]); // a doc string
2886 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002888 }
2889 }
2890 }
2891}
2892
2893void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2894 comp->pass = pass;
2895 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002896 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002898
2899 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002900 // reset maximum stack sizes in scope
2901 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002902 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002903 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002904 }
2905
Damien5ac1b2e2013-10-18 19:58:12 +01002906#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002907 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002908 scope_print_info(scope);
2909 }
Damien5ac1b2e2013-10-18 19:58:12 +01002910#endif
Damien429d7192013-10-04 19:53:11 +01002911
2912 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002913 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2914 assert(scope->kind == SCOPE_MODULE);
2915 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2916 compile_node(comp, pns->nodes[0]); // compile the expression
2917 EMIT(return_value);
2918 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002919 if (!comp->is_repl) {
2920 check_for_doc_string(comp, scope->pn);
2921 }
Damien429d7192013-10-04 19:53:11 +01002922 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002923 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002924 EMIT(return_value);
2925 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002926 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2927 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2928 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002929
2930 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002931 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002932 if (comp->pass == PASS_1) {
2933 comp->have_bare_star = false;
2934 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2935 }
2936
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002937 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002938
2939 compile_node(comp, pns->nodes[3]); // 3 is function body
2940 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002941 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002942 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002943 EMIT(return_value);
2944 }
2945 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002946 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2947 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2948 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002949
2950 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002951 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002952 if (comp->pass == PASS_1) {
2953 comp->have_bare_star = false;
2954 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2955 }
2956
2957 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2958 EMIT(return_value);
2959 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2960 // a bit of a hack at the moment
2961
Damiend99b0522013-12-21 18:17:45 +00002962 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2963 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2964 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2965 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2966 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002967
Damien George55baff42014-01-21 21:40:13 +00002968 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002969 if (comp->pass == PASS_1) {
2970 bool added;
2971 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2972 assert(added);
2973 id_info->kind = ID_INFO_KIND_LOCAL;
2974 scope->num_params = 1;
2975 }
2976
2977 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002978 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002979 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002980 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002981 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002982 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002983 }
2984
Damien George6f355fd2014-04-10 14:11:31 +01002985 uint l_end = comp_next_label(comp);
2986 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002987 EMIT_ARG(load_id, qstr_arg);
2988 EMIT_ARG(label_assign, l_top);
2989 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002990 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2991 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002992 EMIT_ARG(jump, l_top);
2993 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002994 EMIT(for_iter_end);
2995
2996 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002997 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002998 }
2999 EMIT(return_value);
3000 } else {
3001 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003002 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3003 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3004 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003005
3006 if (comp->pass == PASS_1) {
3007 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003008 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003009 assert(added);
3010 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003011 }
3012
Damien Georgeb9791222014-01-23 00:34:21 +00003013 EMIT_ARG(load_id, MP_QSTR___name__);
3014 EMIT_ARG(store_id, MP_QSTR___module__);
3015 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3016 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003017
3018 check_for_doc_string(comp, pns->nodes[2]);
3019 compile_node(comp, pns->nodes[2]); // 2 is class body
3020
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003021 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003022 assert(id != NULL);
3023 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003024 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003025 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003026#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003027 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003028#else
Damien George2bf7c092014-04-09 15:26:46 +01003029 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003030#endif
Damien429d7192013-10-04 19:53:11 +01003031 }
3032 EMIT(return_value);
3033 }
3034
Damien415eb6f2013-10-05 12:19:06 +01003035 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003036
3037 // make sure we match all the exception levels
3038 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003039}
3040
Damien George094d4502014-04-02 17:31:27 +01003041#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003042void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3043 comp->pass = pass;
3044 comp->scope_cur = scope;
3045 comp->next_label = 1;
3046
3047 if (scope->kind != SCOPE_FUNCTION) {
3048 printf("Error: inline assembler must be a function\n");
3049 return;
3050 }
3051
Damiena2f2f7d2013-10-06 00:14:13 +01003052 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003054 }
3055
Damien826005c2013-10-05 23:17:28 +01003056 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003057 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3058 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3059 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003060
Damiend99b0522013-12-21 18:17:45 +00003061 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003062
Damiena2f2f7d2013-10-06 00:14:13 +01003063 // parameters are in pns->nodes[1]
3064 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003065 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003066 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003067 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003068 }
3069
Damiend99b0522013-12-21 18:17:45 +00003070 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003071
Damiend99b0522013-12-21 18:17:45 +00003072 mp_parse_node_t pn_body = pns->nodes[3]; // body
3073 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003074 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3075
Damien Georgecbd2f742014-01-19 11:48:48 +00003076 /*
Damien826005c2013-10-05 23:17:28 +01003077 if (comp->pass == PASS_3) {
3078 //printf("----\n");
3079 scope_print_info(scope);
3080 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003081 */
Damien826005c2013-10-05 23:17:28 +01003082
3083 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003084 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3085 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3086 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3087 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3088 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3089 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3090 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3091 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3092 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3093 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3094 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3095 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3096 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003097 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3098
3099 // emit instructions
3100 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003101 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003102 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003103 return;
3104 }
Damien George6f355fd2014-04-10 14:11:31 +01003105 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003106 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003107 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003108 }
3109 } else {
3110 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003111 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003112 }
3113 }
3114 }
3115
3116 if (comp->pass > PASS_1) {
3117 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003118 }
Damien429d7192013-10-04 19:53:11 +01003119}
Damien George094d4502014-04-02 17:31:27 +01003120#endif
Damien429d7192013-10-04 19:53:11 +01003121
3122void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3123 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003124 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003125 scope->num_locals = 0;
3126 for (int i = 0; i < scope->id_info_len; i++) {
3127 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003128 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003129 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3130 continue;
3131 }
3132 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3133 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3134 }
Damien9ecbcff2013-12-11 00:41:43 +00003135 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003136 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003137 id->local_num = scope->num_locals;
3138 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003139 }
3140 }
3141
3142 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003143#if MICROPY_EMIT_CPYTHON
3144 int num_cell = 0;
3145#endif
Damien9ecbcff2013-12-11 00:41:43 +00003146 for (int i = 0; i < scope->id_info_len; i++) {
3147 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003148#if MICROPY_EMIT_CPYTHON
3149 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003150 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003151 id->local_num = num_cell;
3152 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003153 }
Damien George6baf76e2013-12-30 22:32:17 +00003154#else
3155 // in Micro Python the cells come right after the fast locals
3156 // parameters are not counted here, since they remain at the start
3157 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003158 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003159 id->local_num = scope->num_locals;
3160 scope->num_locals += 1;
3161 }
3162#endif
Damien9ecbcff2013-12-11 00:41:43 +00003163 }
Damien9ecbcff2013-12-11 00:41:43 +00003164
3165 // compute the index of free vars (freevars[idx] in CPython)
3166 // make sure they are in the order of the parent scope
3167 if (scope->parent != NULL) {
3168 int num_free = 0;
3169 for (int i = 0; i < scope->parent->id_info_len; i++) {
3170 id_info_t *id = &scope->parent->id_info[i];
3171 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3172 for (int j = 0; j < scope->id_info_len; j++) {
3173 id_info_t *id2 = &scope->id_info[j];
3174 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003175 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003176#if MICROPY_EMIT_CPYTHON
3177 // in CPython the frees are numbered after the cells
3178 id2->local_num = num_cell + num_free;
3179#else
3180 // in Micro Python the frees come first, before the params
3181 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003182#endif
3183 num_free += 1;
3184 }
3185 }
3186 }
Damien429d7192013-10-04 19:53:11 +01003187 }
Damien George6baf76e2013-12-30 22:32:17 +00003188#if !MICROPY_EMIT_CPYTHON
3189 // in Micro Python shift all other locals after the free locals
3190 if (num_free > 0) {
3191 for (int i = 0; i < scope->id_info_len; i++) {
3192 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003193 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003194 id->local_num += num_free;
3195 }
3196 }
3197 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3198 scope->num_locals += num_free;
3199 }
3200#endif
Damien429d7192013-10-04 19:53:11 +01003201 }
3202
Damien George8725f8f2014-02-15 19:33:11 +00003203 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003204
3205#if MICROPY_EMIT_CPYTHON
3206 // these flags computed here are for CPython compatibility only
3207 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003208 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003209 }
3210 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) {
3211 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003212 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003213
3214 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003215 // Note that we don't actually use this information at the moment (for CPython compat only)
3216 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 +00003217 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003218 }
3219 }
Damien George882b3632014-04-02 15:56:31 +01003220#endif
3221
Damien429d7192013-10-04 19:53:11 +01003222 int num_free = 0;
3223 for (int i = 0; i < scope->id_info_len; i++) {
3224 id_info_t *id = &scope->id_info[i];
3225 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3226 num_free += 1;
3227 }
3228 }
3229 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003230 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003231 }
3232}
3233
Damien George65cad122014-04-06 11:48:15 +01003234mp_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 +01003235 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003236 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003237 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003238
Damien826005c2013-10-05 23:17:28 +01003239 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003240 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003241
3242 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003243 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003244
Damien826005c2013-10-05 23:17:28 +01003245 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003246 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003247 comp->emit_method_table = &emit_pass1_method_table;
3248 comp->emit_inline_asm = NULL;
3249 comp->emit_inline_asm_method_table = NULL;
3250 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003251 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003252 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003253#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003254 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003255 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003256#endif
Damien826005c2013-10-05 23:17:28 +01003257 } else {
3258 compile_scope(comp, s, PASS_1);
3259 }
3260
3261 // update maximim number of labels needed
3262 if (comp->next_label > max_num_labels) {
3263 max_num_labels = comp->next_label;
3264 }
Damien429d7192013-10-04 19:53:11 +01003265 }
3266
Damien826005c2013-10-05 23:17:28 +01003267 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003268 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003269 compile_scope_compute_things(comp, s);
3270 }
3271
Damien826005c2013-10-05 23:17:28 +01003272 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003273 emit_pass1_free(comp->emit);
3274
Damien826005c2013-10-05 23:17:28 +01003275 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003276#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003277 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003278#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003279 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003280#endif
Damien3ef4abb2013-10-12 16:53:13 +01003281#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003282 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003283#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003284#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003285 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003286 if (false) {
3287 // dummy
3288
Damien3ef4abb2013-10-12 16:53:13 +01003289#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003290 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003291 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003292 if (emit_inline_thumb == NULL) {
3293 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3294 }
3295 comp->emit = NULL;
3296 comp->emit_method_table = NULL;
3297 comp->emit_inline_asm = emit_inline_thumb;
3298 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3299 compile_scope_inline_asm(comp, s, PASS_2);
3300 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003301#endif
3302
Damien826005c2013-10-05 23:17:28 +01003303 } else {
Damienc025ebb2013-10-12 14:30:21 +01003304
3305 // choose the emit type
3306
Damien3ef4abb2013-10-12 16:53:13 +01003307#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003308 comp->emit = emit_cpython_new(max_num_labels);
3309 comp->emit_method_table = &emit_cpython_method_table;
3310#else
Damien826005c2013-10-05 23:17:28 +01003311 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003312
3313#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003314 case MP_EMIT_OPT_NATIVE_PYTHON:
3315 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003316#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003317 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003318 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003319 }
Damien13ed3a62013-10-08 09:05:10 +01003320 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003321#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003322 if (emit_native == NULL) {
3323 emit_native = emit_native_thumb_new(max_num_labels);
3324 }
3325 comp->emit_method_table = &emit_native_thumb_method_table;
3326#endif
3327 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003328 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003329 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003330#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003331
Damien826005c2013-10-05 23:17:28 +01003332 default:
3333 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003334 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003335 }
3336 comp->emit = emit_bc;
3337 comp->emit_method_table = &emit_bc_method_table;
3338 break;
3339 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003340#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003341
3342 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003343 compile_scope(comp, s, PASS_2);
3344 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003345 }
Damien429d7192013-10-04 19:53:11 +01003346 }
3347
Damien George41d02b62014-01-24 22:42:28 +00003348 // free the emitters
3349#if !MICROPY_EMIT_CPYTHON
3350 if (emit_bc != NULL) {
3351 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003352 }
Damien George41d02b62014-01-24 22:42:28 +00003353#if MICROPY_EMIT_NATIVE
3354 if (emit_native != NULL) {
3355#if MICROPY_EMIT_X64
3356 emit_native_x64_free(emit_native);
3357#elif MICROPY_EMIT_THUMB
3358 emit_native_thumb_free(emit_native);
3359#endif
3360 }
3361#endif
3362#if MICROPY_EMIT_INLINE_THUMB
3363 if (emit_inline_thumb != NULL) {
3364 emit_inline_thumb_free(emit_inline_thumb);
3365 }
3366#endif
3367#endif // !MICROPY_EMIT_CPYTHON
3368
3369 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003370 uint unique_code_id = module_scope->unique_code_id;
3371 for (scope_t *s = module_scope; s;) {
3372 scope_t *next = s->next;
3373 scope_free(s);
3374 s = next;
3375 }
Damien5ac1b2e2013-10-18 19:58:12 +01003376
Damien George41d02b62014-01-24 22:42:28 +00003377 // free the compiler
3378 bool had_error = comp->had_error;
3379 m_del_obj(compiler_t, comp);
3380
Damien George1fb03172014-01-03 14:22:03 +00003381 if (had_error) {
3382 // TODO return a proper error message
3383 return mp_const_none;
3384 } else {
3385#if MICROPY_EMIT_CPYTHON
3386 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003387 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003388 return mp_const_true;
3389#else
3390 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003391 // 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 +01003392 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003393#endif
3394 }
Damien429d7192013-10-04 19:53:11 +01003395}