blob: 4738c0ae554118d75d79ad63845efebdff252ef7 [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
Damien George57e99eb2014-04-10 22:42:11 +010081STATIC const mp_map_elem_t mp_constants_table[] = {
82 // Extra constants as defined by a port
83 MICROPY_EXTRA_CONSTANTS
84};
85
86STATIC const mp_map_t mp_constants_map = {
87 .all_keys_are_qstrs = 1,
88 .table_is_fixed_array = 1,
89 .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90 .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
91 .table = (mp_map_elem_t*)mp_constants_table,
92};
93
Damiend99b0522013-12-21 18:17:45 +000094mp_parse_node_t fold_constants(mp_parse_node_t pn) {
95 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
96 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
97 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010098
99 // fold arguments first
100 for (int i = 0; i < n; i++) {
101 pns->nodes[i] = fold_constants(pns->nodes[i]);
102 }
103
Damiend99b0522013-12-21 18:17:45 +0000104 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100105 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000106 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 +0200107 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
108 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000109 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100110#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100111 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000112 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100113#endif
Damiend99b0522013-12-21 18:17:45 +0000114 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
115 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100116 } else {
117 // shouldn't happen
118 assert(0);
119 }
120 }
121 break;
122
123 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100124 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000125 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 +0200126 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
127 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000128 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000129 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000131 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100132 } else {
133 // shouldn't happen
134 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100135 }
Damien Georgeaf272592014-04-04 11:21:58 +0000136 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
137 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100138 }
139 }
140 break;
141
142 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000143 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 +0000144 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
145 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000146 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000147 if (!mp_small_int_mul_overflow(arg0, arg1)) {
148 arg0 *= arg1;
149 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
150 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
151 }
152 }
Damiend99b0522013-12-21 18:17:45 +0000153 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100154 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000155 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000156 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300158 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000159 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 +0300160 }
Damien429d7192013-10-04 19:53:11 +0100161 } else {
162 // shouldn't happen
163 assert(0);
164 }
165 }
166 break;
167
168 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000169 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200170 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000171 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
172 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
173 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
174 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
175 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
176 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100177 } else {
178 // shouldn't happen
179 assert(0);
180 }
181 }
182 break;
183
184 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100185 if (0) {
186#if MICROPY_EMIT_CPYTHON
187 } else 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])) {
188 // int**x
189 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000190 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
191 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200192 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100193 if (power >= 0) {
194 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200195 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100196 for (; power > 0; power--) {
197 ans *= base;
198 }
Damiend99b0522013-12-21 18:17:45 +0000199 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100200 }
201 }
Damien George57e99eb2014-04-10 22:42:11 +0100202#endif
203 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
204 // id.id
205 // look it up in constant table, see if it can be replaced with an integer
206 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
207 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
208 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
209 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
210 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
211 if (elem != NULL) {
212 mp_obj_t dest[2];
213 mp_load_method_maybe(elem->value, q_attr, dest);
214 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
215 machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
216 if (MP_PARSE_FITS_SMALL_INT(val)) {
217 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
218 }
219 }
220 }
Damien429d7192013-10-04 19:53:11 +0100221 }
222 break;
223 }
224 }
225
226 return pn;
227}
228
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200229STATIC 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 +0000230STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100231
Damien George6f355fd2014-04-10 14:11:31 +0100232STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100233 return comp->next_label++;
234}
235
Damien George8dcc0c72014-03-27 10:55:21 +0000236STATIC void compile_increase_except_level(compiler_t *comp) {
237 comp->cur_except_level += 1;
238 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
239 comp->scope_cur->exc_stack_size = comp->cur_except_level;
240 }
241}
242
243STATIC void compile_decrease_except_level(compiler_t *comp) {
244 assert(comp->cur_except_level > 0);
245 comp->cur_except_level -= 1;
246}
247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200248STATIC 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 +0000249 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 +0100250 scope->parent = comp->scope_cur;
251 scope->next = NULL;
252 if (comp->scope_head == NULL) {
253 comp->scope_head = scope;
254 } else {
255 scope_t *s = comp->scope_head;
256 while (s->next != NULL) {
257 s = s->next;
258 }
259 s->next = scope;
260 }
261 return scope;
262}
263
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200264STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000265 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100266 return 0;
Damiend99b0522013-12-21 18:17:45 +0000267 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100268 return 1;
269 } else {
Damiend99b0522013-12-21 18:17:45 +0000270 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
271 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100272 return 1;
273 } else {
Damiend99b0522013-12-21 18:17:45 +0000274 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100275 }
276 }
277}
278
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200279STATIC 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 +0000280 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
281 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 f(comp, pns->nodes[i]);
285 }
Damiend99b0522013-12-21 18:17:45 +0000286 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100287 f(comp, pn);
288 }
289}
290
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200291STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 *nodes = NULL;
294 return 0;
Damiend99b0522013-12-21 18:17:45 +0000295 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100296 *nodes = pn;
297 return 1;
298 } else {
Damiend99b0522013-12-21 18:17:45 +0000299 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
300 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100301 *nodes = pn;
302 return 1;
303 } else {
304 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000305 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100306 }
307 }
308}
309
Damiend99b0522013-12-21 18:17:45 +0000310void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100311}
312
Damiend99b0522013-12-21 18:17:45 +0000313void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
314 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100315 for (int i = 0; i < num_nodes; i++) {
316 compile_node(comp, pns->nodes[i]);
317 }
318}
319
Damien3ef4abb2013-10-12 16:53:13 +0100320#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200321STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000322 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100323 return false;
324 }
Damiend99b0522013-12-21 18:17:45 +0000325 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100326 return false;
327 }
328 return true;
329}
330
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200331STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000332 uint len;
333 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000334 bool has_single_quote = false;
335 bool has_double_quote = false;
336 for (int i = 0; i < len; i++) {
337 if (str[i] == '\'') {
338 has_single_quote = true;
339 } else if (str[i] == '"') {
340 has_double_quote = true;
341 }
342 }
343 if (bytes) {
344 vstr_printf(vstr, "b");
345 }
346 bool quote_single = false;
347 if (has_single_quote && !has_double_quote) {
348 vstr_printf(vstr, "\"");
349 } else {
350 quote_single = true;
351 vstr_printf(vstr, "'");
352 }
353 for (int i = 0; i < len; i++) {
354 if (str[i] == '\n') {
355 vstr_printf(vstr, "\\n");
356 } else if (str[i] == '\\') {
357 vstr_printf(vstr, "\\\\");
358 } else if (str[i] == '\'' && quote_single) {
359 vstr_printf(vstr, "\\'");
360 } else {
361 vstr_printf(vstr, "%c", str[i]);
362 }
363 }
364 if (has_single_quote && !has_double_quote) {
365 vstr_printf(vstr, "\"");
366 } else {
367 vstr_printf(vstr, "'");
368 }
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000372 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200373 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
374 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
375 return;
376 }
377
Damiend99b0522013-12-21 18:17:45 +0000378 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
379 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
380 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000381 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
382 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
383 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
384 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
385 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100386 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000387 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
388 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
389 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100390 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100391 }
392 break;
393 default: assert(0);
394 }
395}
396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200397STATIC 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 +0100398 int n = 0;
399 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000400 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100401 }
402 int total = n;
403 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000404 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100405 total += 1;
Damien3a205172013-10-12 15:01:56 +0100406 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100407 is_const = false;
408 }
409 }
410 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100411 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100412 is_const = false;
413 break;
414 }
415 }
416 if (total > 0 && is_const) {
417 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000418 vstr_t *vstr = vstr_new();
419 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000420 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000421 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100422 need_comma = true;
423 }
424 for (int i = 0; i < n; i++) {
425 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000426 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100427 }
Damien02f89412013-12-12 15:13:36 +0000428 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100429 need_comma = true;
430 }
431 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000432 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100433 } else {
Damien02f89412013-12-12 15:13:36 +0000434 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100435 }
Damien Georgeb9791222014-01-23 00:34:21 +0000436 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000437 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100438 } else {
Damiend99b0522013-12-21 18:17:45 +0000439 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100440 compile_node(comp, pn);
441 }
442 for (int i = 0; i < n; i++) {
443 compile_node(comp, pns_list->nodes[i]);
444 }
Damien Georgeb9791222014-01-23 00:34:21 +0000445 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100446 }
447}
Damien3a205172013-10-12 15:01:56 +0100448#endif
449
450// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000451void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100452#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100453 cpython_c_tuple(comp, pn, pns_list);
454#else
455 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000456 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100457 compile_node(comp, pn);
458 total += 1;
459 }
460 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000461 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100462 for (int i = 0; i < n; i++) {
463 compile_node(comp, pns_list->nodes[i]);
464 }
465 total += n;
466 }
Damien Georgeb9791222014-01-23 00:34:21 +0000467 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100468#endif
469}
Damien429d7192013-10-04 19:53:11 +0100470
Damiend99b0522013-12-21 18:17:45 +0000471void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100472 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000473 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100474}
475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200476STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000477 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200478 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200482 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 +0100483}
484
Damien3ef4abb2013-10-12 16:53:13 +0100485#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100486// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC 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 +0100488 if (node_is_const_false(pn)) {
489 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000490 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100491 }
492 return;
493 } else if (node_is_const_true(pn)) {
494 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000495 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100496 }
497 return;
Damiend99b0522013-12-21 18:17:45 +0000498 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
499 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
500 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
501 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100502 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100503 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100504 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100505 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100506 }
Damien3a205172013-10-12 15:01:56 +0100507 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100509 } else {
510 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100511 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100512 }
513 }
514 return;
Damiend99b0522013-12-21 18:17:45 +0000515 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100516 if (jump_if == false) {
517 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100518 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100519 }
520 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100521 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100522 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100523 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100524 }
Damien3a205172013-10-12 15:01:56 +0100525 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000526 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100527 }
528 return;
Damiend99b0522013-12-21 18:17:45 +0000529 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100530 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100531 return;
532 }
533 }
534
535 // nothing special, fall back to default compiling for node and jump
536 compile_node(comp, pn);
537 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000538 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100539 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000540 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100541 }
542}
Damien3a205172013-10-12 15:01:56 +0100543#endif
Damien429d7192013-10-04 19:53:11 +0100544
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200545STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100546#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100547 cpython_c_if_cond(comp, pn, jump_if, label, false);
548#else
549 if (node_is_const_false(pn)) {
550 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000551 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100552 }
553 return;
554 } else if (node_is_const_true(pn)) {
555 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000556 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100557 }
558 return;
Damiend99b0522013-12-21 18:17:45 +0000559 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
560 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
561 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
562 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100563 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100564 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100565 for (int i = 0; i < n - 1; i++) {
566 c_if_cond(comp, pns->nodes[i], true, label2);
567 }
568 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100570 } else {
571 for (int i = 0; i < n; i++) {
572 c_if_cond(comp, pns->nodes[i], true, label);
573 }
574 }
575 return;
Damiend99b0522013-12-21 18:17:45 +0000576 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100577 if (jump_if == false) {
578 for (int i = 0; i < n; i++) {
579 c_if_cond(comp, pns->nodes[i], false, label);
580 }
581 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100582 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100583 for (int i = 0; i < n - 1; i++) {
584 c_if_cond(comp, pns->nodes[i], false, label2);
585 }
586 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000587 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100588 }
589 return;
Damiend99b0522013-12-21 18:17:45 +0000590 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100591 c_if_cond(comp, pns->nodes[0], !jump_if, label);
592 return;
593 }
594 }
595
596 // nothing special, fall back to default compiling for node and jump
597 compile_node(comp, pn);
598 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000599 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100600 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000601 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100602 }
603#endif
Damien429d7192013-10-04 19:53:11 +0100604}
605
606typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000607void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100608
Damiend99b0522013-12-21 18:17:45 +0000609void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100610 if (assign_kind != ASSIGN_AUG_STORE) {
611 compile_node(comp, pns->nodes[0]);
612 }
613
Damiend99b0522013-12-21 18:17:45 +0000614 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
615 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
616 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
617 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100618 if (assign_kind != ASSIGN_AUG_STORE) {
619 for (int i = 0; i < n - 1; i++) {
620 compile_node(comp, pns1->nodes[i]);
621 }
622 }
Damiend99b0522013-12-21 18:17:45 +0000623 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
624 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100625 }
Damiend99b0522013-12-21 18:17:45 +0000626 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100627 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000628 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100629 if (assign_kind == ASSIGN_AUG_STORE) {
630 EMIT(rot_three);
631 EMIT(store_subscr);
632 } else {
633 compile_node(comp, pns1->nodes[0]);
634 if (assign_kind == ASSIGN_AUG_LOAD) {
635 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100636 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100637 } else {
638 EMIT(store_subscr);
639 }
640 }
Damiend99b0522013-12-21 18:17:45 +0000641 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
642 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100643 if (assign_kind == ASSIGN_AUG_LOAD) {
644 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000645 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100646 } else {
647 if (assign_kind == ASSIGN_AUG_STORE) {
648 EMIT(rot_two);
649 }
Damien Georgeb9791222014-01-23 00:34:21 +0000650 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100651 }
652 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100653 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100654 }
655 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100656 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100657 }
658
Damiend99b0522013-12-21 18:17:45 +0000659 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100660 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100661 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100662
663 return;
664
665cannot_assign:
666 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100667}
668
Damiend99b0522013-12-21 18:17:45 +0000669void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100670 assert(n >= 0);
671 int have_star_index = -1;
672 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000673 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100674 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100676 have_star_index = i;
677 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100678 compile_syntax_error(comp, nodes[i], "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100679 return;
680 }
681 }
682 }
683 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000684 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100685 }
686 for (int i = 0; i < n; i++) {
687 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000688 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100689 } else {
690 c_assign(comp, nodes[i], ASSIGN_STORE);
691 }
692 }
693}
694
695// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000696void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100697 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000698 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100699 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000700 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
701 if (MP_PARSE_NODE_IS_ID(pn)) {
702 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100703 switch (assign_kind) {
704 case ASSIGN_STORE:
705 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000706 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100707 break;
708 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000709 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100710 break;
711 }
712 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100713 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100714 return;
715 }
716 } else {
Damiend99b0522013-12-21 18:17:45 +0000717 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
718 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100719 case PN_power:
720 // lhs is an index or attribute
721 c_assign_power(comp, pns, assign_kind);
722 break;
723
724 case PN_testlist_star_expr:
725 case PN_exprlist:
726 // lhs is a tuple
727 if (assign_kind != ASSIGN_STORE) {
728 goto bad_aug;
729 }
Damiend99b0522013-12-21 18:17:45 +0000730 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100731 break;
732
733 case PN_atom_paren:
734 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000735 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100736 // empty tuple
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100737 compile_syntax_error(comp, pn, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100738 return;
Damiend99b0522013-12-21 18:17:45 +0000739 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
740 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100741 goto testlist_comp;
742 } else {
743 // parenthesis around 1 item, is just that item
744 pn = pns->nodes[0];
745 goto tail_recursion;
746 }
747 break;
748
749 case PN_atom_bracket:
750 // lhs is something in brackets
751 if (assign_kind != ASSIGN_STORE) {
752 goto bad_aug;
753 }
Damiend99b0522013-12-21 18:17:45 +0000754 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100755 // empty list, assignment allowed
756 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000757 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
758 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100759 goto testlist_comp;
760 } else {
761 // brackets around 1 item
762 c_assign_tuple(comp, 1, &pns->nodes[0]);
763 }
764 break;
765
766 default:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100767 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
768 return;
Damien429d7192013-10-04 19:53:11 +0100769 }
770 return;
771
772 testlist_comp:
773 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000774 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
775 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
776 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100777 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000778 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100779 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000780 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100781 // sequence of many items
782 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000783 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000784 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100785 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
786 for (int i = 0; i < n; i++) {
787 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
788 }
Damiend99b0522013-12-21 18:17:45 +0000789 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100790 // TODO can we ever get here? can it be compiled?
791 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
792 return;
Damien429d7192013-10-04 19:53:11 +0100793 } else {
794 // sequence with 2 items
795 goto sequence_with_2_items;
796 }
797 } else {
798 // sequence with 2 items
799 sequence_with_2_items:
800 c_assign_tuple(comp, 2, pns->nodes);
801 }
802 return;
803 }
804 return;
805
806 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100807 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100808}
809
810// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100811// if we are not in CPython compatibility mode then:
812// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
813// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
814// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100815void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
816 assert(n_pos_defaults >= 0);
817 assert(n_kw_defaults >= 0);
818
Damien429d7192013-10-04 19:53:11 +0100819 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000820 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100821 int nfree = 0;
822 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000823 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
824 id_info_t *id = &comp->scope_cur->id_info[i];
825 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
826 for (int j = 0; j < this_scope->id_info_len; j++) {
827 id_info_t *id2 = &this_scope->id_info[j];
828 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000829#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000830 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000831#else
832 // in Micro Python we load closures using LOAD_FAST
Damien George2bf7c092014-04-09 15:26:46 +0100833 EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000834#endif
Damien318aec62013-12-10 18:28:17 +0000835 nfree += 1;
836 }
837 }
Damien429d7192013-10-04 19:53:11 +0100838 }
839 }
840 }
Damien429d7192013-10-04 19:53:11 +0100841
842 // make the function/closure
843 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100844 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100845 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000846 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100847 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100848 }
849}
850
Damiend99b0522013-12-21 18:17:45 +0000851void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000852 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000853 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
854 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100855 // bare star
856 comp->have_bare_star = true;
857 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000858
859 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
860 // TODO do we need to do anything with this?
861
862 } else {
863 mp_parse_node_t pn_id;
864 mp_parse_node_t pn_colon;
865 mp_parse_node_t pn_equal;
866 if (MP_PARSE_NODE_IS_ID(pn)) {
867 // this parameter is just an id
868
869 pn_id = pn;
870 pn_colon = MP_PARSE_NODE_NULL;
871 pn_equal = MP_PARSE_NODE_NULL;
872
873 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
874 // this parameter has a colon and/or equal specifier
875
876 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
877 pn_id = pns->nodes[0];
878 pn_colon = pns->nodes[1];
879 pn_equal = pns->nodes[2];
880
881 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100882 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +0000883 assert(0);
884 return;
885 }
886
887 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
888 // this parameter does not have a default value
889
890 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
891 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100892 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000893 return;
894 }
895
896 } else {
897 // this parameter has a default value
898 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
899
900 if (comp->have_bare_star) {
901 comp->param_pass_num_dict_params += 1;
902 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100903#if !MICROPY_EMIT_CPYTHON
904 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
905 if (comp->param_pass_num_dict_params == 1) {
906 // first default dict param, so make the map
907 EMIT_ARG(build_map, 0);
908 }
909#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000910 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
911 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100912#if !MICROPY_EMIT_CPYTHON
913 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
914 EMIT(store_map);
915#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000916 }
917 } else {
918 comp->param_pass_num_default_params += 1;
919 if (comp->param_pass == 2) {
920 compile_node(comp, pn_equal);
921 }
922 }
923 }
924
925 // TODO pn_colon not implemented
926 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100927 }
928}
929
930// leaves function object on stack
931// returns function name
Damiend99b0522013-12-21 18:17:45 +0000932qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100933 if (comp->pass == PASS_1) {
934 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000935 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100936 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000937 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100938 }
939
940 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George02a4c052014-04-09 12:50:58 +0100941 uint old_have_bare_star = comp->have_bare_star;
942 uint old_param_pass = comp->param_pass;
943 uint old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
944 uint old_param_pass_num_default_params = comp->param_pass_num_default_params;
Damien429d7192013-10-04 19:53:11 +0100945
946 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000947
948 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100949 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000950 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100951 comp->param_pass_num_dict_params = 0;
952 comp->param_pass_num_default_params = 0;
953 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000954
955 if (comp->had_error) {
956 return MP_QSTR_NULL;
957 }
958
959 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100960 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000961 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100962 comp->param_pass_num_dict_params = 0;
963 comp->param_pass_num_default_params = 0;
964 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
965
Damien Georgee337f1e2014-03-31 15:18:37 +0100966#if !MICROPY_EMIT_CPYTHON
967 // in Micro Python we put the default positional parameters into a tuple using the bytecode
968 if (comp->param_pass_num_default_params > 0) {
969 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
970 }
971#endif
972
Damien429d7192013-10-04 19:53:11 +0100973 // get the scope for this function
974 scope_t *fscope = (scope_t*)pns->nodes[4];
975
976 // make the function
Damien George30565092014-03-31 11:30:17 +0100977 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100978
979 // restore variables
980 comp->have_bare_star = old_have_bare_star;
981 comp->param_pass = old_param_pass;
982 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
983 comp->param_pass_num_default_params = old_param_pass_num_default_params;
984
985 // return its name (the 'f' in "def f(...):")
986 return fscope->simple_name;
987}
988
989// leaves class object on stack
990// returns class name
Damiend99b0522013-12-21 18:17:45 +0000991qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100992 if (comp->pass == PASS_1) {
993 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000994 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100995 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000996 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100997 }
998
999 EMIT(load_build_class);
1000
1001 // scope for this class
1002 scope_t *cscope = (scope_t*)pns->nodes[3];
1003
1004 // compile the class
1005 close_over_variables_etc(comp, cscope, 0, 0);
1006
1007 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +00001008 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +01001009
1010 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001011 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1012 mp_parse_node_t parents = pns->nodes[1];
1013 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1014 parents = MP_PARSE_NODE_NULL;
1015 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001016 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001017 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001018
1019 // return its name (the 'C' in class C(...):")
1020 return cscope->simple_name;
1021}
1022
Damien6cdd3af2013-10-05 18:08:26 +01001023// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001024STATIC 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 +00001025 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001026 return false;
1027 }
1028
1029 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001030 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001031 return true;
1032 }
1033
Damiend99b0522013-12-21 18:17:45 +00001034 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001035 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +01001036 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001037#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001038 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001039 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001040 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001041 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001042#endif
Damien3ef4abb2013-10-12 16:53:13 +01001043#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001044 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001045 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001046#endif
Damien6cdd3af2013-10-05 18:08:26 +01001047 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001048 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001049 }
1050
1051 return true;
1052}
1053
Damiend99b0522013-12-21 18:17:45 +00001054void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001055 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001056 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001057 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1058
Damien6cdd3af2013-10-05 18:08:26 +01001059 // inherit emit options for this function/class definition
1060 uint emit_options = comp->scope_cur->emit_options;
1061
1062 // compile each decorator
1063 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001064 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001065 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1066 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001067
1068 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001069 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001070 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1071
1072 // check for built-in decorators
1073 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1074 // this was a built-in
1075 num_built_in_decorators += 1;
1076
1077 } else {
1078 // not a built-in, compile normally
1079
1080 // compile the decorator function
1081 compile_node(comp, name_nodes[0]);
1082 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001083 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001084 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001085 }
1086
1087 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001088 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001089 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001090 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001091 compile_node(comp, pns_decorator->nodes[1]);
1092 }
Damien429d7192013-10-04 19:53:11 +01001093 }
1094 }
1095
1096 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001097 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001098 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001099 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001100 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001101 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001102 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001103 } else {
1104 // shouldn't happen
1105 assert(0);
1106 }
1107
1108 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001109 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001110 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001111 }
1112
1113 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001114 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001115}
1116
Damiend99b0522013-12-21 18:17:45 +00001117void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001118 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001119 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001120 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001121}
1122
Damiend99b0522013-12-21 18:17:45 +00001123void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1124 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001125 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001126 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1127 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001128
1129 compile_node(comp, pns->nodes[0]); // base of the power node
1130
Damiend99b0522013-12-21 18:17:45 +00001131 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1132 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1133 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1134 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001135 for (int i = 0; i < n - 1; i++) {
1136 compile_node(comp, pns1->nodes[i]);
1137 }
Damiend99b0522013-12-21 18:17:45 +00001138 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1139 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001140 }
Damiend99b0522013-12-21 18:17:45 +00001141 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001142 // can't delete function calls
1143 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001144 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001145 compile_node(comp, pns1->nodes[0]);
1146 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001147 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1148 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001149 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001150 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001151 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001152 }
1153 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001154 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001155 }
1156
Damiend99b0522013-12-21 18:17:45 +00001157 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001158 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001159 }
Damiend99b0522013-12-21 18:17:45 +00001160 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1161 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1162 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1163 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001164 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1165
Damiend99b0522013-12-21 18:17:45 +00001166 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1167 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1168 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001169 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001170 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001171 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001172 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001173 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001174 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001175 c_del_stmt(comp, pns->nodes[0]);
1176 for (int i = 0; i < n; i++) {
1177 c_del_stmt(comp, pns1->nodes[i]);
1178 }
Damiend99b0522013-12-21 18:17:45 +00001179 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001180 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001181 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001182 } else {
1183 // sequence with 2 items
1184 goto sequence_with_2_items;
1185 }
1186 } else {
1187 // sequence with 2 items
1188 sequence_with_2_items:
1189 c_del_stmt(comp, pns->nodes[0]);
1190 c_del_stmt(comp, pns->nodes[1]);
1191 }
1192 } else {
1193 // tuple with 1 element
1194 c_del_stmt(comp, pn);
1195 }
1196 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001197 // TODO is there anything else to implement?
1198 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001199 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001200
1201 return;
1202
1203cannot_delete:
1204 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001205}
1206
Damiend99b0522013-12-21 18:17:45 +00001207void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001208 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1209}
1210
Damiend99b0522013-12-21 18:17:45 +00001211void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001212 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001213 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001214 }
Damien Georgecbddb272014-02-01 20:08:18 +00001215 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001216}
1217
Damiend99b0522013-12-21 18:17:45 +00001218void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001219 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001220 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001221 }
Damien Georgecbddb272014-02-01 20:08:18 +00001222 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001223}
1224
Damiend99b0522013-12-21 18:17:45 +00001225void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001226 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001227 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001228 return;
1229 }
Damiend99b0522013-12-21 18:17:45 +00001230 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001231 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001232 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001233 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001234 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001235 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1236 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 +01001237
Damien George6f355fd2014-04-10 14:11:31 +01001238 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001239 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1240 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1241 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001242 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001243 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1244 } else {
1245 compile_node(comp, pns->nodes[0]);
1246 }
1247 EMIT(return_value);
1248}
1249
Damiend99b0522013-12-21 18:17:45 +00001250void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001251 compile_node(comp, pns->nodes[0]);
1252 EMIT(pop_top);
1253}
1254
Damiend99b0522013-12-21 18:17:45 +00001255void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1256 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001257 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001258 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001259 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001260 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001261 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001262 compile_node(comp, pns->nodes[0]);
1263 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001264 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001265 } else {
1266 // raise x
1267 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001268 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001269 }
1270}
1271
Damien George635543c2014-04-10 12:56:52 +01001272// q_base holds the base of the name
1273// eg a -> q_base=a
1274// a.b.c -> q_base=a
1275void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001276 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001277 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1278 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001279 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001280 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001281 pn = pns->nodes[0];
1282 is_as = true;
1283 }
Damien George635543c2014-04-10 12:56:52 +01001284 if (MP_PARSE_NODE_IS_NULL(pn)) {
1285 // empty name (eg, from . import x)
1286 *q_base = MP_QSTR_;
1287 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1288 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001289 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001290 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001291 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001292 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001293 }
Damien George635543c2014-04-10 12:56:52 +01001294 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001295 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1296 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1297 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001298 // a name of the form a.b.c
1299 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001300 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001301 }
Damiend99b0522013-12-21 18:17:45 +00001302 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001303 int len = n - 1;
1304 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001305 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001306 }
Damien George55baff42014-01-21 21:40:13 +00001307 byte *q_ptr;
1308 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001309 for (int i = 0; i < n; i++) {
1310 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001311 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001312 }
Damien George55baff42014-01-21 21:40:13 +00001313 uint str_src_len;
1314 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001315 memcpy(str_dest, str_src, str_src_len);
1316 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001317 }
Damien George635543c2014-04-10 12:56:52 +01001318 qstr q_full = qstr_build_end(q_ptr);
1319 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001320 if (is_as) {
1321 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001322 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001323 }
1324 }
1325 } else {
Damien George635543c2014-04-10 12:56:52 +01001326 // shouldn't happen
1327 assert(0);
Damien429d7192013-10-04 19:53:11 +01001328 }
1329 } else {
Damien George635543c2014-04-10 12:56:52 +01001330 // shouldn't happen
1331 assert(0);
Damien429d7192013-10-04 19:53:11 +01001332 }
1333}
1334
Damiend99b0522013-12-21 18:17:45 +00001335void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001336 EMIT_ARG(load_const_small_int, 0); // level 0 import
1337 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1338 qstr q_base;
1339 do_import_name(comp, pn, &q_base);
1340 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001341}
1342
Damiend99b0522013-12-21 18:17:45 +00001343void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001344 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1345}
1346
Damiend99b0522013-12-21 18:17:45 +00001347void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001348 mp_parse_node_t pn_import_source = pns->nodes[0];
1349
1350 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1351 uint import_level = 0;
1352 do {
1353 mp_parse_node_t pn_rel;
1354 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)) {
1355 // This covers relative imports with dots only like "from .. import"
1356 pn_rel = pn_import_source;
1357 pn_import_source = MP_PARSE_NODE_NULL;
1358 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1359 // This covers relative imports starting with dot(s) like "from .foo import"
1360 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1361 pn_rel = pns_2b->nodes[0];
1362 pn_import_source = pns_2b->nodes[1];
1363 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1364 } else {
1365 // Not a relative import
1366 break;
1367 }
1368
1369 // get the list of . and/or ...'s
1370 mp_parse_node_t *nodes;
1371 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1372
1373 // count the total number of .'s
1374 for (int i = 0; i < n; i++) {
1375 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1376 import_level++;
1377 } else {
1378 // should be an MP_TOKEN_ELLIPSIS
1379 import_level += 3;
1380 }
1381 }
1382 } while (0);
1383
Damiend99b0522013-12-21 18:17:45 +00001384 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001385 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001386
1387 // build the "fromlist" tuple
1388#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001389 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001390#else
Damien Georgeb9791222014-01-23 00:34:21 +00001391 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1392 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001393#endif
1394
1395 // do the import
Damien George635543c2014-04-10 12:56:52 +01001396 qstr dummy_q;
1397 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001398 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001399
Damien429d7192013-10-04 19:53:11 +01001400 } else {
Damien George635543c2014-04-10 12:56:52 +01001401 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001402
1403 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001404 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001405 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001406#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001407 {
1408 vstr_t *vstr = vstr_new();
1409 vstr_printf(vstr, "(");
1410 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
Damien02f89412013-12-12 15:13:36 +00001414 if (i > 0) {
1415 vstr_printf(vstr, ", ");
1416 }
1417 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001418 uint len;
1419 const byte *str = qstr_data(id2, &len);
1420 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001421 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001422 }
Damien02f89412013-12-12 15:13:36 +00001423 if (n == 1) {
1424 vstr_printf(vstr, ",");
1425 }
1426 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001428 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001429 }
Damiendb4c3612013-12-10 17:27:24 +00001430#else
1431 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001432 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1433 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1434 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001435 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001436 }
Damien Georgeb9791222014-01-23 00:34:21 +00001437 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001438#endif
1439
1440 // do the import
Damien George635543c2014-04-10 12:56:52 +01001441 qstr dummy_q;
1442 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001443 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001444 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1445 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1446 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001447 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001448 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001449 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001450 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001451 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001452 }
1453 }
1454 EMIT(pop_top);
1455 }
1456}
1457
Damiend99b0522013-12-21 18:17:45 +00001458void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001459 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001460 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1461 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001462 } else {
Damiend99b0522013-12-21 18:17:45 +00001463 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1464 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001465 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001466 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001467 }
Damien429d7192013-10-04 19:53:11 +01001468 }
1469 }
1470}
1471
Damiend99b0522013-12-21 18:17:45 +00001472void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001473 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001474 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1475 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001476 } else {
Damiend99b0522013-12-21 18:17:45 +00001477 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1478 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001479 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001480 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001481 }
Damien429d7192013-10-04 19:53:11 +01001482 }
1483 }
1484}
1485
Damiend99b0522013-12-21 18:17:45 +00001486void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001487 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001488 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001489 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 +00001490 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001491 // assertion message
1492 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001493 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001494 }
Damien Georgeb9791222014-01-23 00:34:21 +00001495 EMIT_ARG(raise_varargs, 1);
1496 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001497}
1498
Damiend99b0522013-12-21 18:17:45 +00001499void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001500 // TODO proper and/or short circuiting
1501
Damien George6f355fd2014-04-10 14:11:31 +01001502 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001503
Damien George6f355fd2014-04-10 14:11:31 +01001504 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001505 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1506
1507 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001508
1509 if (
1510#if !MICROPY_EMIT_CPYTHON
1511 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1512 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1513#endif
1514 // optimisation to not jump if last instruction was return
1515 !EMIT(last_emit_was_return_value)
1516 ) {
1517 // jump over elif/else blocks
1518 EMIT_ARG(jump, l_end);
1519 }
1520
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001522
Damiend99b0522013-12-21 18:17:45 +00001523 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001524 // compile elif blocks
1525
Damiend99b0522013-12-21 18:17:45 +00001526 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001527
Damiend99b0522013-12-21 18:17:45 +00001528 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001529 // multiple elif blocks
1530
Damiend99b0522013-12-21 18:17:45 +00001531 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001532 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001533 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001534 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001535 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1536
1537 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001538 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001539 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001540 }
Damien Georgeb9791222014-01-23 00:34:21 +00001541 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001542 }
1543
1544 } else {
1545 // a single elif block
1546
Damienb05d7072013-10-05 13:37:10 +01001547 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001548 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1549
1550 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001551 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001553 }
Damien Georgeb9791222014-01-23 00:34:21 +00001554 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001555 }
1556 }
1557
1558 // compile else block
1559 compile_node(comp, pns->nodes[3]); // can be null
1560
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001562}
1563
Damien Georgecbddb272014-02-01 20:08:18 +00001564#define START_BREAK_CONTINUE_BLOCK \
Damien George6f355fd2014-04-10 14:11:31 +01001565 uint old_break_label = comp->break_label; \
1566 uint old_continue_label = comp->continue_label; \
1567 uint break_label = comp_next_label(comp); \
1568 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001569 comp->break_label = break_label; \
1570 comp->continue_label = continue_label; \
1571 comp->break_continue_except_level = comp->cur_except_level;
1572
1573#define END_BREAK_CONTINUE_BLOCK \
1574 comp->break_label = old_break_label; \
1575 comp->continue_label = old_continue_label; \
1576 comp->break_continue_except_level = comp->cur_except_level;
1577
Damiend99b0522013-12-21 18:17:45 +00001578void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001579 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001580
Damience89a212013-10-15 22:25:17 +01001581 // compared to CPython, we have an optimised version of while loops
1582#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001583 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001584 EMIT_ARG(setup_loop, break_label);
1585 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001586 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1587 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001588 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001590 }
Damien Georgeb9791222014-01-23 00:34:21 +00001591 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001592 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1593 // this is a small hack to agree with CPython
1594 if (!node_is_const_true(pns->nodes[0])) {
1595 EMIT(pop_block);
1596 }
Damience89a212013-10-15 22:25:17 +01001597#else
Damien George6f355fd2014-04-10 14:11:31 +01001598 uint top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001599 EMIT_ARG(jump, continue_label);
1600 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001601 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001602 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001603 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1604#endif
1605
1606 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001607 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001608
1609 compile_node(comp, pns->nodes[2]); // else
1610
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001612}
1613
Damienf72fd0e2013-11-06 20:20:49 +00001614// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001615// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1616// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001617void 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 +00001618 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001619
Damien George6f355fd2014-04-10 14:11:31 +01001620 uint top_label = comp_next_label(comp);
1621 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001622
Damien George3ff2d032014-03-31 18:02:22 +01001623 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001624 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001625 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001626
Damien Georgeb9791222014-01-23 00:34:21 +00001627 EMIT_ARG(jump, entry_label);
1628 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001629
Damien George3ff2d032014-03-31 18:02:22 +01001630 // at this point we actually have 1 less element on the stack
Damien Georged66ae182014-04-10 17:28:54 +00001631 EMIT_ARG(adjust_stack_size, -1);
Damien George3ff2d032014-03-31 18:02:22 +01001632
1633 // store next value to var
1634 c_assign(comp, pn_var, ASSIGN_STORE);
1635
Damienf3822fc2013-11-09 20:12:03 +00001636 // compile body
1637 compile_node(comp, pn_body);
1638
Damien Georgeb9791222014-01-23 00:34:21 +00001639 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001640
Damien George3ff2d032014-03-31 18:02:22 +01001641 // compile: var + step, duplicated on stack
1642 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001643 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001644 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001645 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001646
Damien Georgeb9791222014-01-23 00:34:21 +00001647 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001648
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001649 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001650 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001651 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1652 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001653 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001654 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001655 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001656 }
Damien Georgeb9791222014-01-23 00:34:21 +00001657 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001658
Damien George3ff2d032014-03-31 18:02:22 +01001659 // discard final value of var that failed the loop condition
1660 EMIT(pop_top);
1661
Damienf72fd0e2013-11-06 20:20:49 +00001662 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001663 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001664
1665 compile_node(comp, pn_else);
1666
Damien Georgeb9791222014-01-23 00:34:21 +00001667 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001668}
1669
Damiend99b0522013-12-21 18:17:45 +00001670void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001671#if !MICROPY_EMIT_CPYTHON
1672 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1673 // this is actually slower, but uses no heap memory
1674 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001675 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 +00001676 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001677 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1678 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1679 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1680 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001681 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1682 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001683 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001684 mp_parse_node_t pn_range_start;
1685 mp_parse_node_t pn_range_end;
1686 mp_parse_node_t pn_range_step;
1687 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001688 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001689 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001690 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001691 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001692 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001693 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001694 } else if (n_args == 2) {
1695 pn_range_start = args[0];
1696 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001697 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001698 } else {
1699 pn_range_start = args[0];
1700 pn_range_end = args[1];
1701 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001702 // We need to know sign of step. This is possible only if it's constant
1703 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1704 optimize = false;
1705 }
Damienf72fd0e2013-11-06 20:20:49 +00001706 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001707 }
1708 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001709 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1710 return;
1711 }
1712 }
1713 }
1714#endif
1715
Damien Georgecbddb272014-02-01 20:08:18 +00001716 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001717
Damien George6f355fd2014-04-10 14:11:31 +01001718 uint pop_label = comp_next_label(comp);
1719 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001720
Damience89a212013-10-15 22:25:17 +01001721 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1722#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001724#endif
1725
Damien429d7192013-10-04 19:53:11 +01001726 compile_node(comp, pns->nodes[1]); // iterator
1727 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001728 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001729 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001730 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1731 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001732 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001733 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001734 }
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001736 EMIT(for_iter_end);
1737
1738 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001739 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001740
Damience89a212013-10-15 22:25:17 +01001741#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001742 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001743#endif
Damien429d7192013-10-04 19:53:11 +01001744
1745 compile_node(comp, pns->nodes[3]); // else (not tested)
1746
Damien Georgeb9791222014-01-23 00:34:21 +00001747 EMIT_ARG(label_assign, break_label);
1748 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001749}
1750
Damiend99b0522013-12-21 18:17:45 +00001751void 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 +01001752 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001753 uint l1 = comp_next_label(comp);
1754 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001755
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001757 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001758
Damien429d7192013-10-04 19:53:11 +01001759 compile_node(comp, pn_body); // body
1760 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001761 EMIT_ARG(jump, success_label); // jump over exception handler
1762
1763 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georged66ae182014-04-10 17:28:54 +00001764 EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
Damien George069a35e2014-04-10 17:22:19 +00001765
Damien George6f355fd2014-04-10 14:11:31 +01001766 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001767
1768 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001769 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1770 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001771
1772 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001773 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001774
Damiend99b0522013-12-21 18:17:45 +00001775 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001776 // this is a catch all exception handler
1777 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001778 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001779 return;
1780 }
1781 } else {
1782 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001783 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1784 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1785 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1786 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001787 // handler binds the exception to a local
1788 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001789 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001790 }
1791 }
1792 EMIT(dup_top);
1793 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001794 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001795 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001796 }
1797
1798 EMIT(pop_top);
1799
1800 if (qstr_exception_local == 0) {
1801 EMIT(pop_top);
1802 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001803 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001804 }
1805
1806 EMIT(pop_top);
1807
Damien George6f355fd2014-04-10 14:11:31 +01001808 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001809 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001810 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001811 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001812 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001813 }
1814 compile_node(comp, pns_except->nodes[1]);
1815 if (qstr_exception_local != 0) {
1816 EMIT(pop_block);
1817 }
1818 EMIT(pop_except);
1819 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001820 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1821 EMIT_ARG(label_assign, l3);
1822 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1823 EMIT_ARG(store_id, qstr_exception_local);
1824 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001825
Damien George8dcc0c72014-03-27 10:55:21 +00001826 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001827 EMIT(end_finally);
1828 }
Damien Georgeb9791222014-01-23 00:34:21 +00001829 EMIT_ARG(jump, l2);
1830 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00001831 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01001832 }
1833
Damien George8dcc0c72014-03-27 10:55:21 +00001834 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001835 EMIT(end_finally);
Damien Georged66ae182014-04-10 17:28:54 +00001836 EMIT_ARG(adjust_stack_size, -5); // stack adjust
Damien Georgecbddb272014-02-01 20:08:18 +00001837
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001839 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001840 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001841}
1842
Damiend99b0522013-12-21 18:17:45 +00001843void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
Damien George6f355fd2014-04-10 14:11:31 +01001844 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001845
Damien Georgeb9791222014-01-23 00:34:21 +00001846 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001847 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001848
Damien429d7192013-10-04 19:53:11 +01001849 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001850 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001851 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001852 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001853 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001854 } else {
1855 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1856 }
1857 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001858 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1859 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001860 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001861
Damien George8dcc0c72014-03-27 10:55:21 +00001862 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001863 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001864}
1865
Damiend99b0522013-12-21 18:17:45 +00001866void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1867 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1868 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1869 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001870 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001871 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1872 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001873 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001874 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001875 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001876 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001877 // no finally
1878 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1879 } else {
1880 // have finally
Damiend99b0522013-12-21 18:17:45 +00001881 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 +01001882 }
1883 } else {
1884 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001885 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001886 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001887 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001888 }
1889 } else {
1890 // shouldn't happen
1891 assert(0);
1892 }
1893}
1894
Damiend99b0522013-12-21 18:17:45 +00001895void 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 +01001896 if (n == 0) {
1897 // no more pre-bits, compile the body of the with
1898 compile_node(comp, body);
1899 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001900 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001901 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001902 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001903 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001904 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001905 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001906 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1907 } else {
1908 // this pre-bit is just an expression
1909 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001910 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001911 EMIT(pop_top);
1912 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001913 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001914 // compile additional pre-bits and the body
1915 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1916 // finish this with block
1917 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001918 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1919 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001920 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001921 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001922 EMIT(end_finally);
1923 }
1924}
1925
Damiend99b0522013-12-21 18:17:45 +00001926void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001927 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001928 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001929 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1930 assert(n > 0);
1931
1932 // compile in a nested fashion
1933 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1934}
1935
Damiend99b0522013-12-21 18:17:45 +00001936void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1937 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001938 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1939 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001941 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001942 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001943 EMIT(pop_top);
1944
Damien429d7192013-10-04 19:53:11 +01001945 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001946 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001947 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001948 // do nothing with a lonely constant
1949 } else {
1950 compile_node(comp, pns->nodes[0]); // just an expression
1951 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1952 }
Damien429d7192013-10-04 19:53:11 +01001953 }
1954 } else {
Damiend99b0522013-12-21 18:17:45 +00001955 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1956 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001957 if (kind == PN_expr_stmt_augassign) {
1958 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1959 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001960 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001961 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001962 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001963 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1964 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1965 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1966 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1967 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1968 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1969 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1970 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1971 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1972 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1973 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1974 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1975 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001976 }
Damien George7e5fb242014-02-01 22:18:47 +00001977 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001978 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1979 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001980 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1981 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001982 // following CPython, we store left-most first
1983 if (rhs > 0) {
1984 EMIT(dup_top);
1985 }
1986 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1987 for (int i = 0; i < rhs; i++) {
1988 if (i + 1 < rhs) {
1989 EMIT(dup_top);
1990 }
Damiend99b0522013-12-21 18:17:45 +00001991 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001992 }
1993 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001994 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1995 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1996 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1997 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001998 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001999 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2000 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002001 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2002 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2003 // can't optimise when it's a star expression on the lhs
2004 goto no_optimisation;
2005 }
Damien429d7192013-10-04 19:53:11 +01002006 compile_node(comp, pns10->nodes[0]); // rhs
2007 compile_node(comp, pns10->nodes[1]); // rhs
2008 EMIT(rot_two);
2009 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2010 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002011 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2012 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2013 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2014 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002015 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002016 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2017 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002018 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2019 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2020 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2021 // can't optimise when it's a star expression on the lhs
2022 goto no_optimisation;
2023 }
Damien429d7192013-10-04 19:53:11 +01002024 compile_node(comp, pns10->nodes[0]); // rhs
2025 compile_node(comp, pns10->nodes[1]); // rhs
2026 compile_node(comp, pns10->nodes[2]); // rhs
2027 EMIT(rot_three);
2028 EMIT(rot_two);
2029 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2030 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2031 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2032 } else {
Damien George495d7812014-04-08 17:51:47 +01002033 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002034 compile_node(comp, pns1->nodes[0]); // rhs
2035 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2036 }
2037 } else {
2038 // shouldn't happen
2039 assert(0);
2040 }
2041 }
2042}
2043
Damien Georged17926d2014-03-30 13:35:08 +01002044void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00002045 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002046 compile_node(comp, pns->nodes[0]);
2047 for (int i = 1; i < num_nodes; i += 1) {
2048 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002050 }
2051}
2052
Damiend99b0522013-12-21 18:17:45 +00002053void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2054 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2055 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002056
Damien George6f355fd2014-04-10 14:11:31 +01002057 uint l_fail = comp_next_label(comp);
2058 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002059 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2060 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002061 EMIT_ARG(jump, l_end);
2062 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002063 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002064 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002065 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002066}
2067
Damiend99b0522013-12-21 18:17:45 +00002068void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002069 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002070 //mp_parse_node_t pn_params = pns->nodes[0];
2071 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002072
2073 if (comp->pass == PASS_1) {
2074 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002075 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 +01002076 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002077 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002078 }
2079
2080 // get the scope for this lambda
2081 scope_t *this_scope = (scope_t*)pns->nodes[2];
2082
2083 // make the lambda
2084 close_over_variables_etc(comp, this_scope, 0, 0);
2085}
2086
Damiend99b0522013-12-21 18:17:45 +00002087void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002088 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002089 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002090 for (int i = 0; i < n; i += 1) {
2091 compile_node(comp, pns->nodes[i]);
2092 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002094 }
2095 }
Damien Georgeb9791222014-01-23 00:34:21 +00002096 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002097}
2098
Damiend99b0522013-12-21 18:17:45 +00002099void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002100 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002101 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002102 for (int i = 0; i < n; i += 1) {
2103 compile_node(comp, pns->nodes[i]);
2104 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002105 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002106 }
2107 }
Damien Georgeb9791222014-01-23 00:34:21 +00002108 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damiend99b0522013-12-21 18:17:45 +00002111void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002112 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002113 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002114}
2115
Damiend99b0522013-12-21 18:17:45 +00002116void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002117 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002118 compile_node(comp, pns->nodes[0]);
2119 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002120 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002121 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002122 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002123 }
2124 for (int i = 1; i + 1 < num_nodes; i += 2) {
2125 compile_node(comp, pns->nodes[i + 1]);
2126 if (i + 2 < num_nodes) {
2127 EMIT(dup_top);
2128 EMIT(rot_three);
2129 }
Damien George7e5fb242014-02-01 22:18:47 +00002130 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002132 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002133 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2134 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2135 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2136 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2137 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2138 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2139 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2140 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002141 }
2142 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002143 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2144 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2145 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002146 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002147 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002148 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002149 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002150 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002151 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002152 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002153 }
2154 } else {
2155 // shouldn't happen
2156 assert(0);
2157 }
2158 } else {
2159 // shouldn't happen
2160 assert(0);
2161 }
2162 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002163 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002164 }
2165 }
2166 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002167 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002168 EMIT_ARG(jump, l_end);
2169 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002170 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002171 EMIT(rot_two);
2172 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002173 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002174 }
2175}
2176
Damiend99b0522013-12-21 18:17:45 +00002177void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002178 compile_syntax_error(comp, (mp_parse_node_t)pns, "can use starred expression only as assignment target");
Damien429d7192013-10-04 19:53:11 +01002179}
2180
Damiend99b0522013-12-21 18:17:45 +00002181void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002182 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002183}
2184
Damiend99b0522013-12-21 18:17:45 +00002185void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002186 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002187}
2188
Damiend99b0522013-12-21 18:17:45 +00002189void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002190 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002191}
2192
Damiend99b0522013-12-21 18:17:45 +00002193void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2194 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002195 compile_node(comp, pns->nodes[0]);
2196 for (int i = 1; i + 1 < num_nodes; i += 2) {
2197 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002198 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002199 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002200 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002201 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002202 } else {
2203 // shouldn't happen
2204 assert(0);
2205 }
2206 }
2207}
2208
Damiend99b0522013-12-21 18:17:45 +00002209void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2210 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002211 compile_node(comp, pns->nodes[0]);
2212 for (int i = 1; i + 1 < num_nodes; i += 2) {
2213 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002214 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002215 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002216 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002217 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002218 } else {
2219 // shouldn't happen
2220 assert(0);
2221 }
2222 }
2223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2226 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002227 compile_node(comp, pns->nodes[0]);
2228 for (int i = 1; i + 1 < num_nodes; i += 2) {
2229 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002230 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002231 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002232 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002233 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002234 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002235 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002236 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002237 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002238 } else {
2239 // shouldn't happen
2240 assert(0);
2241 }
2242 }
2243}
2244
Damiend99b0522013-12-21 18:17:45 +00002245void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002246 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002247 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002248 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002249 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002250 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002251 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002252 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002253 } else {
2254 // shouldn't happen
2255 assert(0);
2256 }
2257}
2258
Damien George35e2a4e2014-02-05 00:51:47 +00002259void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2260 // this is to handle special super() call
2261 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2262
2263 compile_generic_all_nodes(comp, pns);
2264}
2265
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002266STATIC 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 +01002267 // function to call is on top of stack
2268
Damien George35e2a4e2014-02-05 00:51:47 +00002269#if !MICROPY_EMIT_CPYTHON
2270 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002271 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 +00002272 EMIT_ARG(load_id, MP_QSTR___class__);
2273 // get first argument to function
2274 bool found = false;
2275 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002276 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2277 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 +00002278 found = true;
2279 break;
2280 }
2281 }
2282 if (!found) {
2283 printf("TypeError: super() call cannot find self\n");
2284 return;
2285 }
Damien George922ddd62014-04-09 12:43:17 +01002286 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002287 return;
2288 }
2289#endif
2290
Damien George02a4c052014-04-09 12:50:58 +01002291 uint old_n_arg_keyword = comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002292 uint old_star_flags = comp->star_flags;
Damien429d7192013-10-04 19:53:11 +01002293 comp->n_arg_keyword = 0;
Damien George922ddd62014-04-09 12:43:17 +01002294 comp->star_flags = 0;
Damien429d7192013-10-04 19:53:11 +01002295
Damien Georgebbcd49a2014-02-06 20:30:16 +00002296 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002297
2298 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002299 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002300 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien429d7192013-10-04 19:53:11 +01002301 n_positional -= 1;
2302 }
Damien George922ddd62014-04-09 12:43:17 +01002303 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien429d7192013-10-04 19:53:11 +01002304 n_positional -= 1;
2305 }
2306
2307 if (is_method_call) {
Damien George922ddd62014-04-09 12:43:17 +01002308 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002309 } else {
Damien George922ddd62014-04-09 12:43:17 +01002310 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->star_flags);
Damien429d7192013-10-04 19:53:11 +01002311 }
2312
2313 comp->n_arg_keyword = old_n_arg_keyword;
Damien George922ddd62014-04-09 12:43:17 +01002314 comp->star_flags = old_star_flags;
Damien429d7192013-10-04 19:53:11 +01002315}
2316
Damiend99b0522013-12-21 18:17:45 +00002317void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2318 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002319 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002320 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 +01002321 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002322 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2323 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002324 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002325 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002326 i += 1;
2327 } else {
2328 compile_node(comp, pns->nodes[i]);
2329 }
Damien George35e2a4e2014-02-05 00:51:47 +00002330 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002331 }
2332}
2333
Damiend99b0522013-12-21 18:17:45 +00002334void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002335 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002336 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002337}
2338
Damiend99b0522013-12-21 18:17:45 +00002339void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002340 // a list of strings
Damien63321742013-12-10 17:41:49 +00002341
2342 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002343 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002344 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002345 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002346 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002347 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2348 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2349 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002350 if (i == 0) {
2351 string_kind = pn_kind;
2352 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002353 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002354 return;
2355 }
Damien George55baff42014-01-21 21:40:13 +00002356 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002357 }
Damien63321742013-12-10 17:41:49 +00002358
Damien63321742013-12-10 17:41:49 +00002359 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002360 byte *q_ptr;
2361 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002362 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002363 uint s_len;
2364 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002365 memcpy(s_dest, s, s_len);
2366 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002367 }
Damien George55baff42014-01-21 21:40:13 +00002368 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002369
Damien Georgeb9791222014-01-23 00:34:21 +00002370 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002371}
2372
2373// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002374void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2375 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2376 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2377 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002378
2379 if (comp->pass == PASS_1) {
2380 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002381 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 +01002382 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002383 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002384 }
2385
2386 // get the scope for this comprehension
2387 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2388
2389 // compile the comprehension
2390 close_over_variables_etc(comp, this_scope, 0, 0);
2391
2392 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2393 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002394 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002395}
2396
Damiend99b0522013-12-21 18:17:45 +00002397void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2398 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002399 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002400 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2401 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2402 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2403 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2404 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2405 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2406 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002407 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002408 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002409 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002410 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002411 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002412 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002413 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002414 // generator expression
2415 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2416 } else {
2417 // tuple with 2 items
2418 goto tuple_with_2_items;
2419 }
2420 } else {
2421 // tuple with 2 items
2422 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002423 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002424 }
2425 } else {
2426 // parenthesis around a single item, is just that item
2427 compile_node(comp, pns->nodes[0]);
2428 }
2429}
2430
Damiend99b0522013-12-21 18:17:45 +00002431void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2432 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002433 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002434 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002435 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2436 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2437 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2438 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2439 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002440 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002441 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002442 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002443 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002444 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002445 // list of many items
2446 compile_node(comp, pns2->nodes[0]);
2447 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002448 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002450 // list comprehension
2451 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2452 } else {
2453 // list with 2 items
2454 goto list_with_2_items;
2455 }
2456 } else {
2457 // list with 2 items
2458 list_with_2_items:
2459 compile_node(comp, pns2->nodes[0]);
2460 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002461 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002462 }
2463 } else {
2464 // list with 1 item
2465 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002466 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002467 }
2468}
2469
Damiend99b0522013-12-21 18:17:45 +00002470void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2471 mp_parse_node_t pn = pns->nodes[0];
2472 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002473 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002474 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002475 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2476 pns = (mp_parse_node_struct_t*)pn;
2477 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002478 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002479 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002480 compile_node(comp, pn);
2481 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002482 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2483 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2484 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2485 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002486 // dict/set with multiple elements
2487
2488 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002489 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002490 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2491
2492 // first element sets whether it's a dict or set
2493 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002494 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002495 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002496 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002497 compile_node(comp, pns->nodes[0]);
2498 EMIT(store_map);
2499 is_dict = true;
2500 } else {
2501 // a set
2502 compile_node(comp, pns->nodes[0]); // 1st value of set
2503 is_dict = false;
2504 }
2505
2506 // process rest of elements
2507 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002508 mp_parse_node_t pn = nodes[i];
2509 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002510 compile_node(comp, pn);
2511 if (is_dict) {
2512 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002513 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002514 return;
2515 }
2516 EMIT(store_map);
2517 } else {
2518 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002519 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002520 return;
2521 }
2522 }
2523 }
2524
2525 // if it's a set, build it
2526 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002527 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002528 }
Damiend99b0522013-12-21 18:17:45 +00002529 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002530 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002531 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002532 // a dictionary comprehension
2533 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2534 } else {
2535 // a set comprehension
2536 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2537 }
2538 } else {
2539 // shouldn't happen
2540 assert(0);
2541 }
2542 } else {
2543 // set with one element
2544 goto set_with_one_element;
2545 }
2546 } else {
2547 // set with one element
2548 set_with_one_element:
2549 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002550 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002551 }
2552}
2553
Damiend99b0522013-12-21 18:17:45 +00002554void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002555 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002556}
2557
Damiend99b0522013-12-21 18:17:45 +00002558void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002559 // object who's index we want is on top of stack
2560 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002561 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002562}
2563
Damiend99b0522013-12-21 18:17:45 +00002564void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002565 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002567}
2568
Damiend99b0522013-12-21 18:17:45 +00002569void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2570 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2571 mp_parse_node_t pn = pns->nodes[0];
2572 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002573 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2575 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002576 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2577 pns = (mp_parse_node_struct_t*)pn;
2578 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002579 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002580 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002581 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002582 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002583 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002584 } else {
2585 // [?::x]
2586 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002587 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002588 }
Damiend99b0522013-12-21 18:17:45 +00002589 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002590 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002591 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2592 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2593 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2594 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002595 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002596 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002597 } else {
2598 // [?:x:x]
2599 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002600 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002601 }
2602 } else {
2603 // [?:x]
2604 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002606 }
2607 } else {
2608 // [?:x]
2609 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002610 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002611 }
2612}
2613
Damiend99b0522013-12-21 18:17:45 +00002614void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002615 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002616 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2617 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002618}
2619
Damiend99b0522013-12-21 18:17:45 +00002620void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002621 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002622 compile_subscript_3_helper(comp, pns);
2623}
2624
Damiend99b0522013-12-21 18:17:45 +00002625void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002626 // if this is called then we are compiling a dict key:value pair
2627 compile_node(comp, pns->nodes[1]); // value
2628 compile_node(comp, pns->nodes[0]); // key
2629}
2630
Damiend99b0522013-12-21 18:17:45 +00002631void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002632 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002633 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002634 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002635}
2636
Damiend99b0522013-12-21 18:17:45 +00002637void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002638 if (comp->star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002639 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002640 return;
2641 }
Damien George922ddd62014-04-09 12:43:17 +01002642 comp->star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Damien429d7192013-10-04 19:53:11 +01002643 compile_node(comp, pns->nodes[0]);
2644}
2645
Damiend99b0522013-12-21 18:17:45 +00002646void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George922ddd62014-04-09 12:43:17 +01002647 if (comp->star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002648 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002649 return;
2650 }
Damien George922ddd62014-04-09 12:43:17 +01002651 comp->star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Damien429d7192013-10-04 19:53:11 +01002652 compile_node(comp, pns->nodes[0]);
2653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2656 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2657 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2658 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2659 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002660 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 +01002661 return;
2662 }
Damien Georgeb9791222014-01-23 00:34:21 +00002663 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002664 compile_node(comp, pns2->nodes[0]);
2665 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002666 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002667 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2668 } else {
2669 // shouldn't happen
2670 assert(0);
2671 }
2672}
2673
Damiend99b0522013-12-21 18:17:45 +00002674void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002675 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002676 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002677 return;
2678 }
Damiend99b0522013-12-21 18:17:45 +00002679 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002680 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002681 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002682 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2683 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002684 compile_node(comp, pns->nodes[0]);
2685 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002686 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002687 EMIT(yield_from);
2688 } else {
2689 compile_node(comp, pns->nodes[0]);
2690 EMIT(yield_value);
2691 }
2692}
2693
Damiend99b0522013-12-21 18:17:45 +00002694typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002695STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002696 NULL,
2697#define nc NULL
2698#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002699#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002700#include "grammar.h"
2701#undef nc
2702#undef c
2703#undef DEF_RULE
2704};
2705
Damiend99b0522013-12-21 18:17:45 +00002706void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2707 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002708 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002709 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2710 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2711 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002712 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002713 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002714 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002715 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002716 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2717 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2718 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2719 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002720 case MP_PARSE_NODE_TOKEN:
2721 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002722 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002723 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002724 // do nothing
2725 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002726 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002727 }
2728 break;
Damien429d7192013-10-04 19:53:11 +01002729 default: assert(0);
2730 }
2731 } else {
Damiend99b0522013-12-21 18:17:45 +00002732 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002733 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002734 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002735 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002736 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002737#if MICROPY_DEBUG_PRINTERS
2738 mp_parse_node_print(pn, 0);
2739#endif
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002740 compile_syntax_error(comp, pn, "internal compiler error");
Damien429d7192013-10-04 19:53:11 +01002741 } else {
2742 f(comp, pns);
2743 }
2744 }
2745}
2746
Damiend99b0522013-12-21 18:17:45 +00002747void 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 +01002748 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002749 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002750 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2751 if (MP_PARSE_NODE_IS_ID(pn)) {
2752 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002753 if (comp->have_bare_star) {
2754 // comes after a bare star, so doesn't count as a parameter
2755 } else {
2756 comp->scope_cur->num_params += 1;
2757 }
Damienb14de212013-10-06 00:28:28 +01002758 } else {
Damiend99b0522013-12-21 18:17:45 +00002759 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2760 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2761 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2762 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002763 //int node_index = 1; unused
2764 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002765 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002766 // this parameter has an annotation
2767 pn_annotation = pns->nodes[1];
2768 }
2769 //node_index = 2; unused
2770 }
2771 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002772 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002773 // this parameter has a default value
2774 if (comp->have_bare_star) {
2775 comp->scope_cur->num_dict_params += 1;
2776 } else {
2777 comp->scope_cur->num_default_params += 1;
2778 }
2779 }
2780 */
2781 if (comp->have_bare_star) {
2782 // comes after a bare star, so doesn't count as a parameter
2783 } else {
2784 comp->scope_cur->num_params += 1;
2785 }
Damiend99b0522013-12-21 18:17:45 +00002786 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2787 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002788 // bare star
2789 // TODO see http://www.python.org/dev/peps/pep-3102/
2790 comp->have_bare_star = true;
2791 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002792 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002793 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002794 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002795 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2796 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002797 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002798 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002799 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2800 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002801 pn_annotation = pns->nodes[1];
2802 } else {
2803 // shouldn't happen
2804 assert(0);
2805 }
Damiend99b0522013-12-21 18:17:45 +00002806 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2807 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2808 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002809 // this parameter has an annotation
2810 pn_annotation = pns->nodes[1];
2811 }
Damien George8725f8f2014-02-15 19:33:11 +00002812 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002813 } else {
Damienb14de212013-10-06 00:28:28 +01002814 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002815 assert(0);
2816 }
Damien429d7192013-10-04 19:53:11 +01002817 }
2818
2819 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002820 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002821 // TODO this parameter has an annotation
2822 }
2823 bool added;
2824 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2825 if (!added) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002826 compile_syntax_error(comp, pn, "same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002827 return;
2828 }
Damien429d7192013-10-04 19:53:11 +01002829 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George11d8cd52014-04-09 14:42:51 +01002830 id_info->flags |= ID_FLAG_IS_PARAM;
Damien429d7192013-10-04 19:53:11 +01002831 }
2832}
2833
Damiend99b0522013-12-21 18:17:45 +00002834void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002835 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2836}
2837
Damiend99b0522013-12-21 18:17:45 +00002838void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002839 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2840}
2841
Damiend99b0522013-12-21 18:17:45 +00002842void 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 +01002843 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002844 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002845 // no more nested if/for; compile inner expression
2846 compile_node(comp, pn_inner_expr);
2847 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002848 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002849 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002850 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002851 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002853 } else {
2854 EMIT(yield_value);
2855 EMIT(pop_top);
2856 }
Damiend99b0522013-12-21 18:17:45 +00002857 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002858 // if condition
Damiend99b0522013-12-21 18:17:45 +00002859 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002860 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2861 pn_iter = pns_comp_if->nodes[1];
2862 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002863 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002864 // for loop
Damiend99b0522013-12-21 18:17:45 +00002865 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002866 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01002867 uint l_end2 = comp_next_label(comp);
2868 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002869 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002870 EMIT_ARG(label_assign, l_top2);
2871 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002872 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2873 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 +00002874 EMIT_ARG(jump, l_top2);
2875 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002876 EMIT(for_iter_end);
2877 } else {
2878 // shouldn't happen
2879 assert(0);
2880 }
2881}
2882
Damiend99b0522013-12-21 18:17:45 +00002883void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002884 // see http://www.python.org/dev/peps/pep-0257/
2885
2886 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002887 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002888 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002889 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002890 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002891 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2892 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002893 for (int i = 0; i < num_nodes; i++) {
2894 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002895 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 +00002896 // not a newline, so this is the first statement; finish search
2897 break;
2898 }
2899 }
2900 // 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 +00002901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002902 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002903 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002904 } else {
2905 return;
2906 }
2907
2908 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002909 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2910 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2911 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2912 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2913 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002914 compile_node(comp, pns->nodes[0]); // a doc string
2915 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002916 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002917 }
2918 }
2919 }
2920}
2921
2922void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2923 comp->pass = pass;
2924 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002925 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002926 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002927
2928 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002929 // reset maximum stack sizes in scope
2930 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002931 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002932 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002933 }
2934
Damien5ac1b2e2013-10-18 19:58:12 +01002935#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002936 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002937 scope_print_info(scope);
2938 }
Damien5ac1b2e2013-10-18 19:58:12 +01002939#endif
Damien429d7192013-10-04 19:53:11 +01002940
2941 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002942 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2943 assert(scope->kind == SCOPE_MODULE);
2944 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2945 compile_node(comp, pns->nodes[0]); // compile the expression
2946 EMIT(return_value);
2947 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002948 if (!comp->is_repl) {
2949 check_for_doc_string(comp, scope->pn);
2950 }
Damien429d7192013-10-04 19:53:11 +01002951 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002952 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002953 EMIT(return_value);
2954 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002955 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2956 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2957 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002958
2959 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002960 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002961 if (comp->pass == PASS_1) {
2962 comp->have_bare_star = false;
2963 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2964 }
2965
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002966 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002967
2968 compile_node(comp, pns->nodes[3]); // 3 is function body
2969 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002970 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002971 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002972 EMIT(return_value);
2973 }
2974 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002975 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2976 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2977 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002978
2979 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002980 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002981 if (comp->pass == PASS_1) {
2982 comp->have_bare_star = false;
2983 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2984 }
2985
2986 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2987 EMIT(return_value);
2988 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2989 // a bit of a hack at the moment
2990
Damiend99b0522013-12-21 18:17:45 +00002991 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2992 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2993 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2994 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2995 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002996
Damien George55baff42014-01-21 21:40:13 +00002997 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002998 if (comp->pass == PASS_1) {
2999 bool added;
3000 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3001 assert(added);
3002 id_info->kind = ID_INFO_KIND_LOCAL;
3003 scope->num_params = 1;
3004 }
3005
3006 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003007 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003008 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003009 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003010 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003011 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003012 }
3013
Damien George6f355fd2014-04-10 14:11:31 +01003014 uint l_end = comp_next_label(comp);
3015 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003016 EMIT_ARG(load_id, qstr_arg);
3017 EMIT_ARG(label_assign, l_top);
3018 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003019 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3020 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003021 EMIT_ARG(jump, l_top);
3022 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003023 EMIT(for_iter_end);
3024
3025 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003026 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003027 }
3028 EMIT(return_value);
3029 } else {
3030 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003031 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3032 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3033 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003034
3035 if (comp->pass == PASS_1) {
3036 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003037 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003038 assert(added);
3039 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003040 }
3041
Damien Georgeb9791222014-01-23 00:34:21 +00003042 EMIT_ARG(load_id, MP_QSTR___name__);
3043 EMIT_ARG(store_id, MP_QSTR___module__);
3044 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3045 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003046
3047 check_for_doc_string(comp, pns->nodes[2]);
3048 compile_node(comp, pns->nodes[2]); // 2 is class body
3049
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003050 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003051 assert(id != NULL);
3052 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003054 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003055#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003056 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003057#else
Damien George2bf7c092014-04-09 15:26:46 +01003058 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003059#endif
Damien429d7192013-10-04 19:53:11 +01003060 }
3061 EMIT(return_value);
3062 }
3063
Damien415eb6f2013-10-05 12:19:06 +01003064 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003065
3066 // make sure we match all the exception levels
3067 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003068}
3069
Damien George094d4502014-04-02 17:31:27 +01003070#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003071void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3072 comp->pass = pass;
3073 comp->scope_cur = scope;
3074 comp->next_label = 1;
3075
3076 if (scope->kind != SCOPE_FUNCTION) {
3077 printf("Error: inline assembler must be a function\n");
3078 return;
3079 }
3080
Damiena2f2f7d2013-10-06 00:14:13 +01003081 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003082 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003083 }
3084
Damien826005c2013-10-05 23:17:28 +01003085 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003086 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3087 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3088 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003089
Damiend99b0522013-12-21 18:17:45 +00003090 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003091
Damiena2f2f7d2013-10-06 00:14:13 +01003092 // parameters are in pns->nodes[1]
3093 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003094 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003095 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003096 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003097 }
3098
Damiend99b0522013-12-21 18:17:45 +00003099 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003100
Damiend99b0522013-12-21 18:17:45 +00003101 mp_parse_node_t pn_body = pns->nodes[3]; // body
3102 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003103 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3104
Damien Georgecbd2f742014-01-19 11:48:48 +00003105 /*
Damien826005c2013-10-05 23:17:28 +01003106 if (comp->pass == PASS_3) {
3107 //printf("----\n");
3108 scope_print_info(scope);
3109 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003110 */
Damien826005c2013-10-05 23:17:28 +01003111
3112 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003113 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3114 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3115 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3116 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3117 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3118 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3119 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3120 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3121 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3122 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3123 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3124 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3125 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003126 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3127
3128 // emit instructions
3129 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003130 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003131 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003132 return;
3133 }
Damien George6f355fd2014-04-10 14:11:31 +01003134 uint lab = comp_next_label(comp);
Damien826005c2013-10-05 23:17:28 +01003135 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003136 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003137 }
3138 } else {
3139 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003140 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003141 }
3142 }
3143 }
3144
3145 if (comp->pass > PASS_1) {
3146 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003147 }
Damien429d7192013-10-04 19:53:11 +01003148}
Damien George094d4502014-04-02 17:31:27 +01003149#endif
Damien429d7192013-10-04 19:53:11 +01003150
3151void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3152 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003153 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003154 scope->num_locals = 0;
3155 for (int i = 0; i < scope->id_info_len; i++) {
3156 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003157 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003158 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3159 continue;
3160 }
3161 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3162 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3163 }
Damien9ecbcff2013-12-11 00:41:43 +00003164 // note: params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003165 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien429d7192013-10-04 19:53:11 +01003166 id->local_num = scope->num_locals;
3167 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003168 }
3169 }
3170
3171 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003172#if MICROPY_EMIT_CPYTHON
3173 int num_cell = 0;
3174#endif
Damien9ecbcff2013-12-11 00:41:43 +00003175 for (int i = 0; i < scope->id_info_len; i++) {
3176 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003177#if MICROPY_EMIT_CPYTHON
3178 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003179 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003180 id->local_num = num_cell;
3181 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003182 }
Damien George6baf76e2013-12-30 22:32:17 +00003183#else
3184 // in Micro Python the cells come right after the fast locals
3185 // parameters are not counted here, since they remain at the start
3186 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003187 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003188 id->local_num = scope->num_locals;
3189 scope->num_locals += 1;
3190 }
3191#endif
Damien9ecbcff2013-12-11 00:41:43 +00003192 }
Damien9ecbcff2013-12-11 00:41:43 +00003193
3194 // compute the index of free vars (freevars[idx] in CPython)
3195 // make sure they are in the order of the parent scope
3196 if (scope->parent != NULL) {
3197 int num_free = 0;
3198 for (int i = 0; i < scope->parent->id_info_len; i++) {
3199 id_info_t *id = &scope->parent->id_info[i];
3200 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3201 for (int j = 0; j < scope->id_info_len; j++) {
3202 id_info_t *id2 = &scope->id_info[j];
3203 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George11d8cd52014-04-09 14:42:51 +01003204 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003205#if MICROPY_EMIT_CPYTHON
3206 // in CPython the frees are numbered after the cells
3207 id2->local_num = num_cell + num_free;
3208#else
3209 // in Micro Python the frees come first, before the params
3210 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003211#endif
3212 num_free += 1;
3213 }
3214 }
3215 }
Damien429d7192013-10-04 19:53:11 +01003216 }
Damien George6baf76e2013-12-30 22:32:17 +00003217#if !MICROPY_EMIT_CPYTHON
3218 // in Micro Python shift all other locals after the free locals
3219 if (num_free > 0) {
3220 for (int i = 0; i < scope->id_info_len; i++) {
3221 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003222 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003223 id->local_num += num_free;
3224 }
3225 }
3226 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3227 scope->num_locals += num_free;
3228 }
3229#endif
Damien429d7192013-10-04 19:53:11 +01003230 }
3231
Damien George8725f8f2014-02-15 19:33:11 +00003232 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003233
3234#if MICROPY_EMIT_CPYTHON
3235 // these flags computed here are for CPython compatibility only
3236 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003237 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003238 }
3239 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) {
3240 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003241 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003242
3243 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003244 // Note that we don't actually use this information at the moment (for CPython compat only)
3245 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 +00003246 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003247 }
3248 }
Damien George882b3632014-04-02 15:56:31 +01003249#endif
3250
Damien429d7192013-10-04 19:53:11 +01003251 int num_free = 0;
3252 for (int i = 0; i < scope->id_info_len; i++) {
3253 id_info_t *id = &scope->id_info[i];
3254 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3255 num_free += 1;
3256 }
3257 }
3258 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003259 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003260 }
3261}
3262
Damien George65cad122014-04-06 11:48:15 +01003263mp_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 +01003264 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003265 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003266 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003267
Damien826005c2013-10-05 23:17:28 +01003268 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003269 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003270
3271 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003272 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003273
Damien826005c2013-10-05 23:17:28 +01003274 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003275 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003276 comp->emit_method_table = &emit_pass1_method_table;
3277 comp->emit_inline_asm = NULL;
3278 comp->emit_inline_asm_method_table = NULL;
3279 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003280 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003281 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003282#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003283 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003284 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003285#endif
Damien826005c2013-10-05 23:17:28 +01003286 } else {
3287 compile_scope(comp, s, PASS_1);
3288 }
3289
3290 // update maximim number of labels needed
3291 if (comp->next_label > max_num_labels) {
3292 max_num_labels = comp->next_label;
3293 }
Damien429d7192013-10-04 19:53:11 +01003294 }
3295
Damien826005c2013-10-05 23:17:28 +01003296 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003297 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003298 compile_scope_compute_things(comp, s);
3299 }
3300
Damien826005c2013-10-05 23:17:28 +01003301 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003302 emit_pass1_free(comp->emit);
3303
Damien826005c2013-10-05 23:17:28 +01003304 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003305#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003306 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003307#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003308 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003309#endif
Damien3ef4abb2013-10-12 16:53:13 +01003310#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003311 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003312#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003313#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003314 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003315 if (false) {
3316 // dummy
3317
Damien3ef4abb2013-10-12 16:53:13 +01003318#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003319 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003320 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003321 if (emit_inline_thumb == NULL) {
3322 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3323 }
3324 comp->emit = NULL;
3325 comp->emit_method_table = NULL;
3326 comp->emit_inline_asm = emit_inline_thumb;
3327 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3328 compile_scope_inline_asm(comp, s, PASS_2);
3329 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003330#endif
3331
Damien826005c2013-10-05 23:17:28 +01003332 } else {
Damienc025ebb2013-10-12 14:30:21 +01003333
3334 // choose the emit type
3335
Damien3ef4abb2013-10-12 16:53:13 +01003336#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003337 comp->emit = emit_cpython_new(max_num_labels);
3338 comp->emit_method_table = &emit_cpython_method_table;
3339#else
Damien826005c2013-10-05 23:17:28 +01003340 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003341
3342#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003343 case MP_EMIT_OPT_NATIVE_PYTHON:
3344 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003345#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003346 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003347 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003348 }
Damien13ed3a62013-10-08 09:05:10 +01003349 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003350#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003351 if (emit_native == NULL) {
3352 emit_native = emit_native_thumb_new(max_num_labels);
3353 }
3354 comp->emit_method_table = &emit_native_thumb_method_table;
3355#endif
3356 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003357 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003358 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003359#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003360
Damien826005c2013-10-05 23:17:28 +01003361 default:
3362 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003363 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003364 }
3365 comp->emit = emit_bc;
3366 comp->emit_method_table = &emit_bc_method_table;
3367 break;
3368 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003369#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003370
3371 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003372 compile_scope(comp, s, PASS_2);
3373 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003374 }
Damien429d7192013-10-04 19:53:11 +01003375 }
3376
Damien George41d02b62014-01-24 22:42:28 +00003377 // free the emitters
3378#if !MICROPY_EMIT_CPYTHON
3379 if (emit_bc != NULL) {
3380 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003381 }
Damien George41d02b62014-01-24 22:42:28 +00003382#if MICROPY_EMIT_NATIVE
3383 if (emit_native != NULL) {
3384#if MICROPY_EMIT_X64
3385 emit_native_x64_free(emit_native);
3386#elif MICROPY_EMIT_THUMB
3387 emit_native_thumb_free(emit_native);
3388#endif
3389 }
3390#endif
3391#if MICROPY_EMIT_INLINE_THUMB
3392 if (emit_inline_thumb != NULL) {
3393 emit_inline_thumb_free(emit_inline_thumb);
3394 }
3395#endif
3396#endif // !MICROPY_EMIT_CPYTHON
3397
3398 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003399 uint unique_code_id = module_scope->unique_code_id;
3400 for (scope_t *s = module_scope; s;) {
3401 scope_t *next = s->next;
3402 scope_free(s);
3403 s = next;
3404 }
Damien5ac1b2e2013-10-18 19:58:12 +01003405
Damien George41d02b62014-01-24 22:42:28 +00003406 // free the compiler
3407 bool had_error = comp->had_error;
3408 m_del_obj(compiler_t, comp);
3409
Damien George1fb03172014-01-03 14:22:03 +00003410 if (had_error) {
3411 // TODO return a proper error message
3412 return mp_const_none;
3413 } else {
3414#if MICROPY_EMIT_CPYTHON
3415 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003416 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003417 return mp_const_true;
3418#else
3419 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003420 // 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 +01003421 return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003422#endif
3423 }
Damien429d7192013-10-04 19:53:11 +01003424}