blob: f157af49f5795e772d9430e51ee95279448442c2 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <unistd.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <assert.h>
7
8#include "misc.h"
Damienc025ebb2013-10-12 14:30:21 +01009#include "mpyconfig.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
12#include "scope.h"
13#include "compile.h"
14#include "runtime.h"
15#include "emit.h"
16
17// TODO need to mangle __attr names
18
Damience89a212013-10-15 22:25:17 +010019#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
20
Damien429d7192013-10-04 19:53:11 +010021typedef enum {
22 PN_none = 0,
23#define DEF_RULE(rule, comp, kind, arg...) PN_##rule,
24#include "grammar.h"
25#undef DEF_RULE
26 PN_maximum_number_of,
27} pn_kind_t;
28
Damien415eb6f2013-10-05 12:19:06 +010029#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
Damien826005c2013-10-05 23:17:28 +010030#define EMIT_INLINE_ASM(fun, arg...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, ##arg))
Damien429d7192013-10-04 19:53:11 +010031
Damien6cdd3af2013-10-05 18:08:26 +010032#define EMIT_OPT_NONE (0)
33#define EMIT_OPT_BYTE_CODE (1)
34#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010035#define EMIT_OPT_VIPER (3)
36#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010037
Damien429d7192013-10-04 19:53:11 +010038typedef struct _compiler_t {
39 qstr qstr___class__;
40 qstr qstr___locals__;
41 qstr qstr___name__;
42 qstr qstr___module__;
43 qstr qstr___qualname__;
44 qstr qstr___doc__;
45 qstr qstr_assertion_error;
Damien6cdd3af2013-10-05 18:08:26 +010046 qstr qstr_micropython;
Damien5ac1b2e2013-10-18 19:58:12 +010047 qstr qstr_byte_code;
Damien6cdd3af2013-10-05 18:08:26 +010048 qstr qstr_native;
Damien7af3d192013-10-07 00:02:49 +010049 qstr qstr_viper;
Damien5bfb7592013-10-05 18:41:24 +010050 qstr qstr_asm_thumb;
Damiend7933892013-11-28 19:12:18 +000051 qstr qstr_range;
Damien429d7192013-10-04 19:53:11 +010052
Damien5ac1b2e2013-10-18 19:58:12 +010053 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010054 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010055 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010056
Damienb05d7072013-10-05 13:37:10 +010057 int next_label;
Damienb05d7072013-10-05 13:37:10 +010058
Damien429d7192013-10-04 19:53:11 +010059 int break_label;
60 int continue_label;
61 int except_nest_level;
62
63 int n_arg_keyword;
64 bool have_star_arg;
65 bool have_dbl_star_arg;
66 bool have_bare_star;
67 int param_pass;
68 int param_pass_num_dict_params;
69 int param_pass_num_default_params;
70
71 scope_t *scope_head;
72 scope_t *scope_cur;
73
Damien6cdd3af2013-10-05 18:08:26 +010074 emit_t *emit; // current emitter
75 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010076
77 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
78 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 +010079} compiler_t;
80
81py_parse_node_t fold_constants(py_parse_node_t pn) {
82 if (PY_PARSE_NODE_IS_STRUCT(pn)) {
83 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
84 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
85
86 // fold arguments first
87 for (int i = 0; i < n; i++) {
88 pns->nodes[i] = fold_constants(pns->nodes[i]);
89 }
90
91 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
92 case PN_shift_expr:
93 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
94 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
95 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
96 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010097#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010098 // can overflow; enabled only to compare with CPython
99 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 << arg1);
100#endif
Damien429d7192013-10-04 19:53:11 +0100101 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_MORE)) {
102 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 >> arg1);
103 } else {
104 // shouldn't happen
105 assert(0);
106 }
107 }
108 break;
109
110 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100111 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damien429d7192013-10-04 19:53:11 +0100112 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien0efb3a12013-10-12 16:16:56 +0100113 machine_int_t arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
114 machine_int_t arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
115 machine_int_t res;
Damien429d7192013-10-04 19:53:11 +0100116 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100117 res = arg0 + arg1;
Damien429d7192013-10-04 19:53:11 +0100118 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100119 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100120 } else {
121 // shouldn't happen
122 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100123 res = 0;
124 }
125 if (PY_FIT_SMALL_INT(res)) {
126 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100127 }
128 }
129 break;
130
131 case PN_term:
Damien429d7192013-10-04 19:53:11 +0100132 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
133 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
134 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
135 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100136#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100137 // can overflow; enabled only to compare with CPython
Damien429d7192013-10-04 19:53:11 +0100138 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100139#endif
Damien429d7192013-10-04 19:53:11 +0100140 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_SLASH)) {
141 ; // pass
Damien0efb3a12013-10-12 16:16:56 +0100142 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PERCENT)) {
143 // XXX implement this properly as Python's % operator acts differently to C's
144 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 % arg1);
145 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_SLASH)) {
146 // XXX implement this properly as Python's // operator acts differently to C's
147 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 / arg1);
Damien429d7192013-10-04 19:53:11 +0100148 } else {
149 // shouldn't happen
150 assert(0);
151 }
152 }
153 break;
154
155 case PN_factor_2:
156 if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
157 machine_int_t arg = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
158 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
159 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg);
160 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
161 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, -arg);
162 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
163 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ~arg);
164 } else {
165 // shouldn't happen
166 assert(0);
167 }
168 }
169 break;
170
Damien3ef4abb2013-10-12 16:53:13 +0100171#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100172 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100173 // can overflow; enabled only to compare with CPython
Damien429d7192013-10-04 19:53:11 +0100174 if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_NULL(pns->nodes[1]) && !PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
175 py_parse_node_struct_t* pns2 = (py_parse_node_struct_t*)pns->nodes[2];
176 if (PY_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
177 int power = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
178 if (power >= 0) {
179 int ans = 1;
180 int base = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
181 for (; power > 0; power--) {
182 ans *= base;
183 }
184 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ans);
185 }
186 }
187 }
188 break;
Damien0efb3a12013-10-12 16:16:56 +0100189#endif
Damien429d7192013-10-04 19:53:11 +0100190 }
191 }
192
193 return pn;
194}
195
196void compile_node(compiler_t *comp, py_parse_node_t pn);
197
Damienb05d7072013-10-05 13:37:10 +0100198static int comp_next_label(compiler_t *comp) {
199 return comp->next_label++;
200}
201
Damien6cdd3af2013-10-05 18:08:26 +0100202static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn, uint emit_options) {
Damien5ac1b2e2013-10-18 19:58:12 +0100203 scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(kind == SCOPE_MODULE), emit_options);
Damien429d7192013-10-04 19:53:11 +0100204 scope->parent = comp->scope_cur;
205 scope->next = NULL;
206 if (comp->scope_head == NULL) {
207 comp->scope_head = scope;
208 } else {
209 scope_t *s = comp->scope_head;
210 while (s->next != NULL) {
211 s = s->next;
212 }
213 s->next = scope;
214 }
215 return scope;
216}
217
Damienb05d7072013-10-05 13:37:10 +0100218static int list_len(py_parse_node_t pn, int pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100219 if (PY_PARSE_NODE_IS_NULL(pn)) {
220 return 0;
221 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
222 return 1;
223 } else {
224 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
225 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
226 return 1;
227 } else {
228 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
229 }
230 }
231}
232
Damienb05d7072013-10-05 13:37:10 +0100233static void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, py_parse_node_t)) {
Damien429d7192013-10-04 19:53:11 +0100234 if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) {
235 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
236 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
237 for (int i = 0; i < num_nodes; i++) {
238 f(comp, pns->nodes[i]);
239 }
240 } else if (!PY_PARSE_NODE_IS_NULL(pn)) {
241 f(comp, pn);
242 }
243}
244
Damienb05d7072013-10-05 13:37:10 +0100245static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
Damien429d7192013-10-04 19:53:11 +0100246 if (PY_PARSE_NODE_IS_NULL(*pn)) {
247 *nodes = NULL;
248 return 0;
249 } else if (PY_PARSE_NODE_IS_LEAF(*pn)) {
250 *nodes = pn;
251 return 1;
252 } else {
253 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)(*pn);
254 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
255 *nodes = pn;
256 return 1;
257 } else {
258 *nodes = pns->nodes;
259 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
260 }
261 }
262}
263
264void compile_do_nothing(compiler_t *comp, py_parse_node_struct_t *pns) {
265}
266
267void compile_generic_all_nodes(compiler_t *comp, py_parse_node_struct_t *pns) {
268 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
269 for (int i = 0; i < num_nodes; i++) {
270 compile_node(comp, pns->nodes[i]);
271 }
272}
273
Damien3ef4abb2013-10-12 16:53:13 +0100274#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100275static bool cpython_c_tuple_is_const(py_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +0100276 if (!PY_PARSE_NODE_IS_LEAF(pn)) {
277 return false;
278 }
279 if (PY_PARSE_NODE_IS_ID(pn)) {
280 return false;
281 }
282 return true;
283}
284
Damien3a205172013-10-12 15:01:56 +0100285static void cpython_c_tuple_emit_const(compiler_t *comp, py_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +0100286 assert(PY_PARSE_NODE_IS_LEAF(pn));
287 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
288 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
289 case PY_PARSE_NODE_ID: assert(0);
290 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_verbatim_int, arg); break;
291 case PY_PARSE_NODE_INTEGER: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
292 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
293 case PY_PARSE_NODE_STRING: EMIT(load_const_verbatim_quoted_str, arg, false); break;
294 case PY_PARSE_NODE_BYTES: EMIT(load_const_verbatim_quoted_str, arg, true); break;
295 case PY_PARSE_NODE_TOKEN:
296 switch (arg) {
297 case PY_TOKEN_KW_FALSE: EMIT(load_const_verbatim_str, "False"); break;
298 case PY_TOKEN_KW_NONE: EMIT(load_const_verbatim_str, "None"); break;
299 case PY_TOKEN_KW_TRUE: EMIT(load_const_verbatim_str, "True"); break;
300 default: assert(0);
301 }
302 break;
303 default: assert(0);
304 }
305}
306
Damien3a205172013-10-12 15:01:56 +0100307static void cpython_c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100308 int n = 0;
309 if (pns_list != NULL) {
310 n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
311 }
312 int total = n;
313 bool is_const = true;
314 if (!PY_PARSE_NODE_IS_NULL(pn)) {
315 total += 1;
Damien3a205172013-10-12 15:01:56 +0100316 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100317 is_const = false;
318 }
319 }
320 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100321 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100322 is_const = false;
323 break;
324 }
325 }
326 if (total > 0 && is_const) {
327 bool need_comma = false;
328 EMIT(load_const_verbatim_start);
329 EMIT(load_const_verbatim_str, "(");
330 if (!PY_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100331 cpython_c_tuple_emit_const(comp, pn);
Damien429d7192013-10-04 19:53:11 +0100332 need_comma = true;
333 }
334 for (int i = 0; i < n; i++) {
335 if (need_comma) {
336 EMIT(load_const_verbatim_str, ", ");
337 }
Damien3a205172013-10-12 15:01:56 +0100338 cpython_c_tuple_emit_const(comp, pns_list->nodes[i]);
Damien429d7192013-10-04 19:53:11 +0100339 need_comma = true;
340 }
341 if (total == 1) {
342 EMIT(load_const_verbatim_str, ",)");
343 } else {
344 EMIT(load_const_verbatim_str, ")");
345 }
346 EMIT(load_const_verbatim_end);
347 } else {
348 if (!PY_PARSE_NODE_IS_NULL(pn)) {
349 compile_node(comp, pn);
350 }
351 for (int i = 0; i < n; i++) {
352 compile_node(comp, pns_list->nodes[i]);
353 }
354 EMIT(build_tuple, total);
355 }
356}
Damien3a205172013-10-12 15:01:56 +0100357#endif
358
359// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
360void c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100361#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100362 cpython_c_tuple(comp, pn, pns_list);
363#else
364 int total = 0;
365 if (!PY_PARSE_NODE_IS_NULL(pn)) {
366 compile_node(comp, pn);
367 total += 1;
368 }
369 if (pns_list != NULL) {
370 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
371 for (int i = 0; i < n; i++) {
372 compile_node(comp, pns_list->nodes[i]);
373 }
374 total += n;
375 }
376 EMIT(build_tuple, total);
377#endif
378}
Damien429d7192013-10-04 19:53:11 +0100379
380void compile_generic_tuple(compiler_t *comp, py_parse_node_struct_t *pns) {
381 // a simple tuple expression
Damien429d7192013-10-04 19:53:11 +0100382 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
383}
384
Damien3a205172013-10-12 15:01:56 +0100385static bool node_is_const_false(py_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +0100386 return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_FALSE);
387 // untested: || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1);
388}
389
Damien3a205172013-10-12 15:01:56 +0100390static bool node_is_const_true(py_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +0100391 return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_TRUE) || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1);
392}
393
Damien3ef4abb2013-10-12 16:53:13 +0100394#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100395// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
396static void cpython_c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100397 if (node_is_const_false(pn)) {
398 if (jump_if == false) {
399 EMIT(jump, label);
400 }
401 return;
402 } else if (node_is_const_true(pn)) {
403 if (jump_if == true) {
404 EMIT(jump, label);
405 }
406 return;
407 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
408 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
409 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
410 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
411 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100412 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100413 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100414 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100415 }
Damien3a205172013-10-12 15:01:56 +0100416 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100417 EMIT(label_assign, label2);
418 } else {
419 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100420 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100421 }
422 }
423 return;
424 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
425 if (jump_if == false) {
426 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100427 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100428 }
429 } else {
Damienb05d7072013-10-05 13:37:10 +0100430 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100431 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100432 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100433 }
Damien3a205172013-10-12 15:01:56 +0100434 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100435 EMIT(label_assign, label2);
436 }
437 return;
438 } else if (!is_nested && PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100439 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100440 return;
441 }
442 }
443
444 // nothing special, fall back to default compiling for node and jump
445 compile_node(comp, pn);
446 if (jump_if == false) {
447 EMIT(pop_jump_if_false, label);
448 } else {
449 EMIT(pop_jump_if_true, label);
450 }
451}
Damien3a205172013-10-12 15:01:56 +0100452#endif
Damien429d7192013-10-04 19:53:11 +0100453
Damien3a205172013-10-12 15:01:56 +0100454static void c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100455#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100456 cpython_c_if_cond(comp, pn, jump_if, label, false);
457#else
458 if (node_is_const_false(pn)) {
459 if (jump_if == false) {
460 EMIT(jump, label);
461 }
462 return;
463 } else if (node_is_const_true(pn)) {
464 if (jump_if == true) {
465 EMIT(jump, label);
466 }
467 return;
468 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
469 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
470 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
471 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
472 if (jump_if == false) {
473 int label2 = comp_next_label(comp);
474 for (int i = 0; i < n - 1; i++) {
475 c_if_cond(comp, pns->nodes[i], true, label2);
476 }
477 c_if_cond(comp, pns->nodes[n - 1], false, label);
478 EMIT(label_assign, label2);
479 } else {
480 for (int i = 0; i < n; i++) {
481 c_if_cond(comp, pns->nodes[i], true, label);
482 }
483 }
484 return;
485 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
486 if (jump_if == false) {
487 for (int i = 0; i < n; i++) {
488 c_if_cond(comp, pns->nodes[i], false, label);
489 }
490 } else {
491 int label2 = comp_next_label(comp);
492 for (int i = 0; i < n - 1; i++) {
493 c_if_cond(comp, pns->nodes[i], false, label2);
494 }
495 c_if_cond(comp, pns->nodes[n - 1], true, label);
496 EMIT(label_assign, label2);
497 }
498 return;
499 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
500 c_if_cond(comp, pns->nodes[0], !jump_if, label);
501 return;
502 }
503 }
504
505 // nothing special, fall back to default compiling for node and jump
506 compile_node(comp, pn);
507 if (jump_if == false) {
508 EMIT(pop_jump_if_false, label);
509 } else {
510 EMIT(pop_jump_if_true, label);
511 }
512#endif
Damien429d7192013-10-04 19:53:11 +0100513}
514
515typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
516void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t kind);
517
518void c_assign_power(compiler_t *comp, py_parse_node_struct_t *pns, assign_kind_t assign_kind) {
519 if (assign_kind != ASSIGN_AUG_STORE) {
520 compile_node(comp, pns->nodes[0]);
521 }
522
523 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
524 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
525 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
526 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
527 if (assign_kind != ASSIGN_AUG_STORE) {
528 for (int i = 0; i < n - 1; i++) {
529 compile_node(comp, pns1->nodes[i]);
530 }
531 }
532 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
533 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
534 }
535 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
536 printf("SyntaxError: can't assign to function call\n");
537 return;
538 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
539 if (assign_kind == ASSIGN_AUG_STORE) {
540 EMIT(rot_three);
541 EMIT(store_subscr);
542 } else {
543 compile_node(comp, pns1->nodes[0]);
544 if (assign_kind == ASSIGN_AUG_LOAD) {
545 EMIT(dup_top_two);
546 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
547 } else {
548 EMIT(store_subscr);
549 }
550 }
551 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
552 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
553 if (assign_kind == ASSIGN_AUG_LOAD) {
554 EMIT(dup_top);
555 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
556 } else {
557 if (assign_kind == ASSIGN_AUG_STORE) {
558 EMIT(rot_two);
559 }
560 EMIT(store_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
561 }
562 } else {
563 // shouldn't happen
564 assert(0);
565 }
566 } else {
567 // shouldn't happen
568 assert(0);
569 }
570
571 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
572 // SyntaxError, cannot assign
573 assert(0);
574 }
575}
576
577void c_assign_tuple(compiler_t *comp, int n, py_parse_node_t *nodes) {
578 assert(n >= 0);
579 int have_star_index = -1;
580 for (int i = 0; i < n; i++) {
581 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
582 if (have_star_index < 0) {
583 EMIT(unpack_ex, i, n - i - 1);
584 have_star_index = i;
585 } else {
586 printf("SyntaxError: two starred expressions in assignment\n");
587 return;
588 }
589 }
590 }
591 if (have_star_index < 0) {
592 EMIT(unpack_sequence, n);
593 }
594 for (int i = 0; i < n; i++) {
595 if (i == have_star_index) {
596 c_assign(comp, ((py_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
597 } else {
598 c_assign(comp, nodes[i], ASSIGN_STORE);
599 }
600 }
601}
602
603// assigns top of stack to pn
604void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) {
605 tail_recursion:
606 if (PY_PARSE_NODE_IS_NULL(pn)) {
607 assert(0);
608 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
609 if (PY_PARSE_NODE_IS_ID(pn)) {
610 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
611 switch (assign_kind) {
612 case ASSIGN_STORE:
613 case ASSIGN_AUG_STORE:
Damien4b03e772013-10-05 14:17:09 +0100614 EMIT(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100615 break;
616 case ASSIGN_AUG_LOAD:
Damien4b03e772013-10-05 14:17:09 +0100617 EMIT(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100618 break;
619 }
620 } else {
621 printf("SyntaxError: can't assign to literal\n");
622 return;
623 }
624 } else {
625 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
626 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
627 case PN_power:
628 // lhs is an index or attribute
629 c_assign_power(comp, pns, assign_kind);
630 break;
631
632 case PN_testlist_star_expr:
633 case PN_exprlist:
634 // lhs is a tuple
635 if (assign_kind != ASSIGN_STORE) {
636 goto bad_aug;
637 }
638 c_assign_tuple(comp, PY_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
639 break;
640
641 case PN_atom_paren:
642 // lhs is something in parenthesis
643 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
644 // empty tuple
645 printf("SyntaxError: can't assign to ()\n");
646 return;
647 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
648 pns = (py_parse_node_struct_t*)pns->nodes[0];
649 goto testlist_comp;
650 } else {
651 // parenthesis around 1 item, is just that item
652 pn = pns->nodes[0];
653 goto tail_recursion;
654 }
655 break;
656
657 case PN_atom_bracket:
658 // lhs is something in brackets
659 if (assign_kind != ASSIGN_STORE) {
660 goto bad_aug;
661 }
662 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
663 // empty list, assignment allowed
664 c_assign_tuple(comp, 0, NULL);
665 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
666 pns = (py_parse_node_struct_t*)pns->nodes[0];
667 goto testlist_comp;
668 } else {
669 // brackets around 1 item
670 c_assign_tuple(comp, 1, &pns->nodes[0]);
671 }
672 break;
673
674 default:
675 printf("unknown assign, %u\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
676 assert(0);
677 }
678 return;
679
680 testlist_comp:
681 // lhs is a sequence
682 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
683 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
684 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
685 // sequence of one item, with trailing comma
686 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
687 c_assign_tuple(comp, 1, &pns->nodes[0]);
688 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
689 // sequence of many items
690 // TODO call c_assign_tuple instead
691 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns2);
692 EMIT(unpack_sequence, 1 + n);
693 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
694 for (int i = 0; i < n; i++) {
695 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
696 }
697 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
698 // TODO not implemented
699 assert(0);
700 } else {
701 // sequence with 2 items
702 goto sequence_with_2_items;
703 }
704 } else {
705 // sequence with 2 items
706 sequence_with_2_items:
707 c_assign_tuple(comp, 2, pns->nodes);
708 }
709 return;
710 }
711 return;
712
713 bad_aug:
714 printf("SyntaxError: illegal expression for augmented assignment\n");
715}
716
717// stuff for lambda and comprehensions and generators
718void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
719 // make closed over variables, if any
720 int nfree = 0;
721 if (comp->scope_cur->kind != SCOPE_MODULE) {
722 for (int i = 0; i < this_scope->id_info_len; i++) {
723 id_info_t *id_info = &this_scope->id_info[i];
724 if (id_info->kind == ID_INFO_KIND_FREE) {
Damien27fb45e2013-10-20 15:07:49 +0100725 EMIT(load_closure, id_info->qstr, id_info->local_num);
Damien429d7192013-10-04 19:53:11 +0100726 nfree += 1;
727 }
728 }
729 }
730 if (nfree > 0) {
731 EMIT(build_tuple, nfree);
732 }
733
734 // make the function/closure
735 if (nfree == 0) {
736 EMIT(make_function, this_scope, n_dict_params, n_default_params);
737 } else {
738 EMIT(make_closure, this_scope, n_dict_params, n_default_params);
739 }
740}
741
742void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) {
Damienb14de212013-10-06 00:28:28 +0100743 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
744 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100745 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
746 // this parameter has a default value
747 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
748 if (comp->have_bare_star) {
749 comp->param_pass_num_dict_params += 1;
750 if (comp->param_pass == 1) {
751 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
752 compile_node(comp, pns->nodes[2]);
753 }
754 } else {
755 comp->param_pass_num_default_params += 1;
756 if (comp->param_pass == 2) {
757 compile_node(comp, pns->nodes[2]);
758 }
759 }
760 }
Damienb14de212013-10-06 00:28:28 +0100761 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
762 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100763 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
764 // bare star
765 comp->have_bare_star = true;
766 }
767 }
768}
769
770// leaves function object on stack
771// returns function name
Damien6cdd3af2013-10-05 18:08:26 +0100772qstr compile_funcdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100773 if (comp->pass == PASS_1) {
774 // create a new scope for this function
Damien6cdd3af2013-10-05 18:08:26 +0100775 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (py_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100776 // store the function scope so the compiling function can use it at each pass
777 pns->nodes[4] = (py_parse_node_t)s;
778 }
779
780 // save variables (probably don't need to do this, since we can't have nested definitions..?)
781 bool old_have_bare_star = comp->have_bare_star;
782 int old_param_pass = comp->param_pass;
783 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
784 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
785
786 // compile default parameters
787 comp->have_bare_star = false;
788 comp->param_pass = 1; // pass 1 does any default parameters after bare star
789 comp->param_pass_num_dict_params = 0;
790 comp->param_pass_num_default_params = 0;
791 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
792 comp->have_bare_star = false;
793 comp->param_pass = 2; // pass 2 does any default parameters before bare star
794 comp->param_pass_num_dict_params = 0;
795 comp->param_pass_num_default_params = 0;
796 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
797
798 // get the scope for this function
799 scope_t *fscope = (scope_t*)pns->nodes[4];
800
801 // make the function
802 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
803
804 // restore variables
805 comp->have_bare_star = old_have_bare_star;
806 comp->param_pass = old_param_pass;
807 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
808 comp->param_pass_num_default_params = old_param_pass_num_default_params;
809
810 // return its name (the 'f' in "def f(...):")
811 return fscope->simple_name;
812}
813
814// leaves class object on stack
815// returns class name
Damien6cdd3af2013-10-05 18:08:26 +0100816qstr compile_classdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100817 if (comp->pass == PASS_1) {
818 // create a new scope for this class
Damien6cdd3af2013-10-05 18:08:26 +0100819 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (py_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100820 // store the class scope so the compiling function can use it at each pass
821 pns->nodes[3] = (py_parse_node_t)s;
822 }
823
824 EMIT(load_build_class);
825
826 // scope for this class
827 scope_t *cscope = (scope_t*)pns->nodes[3];
828
829 // compile the class
830 close_over_variables_etc(comp, cscope, 0, 0);
831
832 // get its name
833 EMIT(load_const_id, cscope->simple_name);
834
835 // nodes[1] has parent classes, if any
836 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
837 // no parent classes
838 EMIT(call_function, 2, 0, false, false);
839 } else {
840 // have a parent class or classes
841 // TODO what if we have, eg, *a or **a in the parent list?
842 compile_node(comp, pns->nodes[1]);
843 EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
844 }
845
846 // return its name (the 'C' in class C(...):")
847 return cscope->simple_name;
848}
849
Damien6cdd3af2013-10-05 18:08:26 +0100850// returns true if it was a built-in decorator (even if the built-in had an error)
851static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_node_t *name_nodes, uint *emit_options) {
852 if (PY_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) {
853 return false;
854 }
855
856 if (name_len != 2) {
857 printf("SyntaxError: invalid micropython decorator\n");
858 return true;
859 }
860
861 qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien5ac1b2e2013-10-18 19:58:12 +0100862 if (attr == comp->qstr_byte_code) {
863 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100864#if MICROPY_EMIT_NATIVE
865 } else if (attr == comp->qstr_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100866 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien7af3d192013-10-07 00:02:49 +0100867 } else if (attr == comp->qstr_viper) {
868 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100869#endif
Damien3ef4abb2013-10-12 16:53:13 +0100870#if MICROPY_EMIT_INLINE_THUMB
Damien5bfb7592013-10-05 18:41:24 +0100871 } else if (attr == comp->qstr_asm_thumb) {
872 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100873#endif
Damien6cdd3af2013-10-05 18:08:26 +0100874 } else {
Damience89a212013-10-15 22:25:17 +0100875 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100876 }
877
878 return true;
879}
880
Damien429d7192013-10-04 19:53:11 +0100881void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) {
882 // get the list of decorators
883 py_parse_node_t *nodes;
884 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
885
Damien6cdd3af2013-10-05 18:08:26 +0100886 // inherit emit options for this function/class definition
887 uint emit_options = comp->scope_cur->emit_options;
888
889 // compile each decorator
890 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100891 for (int i = 0; i < n; i++) {
892 assert(PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
893 py_parse_node_struct_t *pns_decorator = (py_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100894
895 // nodes[0] contains the decorator function, which is a dotted name
896 py_parse_node_t *name_nodes;
897 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
898
899 // check for built-in decorators
900 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
901 // this was a built-in
902 num_built_in_decorators += 1;
903
904 } else {
905 // not a built-in, compile normally
906
907 // compile the decorator function
908 compile_node(comp, name_nodes[0]);
909 for (int i = 1; i < name_len; i++) {
910 assert(PY_PARSE_NODE_IS_ID(name_nodes[i])); // should be
911 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(name_nodes[i]));
912 }
913
914 // nodes[1] contains arguments to the decorator function, if any
915 if (!PY_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
916 // call the decorator function with the arguments in nodes[1]
917 compile_node(comp, pns_decorator->nodes[1]);
918 }
Damien429d7192013-10-04 19:53:11 +0100919 }
920 }
921
922 // compile the body (funcdef or classdef) and get its name
923 py_parse_node_struct_t *pns_body = (py_parse_node_struct_t*)pns->nodes[1];
924 qstr body_name = 0;
925 if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100926 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100927 } else if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100928 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100929 } else {
930 // shouldn't happen
931 assert(0);
932 }
933
934 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100935 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien429d7192013-10-04 19:53:11 +0100936 EMIT(call_function, 1, 0, false, false);
937 }
938
939 // store func/class object into name
Damien4b03e772013-10-05 14:17:09 +0100940 EMIT(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100941}
942
943void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100944 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100945 // store function object into function name
Damien4b03e772013-10-05 14:17:09 +0100946 EMIT(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100947}
948
949void c_del_stmt(compiler_t *comp, py_parse_node_t pn) {
950 if (PY_PARSE_NODE_IS_ID(pn)) {
Damien4b03e772013-10-05 14:17:09 +0100951 EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn));
Damien429d7192013-10-04 19:53:11 +0100952 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
953 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
954
955 compile_node(comp, pns->nodes[0]); // base of the power node
956
957 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
958 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
959 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
960 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
961 for (int i = 0; i < n - 1; i++) {
962 compile_node(comp, pns1->nodes[i]);
963 }
964 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
965 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
966 }
967 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
968 // SyntaxError: can't delete a function call
969 assert(0);
970 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
971 compile_node(comp, pns1->nodes[0]);
972 EMIT(delete_subscr);
973 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
974 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
975 EMIT(delete_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
976 } else {
977 // shouldn't happen
978 assert(0);
979 }
980 } else {
981 // shouldn't happen
982 assert(0);
983 }
984
985 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
986 // SyntaxError, cannot delete
987 assert(0);
988 }
989 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
990 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
991 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
992 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
993 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
994
995 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
996 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
997 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
998 // sequence of one item, with trailing comma
999 assert(PY_PARSE_NODE_IS_NULL(pns1->nodes[0]));
1000 c_del_stmt(comp, pns->nodes[0]);
1001 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
1002 // sequence of many items
1003 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
1004 c_del_stmt(comp, pns->nodes[0]);
1005 for (int i = 0; i < n; i++) {
1006 c_del_stmt(comp, pns1->nodes[i]);
1007 }
1008 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
1009 // TODO not implemented; can't del comprehension?
1010 assert(0);
1011 } else {
1012 // sequence with 2 items
1013 goto sequence_with_2_items;
1014 }
1015 } else {
1016 // sequence with 2 items
1017 sequence_with_2_items:
1018 c_del_stmt(comp, pns->nodes[0]);
1019 c_del_stmt(comp, pns->nodes[1]);
1020 }
1021 } else {
1022 // tuple with 1 element
1023 c_del_stmt(comp, pn);
1024 }
1025 } else {
1026 // not implemented
1027 assert(0);
1028 }
1029}
1030
1031void compile_del_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1032 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1033}
1034
1035void compile_break_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1036 if (comp->break_label == 0) {
1037 printf("ERROR: cannot break from here\n");
1038 }
1039 EMIT(break_loop, comp->break_label);
1040}
1041
1042void compile_continue_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1043 if (comp->continue_label == 0) {
1044 printf("ERROR: cannot continue from here\n");
1045 }
1046 if (comp->except_nest_level > 0) {
1047 EMIT(continue_loop, comp->continue_label);
1048 } else {
1049 EMIT(jump, comp->continue_label);
1050 }
1051}
1052
1053void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001054 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1055 printf("SyntaxError: 'return' outside function\n");
1056 comp->had_error = true;
1057 return;
1058 }
Damien429d7192013-10-04 19:53:11 +01001059 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001060 // no argument to 'return', so return None
Damien429d7192013-10-04 19:53:11 +01001061 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1062 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
1063 // special case when returning an if-expression; to match CPython optimisation
1064 py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0];
1065 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1];
1066
Damienb05d7072013-10-05 13:37:10 +01001067 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001068 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1069 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1070 EMIT(return_value);
1071 EMIT(label_assign, l_fail);
1072 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1073 } else {
1074 compile_node(comp, pns->nodes[0]);
1075 }
1076 EMIT(return_value);
1077}
1078
1079void compile_yield_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1080 compile_node(comp, pns->nodes[0]);
1081 EMIT(pop_top);
1082}
1083
1084void compile_raise_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1085 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1086 // raise
1087 EMIT(raise_varargs, 0);
1088 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
1089 // raise x from y
1090 pns = (py_parse_node_struct_t*)pns->nodes[0];
1091 compile_node(comp, pns->nodes[0]);
1092 compile_node(comp, pns->nodes[1]);
1093 EMIT(raise_varargs, 2);
1094 } else {
1095 // raise x
1096 compile_node(comp, pns->nodes[0]);
1097 EMIT(raise_varargs, 1);
1098 }
1099}
1100
1101// q1 holds the base, q2 the full name
1102// eg a -> q1=q2=a
1103// a.b.c -> q1=a, q2=a.b.c
1104void do_import_name(compiler_t *comp, py_parse_node_t pn, qstr *q1, qstr *q2) {
1105 bool is_as = false;
1106 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1107 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
1108 // a name of the form x as y; unwrap it
1109 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1110 pn = pns->nodes[0];
1111 is_as = true;
1112 }
1113 if (PY_PARSE_NODE_IS_ID(pn)) {
1114 // just a simple name
1115 *q2 = PY_PARSE_NODE_LEAF_ARG(pn);
1116 if (!is_as) {
1117 *q1 = *q2;
1118 }
1119 EMIT(import_name, *q2);
1120 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
1121 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
1122 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
1123 // a name of the form a.b.c
1124 if (!is_as) {
1125 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1126 }
1127 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1128 int len = n - 1;
1129 for (int i = 0; i < n; i++) {
1130 len += strlen(qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
1131 }
1132 char *str = m_new(char, len + 1);
1133 str[0] = 0;
1134 for (int i = 0; i < n; i++) {
1135 if (i > 0) {
1136 strcat(str, ".");
1137 }
1138 strcat(str, qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
1139 }
1140 *q2 = qstr_from_str_take(str);
1141 EMIT(import_name, *q2);
1142 if (is_as) {
1143 for (int i = 1; i < n; i++) {
1144 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1145 }
1146 }
1147 } else {
1148 // TODO not implemented
1149 assert(0);
1150 }
1151 } else {
1152 // TODO not implemented
1153 assert(0);
1154 }
1155}
1156
1157void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) {
1158 EMIT(load_const_small_int, 0); // ??
1159 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1160 qstr q1, q2;
1161 do_import_name(comp, pn, &q1, &q2);
Damien4b03e772013-10-05 14:17:09 +01001162 EMIT(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001163}
1164
1165void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) {
1166 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1167}
1168
1169void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) {
1170 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
1171 EMIT(load_const_small_int, 0); // what's this for??
1172 EMIT(load_const_verbatim_start);
1173 EMIT(load_const_verbatim_str, "('*',)");
1174 EMIT(load_const_verbatim_end);
1175 qstr dummy_q, id1;
1176 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1177 EMIT(import_star);
1178 } else {
1179 py_parse_node_t *pn_nodes;
1180 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1181
1182 EMIT(load_const_small_int, 0); // what's this for??
1183 EMIT(load_const_verbatim_start);
1184 EMIT(load_const_verbatim_str, "(");
1185 for (int i = 0; i < n; i++) {
1186 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1187 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1188 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1189 if (i > 0) {
1190 EMIT(load_const_verbatim_str, ", ");
1191 }
1192 EMIT(load_const_verbatim_str, "'");
1193 EMIT(load_const_verbatim_str, qstr_str(id2));
1194 EMIT(load_const_verbatim_str, "'");
1195 }
1196 if (n == 1) {
1197 EMIT(load_const_verbatim_str, ",");
1198 }
1199 EMIT(load_const_verbatim_str, ")");
1200 EMIT(load_const_verbatim_end);
1201 qstr dummy_q, id1;
1202 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1203 for (int i = 0; i < n; i++) {
1204 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1205 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1206 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1207 EMIT(import_from, id2);
1208 if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien4b03e772013-10-05 14:17:09 +01001209 EMIT(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001210 } else {
Damien4b03e772013-10-05 14:17:09 +01001211 EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001212 }
1213 }
1214 EMIT(pop_top);
1215 }
1216}
1217
1218void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001219 if (comp->pass == PASS_1) {
1220 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1221 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1222 } else {
1223 pns = (py_parse_node_struct_t*)pns->nodes[0];
1224 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1225 for (int i = 0; i < num_nodes; i++) {
1226 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1227 }
Damien429d7192013-10-04 19:53:11 +01001228 }
1229 }
1230}
1231
1232void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001233 if (comp->pass == PASS_1) {
1234 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1235 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1236 } else {
1237 pns = (py_parse_node_struct_t*)pns->nodes[0];
1238 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1239 for (int i = 0; i < num_nodes; i++) {
1240 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1241 }
Damien429d7192013-10-04 19:53:11 +01001242 }
1243 }
1244}
1245
1246void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001247 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001248 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien4b03e772013-10-05 14:17:09 +01001249 EMIT(load_id, comp->qstr_assertion_error);
Damien429d7192013-10-04 19:53:11 +01001250 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1251 // assertion message
1252 compile_node(comp, pns->nodes[1]);
1253 EMIT(call_function, 1, 0, false, false);
1254 }
1255 EMIT(raise_varargs, 1);
1256 EMIT(label_assign, l_end);
1257}
1258
1259void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1260 // TODO proper and/or short circuiting
1261
Damienb05d7072013-10-05 13:37:10 +01001262 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001263
Damienb05d7072013-10-05 13:37:10 +01001264 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001265 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1266
1267 compile_node(comp, pns->nodes[1]); // if block
1268 //if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
1269 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001270 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001271 EMIT(jump, l_end);
1272 }
1273 //}
1274 EMIT(label_assign, l_fail);
1275
1276 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
1277 // compile elif blocks
1278
1279 py_parse_node_struct_t *pns_elif = (py_parse_node_struct_t*)pns->nodes[2];
1280
1281 if (PY_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
1282 // multiple elif blocks
1283
1284 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
1285 for (int i = 0; i < n; i++) {
1286 py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001287 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001288 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1289
1290 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001291 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001292 EMIT(jump, l_end);
1293 }
1294 EMIT(label_assign, l_fail);
1295 }
1296
1297 } else {
1298 // a single elif block
1299
Damienb05d7072013-10-05 13:37:10 +01001300 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001301 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1302
1303 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001304 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001305 EMIT(jump, l_end);
1306 }
1307 EMIT(label_assign, l_fail);
1308 }
1309 }
1310
1311 // compile else block
1312 compile_node(comp, pns->nodes[3]); // can be null
1313
1314 EMIT(label_assign, l_end);
1315}
1316
1317void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1318 int old_break_label = comp->break_label;
1319 int old_continue_label = comp->continue_label;
1320
Damienb05d7072013-10-05 13:37:10 +01001321 int break_label = comp_next_label(comp);
1322 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001323
1324 comp->break_label = break_label;
1325 comp->continue_label = continue_label;
1326
Damience89a212013-10-15 22:25:17 +01001327 // compared to CPython, we have an optimised version of while loops
1328#if MICROPY_EMIT_CPYTHON
1329 int done_label = comp_next_label(comp);
1330 EMIT(setup_loop, break_label);
Damien429d7192013-10-04 19:53:11 +01001331 EMIT(label_assign, continue_label);
1332 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1333 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001334 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001335 EMIT(jump, continue_label);
1336 }
1337 EMIT(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001338 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1339 // this is a small hack to agree with CPython
1340 if (!node_is_const_true(pns->nodes[0])) {
1341 EMIT(pop_block);
1342 }
Damience89a212013-10-15 22:25:17 +01001343#else
1344 int top_label = comp_next_label(comp);
1345 EMIT(jump, continue_label);
1346 EMIT(label_assign, top_label);
1347 compile_node(comp, pns->nodes[1]); // body
1348 EMIT(label_assign, continue_label);
1349 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1350#endif
1351
1352 // break/continue apply to outer loop (if any) in the else block
1353 comp->break_label = old_break_label;
1354 comp->continue_label = old_continue_label;
Damien429d7192013-10-04 19:53:11 +01001355
1356 compile_node(comp, pns->nodes[2]); // else
1357
1358 EMIT(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001359}
1360
Damienf72fd0e2013-11-06 20:20:49 +00001361// TODO preload end and step onto stack if they are not constants
1362// TODO check if step is negative and do opposite test
1363void compile_for_stmt_optimised_range(compiler_t *comp, py_parse_node_t pn_var, py_parse_node_t pn_start, py_parse_node_t pn_end, py_parse_node_t pn_step, py_parse_node_t pn_body, py_parse_node_t pn_else) {
1364 int old_break_label = comp->break_label;
1365 int old_continue_label = comp->continue_label;
1366
1367 int break_label = comp_next_label(comp);
1368 int continue_label = comp_next_label(comp);
1369
1370 comp->break_label = break_label;
1371 comp->continue_label = continue_label;
1372
1373 int top_label = comp_next_label(comp);
1374
1375 // compile: var = start
1376 compile_node(comp, pn_start);
1377 c_assign(comp, pn_var, ASSIGN_STORE);
1378
1379 EMIT(jump, continue_label);
1380 EMIT(label_assign, top_label);
1381
Damienf3822fc2013-11-09 20:12:03 +00001382 // compile body
1383 compile_node(comp, pn_body);
1384
Damienf72fd0e2013-11-06 20:20:49 +00001385 // compile: var += step
1386 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1387 compile_node(comp, pn_step);
1388 EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD);
1389 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1390
Damienf72fd0e2013-11-06 20:20:49 +00001391 EMIT(label_assign, continue_label);
1392
1393 // compile: if var < end: goto top
1394 compile_node(comp, pn_var);
1395 compile_node(comp, pn_end);
1396 EMIT(compare_op, RT_COMPARE_OP_LESS);
1397 EMIT(pop_jump_if_true, top_label);
1398
1399 // break/continue apply to outer loop (if any) in the else block
1400 comp->break_label = old_break_label;
1401 comp->continue_label = old_continue_label;
1402
1403 compile_node(comp, pn_else);
1404
1405 EMIT(label_assign, break_label);
1406}
1407
Damien429d7192013-10-04 19:53:11 +01001408void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001409#if !MICROPY_EMIT_CPYTHON
1410 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1411 // this is actually slower, but uses no heap memory
1412 // for viper it will be much, much faster
Damiend7933892013-11-28 19:12:18 +00001413 if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ PY_PARSE_NODE_IS_ID(pns->nodes[0]) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
Damienf72fd0e2013-11-06 20:20:49 +00001414 py_parse_node_struct_t *pns_it = (py_parse_node_struct_t*)pns->nodes[1];
Damiend7933892013-11-28 19:12:18 +00001415 if (PY_PARSE_NODE_IS_ID(pns_it->nodes[0]) && PY_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == comp->qstr_range && PY_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && PY_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damienf72fd0e2013-11-06 20:20:49 +00001416 py_parse_node_t pn_range_args = ((py_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1417 py_parse_node_t *args;
1418 int n_args = list_get(&pn_range_args, PN_arglist, &args);
1419 if (1 <= n_args && n_args <= 3) {
1420 py_parse_node_t pn_range_start;
1421 py_parse_node_t pn_range_end;
1422 py_parse_node_t pn_range_step;
1423 if (n_args == 1) {
1424 pn_range_start = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 0);
1425 pn_range_end = args[0];
1426 pn_range_step = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 1);
1427 } else if (n_args == 2) {
1428 pn_range_start = args[0];
1429 pn_range_end = args[1];
1430 pn_range_step = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 1);
1431 } else {
1432 pn_range_start = args[0];
1433 pn_range_end = args[1];
1434 pn_range_step = args[2];
1435 }
1436 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1437 return;
1438 }
1439 }
1440 }
1441#endif
1442
Damien429d7192013-10-04 19:53:11 +01001443 int old_break_label = comp->break_label;
1444 int old_continue_label = comp->continue_label;
1445
Damienb05d7072013-10-05 13:37:10 +01001446 int for_label = comp_next_label(comp);
1447 int pop_label = comp_next_label(comp);
1448 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001449
Damienb05d7072013-10-05 13:37:10 +01001450 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001451
1452 comp->continue_label = for_label;
1453 comp->break_label = break_label;
1454
Damience89a212013-10-15 22:25:17 +01001455 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1456#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001457 EMIT(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001458#endif
1459
Damien429d7192013-10-04 19:53:11 +01001460 compile_node(comp, pns->nodes[1]); // iterator
1461 EMIT(get_iter);
1462 EMIT(label_assign, for_label);
1463 EMIT(for_iter, pop_label);
1464 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1465 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001466 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001467 EMIT(jump, for_label);
1468 }
1469 EMIT(label_assign, pop_label);
1470 EMIT(for_iter_end);
1471
1472 // break/continue apply to outer loop (if any) in the else block
1473 comp->break_label = old_break_label;
1474 comp->continue_label = old_continue_label;
1475
Damience89a212013-10-15 22:25:17 +01001476#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001477 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001478#endif
Damien429d7192013-10-04 19:53:11 +01001479
1480 compile_node(comp, pns->nodes[3]); // else (not tested)
1481
1482 EMIT(label_assign, break_label);
1483 EMIT(label_assign, end_label);
1484}
1485
1486void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_excepts, py_parse_node_t pn_else) {
1487 // this function is a bit of a hack at the moment
1488 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1489
1490 // setup code
1491 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001492 int l1 = comp_next_label(comp);
1493 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001494 comp->except_nest_level += 1; // for correct handling of continue
1495 EMIT(setup_except, l1);
1496 compile_node(comp, pn_body); // body
1497 EMIT(pop_block);
1498 EMIT(jump, success_label);
1499 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001500 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001501
1502 for (int i = 0; i < n_except; i++) {
1503 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1504 py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i];
1505
1506 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001507 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001508
1509 if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
1510 // this is a catch all exception handler
1511 if (i + 1 != n_except) {
1512 printf("SyntaxError: default 'except:' must be last\n");
1513 return;
1514 }
1515 } else {
1516 // this exception handler requires a match to a certain type of exception
1517 py_parse_node_t pns_exception_expr = pns_except->nodes[0];
1518 if (PY_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1519 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns_exception_expr;
1520 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
1521 // handler binds the exception to a local
1522 pns_exception_expr = pns3->nodes[0];
1523 qstr_exception_local = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
1524 }
1525 }
1526 EMIT(dup_top);
1527 compile_node(comp, pns_exception_expr);
1528 EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1529 EMIT(pop_jump_if_false, end_finally_label);
1530 }
1531
1532 EMIT(pop_top);
1533
1534 if (qstr_exception_local == 0) {
1535 EMIT(pop_top);
1536 } else {
Damien4b03e772013-10-05 14:17:09 +01001537 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001538 }
1539
1540 EMIT(pop_top);
1541
1542 int l3;
1543 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001544 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001545 EMIT(setup_finally, l3);
1546 }
1547 compile_node(comp, pns_except->nodes[1]);
1548 if (qstr_exception_local != 0) {
1549 EMIT(pop_block);
1550 }
1551 EMIT(pop_except);
1552 if (qstr_exception_local != 0) {
1553 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1554 EMIT(label_assign, l3);
1555 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001556 EMIT(store_id, qstr_exception_local);
1557 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001558 EMIT(end_finally);
1559 }
1560 EMIT(jump, l2);
1561 EMIT(label_assign, end_finally_label);
1562 }
1563
1564 EMIT(end_finally);
1565 EMIT(label_assign, success_label);
1566 comp->except_nest_level -= 1;
1567 compile_node(comp, pn_else); // else block, can be null
1568 EMIT(label_assign, l2);
1569 EMIT(set_stack_size, stack_size);
1570}
1571
1572void compile_try_finally(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_except, py_parse_node_t pn_else, py_parse_node_t pn_finally) {
1573 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1574 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001575 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001576 EMIT(setup_finally, l_finally_block);
1577 if (n_except == 0) {
1578 assert(PY_PARSE_NODE_IS_NULL(pn_else));
1579 compile_node(comp, pn_body);
1580 } else {
1581 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1582 }
1583 EMIT(pop_block);
1584 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1585 EMIT(label_assign, l_finally_block);
1586 compile_node(comp, pn_finally);
1587 EMIT(end_finally);
1588 EMIT(set_stack_size, stack_size);
1589}
1590
1591void compile_try_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1592 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1593 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
1594 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
1595 // just try-finally
1596 compile_try_finally(comp, pns->nodes[0], 0, NULL, PY_PARSE_NODE_NULL, pns2->nodes[0]);
1597 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
1598 // try-except and possibly else and/or finally
1599 py_parse_node_t *pn_excepts;
1600 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1601 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
1602 // no finally
1603 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1604 } else {
1605 // have finally
1606 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((py_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
1607 }
1608 } else {
1609 // just try-except
1610 py_parse_node_t *pn_excepts;
1611 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1612 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, PY_PARSE_NODE_NULL);
1613 }
1614 } else {
1615 // shouldn't happen
1616 assert(0);
1617 }
1618}
1619
1620void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, py_parse_node_t body) {
1621 if (n == 0) {
1622 // no more pre-bits, compile the body of the with
1623 compile_node(comp, body);
1624 } else {
Damienb05d7072013-10-05 13:37:10 +01001625 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001626 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1627 // this pre-bit is of the form "a as b"
1628 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0];
1629 compile_node(comp, pns->nodes[0]);
1630 EMIT(setup_with, l_end);
1631 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1632 } else {
1633 // this pre-bit is just an expression
1634 compile_node(comp, nodes[0]);
1635 EMIT(setup_with, l_end);
1636 EMIT(pop_top);
1637 }
1638 // compile additional pre-bits and the body
1639 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1640 // finish this with block
1641 EMIT(pop_block);
1642 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1643 EMIT(label_assign, l_end);
1644 EMIT(with_cleanup);
1645 EMIT(end_finally);
1646 }
1647}
1648
1649void compile_with_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1650 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1651 py_parse_node_t *nodes;
1652 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1653 assert(n > 0);
1654
1655 // compile in a nested fashion
1656 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1657}
1658
1659void compile_expr_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1660 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001661 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1662 // for REPL, evaluate then print the expression
1663 EMIT(load_id, qstr_from_str_static("__repl_print__"));
1664 compile_node(comp, pns->nodes[0]);
1665 EMIT(call_function, 1, 0, false, false);
1666 EMIT(pop_top);
1667
Damien429d7192013-10-04 19:53:11 +01001668 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001669 // for non-REPL, evaluate then discard the expression
1670 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
1671 // do nothing with a lonely constant
1672 } else {
1673 compile_node(comp, pns->nodes[0]); // just an expression
1674 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1675 }
Damien429d7192013-10-04 19:53:11 +01001676 }
1677 } else {
1678 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
1679 int kind = PY_PARSE_NODE_STRUCT_KIND(pns1);
1680 if (kind == PN_expr_stmt_augassign) {
1681 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1682 compile_node(comp, pns1->nodes[1]); // rhs
1683 assert(PY_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
1684 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
1685 switch (PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1686 case PY_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1687 case PY_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1688 case PY_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1689 case PY_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1690 case PY_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1691 case PY_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1692 case PY_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1693 case PY_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1694 case PY_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1695 case PY_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1696 case PY_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1697 case PY_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
1698 default: assert(0); // shouldn't happen
1699 }
1700 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1701 } else if (kind == PN_expr_stmt_assign_list) {
1702 int rhs = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1703 compile_node(comp, ((py_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
1704 // following CPython, we store left-most first
1705 if (rhs > 0) {
1706 EMIT(dup_top);
1707 }
1708 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1709 for (int i = 0; i < rhs; i++) {
1710 if (i + 1 < rhs) {
1711 EMIT(dup_top);
1712 }
1713 c_assign(comp, ((py_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
1714 }
1715 } else if (kind == PN_expr_stmt_assign) {
1716 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1717 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1718 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 2
1719 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 2) {
1720 // optimisation for a, b = c, d; to match CPython's optimisation
1721 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1722 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1723 compile_node(comp, pns10->nodes[0]); // rhs
1724 compile_node(comp, pns10->nodes[1]); // rhs
1725 EMIT(rot_two);
1726 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1727 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1728 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1729 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1730 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 3
1731 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 3) {
1732 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
1733 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1734 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1735 compile_node(comp, pns10->nodes[0]); // rhs
1736 compile_node(comp, pns10->nodes[1]); // rhs
1737 compile_node(comp, pns10->nodes[2]); // rhs
1738 EMIT(rot_three);
1739 EMIT(rot_two);
1740 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1741 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1742 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1743 } else {
1744 compile_node(comp, pns1->nodes[0]); // rhs
1745 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1746 }
1747 } else {
1748 // shouldn't happen
1749 assert(0);
1750 }
1751 }
1752}
1753
1754void c_binary_op(compiler_t *comp, py_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1755 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1756 compile_node(comp, pns->nodes[0]);
1757 for (int i = 1; i < num_nodes; i += 1) {
1758 compile_node(comp, pns->nodes[i]);
1759 EMIT(binary_op, binary_op);
1760 }
1761}
1762
1763void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1764 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1765 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1];
1766
1767 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001768 int l_fail = comp_next_label(comp);
1769 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001770 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1771 compile_node(comp, pns->nodes[0]); // success value
1772 EMIT(jump, l_end);
1773 EMIT(label_assign, l_fail);
1774 EMIT(set_stack_size, stack_size); // force stack size reset
1775 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1776 EMIT(label_assign, l_end);
1777}
1778
1779void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) {
1780 // TODO default params etc for lambda; possibly just use funcdef code
1781 //py_parse_node_t pn_params = pns->nodes[0];
1782 //py_parse_node_t pn_body = pns->nodes[1];
1783
1784 if (comp->pass == PASS_1) {
1785 // create a new scope for this lambda
Damien6cdd3af2013-10-05 18:08:26 +01001786 scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (py_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001787 // store the lambda scope so the compiling function (this one) can use it at each pass
1788 pns->nodes[2] = (py_parse_node_t)s;
1789 }
1790
1791 // get the scope for this lambda
1792 scope_t *this_scope = (scope_t*)pns->nodes[2];
1793
1794 // make the lambda
1795 close_over_variables_etc(comp, this_scope, 0, 0);
1796}
1797
1798void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001799 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001800 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1801 for (int i = 0; i < n; i += 1) {
1802 compile_node(comp, pns->nodes[i]);
1803 if (i + 1 < n) {
1804 EMIT(jump_if_true_or_pop, l_end);
1805 }
1806 }
1807 EMIT(label_assign, l_end);
1808}
1809
1810void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001811 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001812 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1813 for (int i = 0; i < n; i += 1) {
1814 compile_node(comp, pns->nodes[i]);
1815 if (i + 1 < n) {
1816 EMIT(jump_if_false_or_pop, l_end);
1817 }
1818 }
1819 EMIT(label_assign, l_end);
1820}
1821
1822void compile_not_test_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1823 compile_node(comp, pns->nodes[0]);
1824 EMIT(unary_op, RT_UNARY_OP_NOT);
1825}
1826
1827void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
1828 int stack_size = EMIT(get_stack_size);
1829 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1830 compile_node(comp, pns->nodes[0]);
1831 bool multi = (num_nodes > 3);
1832 int l_fail = 0;
1833 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001834 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001835 }
1836 for (int i = 1; i + 1 < num_nodes; i += 2) {
1837 compile_node(comp, pns->nodes[i + 1]);
1838 if (i + 2 < num_nodes) {
1839 EMIT(dup_top);
1840 EMIT(rot_three);
1841 }
1842 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS)) {
1843 EMIT(compare_op, RT_COMPARE_OP_LESS);
1844 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE)) {
1845 EMIT(compare_op, RT_COMPARE_OP_MORE);
1846 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_EQUAL)) {
1847 EMIT(compare_op, RT_COMPARE_OP_EQUAL);
1848 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS_EQUAL)) {
1849 EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
1850 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE_EQUAL)) {
1851 EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
1852 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_NOT_EQUAL)) {
1853 EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
1854 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_KW_IN)) {
1855 EMIT(compare_op, RT_COMPARE_OP_IN);
1856 } else if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1857 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[i];
1858 int kind = PY_PARSE_NODE_STRUCT_KIND(pns2);
1859 if (kind == PN_comp_op_not_in) {
1860 EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1861 } else if (kind == PN_comp_op_is) {
1862 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
1863 EMIT(compare_op, RT_COMPARE_OP_IS);
1864 } else {
1865 EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1866 }
1867 } else {
1868 // shouldn't happen
1869 assert(0);
1870 }
1871 } else {
1872 // shouldn't happen
1873 assert(0);
1874 }
1875 if (i + 2 < num_nodes) {
1876 EMIT(jump_if_false_or_pop, l_fail);
1877 }
1878 }
1879 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001880 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001881 EMIT(jump, l_end);
1882 EMIT(label_assign, l_fail);
1883 EMIT(rot_two);
1884 EMIT(pop_top);
1885 EMIT(label_assign, l_end);
1886 EMIT(set_stack_size, stack_size + 1); // force stack size
1887 }
1888}
1889
1890void compile_star_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1891 // TODO
1892 assert(0);
1893 compile_node(comp, pns->nodes[0]);
1894 //EMIT(unary_op, "UNARY_STAR");
1895}
1896
1897void compile_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1898 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1899}
1900
1901void compile_xor_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1902 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1903}
1904
1905void compile_and_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1906 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1907}
1908
1909void compile_shift_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1910 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1911 compile_node(comp, pns->nodes[0]);
1912 for (int i = 1; i + 1 < num_nodes; i += 2) {
1913 compile_node(comp, pns->nodes[i + 1]);
1914 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_LESS)) {
1915 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
1916 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_MORE)) {
1917 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
1918 } else {
1919 // shouldn't happen
1920 assert(0);
1921 }
1922 }
1923}
1924
1925void compile_arith_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1926 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1927 compile_node(comp, pns->nodes[0]);
1928 for (int i = 1; i + 1 < num_nodes; i += 2) {
1929 compile_node(comp, pns->nodes[i + 1]);
1930 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PLUS)) {
1931 EMIT(binary_op, RT_BINARY_OP_ADD);
1932 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MINUS)) {
1933 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
1934 } else {
1935 // shouldn't happen
1936 assert(0);
1937 }
1938 }
1939}
1940
1941void compile_term(compiler_t *comp, py_parse_node_struct_t *pns) {
1942 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1943 compile_node(comp, pns->nodes[0]);
1944 for (int i = 1; i + 1 < num_nodes; i += 2) {
1945 compile_node(comp, pns->nodes[i + 1]);
1946 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_STAR)) {
1947 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
1948 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_SLASH)) {
1949 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
1950 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_SLASH)) {
1951 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
1952 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PERCENT)) {
1953 EMIT(binary_op, RT_BINARY_OP_MODULO);
1954 } else {
1955 // shouldn't happen
1956 assert(0);
1957 }
1958 }
1959}
1960
1961void compile_factor_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1962 compile_node(comp, pns->nodes[1]);
1963 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
1964 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
1965 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
1966 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
1967 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
1968 EMIT(unary_op, RT_UNARY_OP_INVERT);
1969 } else {
1970 // shouldn't happen
1971 assert(0);
1972 }
1973}
1974
1975void compile_trailer_paren_helper(compiler_t *comp, py_parse_node_struct_t *pns, bool is_method_call) {
1976 // function to call is on top of stack
1977
1978 int old_n_arg_keyword = comp->n_arg_keyword;
1979 bool old_have_star_arg = comp->have_star_arg;
1980 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
1981 comp->n_arg_keyword = 0;
1982 comp->have_star_arg = false;
1983 comp->have_dbl_star_arg = false;
1984
1985 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
1986
1987 // compute number of positional arguments
1988 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
1989 if (comp->have_star_arg) {
1990 n_positional -= 1;
1991 }
1992 if (comp->have_dbl_star_arg) {
1993 n_positional -= 1;
1994 }
1995
1996 if (is_method_call) {
1997 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
1998 } else {
1999 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2000 }
2001
2002 comp->n_arg_keyword = old_n_arg_keyword;
2003 comp->have_star_arg = old_have_star_arg;
2004 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2005}
2006
2007void compile_power_trailers(compiler_t *comp, py_parse_node_struct_t *pns) {
2008 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
2009 for (int i = 0; i < num_nodes; i++) {
2010 if (i + 1 < num_nodes && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
2011 // optimisation for method calls a.f(...), following PyPy
2012 py_parse_node_struct_t *pns_period = (py_parse_node_struct_t*)pns->nodes[i];
2013 py_parse_node_struct_t *pns_paren = (py_parse_node_struct_t*)pns->nodes[i + 1];
2014 EMIT(load_method, PY_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
2015 compile_trailer_paren_helper(comp, pns_paren, true);
2016 i += 1;
2017 } else {
2018 compile_node(comp, pns->nodes[i]);
2019 }
2020 }
2021}
2022
2023void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2024 compile_node(comp, pns->nodes[0]);
2025 EMIT(binary_op, RT_BINARY_OP_POWER);
2026}
2027
2028void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) {
2029 // a list of strings
2030 EMIT(load_const_verbatim_start);
2031 EMIT(load_const_verbatim_str, "'");
2032 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
2033 for (int i = 0; i < n; i++) {
2034 // TODO allow concatenation of either strings or bytes, but not mixed
2035 assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2036 assert(PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]) == PY_PARSE_NODE_STRING);
2037 const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2038 EMIT(load_const_verbatim_strn, str, strlen(str));
2039 }
2040 EMIT(load_const_verbatim_str, "'");
2041 EMIT(load_const_verbatim_end);
2042}
2043
2044// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
2045void compile_comprehension(compiler_t *comp, py_parse_node_struct_t *pns, scope_kind_t kind) {
2046 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2047 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2048 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
2049
2050 if (comp->pass == PASS_1) {
2051 // create a new scope for this comprehension
Damien6cdd3af2013-10-05 18:08:26 +01002052 scope_t *s = scope_new_and_link(comp, kind, (py_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002053 // store the comprehension scope so the compiling function (this one) can use it at each pass
2054 pns_comp_for->nodes[3] = (py_parse_node_t)s;
2055 }
2056
2057 // get the scope for this comprehension
2058 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2059
2060 // compile the comprehension
2061 close_over_variables_etc(comp, this_scope, 0, 0);
2062
2063 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2064 EMIT(get_iter);
2065 EMIT(call_function, 1, 0, false, false);
2066}
2067
2068void compile_atom_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
2069 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2070 // an empty tuple
Damien429d7192013-10-04 19:53:11 +01002071 c_tuple(comp, PY_PARSE_NODE_NULL, NULL);
2072 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2073 pns = (py_parse_node_struct_t*)pns->nodes[0];
2074 assert(!PY_PARSE_NODE_IS_NULL(pns->nodes[1]));
2075 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2076 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
2077 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
2078 // tuple of one item, with trailing comma
2079 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002080 c_tuple(comp, pns->nodes[0], NULL);
2081 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
2082 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002083 c_tuple(comp, pns->nodes[0], pns2);
2084 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
2085 // generator expression
2086 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2087 } else {
2088 // tuple with 2 items
2089 goto tuple_with_2_items;
2090 }
2091 } else {
2092 // tuple with 2 items
2093 tuple_with_2_items:
Damien429d7192013-10-04 19:53:11 +01002094 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
2095 }
2096 } else {
2097 // parenthesis around a single item, is just that item
2098 compile_node(comp, pns->nodes[0]);
2099 }
2100}
2101
2102void compile_atom_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
2103 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2104 // empty list
2105 EMIT(build_list, 0);
2106 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2107 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[0];
2108 if (PY_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2109 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns2->nodes[1];
2110 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
2111 // list of one item, with trailing comma
2112 assert(PY_PARSE_NODE_IS_NULL(pns3->nodes[0]));
2113 compile_node(comp, pns2->nodes[0]);
2114 EMIT(build_list, 1);
2115 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
2116 // list of many items
2117 compile_node(comp, pns2->nodes[0]);
2118 compile_generic_all_nodes(comp, pns3);
2119 EMIT(build_list, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns3));
2120 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
2121 // list comprehension
2122 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2123 } else {
2124 // list with 2 items
2125 goto list_with_2_items;
2126 }
2127 } else {
2128 // list with 2 items
2129 list_with_2_items:
2130 compile_node(comp, pns2->nodes[0]);
2131 compile_node(comp, pns2->nodes[1]);
2132 EMIT(build_list, 2);
2133 }
2134 } else {
2135 // list with 1 item
2136 compile_node(comp, pns->nodes[0]);
2137 EMIT(build_list, 1);
2138 }
2139}
2140
2141void compile_atom_brace(compiler_t *comp, py_parse_node_struct_t *pns) {
2142 py_parse_node_t pn = pns->nodes[0];
2143 if (PY_PARSE_NODE_IS_NULL(pn)) {
2144 // empty dict
2145 EMIT(build_map, 0);
2146 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
2147 pns = (py_parse_node_struct_t*)pn;
2148 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
2149 // dict with one element
2150 EMIT(build_map, 1);
2151 compile_node(comp, pn);
2152 EMIT(store_map);
2153 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2154 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2155 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
2156 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
2157 // dict/set with multiple elements
2158
2159 // get tail elements (2nd, 3rd, ...)
2160 py_parse_node_t *nodes;
2161 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2162
2163 // first element sets whether it's a dict or set
2164 bool is_dict;
2165 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
2166 // a dictionary
2167 EMIT(build_map, 1 + n);
2168 compile_node(comp, pns->nodes[0]);
2169 EMIT(store_map);
2170 is_dict = true;
2171 } else {
2172 // a set
2173 compile_node(comp, pns->nodes[0]); // 1st value of set
2174 is_dict = false;
2175 }
2176
2177 // process rest of elements
2178 for (int i = 0; i < n; i++) {
2179 py_parse_node_t pn = nodes[i];
2180 bool is_key_value = PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
2181 compile_node(comp, pn);
2182 if (is_dict) {
2183 if (!is_key_value) {
2184 printf("SyntaxError?: expecting key:value for dictionary");
2185 return;
2186 }
2187 EMIT(store_map);
2188 } else {
2189 if (is_key_value) {
2190 printf("SyntaxError?: expecting just a value for set");
2191 return;
2192 }
2193 }
2194 }
2195
2196 // if it's a set, build it
2197 if (!is_dict) {
2198 EMIT(build_set, 1 + n);
2199 }
2200 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
2201 // dict/set comprehension
2202 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
2203 // a dictionary comprehension
2204 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2205 } else {
2206 // a set comprehension
2207 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2208 }
2209 } else {
2210 // shouldn't happen
2211 assert(0);
2212 }
2213 } else {
2214 // set with one element
2215 goto set_with_one_element;
2216 }
2217 } else {
2218 // set with one element
2219 set_with_one_element:
2220 compile_node(comp, pn);
2221 EMIT(build_set, 1);
2222 }
2223}
2224
2225void compile_trailer_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
2226 compile_trailer_paren_helper(comp, pns, false);
2227}
2228
2229void compile_trailer_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
2230 // object who's index we want is on top of stack
2231 compile_node(comp, pns->nodes[0]); // the index
2232 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
2233}
2234
2235void compile_trailer_period(compiler_t *comp, py_parse_node_struct_t *pns) {
2236 // object who's attribute we want is on top of stack
2237 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
2238}
2239
2240void compile_subscript_3_helper(compiler_t *comp, py_parse_node_struct_t *pns) {
2241 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2242 py_parse_node_t pn = pns->nodes[0];
2243 if (PY_PARSE_NODE_IS_NULL(pn)) {
2244 // [?:]
2245 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2246 EMIT(build_slice, 2);
2247 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
2248 pns = (py_parse_node_struct_t*)pn;
2249 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2250 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2251 pn = pns->nodes[0];
2252 if (PY_PARSE_NODE_IS_NULL(pn)) {
2253 // [?::]
2254 EMIT(build_slice, 2);
2255 } else {
2256 // [?::x]
2257 compile_node(comp, pn);
2258 EMIT(build_slice, 3);
2259 }
2260 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
2261 compile_node(comp, pns->nodes[0]);
2262 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2263 pns = (py_parse_node_struct_t*)pns->nodes[1];
2264 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2265 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2266 // [?:x:]
2267 EMIT(build_slice, 2);
2268 } else {
2269 // [?:x:x]
2270 compile_node(comp, pns->nodes[0]);
2271 EMIT(build_slice, 3);
2272 }
2273 } else {
2274 // [?:x]
2275 compile_node(comp, pn);
2276 EMIT(build_slice, 2);
2277 }
2278 } else {
2279 // [?:x]
2280 compile_node(comp, pn);
2281 EMIT(build_slice, 2);
2282 }
2283}
2284
2285void compile_subscript_2(compiler_t *comp, py_parse_node_struct_t *pns) {
2286 compile_node(comp, pns->nodes[0]); // start of slice
2287 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2288 compile_subscript_3_helper(comp, (py_parse_node_struct_t*)pns->nodes[1]);
2289}
2290
2291void compile_subscript_3(compiler_t *comp, py_parse_node_struct_t *pns) {
2292 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2293 compile_subscript_3_helper(comp, pns);
2294}
2295
2296void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns) {
2297 // if this is called then we are compiling a dict key:value pair
2298 compile_node(comp, pns->nodes[1]); // value
2299 compile_node(comp, pns->nodes[0]); // key
2300}
2301
2302void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002303 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002304 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002305 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002306}
2307
2308void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2309 if (comp->have_star_arg) {
2310 printf("SyntaxError?: can't have multiple *x\n");
2311 return;
2312 }
2313 comp->have_star_arg = true;
2314 compile_node(comp, pns->nodes[0]);
2315}
2316
2317void compile_arglist_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2318 if (comp->have_dbl_star_arg) {
2319 printf("SyntaxError?: can't have multiple **x\n");
2320 return;
2321 }
2322 comp->have_dbl_star_arg = true;
2323 compile_node(comp, pns->nodes[0]);
2324}
2325
2326void compile_argument(compiler_t *comp, py_parse_node_struct_t *pns) {
2327 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2328 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
2329 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2330 if (!PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2331 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2332 return;
2333 }
2334 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
2335 compile_node(comp, pns2->nodes[0]);
2336 comp->n_arg_keyword += 1;
2337 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
2338 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2339 } else {
2340 // shouldn't happen
2341 assert(0);
2342 }
2343}
2344
2345void compile_yield_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
2346 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2347 printf("SyntaxError: 'yield' outside function\n");
2348 return;
2349 }
2350 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2351 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2352 EMIT(yield_value);
2353 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2354 pns = (py_parse_node_struct_t*)pns->nodes[0];
2355 compile_node(comp, pns->nodes[0]);
2356 EMIT(get_iter);
2357 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2358 EMIT(yield_from);
2359 } else {
2360 compile_node(comp, pns->nodes[0]);
2361 EMIT(yield_value);
2362 }
2363}
2364
2365typedef void (*compile_function_t)(compiler_t*, py_parse_node_struct_t*);
2366static compile_function_t compile_function[] = {
2367 NULL,
2368#define nc NULL
2369#define c(f) compile_##f
2370#define DEF_RULE(rule, comp, kind, arg...) comp,
2371#include "grammar.h"
2372#undef nc
2373#undef c
2374#undef DEF_RULE
2375};
2376
2377void compile_node(compiler_t *comp, py_parse_node_t pn) {
2378 if (PY_PARSE_NODE_IS_NULL(pn)) {
2379 // pass
2380 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
2381 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
2382 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
Damien4b03e772013-10-05 14:17:09 +01002383 case PY_PARSE_NODE_ID: EMIT(load_id, arg); break;
Damien429d7192013-10-04 19:53:11 +01002384 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2385 case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2386 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2387 case PY_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2388 case PY_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
Damien91d387d2013-10-09 15:09:52 +01002389 case PY_PARSE_NODE_TOKEN:
2390 if (arg == PY_TOKEN_NEWLINE) {
2391 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002392 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002393 // do nothing
2394 } else {
2395 EMIT(load_const_tok, arg);
2396 }
2397 break;
Damien429d7192013-10-04 19:53:11 +01002398 default: assert(0);
2399 }
2400 } else {
2401 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2402 compile_function_t f = compile_function[PY_PARSE_NODE_STRUCT_KIND(pns)];
2403 if (f == NULL) {
2404 printf("node %u cannot be compiled\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
Damien5ac1b2e2013-10-18 19:58:12 +01002405 py_parse_node_show(pn, 0);
Damien429d7192013-10-04 19:53:11 +01002406 assert(0);
2407 } else {
2408 f(comp, pns);
2409 }
2410 }
2411}
2412
2413void compile_scope_func_lambda_param(compiler_t *comp, py_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
2414 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002415 qstr param_name = 0;
2416 py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL;
Damienb14de212013-10-06 00:28:28 +01002417 if (PY_PARSE_NODE_IS_ID(pn)) {
2418 param_name = PY_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002419 if (comp->have_bare_star) {
2420 // comes after a bare star, so doesn't count as a parameter
2421 } else {
2422 comp->scope_cur->num_params += 1;
2423 }
Damienb14de212013-10-06 00:28:28 +01002424 } else {
2425 assert(PY_PARSE_NODE_IS_STRUCT(pn));
2426 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2427 if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
Damien429d7192013-10-04 19:53:11 +01002428 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002429 //int node_index = 1; unused
2430 if (allow_annotations) {
2431 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2432 // this parameter has an annotation
2433 pn_annotation = pns->nodes[1];
2434 }
2435 //node_index = 2; unused
2436 }
2437 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
2438 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
2439 // this parameter has a default value
2440 if (comp->have_bare_star) {
2441 comp->scope_cur->num_dict_params += 1;
2442 } else {
2443 comp->scope_cur->num_default_params += 1;
2444 }
2445 }
2446 */
2447 if (comp->have_bare_star) {
2448 // comes after a bare star, so doesn't count as a parameter
2449 } else {
2450 comp->scope_cur->num_params += 1;
2451 }
2452 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2453 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2454 // bare star
2455 // TODO see http://www.python.org/dev/peps/pep-3102/
2456 comp->have_bare_star = true;
2457 //assert(comp->scope_cur->num_dict_params == 0);
2458 } else if (PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2459 // named star
2460 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2461 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2462 } else if (allow_annotations && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2463 // named star with annotation
2464 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2465 pns = (py_parse_node_struct_t*)pns->nodes[0];
2466 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2467 pn_annotation = pns->nodes[1];
2468 } else {
2469 // shouldn't happen
2470 assert(0);
2471 }
2472 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
Damien429d7192013-10-04 19:53:11 +01002473 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002474 if (allow_annotations && !PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2475 // this parameter has an annotation
2476 pn_annotation = pns->nodes[1];
2477 }
2478 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002479 } else {
Damienb14de212013-10-06 00:28:28 +01002480 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002481 assert(0);
2482 }
Damien429d7192013-10-04 19:53:11 +01002483 }
2484
2485 if (param_name != 0) {
2486 if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) {
2487 // TODO this parameter has an annotation
2488 }
2489 bool added;
2490 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2491 if (!added) {
2492 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2493 return;
2494 }
2495 id_info->param = true;
2496 id_info->kind = ID_INFO_KIND_LOCAL;
2497 }
2498}
2499
2500void compile_scope_func_param(compiler_t *comp, py_parse_node_t pn) {
2501 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2502}
2503
2504void compile_scope_lambda_param(compiler_t *comp, py_parse_node_t pn) {
2505 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2506}
2507
2508void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse_node_t pn_inner_expr, int l_top, int for_depth) {
2509 tail_recursion:
2510 if (PY_PARSE_NODE_IS_NULL(pn_iter)) {
2511 // no more nested if/for; compile inner expression
2512 compile_node(comp, pn_inner_expr);
2513 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2514 EMIT(list_append, for_depth + 2);
2515 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2516 EMIT(map_add, for_depth + 2);
2517 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2518 EMIT(set_add, for_depth + 2);
2519 } else {
2520 EMIT(yield_value);
2521 EMIT(pop_top);
2522 }
2523 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
2524 // if condition
2525 py_parse_node_struct_t *pns_comp_if = (py_parse_node_struct_t*)pn_iter;
2526 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2527 pn_iter = pns_comp_if->nodes[1];
2528 goto tail_recursion;
2529 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
2530 // for loop
2531 py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter;
2532 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002533 int l_end2 = comp_next_label(comp);
2534 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002535 EMIT(get_iter);
2536 EMIT(label_assign, l_top2);
2537 EMIT(for_iter, l_end2);
2538 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2539 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2540 EMIT(jump, l_top2);
2541 EMIT(label_assign, l_end2);
2542 EMIT(for_iter_end);
2543 } else {
2544 // shouldn't happen
2545 assert(0);
2546 }
2547}
2548
2549void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
2550 // see http://www.python.org/dev/peps/pep-0257/
2551
2552 // look for the first statement
2553 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2554 // fall through
2555 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
2556 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2557 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
2558 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2559 } else {
2560 return;
2561 }
2562
2563 // check the first statement for a doc string
2564 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2565 py_parse_node_struct_t* pns = (py_parse_node_struct_t*)pn;
2566 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2567 int kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2568 if (kind == PY_PARSE_NODE_STRING) {
2569 compile_node(comp, pns->nodes[0]); // a doc string
2570 // store doc string
Damien4b03e772013-10-05 14:17:09 +01002571 EMIT(store_id, comp->qstr___doc__);
Damien429d7192013-10-04 19:53:11 +01002572 }
2573 }
2574 }
2575}
2576
2577void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2578 comp->pass = pass;
2579 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002580 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002581 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002582
2583 if (comp->pass == PASS_1) {
2584 scope->stack_size = 0;
2585 }
2586
Damien5ac1b2e2013-10-18 19:58:12 +01002587#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002588 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002589 scope_print_info(scope);
2590 }
Damien5ac1b2e2013-10-18 19:58:12 +01002591#endif
Damien429d7192013-10-04 19:53:11 +01002592
2593 // compile
2594 if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002595 if (!comp->is_repl) {
2596 check_for_doc_string(comp, scope->pn);
2597 }
Damien429d7192013-10-04 19:53:11 +01002598 compile_node(comp, scope->pn);
2599 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2600 EMIT(return_value);
2601 } else if (scope->kind == SCOPE_FUNCTION) {
2602 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2603 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2604 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
2605
2606 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002607 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002608 if (comp->pass == PASS_1) {
2609 comp->have_bare_star = false;
2610 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2611 }
2612
Damien826005c2013-10-05 23:17:28 +01002613 assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002614
2615 compile_node(comp, pns->nodes[3]); // 3 is function body
2616 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002617 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01002618 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2619 EMIT(return_value);
2620 }
2621 } else if (scope->kind == SCOPE_LAMBDA) {
2622 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2623 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2624 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
2625
2626 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002627 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002628 if (comp->pass == PASS_1) {
2629 comp->have_bare_star = false;
2630 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2631 }
2632
2633 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2634 EMIT(return_value);
2635 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2636 // a bit of a hack at the moment
2637
2638 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2639 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2640 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2641 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2642 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
2643
Damien6cdd3af2013-10-05 18:08:26 +01002644 qstr qstr_arg = qstr_from_str_static(".0");
Damien429d7192013-10-04 19:53:11 +01002645 if (comp->pass == PASS_1) {
2646 bool added;
2647 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2648 assert(added);
2649 id_info->kind = ID_INFO_KIND_LOCAL;
2650 scope->num_params = 1;
2651 }
2652
2653 if (scope->kind == SCOPE_LIST_COMP) {
2654 EMIT(build_list, 0);
2655 } else if (scope->kind == SCOPE_DICT_COMP) {
2656 EMIT(build_map, 0);
2657 } else if (scope->kind == SCOPE_SET_COMP) {
2658 EMIT(build_set, 0);
2659 }
2660
Damienb05d7072013-10-05 13:37:10 +01002661 int l_end = comp_next_label(comp);
2662 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002663 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002664 EMIT(label_assign, l_top);
2665 EMIT(for_iter, l_end);
2666 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2667 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2668 EMIT(jump, l_top);
2669 EMIT(label_assign, l_end);
2670 EMIT(for_iter_end);
2671
2672 if (scope->kind == SCOPE_GEN_EXPR) {
2673 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2674 }
2675 EMIT(return_value);
2676 } else {
2677 assert(scope->kind == SCOPE_CLASS);
2678 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2679 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2680 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
2681
2682 if (comp->pass == PASS_1) {
2683 bool added;
2684 id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added);
2685 assert(added);
2686 id_info->kind = ID_INFO_KIND_LOCAL;
2687 id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added);
2688 assert(added);
2689 id_info->kind = ID_INFO_KIND_LOCAL;
2690 id_info->param = true;
2691 scope->num_params = 1; // __locals__ is the parameter
2692 }
2693
Damien4b03e772013-10-05 14:17:09 +01002694 EMIT(load_id, comp->qstr___locals__);
Damien429d7192013-10-04 19:53:11 +01002695 EMIT(store_locals);
Damien4b03e772013-10-05 14:17:09 +01002696 EMIT(load_id, comp->qstr___name__);
2697 EMIT(store_id, comp->qstr___module__);
Damien429d7192013-10-04 19:53:11 +01002698 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien4b03e772013-10-05 14:17:09 +01002699 EMIT(store_id, comp->qstr___qualname__);
Damien429d7192013-10-04 19:53:11 +01002700
2701 check_for_doc_string(comp, pns->nodes[2]);
2702 compile_node(comp, pns->nodes[2]); // 2 is class body
2703
2704 id_info_t *id = scope_find(scope, comp->qstr___class__);
2705 assert(id != NULL);
2706 if (id->kind == ID_INFO_KIND_LOCAL) {
2707 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2708 } else {
Damien27fb45e2013-10-20 15:07:49 +01002709 EMIT(load_closure, comp->qstr___class__, 0); // XXX check this is the correct local num
Damien429d7192013-10-04 19:53:11 +01002710 }
2711 EMIT(return_value);
2712 }
2713
Damien415eb6f2013-10-05 12:19:06 +01002714 EMIT(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002715
Damien826005c2013-10-05 23:17:28 +01002716}
2717
2718void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2719 comp->pass = pass;
2720 comp->scope_cur = scope;
2721 comp->next_label = 1;
2722
2723 if (scope->kind != SCOPE_FUNCTION) {
2724 printf("Error: inline assembler must be a function\n");
2725 return;
2726 }
2727
Damiena2f2f7d2013-10-06 00:14:13 +01002728 if (comp->pass > PASS_1) {
2729 EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur);
2730 }
2731
Damien826005c2013-10-05 23:17:28 +01002732 // get the function definition parse node
2733 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2734 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2735 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
2736
Damiena2f2f7d2013-10-06 00:14:13 +01002737 //qstr f_id = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002738
Damiena2f2f7d2013-10-06 00:14:13 +01002739 // parameters are in pns->nodes[1]
2740 if (comp->pass == PASS_2) {
2741 py_parse_node_t *pn_params;
2742 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
2743 scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params);
2744 }
2745
Damien826005c2013-10-05 23:17:28 +01002746 assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
2747
2748 py_parse_node_t pn_body = pns->nodes[3]; // body
2749 py_parse_node_t *nodes;
2750 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2751
Damien826005c2013-10-05 23:17:28 +01002752 if (comp->pass == PASS_3) {
2753 //printf("----\n");
2754 scope_print_info(scope);
2755 }
2756
2757 for (int i = 0; i < num; i++) {
2758 assert(PY_PARSE_NODE_IS_STRUCT(nodes[i]));
2759 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)nodes[i];
2760 assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2761 assert(PY_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2762 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2763 pns2 = (py_parse_node_struct_t*)pns2->nodes[0];
2764 assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2765 assert(PY_PARSE_NODE_IS_ID(pns2->nodes[0]));
2766 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2767 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2768 qstr op = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2769 pns2 = (py_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2770 py_parse_node_t *pn_arg;
2771 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2772
2773 // emit instructions
2774 if (strcmp(qstr_str(op), "label") == 0) {
2775 if (!(n_args == 1 && PY_PARSE_NODE_IS_ID(pn_arg[0]))) {
2776 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2777 return;
2778 }
2779 int lab = comp_next_label(comp);
2780 if (pass > PASS_1) {
2781 EMIT_INLINE_ASM(label, lab, PY_PARSE_NODE_LEAF_ARG(pn_arg[0]));
2782 }
2783 } else {
2784 if (pass > PASS_1) {
2785 EMIT_INLINE_ASM(op, op, n_args, pn_arg);
2786 }
2787 }
2788 }
2789
2790 if (comp->pass > PASS_1) {
2791 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002792 }
Damien429d7192013-10-04 19:53:11 +01002793}
2794
2795void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2796 // in functions, turn implicit globals into explicit globals
2797 // compute num_locals, and the index of each local
2798 scope->num_locals = 0;
2799 for (int i = 0; i < scope->id_info_len; i++) {
2800 id_info_t *id = &scope->id_info[i];
2801 if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) {
2802 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2803 continue;
2804 }
2805 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2806 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2807 }
2808 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2809 id->local_num = scope->num_locals;
2810 scope->num_locals += 1;
2811 }
2812 }
2813
Damien27fb45e2013-10-20 15:07:49 +01002814 // TODO compute the index of free and cell vars (freevars[idx] in CPython)
2815
Damien429d7192013-10-04 19:53:11 +01002816 // compute flags
2817 //scope->flags = 0; since we set some things in parameters
2818 if (scope->kind != SCOPE_MODULE) {
2819 scope->flags |= SCOPE_FLAG_NEWLOCALS;
2820 }
2821 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) {
2822 assert(scope->parent != NULL);
2823 scope->flags |= SCOPE_FLAG_OPTIMISED;
2824
2825 // TODO possibly other ways it can be nested
2826 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
2827 scope->flags |= SCOPE_FLAG_NESTED;
2828 }
2829 }
2830 int num_free = 0;
2831 for (int i = 0; i < scope->id_info_len; i++) {
2832 id_info_t *id = &scope->id_info[i];
2833 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2834 num_free += 1;
2835 }
2836 }
2837 if (num_free == 0) {
2838 scope->flags |= SCOPE_FLAG_NOFREE;
2839 }
2840}
2841
Damien5ac1b2e2013-10-18 19:58:12 +01002842bool py_compile(py_parse_node_t pn, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01002843 compiler_t *comp = m_new(compiler_t, 1);
2844
Damien6cdd3af2013-10-05 18:08:26 +01002845 comp->qstr___class__ = qstr_from_str_static("__class__");
2846 comp->qstr___locals__ = qstr_from_str_static("__locals__");
2847 comp->qstr___name__ = qstr_from_str_static("__name__");
2848 comp->qstr___module__ = qstr_from_str_static("__module__");
2849 comp->qstr___qualname__ = qstr_from_str_static("__qualname__");
2850 comp->qstr___doc__ = qstr_from_str_static("__doc__");
2851 comp->qstr_assertion_error = qstr_from_str_static("AssertionError");
2852 comp->qstr_micropython = qstr_from_str_static("micropython");
Damien5ac1b2e2013-10-18 19:58:12 +01002853 comp->qstr_byte_code = qstr_from_str_static("byte_code");
Damien6cdd3af2013-10-05 18:08:26 +01002854 comp->qstr_native = qstr_from_str_static("native");
Damien7af3d192013-10-07 00:02:49 +01002855 comp->qstr_viper = qstr_from_str_static("viper");
Damien5bfb7592013-10-05 18:41:24 +01002856 comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb");
Damiend7933892013-11-28 19:12:18 +00002857 comp->qstr_range = qstr_from_str_static("range");
Damien429d7192013-10-04 19:53:11 +01002858
Damien5ac1b2e2013-10-18 19:58:12 +01002859 comp->is_repl = is_repl;
2860 comp->had_error = false;
2861
Damien429d7192013-10-04 19:53:11 +01002862 comp->break_label = 0;
2863 comp->continue_label = 0;
2864 comp->except_nest_level = 0;
2865 comp->scope_head = NULL;
2866 comp->scope_cur = NULL;
2867
Damien826005c2013-10-05 23:17:28 +01002868 // optimise constants
Damien429d7192013-10-04 19:53:11 +01002869 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01002870
2871 // set the outer scope
Damien6cdd3af2013-10-05 18:08:26 +01002872 scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01002873
Damien826005c2013-10-05 23:17:28 +01002874 // compile pass 1
2875 comp->emit = emit_pass1_new(comp->qstr___class__);
2876 comp->emit_method_table = &emit_pass1_method_table;
2877 comp->emit_inline_asm = NULL;
2878 comp->emit_inline_asm_method_table = NULL;
2879 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01002880 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01002881 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01002882#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01002883 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01002884 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01002885#endif
Damien826005c2013-10-05 23:17:28 +01002886 } else {
2887 compile_scope(comp, s, PASS_1);
2888 }
2889
2890 // update maximim number of labels needed
2891 if (comp->next_label > max_num_labels) {
2892 max_num_labels = comp->next_label;
2893 }
Damien429d7192013-10-04 19:53:11 +01002894 }
2895
Damien826005c2013-10-05 23:17:28 +01002896 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01002897 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01002898 compile_scope_compute_things(comp, s);
2899 }
2900
Damien826005c2013-10-05 23:17:28 +01002901 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01002902 emit_pass1_free(comp->emit);
2903
Damien826005c2013-10-05 23:17:28 +01002904 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01002905#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01002906 emit_t *emit_bc = NULL;
Damiendc833822013-10-06 01:01:01 +01002907 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01002908#endif
Damien3ef4abb2013-10-12 16:53:13 +01002909#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01002910 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01002911#endif
Damien5ac1b2e2013-10-18 19:58:12 +01002912 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01002913 if (false) {
2914 // dummy
2915
Damien3ef4abb2013-10-12 16:53:13 +01002916#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01002917 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
2918 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01002919 if (emit_inline_thumb == NULL) {
2920 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
2921 }
2922 comp->emit = NULL;
2923 comp->emit_method_table = NULL;
2924 comp->emit_inline_asm = emit_inline_thumb;
2925 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
2926 compile_scope_inline_asm(comp, s, PASS_2);
2927 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01002928#endif
2929
Damien826005c2013-10-05 23:17:28 +01002930 } else {
Damienc025ebb2013-10-12 14:30:21 +01002931
2932 // choose the emit type
2933
Damien3ef4abb2013-10-12 16:53:13 +01002934#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01002935 comp->emit = emit_cpython_new(max_num_labels);
2936 comp->emit_method_table = &emit_cpython_method_table;
2937#else
Damien826005c2013-10-05 23:17:28 +01002938 switch (s->emit_options) {
2939 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01002940 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01002941#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01002942 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01002943 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01002944 }
Damien13ed3a62013-10-08 09:05:10 +01002945 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01002946#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01002947 if (emit_native == NULL) {
2948 emit_native = emit_native_thumb_new(max_num_labels);
2949 }
2950 comp->emit_method_table = &emit_native_thumb_method_table;
2951#endif
2952 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01002953 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01002954 break;
2955
Damien826005c2013-10-05 23:17:28 +01002956 default:
2957 if (emit_bc == NULL) {
2958 emit_bc = emit_bc_new(max_num_labels);
2959 }
2960 comp->emit = emit_bc;
2961 comp->emit_method_table = &emit_bc_method_table;
2962 break;
2963 }
Damienc025ebb2013-10-12 14:30:21 +01002964#endif
2965
2966 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01002967 compile_scope(comp, s, PASS_2);
2968 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01002969 }
Damien429d7192013-10-04 19:53:11 +01002970 }
2971
2972 m_free(comp);
Damien5ac1b2e2013-10-18 19:58:12 +01002973
2974 return !comp->had_error;
Damien429d7192013-10-04 19:53:11 +01002975}