blob: 9e90f82dd90285057f5e9a12ce1b24a53fb89000 [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"
9#include "lexer.h"
10#include "machine.h"
11#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
Damien415eb6f2013-10-05 12:19:06 +010018// TODO add #define to enable/disable CPython compatibility
Damien429d7192013-10-04 19:53:11 +010019
20typedef enum {
21 PN_none = 0,
22#define DEF_RULE(rule, comp, kind, arg...) PN_##rule,
23#include "grammar.h"
24#undef DEF_RULE
25 PN_maximum_number_of,
26} pn_kind_t;
27
Damien415eb6f2013-10-05 12:19:06 +010028#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
Damien826005c2013-10-05 23:17:28 +010029#define EMIT_INLINE_ASM(fun, arg...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, ##arg))
Damien429d7192013-10-04 19:53:11 +010030
Damien6cdd3af2013-10-05 18:08:26 +010031#define EMIT_OPT_NONE (0)
32#define EMIT_OPT_BYTE_CODE (1)
33#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010034#define EMIT_OPT_VIPER (3)
35#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010036
Damien429d7192013-10-04 19:53:11 +010037typedef struct _compiler_t {
38 qstr qstr___class__;
39 qstr qstr___locals__;
40 qstr qstr___name__;
41 qstr qstr___module__;
42 qstr qstr___qualname__;
43 qstr qstr___doc__;
44 qstr qstr_assertion_error;
Damien6cdd3af2013-10-05 18:08:26 +010045 qstr qstr_micropython;
46 qstr qstr_native;
Damien7af3d192013-10-07 00:02:49 +010047 qstr qstr_viper;
Damien5bfb7592013-10-05 18:41:24 +010048 qstr qstr_asm_thumb;
Damien429d7192013-10-04 19:53:11 +010049
50 pass_kind_t pass;
51
Damienb05d7072013-10-05 13:37:10 +010052 int next_label;
Damienb05d7072013-10-05 13:37:10 +010053
Damien429d7192013-10-04 19:53:11 +010054 int break_label;
55 int continue_label;
56 int except_nest_level;
57
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
66 scope_t *scope_head;
67 scope_t *scope_cur;
68
Damien6cdd3af2013-10-05 18:08:26 +010069 emit_t *emit; // current emitter
70 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010071
72 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
73 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 +010074} compiler_t;
75
76py_parse_node_t fold_constants(py_parse_node_t pn) {
77 if (PY_PARSE_NODE_IS_STRUCT(pn)) {
78 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
79 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
80
81 // fold arguments first
82 for (int i = 0; i < n; i++) {
83 pns->nodes[i] = fold_constants(pns->nodes[i]);
84 }
85
86 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
87 case PN_shift_expr:
88 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
89 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
90 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
91 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_LESS)) {
92 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 << arg1); // XXX can overflow; enabled only to compare with CPython
93 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_MORE)) {
94 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 >> arg1);
95 } else {
96 // shouldn't happen
97 assert(0);
98 }
99 }
100 break;
101
102 case PN_arith_expr:
103 // XXX can overflow; enabled only to compare with CPython
104 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
105 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
106 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
107 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PLUS)) {
108 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 + arg1);
109 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_MINUS)) {
110 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1);
111 } else {
112 // shouldn't happen
113 assert(0);
114 }
115 }
116 break;
117
118 case PN_term:
119 // XXX can overflow; enabled only to compare with CPython
120 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
121 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
122 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
123 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
124 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 * arg1);
125 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_SLASH)) {
126 ; // pass
127 //} else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_)) {
128 //pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1);
129 } else {
130 // shouldn't happen
131 assert(0);
132 }
133 }
134 break;
135
136 case PN_factor_2:
137 if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
138 machine_int_t arg = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
139 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
140 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg);
141 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
142 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, -arg);
143 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
144 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ~arg);
145 } else {
146 // shouldn't happen
147 assert(0);
148 }
149 }
150 break;
151
152 case PN_power:
153 // XXX can overflow; enabled only to compare with CPython
154 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])) {
155 py_parse_node_struct_t* pns2 = (py_parse_node_struct_t*)pns->nodes[2];
156 if (PY_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
157 int power = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
158 if (power >= 0) {
159 int ans = 1;
160 int base = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
161 for (; power > 0; power--) {
162 ans *= base;
163 }
164 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ans);
165 }
166 }
167 }
168 break;
169 }
170 }
171
172 return pn;
173}
174
175void compile_node(compiler_t *comp, py_parse_node_t pn);
176
Damienb05d7072013-10-05 13:37:10 +0100177static int comp_next_label(compiler_t *comp) {
178 return comp->next_label++;
179}
180
Damien6cdd3af2013-10-05 18:08:26 +0100181static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn, uint emit_options) {
182 scope_t *scope = scope_new(kind, pn, rt_get_new_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100183 scope->parent = comp->scope_cur;
184 scope->next = NULL;
185 if (comp->scope_head == NULL) {
186 comp->scope_head = scope;
187 } else {
188 scope_t *s = comp->scope_head;
189 while (s->next != NULL) {
190 s = s->next;
191 }
192 s->next = scope;
193 }
194 return scope;
195}
196
Damienb05d7072013-10-05 13:37:10 +0100197static int list_len(py_parse_node_t pn, int pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100198 if (PY_PARSE_NODE_IS_NULL(pn)) {
199 return 0;
200 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
201 return 1;
202 } else {
203 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
204 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
205 return 1;
206 } else {
207 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
208 }
209 }
210}
211
Damienb05d7072013-10-05 13:37:10 +0100212static 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 +0100213 if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) {
214 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
215 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
216 for (int i = 0; i < num_nodes; i++) {
217 f(comp, pns->nodes[i]);
218 }
219 } else if (!PY_PARSE_NODE_IS_NULL(pn)) {
220 f(comp, pn);
221 }
222}
223
Damienb05d7072013-10-05 13:37:10 +0100224static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
Damien429d7192013-10-04 19:53:11 +0100225 if (PY_PARSE_NODE_IS_NULL(*pn)) {
226 *nodes = NULL;
227 return 0;
228 } else if (PY_PARSE_NODE_IS_LEAF(*pn)) {
229 *nodes = pn;
230 return 1;
231 } else {
232 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)(*pn);
233 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
234 *nodes = pn;
235 return 1;
236 } else {
237 *nodes = pns->nodes;
238 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
239 }
240 }
241}
242
243void compile_do_nothing(compiler_t *comp, py_parse_node_struct_t *pns) {
244}
245
246void compile_generic_all_nodes(compiler_t *comp, py_parse_node_struct_t *pns) {
247 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
248 for (int i = 0; i < num_nodes; i++) {
249 compile_node(comp, pns->nodes[i]);
250 }
251}
252
253bool c_tuple_is_const(py_parse_node_t pn) {
254 if (!PY_PARSE_NODE_IS_LEAF(pn)) {
255 return false;
256 }
257 if (PY_PARSE_NODE_IS_ID(pn)) {
258 return false;
259 }
260 return true;
261}
262
263void c_tuple_emit_const(compiler_t *comp, py_parse_node_t pn) {
264 assert(PY_PARSE_NODE_IS_LEAF(pn));
265 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
266 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
267 case PY_PARSE_NODE_ID: assert(0);
268 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_verbatim_int, arg); break;
269 case PY_PARSE_NODE_INTEGER: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
270 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
271 case PY_PARSE_NODE_STRING: EMIT(load_const_verbatim_quoted_str, arg, false); break;
272 case PY_PARSE_NODE_BYTES: EMIT(load_const_verbatim_quoted_str, arg, true); break;
273 case PY_PARSE_NODE_TOKEN:
274 switch (arg) {
275 case PY_TOKEN_KW_FALSE: EMIT(load_const_verbatim_str, "False"); break;
276 case PY_TOKEN_KW_NONE: EMIT(load_const_verbatim_str, "None"); break;
277 case PY_TOKEN_KW_TRUE: EMIT(load_const_verbatim_str, "True"); break;
278 default: assert(0);
279 }
280 break;
281 default: assert(0);
282 }
283}
284
285// funnelling all tuple creations through this function and all this constant stuff is purely to agree with CPython
286void c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) {
287 int n = 0;
288 if (pns_list != NULL) {
289 n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
290 }
291 int total = n;
292 bool is_const = true;
293 if (!PY_PARSE_NODE_IS_NULL(pn)) {
294 total += 1;
295 if (!c_tuple_is_const(pn)) {
296 is_const = false;
297 }
298 }
299 for (int i = 0; i < n; i++) {
300 if (!c_tuple_is_const(pns_list->nodes[i])) {
301 is_const = false;
302 break;
303 }
304 }
305 if (total > 0 && is_const) {
306 bool need_comma = false;
307 EMIT(load_const_verbatim_start);
308 EMIT(load_const_verbatim_str, "(");
309 if (!PY_PARSE_NODE_IS_NULL(pn)) {
310 c_tuple_emit_const(comp, pn);
311 need_comma = true;
312 }
313 for (int i = 0; i < n; i++) {
314 if (need_comma) {
315 EMIT(load_const_verbatim_str, ", ");
316 }
317 c_tuple_emit_const(comp, pns_list->nodes[i]);
318 need_comma = true;
319 }
320 if (total == 1) {
321 EMIT(load_const_verbatim_str, ",)");
322 } else {
323 EMIT(load_const_verbatim_str, ")");
324 }
325 EMIT(load_const_verbatim_end);
326 } else {
327 if (!PY_PARSE_NODE_IS_NULL(pn)) {
328 compile_node(comp, pn);
329 }
330 for (int i = 0; i < n; i++) {
331 compile_node(comp, pns_list->nodes[i]);
332 }
333 EMIT(build_tuple, total);
334 }
335}
336
337void compile_generic_tuple(compiler_t *comp, py_parse_node_struct_t *pns) {
338 // a simple tuple expression
339 /*
340 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
341 for (int i = 0; i < n; i++) {
342 compile_node(comp, pns->nodes[i]);
343 }
344 EMIT(build_tuple, n);
345 */
346 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
347}
348
349bool node_is_const_false(py_parse_node_t pn) {
350 return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_FALSE);
351 // untested: || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1);
352}
353
354bool node_is_const_true(py_parse_node_t pn) {
355 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);
356}
357
358// having c_if_cond_2 and the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
359void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, bool is_nested) {
360 if (node_is_const_false(pn)) {
361 if (jump_if == false) {
362 EMIT(jump, label);
363 }
364 return;
365 } else if (node_is_const_true(pn)) {
366 if (jump_if == true) {
367 EMIT(jump, label);
368 }
369 return;
370 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
371 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
372 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
373 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
374 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100375 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100376 for (int i = 0; i < n - 1; i++) {
377 c_if_cond_2(comp, pns->nodes[i], true, label2, true);
378 }
379 c_if_cond_2(comp, pns->nodes[n - 1], false, label, true);
380 EMIT(label_assign, label2);
381 } else {
382 for (int i = 0; i < n; i++) {
383 c_if_cond_2(comp, pns->nodes[i], true, label, true);
384 }
385 }
386 return;
387 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
388 if (jump_if == false) {
389 for (int i = 0; i < n; i++) {
390 c_if_cond_2(comp, pns->nodes[i], false, label, true);
391 }
392 } else {
Damienb05d7072013-10-05 13:37:10 +0100393 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100394 for (int i = 0; i < n - 1; i++) {
395 c_if_cond_2(comp, pns->nodes[i], false, label2, true);
396 }
397 c_if_cond_2(comp, pns->nodes[n - 1], true, label, true);
398 EMIT(label_assign, label2);
399 }
400 return;
401 } else if (!is_nested && PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
402 c_if_cond_2(comp, pns->nodes[0], !jump_if, label, true);
403 return;
404 }
405 }
406
407 // nothing special, fall back to default compiling for node and jump
408 compile_node(comp, pn);
409 if (jump_if == false) {
410 EMIT(pop_jump_if_false, label);
411 } else {
412 EMIT(pop_jump_if_true, label);
413 }
414}
415
416void c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label) {
417 c_if_cond_2(comp, pn, jump_if, label, false);
418}
419
420typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
421void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t kind);
422
423void c_assign_power(compiler_t *comp, py_parse_node_struct_t *pns, assign_kind_t assign_kind) {
424 if (assign_kind != ASSIGN_AUG_STORE) {
425 compile_node(comp, pns->nodes[0]);
426 }
427
428 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
429 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
430 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
431 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
432 if (assign_kind != ASSIGN_AUG_STORE) {
433 for (int i = 0; i < n - 1; i++) {
434 compile_node(comp, pns1->nodes[i]);
435 }
436 }
437 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
438 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
439 }
440 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
441 printf("SyntaxError: can't assign to function call\n");
442 return;
443 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
444 if (assign_kind == ASSIGN_AUG_STORE) {
445 EMIT(rot_three);
446 EMIT(store_subscr);
447 } else {
448 compile_node(comp, pns1->nodes[0]);
449 if (assign_kind == ASSIGN_AUG_LOAD) {
450 EMIT(dup_top_two);
451 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
452 } else {
453 EMIT(store_subscr);
454 }
455 }
456 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
457 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
458 if (assign_kind == ASSIGN_AUG_LOAD) {
459 EMIT(dup_top);
460 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
461 } else {
462 if (assign_kind == ASSIGN_AUG_STORE) {
463 EMIT(rot_two);
464 }
465 EMIT(store_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
466 }
467 } else {
468 // shouldn't happen
469 assert(0);
470 }
471 } else {
472 // shouldn't happen
473 assert(0);
474 }
475
476 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
477 // SyntaxError, cannot assign
478 assert(0);
479 }
480}
481
482void c_assign_tuple(compiler_t *comp, int n, py_parse_node_t *nodes) {
483 assert(n >= 0);
484 int have_star_index = -1;
485 for (int i = 0; i < n; i++) {
486 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
487 if (have_star_index < 0) {
488 EMIT(unpack_ex, i, n - i - 1);
489 have_star_index = i;
490 } else {
491 printf("SyntaxError: two starred expressions in assignment\n");
492 return;
493 }
494 }
495 }
496 if (have_star_index < 0) {
497 EMIT(unpack_sequence, n);
498 }
499 for (int i = 0; i < n; i++) {
500 if (i == have_star_index) {
501 c_assign(comp, ((py_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
502 } else {
503 c_assign(comp, nodes[i], ASSIGN_STORE);
504 }
505 }
506}
507
508// assigns top of stack to pn
509void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) {
510 tail_recursion:
511 if (PY_PARSE_NODE_IS_NULL(pn)) {
512 assert(0);
513 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
514 if (PY_PARSE_NODE_IS_ID(pn)) {
515 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
516 switch (assign_kind) {
517 case ASSIGN_STORE:
518 case ASSIGN_AUG_STORE:
Damien4b03e772013-10-05 14:17:09 +0100519 EMIT(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100520 break;
521 case ASSIGN_AUG_LOAD:
Damien4b03e772013-10-05 14:17:09 +0100522 EMIT(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100523 break;
524 }
525 } else {
526 printf("SyntaxError: can't assign to literal\n");
527 return;
528 }
529 } else {
530 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
531 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
532 case PN_power:
533 // lhs is an index or attribute
534 c_assign_power(comp, pns, assign_kind);
535 break;
536
537 case PN_testlist_star_expr:
538 case PN_exprlist:
539 // lhs is a tuple
540 if (assign_kind != ASSIGN_STORE) {
541 goto bad_aug;
542 }
543 c_assign_tuple(comp, PY_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
544 break;
545
546 case PN_atom_paren:
547 // lhs is something in parenthesis
548 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
549 // empty tuple
550 printf("SyntaxError: can't assign to ()\n");
551 return;
552 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
553 pns = (py_parse_node_struct_t*)pns->nodes[0];
554 goto testlist_comp;
555 } else {
556 // parenthesis around 1 item, is just that item
557 pn = pns->nodes[0];
558 goto tail_recursion;
559 }
560 break;
561
562 case PN_atom_bracket:
563 // lhs is something in brackets
564 if (assign_kind != ASSIGN_STORE) {
565 goto bad_aug;
566 }
567 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
568 // empty list, assignment allowed
569 c_assign_tuple(comp, 0, NULL);
570 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
571 pns = (py_parse_node_struct_t*)pns->nodes[0];
572 goto testlist_comp;
573 } else {
574 // brackets around 1 item
575 c_assign_tuple(comp, 1, &pns->nodes[0]);
576 }
577 break;
578
579 default:
580 printf("unknown assign, %u\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
581 assert(0);
582 }
583 return;
584
585 testlist_comp:
586 // lhs is a sequence
587 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
588 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
589 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
590 // sequence of one item, with trailing comma
591 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
592 c_assign_tuple(comp, 1, &pns->nodes[0]);
593 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
594 // sequence of many items
595 // TODO call c_assign_tuple instead
596 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns2);
597 EMIT(unpack_sequence, 1 + n);
598 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
599 for (int i = 0; i < n; i++) {
600 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
601 }
602 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
603 // TODO not implemented
604 assert(0);
605 } else {
606 // sequence with 2 items
607 goto sequence_with_2_items;
608 }
609 } else {
610 // sequence with 2 items
611 sequence_with_2_items:
612 c_assign_tuple(comp, 2, pns->nodes);
613 }
614 return;
615 }
616 return;
617
618 bad_aug:
619 printf("SyntaxError: illegal expression for augmented assignment\n");
620}
621
622// stuff for lambda and comprehensions and generators
623void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
624 // make closed over variables, if any
625 int nfree = 0;
626 if (comp->scope_cur->kind != SCOPE_MODULE) {
627 for (int i = 0; i < this_scope->id_info_len; i++) {
628 id_info_t *id_info = &this_scope->id_info[i];
629 if (id_info->kind == ID_INFO_KIND_FREE) {
630 EMIT(load_closure, id_info->qstr);
631 nfree += 1;
632 }
633 }
634 }
635 if (nfree > 0) {
636 EMIT(build_tuple, nfree);
637 }
638
639 // make the function/closure
640 if (nfree == 0) {
641 EMIT(make_function, this_scope, n_dict_params, n_default_params);
642 } else {
643 EMIT(make_closure, this_scope, n_dict_params, n_default_params);
644 }
645}
646
647void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) {
Damienb14de212013-10-06 00:28:28 +0100648 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
649 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100650 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
651 // this parameter has a default value
652 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
653 if (comp->have_bare_star) {
654 comp->param_pass_num_dict_params += 1;
655 if (comp->param_pass == 1) {
656 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
657 compile_node(comp, pns->nodes[2]);
658 }
659 } else {
660 comp->param_pass_num_default_params += 1;
661 if (comp->param_pass == 2) {
662 compile_node(comp, pns->nodes[2]);
663 }
664 }
665 }
Damienb14de212013-10-06 00:28:28 +0100666 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
667 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100668 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
669 // bare star
670 comp->have_bare_star = true;
671 }
672 }
673}
674
675// leaves function object on stack
676// returns function name
Damien6cdd3af2013-10-05 18:08:26 +0100677qstr compile_funcdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100678 if (comp->pass == PASS_1) {
679 // create a new scope for this function
Damien6cdd3af2013-10-05 18:08:26 +0100680 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (py_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100681 // store the function scope so the compiling function can use it at each pass
682 pns->nodes[4] = (py_parse_node_t)s;
683 }
684
685 // save variables (probably don't need to do this, since we can't have nested definitions..?)
686 bool old_have_bare_star = comp->have_bare_star;
687 int old_param_pass = comp->param_pass;
688 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
689 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
690
691 // compile default parameters
692 comp->have_bare_star = false;
693 comp->param_pass = 1; // pass 1 does any default parameters after bare star
694 comp->param_pass_num_dict_params = 0;
695 comp->param_pass_num_default_params = 0;
696 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
697 comp->have_bare_star = false;
698 comp->param_pass = 2; // pass 2 does any default parameters before bare star
699 comp->param_pass_num_dict_params = 0;
700 comp->param_pass_num_default_params = 0;
701 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
702
703 // get the scope for this function
704 scope_t *fscope = (scope_t*)pns->nodes[4];
705
706 // make the function
707 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
708
709 // restore variables
710 comp->have_bare_star = old_have_bare_star;
711 comp->param_pass = old_param_pass;
712 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
713 comp->param_pass_num_default_params = old_param_pass_num_default_params;
714
715 // return its name (the 'f' in "def f(...):")
716 return fscope->simple_name;
717}
718
719// leaves class object on stack
720// returns class name
Damien6cdd3af2013-10-05 18:08:26 +0100721qstr compile_classdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100722 if (comp->pass == PASS_1) {
723 // create a new scope for this class
Damien6cdd3af2013-10-05 18:08:26 +0100724 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (py_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100725 // store the class scope so the compiling function can use it at each pass
726 pns->nodes[3] = (py_parse_node_t)s;
727 }
728
729 EMIT(load_build_class);
730
731 // scope for this class
732 scope_t *cscope = (scope_t*)pns->nodes[3];
733
734 // compile the class
735 close_over_variables_etc(comp, cscope, 0, 0);
736
737 // get its name
738 EMIT(load_const_id, cscope->simple_name);
739
740 // nodes[1] has parent classes, if any
741 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
742 // no parent classes
743 EMIT(call_function, 2, 0, false, false);
744 } else {
745 // have a parent class or classes
746 // TODO what if we have, eg, *a or **a in the parent list?
747 compile_node(comp, pns->nodes[1]);
748 EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
749 }
750
751 // return its name (the 'C' in class C(...):")
752 return cscope->simple_name;
753}
754
Damien6cdd3af2013-10-05 18:08:26 +0100755// returns true if it was a built-in decorator (even if the built-in had an error)
756static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_node_t *name_nodes, uint *emit_options) {
757 if (PY_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) {
758 return false;
759 }
760
761 if (name_len != 2) {
762 printf("SyntaxError: invalid micropython decorator\n");
763 return true;
764 }
765
766 qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]);
767 if (attr == comp->qstr_native) {
768 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien7af3d192013-10-07 00:02:49 +0100769 } else if (attr == comp->qstr_viper) {
770 *emit_options = EMIT_OPT_VIPER;
Damien5bfb7592013-10-05 18:41:24 +0100771 } else if (attr == comp->qstr_asm_thumb) {
772 *emit_options = EMIT_OPT_ASM_THUMB;
Damien6cdd3af2013-10-05 18:08:26 +0100773 } else {
774 printf("SyntaxError: invalid micropython decorator\n");
775 }
776
777 return true;
778}
779
Damien429d7192013-10-04 19:53:11 +0100780void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) {
781 // get the list of decorators
782 py_parse_node_t *nodes;
783 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
784
Damien6cdd3af2013-10-05 18:08:26 +0100785 // inherit emit options for this function/class definition
786 uint emit_options = comp->scope_cur->emit_options;
787
788 // compile each decorator
789 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100790 for (int i = 0; i < n; i++) {
791 assert(PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
792 py_parse_node_struct_t *pns_decorator = (py_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100793
794 // nodes[0] contains the decorator function, which is a dotted name
795 py_parse_node_t *name_nodes;
796 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
797
798 // check for built-in decorators
799 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
800 // this was a built-in
801 num_built_in_decorators += 1;
802
803 } else {
804 // not a built-in, compile normally
805
806 // compile the decorator function
807 compile_node(comp, name_nodes[0]);
808 for (int i = 1; i < name_len; i++) {
809 assert(PY_PARSE_NODE_IS_ID(name_nodes[i])); // should be
810 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(name_nodes[i]));
811 }
812
813 // nodes[1] contains arguments to the decorator function, if any
814 if (!PY_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
815 // call the decorator function with the arguments in nodes[1]
816 compile_node(comp, pns_decorator->nodes[1]);
817 }
Damien429d7192013-10-04 19:53:11 +0100818 }
819 }
820
821 // compile the body (funcdef or classdef) and get its name
822 py_parse_node_struct_t *pns_body = (py_parse_node_struct_t*)pns->nodes[1];
823 qstr body_name = 0;
824 if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100825 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100826 } else if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100827 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100828 } else {
829 // shouldn't happen
830 assert(0);
831 }
832
833 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100834 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien429d7192013-10-04 19:53:11 +0100835 EMIT(call_function, 1, 0, false, false);
836 }
837
838 // store func/class object into name
Damien4b03e772013-10-05 14:17:09 +0100839 EMIT(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100840}
841
842void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100843 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100844 // store function object into function name
Damien4b03e772013-10-05 14:17:09 +0100845 EMIT(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100846}
847
848void c_del_stmt(compiler_t *comp, py_parse_node_t pn) {
849 if (PY_PARSE_NODE_IS_ID(pn)) {
Damien4b03e772013-10-05 14:17:09 +0100850 EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn));
Damien429d7192013-10-04 19:53:11 +0100851 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
852 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
853
854 compile_node(comp, pns->nodes[0]); // base of the power node
855
856 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
857 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
858 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
859 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
860 for (int i = 0; i < n - 1; i++) {
861 compile_node(comp, pns1->nodes[i]);
862 }
863 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
864 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
865 }
866 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
867 // SyntaxError: can't delete a function call
868 assert(0);
869 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
870 compile_node(comp, pns1->nodes[0]);
871 EMIT(delete_subscr);
872 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
873 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
874 EMIT(delete_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
875 } else {
876 // shouldn't happen
877 assert(0);
878 }
879 } else {
880 // shouldn't happen
881 assert(0);
882 }
883
884 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
885 // SyntaxError, cannot delete
886 assert(0);
887 }
888 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
889 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
890 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
891 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
892 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
893
894 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
895 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
896 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
897 // sequence of one item, with trailing comma
898 assert(PY_PARSE_NODE_IS_NULL(pns1->nodes[0]));
899 c_del_stmt(comp, pns->nodes[0]);
900 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
901 // sequence of many items
902 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
903 c_del_stmt(comp, pns->nodes[0]);
904 for (int i = 0; i < n; i++) {
905 c_del_stmt(comp, pns1->nodes[i]);
906 }
907 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
908 // TODO not implemented; can't del comprehension?
909 assert(0);
910 } else {
911 // sequence with 2 items
912 goto sequence_with_2_items;
913 }
914 } else {
915 // sequence with 2 items
916 sequence_with_2_items:
917 c_del_stmt(comp, pns->nodes[0]);
918 c_del_stmt(comp, pns->nodes[1]);
919 }
920 } else {
921 // tuple with 1 element
922 c_del_stmt(comp, pn);
923 }
924 } else {
925 // not implemented
926 assert(0);
927 }
928}
929
930void compile_del_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
931 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
932}
933
934void compile_break_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
935 if (comp->break_label == 0) {
936 printf("ERROR: cannot break from here\n");
937 }
938 EMIT(break_loop, comp->break_label);
939}
940
941void compile_continue_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
942 if (comp->continue_label == 0) {
943 printf("ERROR: cannot continue from here\n");
944 }
945 if (comp->except_nest_level > 0) {
946 EMIT(continue_loop, comp->continue_label);
947 } else {
948 EMIT(jump, comp->continue_label);
949 }
950}
951
952void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
953 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
954 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
955 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
956 // special case when returning an if-expression; to match CPython optimisation
957 py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0];
958 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1];
959
Damienb05d7072013-10-05 13:37:10 +0100960 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100961 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
962 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
963 EMIT(return_value);
964 EMIT(label_assign, l_fail);
965 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
966 } else {
967 compile_node(comp, pns->nodes[0]);
968 }
969 EMIT(return_value);
970}
971
972void compile_yield_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
973 compile_node(comp, pns->nodes[0]);
974 EMIT(pop_top);
975}
976
977void compile_raise_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
978 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
979 // raise
980 EMIT(raise_varargs, 0);
981 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
982 // raise x from y
983 pns = (py_parse_node_struct_t*)pns->nodes[0];
984 compile_node(comp, pns->nodes[0]);
985 compile_node(comp, pns->nodes[1]);
986 EMIT(raise_varargs, 2);
987 } else {
988 // raise x
989 compile_node(comp, pns->nodes[0]);
990 EMIT(raise_varargs, 1);
991 }
992}
993
994// q1 holds the base, q2 the full name
995// eg a -> q1=q2=a
996// a.b.c -> q1=a, q2=a.b.c
997void do_import_name(compiler_t *comp, py_parse_node_t pn, qstr *q1, qstr *q2) {
998 bool is_as = false;
999 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1000 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
1001 // a name of the form x as y; unwrap it
1002 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1003 pn = pns->nodes[0];
1004 is_as = true;
1005 }
1006 if (PY_PARSE_NODE_IS_ID(pn)) {
1007 // just a simple name
1008 *q2 = PY_PARSE_NODE_LEAF_ARG(pn);
1009 if (!is_as) {
1010 *q1 = *q2;
1011 }
1012 EMIT(import_name, *q2);
1013 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
1014 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
1015 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
1016 // a name of the form a.b.c
1017 if (!is_as) {
1018 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1019 }
1020 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1021 int len = n - 1;
1022 for (int i = 0; i < n; i++) {
1023 len += strlen(qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
1024 }
1025 char *str = m_new(char, len + 1);
1026 str[0] = 0;
1027 for (int i = 0; i < n; i++) {
1028 if (i > 0) {
1029 strcat(str, ".");
1030 }
1031 strcat(str, qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
1032 }
1033 *q2 = qstr_from_str_take(str);
1034 EMIT(import_name, *q2);
1035 if (is_as) {
1036 for (int i = 1; i < n; i++) {
1037 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1038 }
1039 }
1040 } else {
1041 // TODO not implemented
1042 assert(0);
1043 }
1044 } else {
1045 // TODO not implemented
1046 assert(0);
1047 }
1048}
1049
1050void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) {
1051 EMIT(load_const_small_int, 0); // ??
1052 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1053 qstr q1, q2;
1054 do_import_name(comp, pn, &q1, &q2);
Damien4b03e772013-10-05 14:17:09 +01001055 EMIT(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001056}
1057
1058void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) {
1059 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1060}
1061
1062void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) {
1063 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
1064 EMIT(load_const_small_int, 0); // what's this for??
1065 EMIT(load_const_verbatim_start);
1066 EMIT(load_const_verbatim_str, "('*',)");
1067 EMIT(load_const_verbatim_end);
1068 qstr dummy_q, id1;
1069 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1070 EMIT(import_star);
1071 } else {
1072 py_parse_node_t *pn_nodes;
1073 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1074
1075 EMIT(load_const_small_int, 0); // what's this for??
1076 EMIT(load_const_verbatim_start);
1077 EMIT(load_const_verbatim_str, "(");
1078 for (int i = 0; i < n; i++) {
1079 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1080 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1081 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1082 if (i > 0) {
1083 EMIT(load_const_verbatim_str, ", ");
1084 }
1085 EMIT(load_const_verbatim_str, "'");
1086 EMIT(load_const_verbatim_str, qstr_str(id2));
1087 EMIT(load_const_verbatim_str, "'");
1088 }
1089 if (n == 1) {
1090 EMIT(load_const_verbatim_str, ",");
1091 }
1092 EMIT(load_const_verbatim_str, ")");
1093 EMIT(load_const_verbatim_end);
1094 qstr dummy_q, id1;
1095 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1096 for (int i = 0; i < n; i++) {
1097 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1098 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1099 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1100 EMIT(import_from, id2);
1101 if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien4b03e772013-10-05 14:17:09 +01001102 EMIT(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001103 } else {
Damien4b03e772013-10-05 14:17:09 +01001104 EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001105 }
1106 }
1107 EMIT(pop_top);
1108 }
1109}
1110
1111void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001112 if (comp->pass == PASS_1) {
1113 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1114 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1115 } else {
1116 pns = (py_parse_node_struct_t*)pns->nodes[0];
1117 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1118 for (int i = 0; i < num_nodes; i++) {
1119 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1120 }
Damien429d7192013-10-04 19:53:11 +01001121 }
1122 }
1123}
1124
1125void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001126 if (comp->pass == PASS_1) {
1127 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1128 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1129 } else {
1130 pns = (py_parse_node_struct_t*)pns->nodes[0];
1131 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1132 for (int i = 0; i < num_nodes; i++) {
1133 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1134 }
Damien429d7192013-10-04 19:53:11 +01001135 }
1136 }
1137}
1138
1139void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001140 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001141 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien4b03e772013-10-05 14:17:09 +01001142 EMIT(load_id, comp->qstr_assertion_error);
Damien429d7192013-10-04 19:53:11 +01001143 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1144 // assertion message
1145 compile_node(comp, pns->nodes[1]);
1146 EMIT(call_function, 1, 0, false, false);
1147 }
1148 EMIT(raise_varargs, 1);
1149 EMIT(label_assign, l_end);
1150}
1151
1152void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1153 // TODO proper and/or short circuiting
1154
Damienb05d7072013-10-05 13:37:10 +01001155 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001156
Damienb05d7072013-10-05 13:37:10 +01001157 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001158 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1159
1160 compile_node(comp, pns->nodes[1]); // if block
1161 //if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
1162 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001163 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001164 EMIT(jump, l_end);
1165 }
1166 //}
1167 EMIT(label_assign, l_fail);
1168
1169 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
1170 // compile elif blocks
1171
1172 py_parse_node_struct_t *pns_elif = (py_parse_node_struct_t*)pns->nodes[2];
1173
1174 if (PY_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
1175 // multiple elif blocks
1176
1177 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
1178 for (int i = 0; i < n; i++) {
1179 py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001180 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001181 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1182
1183 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001184 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001185 EMIT(jump, l_end);
1186 }
1187 EMIT(label_assign, l_fail);
1188 }
1189
1190 } else {
1191 // a single elif block
1192
Damienb05d7072013-10-05 13:37:10 +01001193 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001194 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1195
1196 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001197 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001198 EMIT(jump, l_end);
1199 }
1200 EMIT(label_assign, l_fail);
1201 }
1202 }
1203
1204 // compile else block
1205 compile_node(comp, pns->nodes[3]); // can be null
1206
1207 EMIT(label_assign, l_end);
1208}
1209
1210void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1211 int old_break_label = comp->break_label;
1212 int old_continue_label = comp->continue_label;
1213
Damienb05d7072013-10-05 13:37:10 +01001214 int done_label = comp_next_label(comp);
1215 int end_label = comp_next_label(comp);
1216 int break_label = comp_next_label(comp);
1217 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001218
1219 comp->break_label = break_label;
1220 comp->continue_label = continue_label;
1221
1222 EMIT(setup_loop, end_label);
1223 EMIT(label_assign, continue_label);
1224 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1225 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001226 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001227 EMIT(jump, continue_label);
1228 }
1229 EMIT(label_assign, done_label);
1230
1231 // break/continue apply to outer loop (if any) in the else block
1232 comp->break_label = old_break_label;
1233 comp->continue_label = old_continue_label;
1234
1235 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1236 // this is a small hack to agree with CPython
1237 if (!node_is_const_true(pns->nodes[0])) {
1238 EMIT(pop_block);
1239 }
1240
1241 compile_node(comp, pns->nodes[2]); // else
1242
1243 EMIT(label_assign, break_label);
1244 EMIT(label_assign, end_label);
1245}
1246
1247void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1248 int old_break_label = comp->break_label;
1249 int old_continue_label = comp->continue_label;
1250
Damienb05d7072013-10-05 13:37:10 +01001251 int for_label = comp_next_label(comp);
1252 int pop_label = comp_next_label(comp);
1253 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001254
Damienb05d7072013-10-05 13:37:10 +01001255 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001256
1257 comp->continue_label = for_label;
1258 comp->break_label = break_label;
1259
1260 EMIT(setup_loop, end_label);
1261 compile_node(comp, pns->nodes[1]); // iterator
1262 EMIT(get_iter);
1263 EMIT(label_assign, for_label);
1264 EMIT(for_iter, pop_label);
1265 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1266 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001267 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001268 EMIT(jump, for_label);
1269 }
1270 EMIT(label_assign, pop_label);
1271 EMIT(for_iter_end);
1272
1273 // break/continue apply to outer loop (if any) in the else block
1274 comp->break_label = old_break_label;
1275 comp->continue_label = old_continue_label;
1276
1277 EMIT(pop_block);
1278
1279 compile_node(comp, pns->nodes[3]); // else (not tested)
1280
1281 EMIT(label_assign, break_label);
1282 EMIT(label_assign, end_label);
1283}
1284
1285void 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) {
1286 // this function is a bit of a hack at the moment
1287 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1288
1289 // setup code
1290 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001291 int l1 = comp_next_label(comp);
1292 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001293 comp->except_nest_level += 1; // for correct handling of continue
1294 EMIT(setup_except, l1);
1295 compile_node(comp, pn_body); // body
1296 EMIT(pop_block);
1297 EMIT(jump, success_label);
1298 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001299 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001300
1301 for (int i = 0; i < n_except; i++) {
1302 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1303 py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i];
1304
1305 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001306 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001307
1308 if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
1309 // this is a catch all exception handler
1310 if (i + 1 != n_except) {
1311 printf("SyntaxError: default 'except:' must be last\n");
1312 return;
1313 }
1314 } else {
1315 // this exception handler requires a match to a certain type of exception
1316 py_parse_node_t pns_exception_expr = pns_except->nodes[0];
1317 if (PY_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1318 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns_exception_expr;
1319 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
1320 // handler binds the exception to a local
1321 pns_exception_expr = pns3->nodes[0];
1322 qstr_exception_local = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
1323 }
1324 }
1325 EMIT(dup_top);
1326 compile_node(comp, pns_exception_expr);
1327 EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1328 EMIT(pop_jump_if_false, end_finally_label);
1329 }
1330
1331 EMIT(pop_top);
1332
1333 if (qstr_exception_local == 0) {
1334 EMIT(pop_top);
1335 } else {
Damien4b03e772013-10-05 14:17:09 +01001336 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001337 }
1338
1339 EMIT(pop_top);
1340
1341 int l3;
1342 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001343 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001344 EMIT(setup_finally, l3);
1345 }
1346 compile_node(comp, pns_except->nodes[1]);
1347 if (qstr_exception_local != 0) {
1348 EMIT(pop_block);
1349 }
1350 EMIT(pop_except);
1351 if (qstr_exception_local != 0) {
1352 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1353 EMIT(label_assign, l3);
1354 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001355 EMIT(store_id, qstr_exception_local);
1356 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001357 EMIT(end_finally);
1358 }
1359 EMIT(jump, l2);
1360 EMIT(label_assign, end_finally_label);
1361 }
1362
1363 EMIT(end_finally);
1364 EMIT(label_assign, success_label);
1365 comp->except_nest_level -= 1;
1366 compile_node(comp, pn_else); // else block, can be null
1367 EMIT(label_assign, l2);
1368 EMIT(set_stack_size, stack_size);
1369}
1370
1371void 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) {
1372 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1373 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001374 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001375 EMIT(setup_finally, l_finally_block);
1376 if (n_except == 0) {
1377 assert(PY_PARSE_NODE_IS_NULL(pn_else));
1378 compile_node(comp, pn_body);
1379 } else {
1380 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1381 }
1382 EMIT(pop_block);
1383 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1384 EMIT(label_assign, l_finally_block);
1385 compile_node(comp, pn_finally);
1386 EMIT(end_finally);
1387 EMIT(set_stack_size, stack_size);
1388}
1389
1390void compile_try_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1391 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1392 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
1393 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
1394 // just try-finally
1395 compile_try_finally(comp, pns->nodes[0], 0, NULL, PY_PARSE_NODE_NULL, pns2->nodes[0]);
1396 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
1397 // try-except and possibly else and/or finally
1398 py_parse_node_t *pn_excepts;
1399 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1400 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
1401 // no finally
1402 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1403 } else {
1404 // have finally
1405 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((py_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
1406 }
1407 } else {
1408 // just try-except
1409 py_parse_node_t *pn_excepts;
1410 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1411 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, PY_PARSE_NODE_NULL);
1412 }
1413 } else {
1414 // shouldn't happen
1415 assert(0);
1416 }
1417}
1418
1419void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, py_parse_node_t body) {
1420 if (n == 0) {
1421 // no more pre-bits, compile the body of the with
1422 compile_node(comp, body);
1423 } else {
Damienb05d7072013-10-05 13:37:10 +01001424 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001425 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1426 // this pre-bit is of the form "a as b"
1427 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0];
1428 compile_node(comp, pns->nodes[0]);
1429 EMIT(setup_with, l_end);
1430 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1431 } else {
1432 // this pre-bit is just an expression
1433 compile_node(comp, nodes[0]);
1434 EMIT(setup_with, l_end);
1435 EMIT(pop_top);
1436 }
1437 // compile additional pre-bits and the body
1438 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1439 // finish this with block
1440 EMIT(pop_block);
1441 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1442 EMIT(label_assign, l_end);
1443 EMIT(with_cleanup);
1444 EMIT(end_finally);
1445 }
1446}
1447
1448void compile_with_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1449 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1450 py_parse_node_t *nodes;
1451 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1452 assert(n > 0);
1453
1454 // compile in a nested fashion
1455 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1456}
1457
1458void compile_expr_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1459 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1460 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
1461 // do nothing with a lonely constant
1462 } else {
1463 compile_node(comp, pns->nodes[0]); // just an expression
1464 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1465 }
1466 } else {
1467 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
1468 int kind = PY_PARSE_NODE_STRUCT_KIND(pns1);
1469 if (kind == PN_expr_stmt_augassign) {
1470 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1471 compile_node(comp, pns1->nodes[1]); // rhs
1472 assert(PY_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
1473 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
1474 switch (PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1475 case PY_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1476 case PY_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1477 case PY_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1478 case PY_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1479 case PY_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1480 case PY_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1481 case PY_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1482 case PY_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1483 case PY_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1484 case PY_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1485 case PY_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1486 case PY_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
1487 default: assert(0); // shouldn't happen
1488 }
1489 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1490 } else if (kind == PN_expr_stmt_assign_list) {
1491 int rhs = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1492 compile_node(comp, ((py_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
1493 // following CPython, we store left-most first
1494 if (rhs > 0) {
1495 EMIT(dup_top);
1496 }
1497 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1498 for (int i = 0; i < rhs; i++) {
1499 if (i + 1 < rhs) {
1500 EMIT(dup_top);
1501 }
1502 c_assign(comp, ((py_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
1503 }
1504 } else if (kind == PN_expr_stmt_assign) {
1505 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1506 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1507 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 2
1508 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 2) {
1509 // optimisation for a, b = c, d; to match CPython's optimisation
1510 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1511 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1512 compile_node(comp, pns10->nodes[0]); // rhs
1513 compile_node(comp, pns10->nodes[1]); // rhs
1514 EMIT(rot_two);
1515 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1516 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1517 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1518 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1519 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 3
1520 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 3) {
1521 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
1522 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1523 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1524 compile_node(comp, pns10->nodes[0]); // rhs
1525 compile_node(comp, pns10->nodes[1]); // rhs
1526 compile_node(comp, pns10->nodes[2]); // rhs
1527 EMIT(rot_three);
1528 EMIT(rot_two);
1529 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1530 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1531 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1532 } else {
1533 compile_node(comp, pns1->nodes[0]); // rhs
1534 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1535 }
1536 } else {
1537 // shouldn't happen
1538 assert(0);
1539 }
1540 }
1541}
1542
1543void c_binary_op(compiler_t *comp, py_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1544 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1545 compile_node(comp, pns->nodes[0]);
1546 for (int i = 1; i < num_nodes; i += 1) {
1547 compile_node(comp, pns->nodes[i]);
1548 EMIT(binary_op, binary_op);
1549 }
1550}
1551
1552void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1553 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1554 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1];
1555
1556 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001557 int l_fail = comp_next_label(comp);
1558 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001559 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1560 compile_node(comp, pns->nodes[0]); // success value
1561 EMIT(jump, l_end);
1562 EMIT(label_assign, l_fail);
1563 EMIT(set_stack_size, stack_size); // force stack size reset
1564 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1565 EMIT(label_assign, l_end);
1566}
1567
1568void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) {
1569 // TODO default params etc for lambda; possibly just use funcdef code
1570 //py_parse_node_t pn_params = pns->nodes[0];
1571 //py_parse_node_t pn_body = pns->nodes[1];
1572
1573 if (comp->pass == PASS_1) {
1574 // create a new scope for this lambda
Damien6cdd3af2013-10-05 18:08:26 +01001575 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 +01001576 // store the lambda scope so the compiling function (this one) can use it at each pass
1577 pns->nodes[2] = (py_parse_node_t)s;
1578 }
1579
1580 // get the scope for this lambda
1581 scope_t *this_scope = (scope_t*)pns->nodes[2];
1582
1583 // make the lambda
1584 close_over_variables_etc(comp, this_scope, 0, 0);
1585}
1586
1587void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001588 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001589 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1590 for (int i = 0; i < n; i += 1) {
1591 compile_node(comp, pns->nodes[i]);
1592 if (i + 1 < n) {
1593 EMIT(jump_if_true_or_pop, l_end);
1594 }
1595 }
1596 EMIT(label_assign, l_end);
1597}
1598
1599void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001600 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001601 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1602 for (int i = 0; i < n; i += 1) {
1603 compile_node(comp, pns->nodes[i]);
1604 if (i + 1 < n) {
1605 EMIT(jump_if_false_or_pop, l_end);
1606 }
1607 }
1608 EMIT(label_assign, l_end);
1609}
1610
1611void compile_not_test_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1612 compile_node(comp, pns->nodes[0]);
1613 EMIT(unary_op, RT_UNARY_OP_NOT);
1614}
1615
1616void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
1617 int stack_size = EMIT(get_stack_size);
1618 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1619 compile_node(comp, pns->nodes[0]);
1620 bool multi = (num_nodes > 3);
1621 int l_fail = 0;
1622 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001623 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001624 }
1625 for (int i = 1; i + 1 < num_nodes; i += 2) {
1626 compile_node(comp, pns->nodes[i + 1]);
1627 if (i + 2 < num_nodes) {
1628 EMIT(dup_top);
1629 EMIT(rot_three);
1630 }
1631 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS)) {
1632 EMIT(compare_op, RT_COMPARE_OP_LESS);
1633 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE)) {
1634 EMIT(compare_op, RT_COMPARE_OP_MORE);
1635 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_EQUAL)) {
1636 EMIT(compare_op, RT_COMPARE_OP_EQUAL);
1637 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS_EQUAL)) {
1638 EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
1639 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE_EQUAL)) {
1640 EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
1641 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_NOT_EQUAL)) {
1642 EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
1643 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_KW_IN)) {
1644 EMIT(compare_op, RT_COMPARE_OP_IN);
1645 } else if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1646 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[i];
1647 int kind = PY_PARSE_NODE_STRUCT_KIND(pns2);
1648 if (kind == PN_comp_op_not_in) {
1649 EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1650 } else if (kind == PN_comp_op_is) {
1651 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
1652 EMIT(compare_op, RT_COMPARE_OP_IS);
1653 } else {
1654 EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1655 }
1656 } else {
1657 // shouldn't happen
1658 assert(0);
1659 }
1660 } else {
1661 // shouldn't happen
1662 assert(0);
1663 }
1664 if (i + 2 < num_nodes) {
1665 EMIT(jump_if_false_or_pop, l_fail);
1666 }
1667 }
1668 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001669 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001670 EMIT(jump, l_end);
1671 EMIT(label_assign, l_fail);
1672 EMIT(rot_two);
1673 EMIT(pop_top);
1674 EMIT(label_assign, l_end);
1675 EMIT(set_stack_size, stack_size + 1); // force stack size
1676 }
1677}
1678
1679void compile_star_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1680 // TODO
1681 assert(0);
1682 compile_node(comp, pns->nodes[0]);
1683 //EMIT(unary_op, "UNARY_STAR");
1684}
1685
1686void compile_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1687 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1688}
1689
1690void compile_xor_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1691 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1692}
1693
1694void compile_and_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1695 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1696}
1697
1698void compile_shift_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1699 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1700 compile_node(comp, pns->nodes[0]);
1701 for (int i = 1; i + 1 < num_nodes; i += 2) {
1702 compile_node(comp, pns->nodes[i + 1]);
1703 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_LESS)) {
1704 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
1705 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_MORE)) {
1706 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
1707 } else {
1708 // shouldn't happen
1709 assert(0);
1710 }
1711 }
1712}
1713
1714void compile_arith_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1715 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1716 compile_node(comp, pns->nodes[0]);
1717 for (int i = 1; i + 1 < num_nodes; i += 2) {
1718 compile_node(comp, pns->nodes[i + 1]);
1719 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PLUS)) {
1720 EMIT(binary_op, RT_BINARY_OP_ADD);
1721 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MINUS)) {
1722 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
1723 } else {
1724 // shouldn't happen
1725 assert(0);
1726 }
1727 }
1728}
1729
1730void compile_term(compiler_t *comp, py_parse_node_struct_t *pns) {
1731 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1732 compile_node(comp, pns->nodes[0]);
1733 for (int i = 1; i + 1 < num_nodes; i += 2) {
1734 compile_node(comp, pns->nodes[i + 1]);
1735 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_STAR)) {
1736 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
1737 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_SLASH)) {
1738 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
1739 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_SLASH)) {
1740 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
1741 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PERCENT)) {
1742 EMIT(binary_op, RT_BINARY_OP_MODULO);
1743 } else {
1744 // shouldn't happen
1745 assert(0);
1746 }
1747 }
1748}
1749
1750void compile_factor_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1751 compile_node(comp, pns->nodes[1]);
1752 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
1753 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
1754 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
1755 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
1756 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
1757 EMIT(unary_op, RT_UNARY_OP_INVERT);
1758 } else {
1759 // shouldn't happen
1760 assert(0);
1761 }
1762}
1763
1764void compile_trailer_paren_helper(compiler_t *comp, py_parse_node_struct_t *pns, bool is_method_call) {
1765 // function to call is on top of stack
1766
1767 int old_n_arg_keyword = comp->n_arg_keyword;
1768 bool old_have_star_arg = comp->have_star_arg;
1769 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
1770 comp->n_arg_keyword = 0;
1771 comp->have_star_arg = false;
1772 comp->have_dbl_star_arg = false;
1773
1774 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
1775
1776 // compute number of positional arguments
1777 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
1778 if (comp->have_star_arg) {
1779 n_positional -= 1;
1780 }
1781 if (comp->have_dbl_star_arg) {
1782 n_positional -= 1;
1783 }
1784
1785 if (is_method_call) {
1786 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
1787 } else {
1788 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
1789 }
1790
1791 comp->n_arg_keyword = old_n_arg_keyword;
1792 comp->have_star_arg = old_have_star_arg;
1793 comp->have_dbl_star_arg = old_have_dbl_star_arg;
1794}
1795
1796void compile_power_trailers(compiler_t *comp, py_parse_node_struct_t *pns) {
1797 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1798 for (int i = 0; i < num_nodes; i++) {
1799 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)) {
1800 // optimisation for method calls a.f(...), following PyPy
1801 py_parse_node_struct_t *pns_period = (py_parse_node_struct_t*)pns->nodes[i];
1802 py_parse_node_struct_t *pns_paren = (py_parse_node_struct_t*)pns->nodes[i + 1];
1803 EMIT(load_method, PY_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
1804 compile_trailer_paren_helper(comp, pns_paren, true);
1805 i += 1;
1806 } else {
1807 compile_node(comp, pns->nodes[i]);
1808 }
1809 }
1810}
1811
1812void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
1813 compile_node(comp, pns->nodes[0]);
1814 EMIT(binary_op, RT_BINARY_OP_POWER);
1815}
1816
1817void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) {
1818 // a list of strings
1819 EMIT(load_const_verbatim_start);
1820 EMIT(load_const_verbatim_str, "'");
1821 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1822 for (int i = 0; i < n; i++) {
1823 // TODO allow concatenation of either strings or bytes, but not mixed
1824 assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i]));
1825 assert(PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]) == PY_PARSE_NODE_STRING);
1826 const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1827 EMIT(load_const_verbatim_strn, str, strlen(str));
1828 }
1829 EMIT(load_const_verbatim_str, "'");
1830 EMIT(load_const_verbatim_end);
1831}
1832
1833// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
1834void compile_comprehension(compiler_t *comp, py_parse_node_struct_t *pns, scope_kind_t kind) {
1835 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
1836 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
1837 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
1838
1839 if (comp->pass == PASS_1) {
1840 // create a new scope for this comprehension
Damien6cdd3af2013-10-05 18:08:26 +01001841 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 +01001842 // store the comprehension scope so the compiling function (this one) can use it at each pass
1843 pns_comp_for->nodes[3] = (py_parse_node_t)s;
1844 }
1845
1846 // get the scope for this comprehension
1847 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
1848
1849 // compile the comprehension
1850 close_over_variables_etc(comp, this_scope, 0, 0);
1851
1852 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
1853 EMIT(get_iter);
1854 EMIT(call_function, 1, 0, false, false);
1855}
1856
1857void compile_atom_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
1858 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1859 // an empty tuple
1860 /*
1861 EMIT(build_tuple, 0);
1862 */
1863 c_tuple(comp, PY_PARSE_NODE_NULL, NULL);
1864 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
1865 pns = (py_parse_node_struct_t*)pns->nodes[0];
1866 assert(!PY_PARSE_NODE_IS_NULL(pns->nodes[1]));
1867 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1868 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
1869 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
1870 // tuple of one item, with trailing comma
1871 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
1872 /*
1873 compile_node(comp, pns->nodes[0]);
1874 EMIT(build_tuple, 1);
1875 */
1876 c_tuple(comp, pns->nodes[0], NULL);
1877 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
1878 // tuple of many items
1879 /*
1880 compile_node(comp, pns->nodes[0]);
1881 compile_generic_all_nodes(comp, pns2);
1882 EMIT(build_tuple, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns2));
1883 */
1884 c_tuple(comp, pns->nodes[0], pns2);
1885 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
1886 // generator expression
1887 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
1888 } else {
1889 // tuple with 2 items
1890 goto tuple_with_2_items;
1891 }
1892 } else {
1893 // tuple with 2 items
1894 tuple_with_2_items:
1895 /*
1896 compile_node(comp, pns->nodes[0]);
1897 compile_node(comp, pns->nodes[1]);
1898 EMIT(build_tuple, 2);
1899 */
1900 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
1901 }
1902 } else {
1903 // parenthesis around a single item, is just that item
1904 compile_node(comp, pns->nodes[0]);
1905 }
1906}
1907
1908void compile_atom_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
1909 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1910 // empty list
1911 EMIT(build_list, 0);
1912 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
1913 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[0];
1914 if (PY_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
1915 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns2->nodes[1];
1916 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
1917 // list of one item, with trailing comma
1918 assert(PY_PARSE_NODE_IS_NULL(pns3->nodes[0]));
1919 compile_node(comp, pns2->nodes[0]);
1920 EMIT(build_list, 1);
1921 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
1922 // list of many items
1923 compile_node(comp, pns2->nodes[0]);
1924 compile_generic_all_nodes(comp, pns3);
1925 EMIT(build_list, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns3));
1926 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
1927 // list comprehension
1928 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
1929 } else {
1930 // list with 2 items
1931 goto list_with_2_items;
1932 }
1933 } else {
1934 // list with 2 items
1935 list_with_2_items:
1936 compile_node(comp, pns2->nodes[0]);
1937 compile_node(comp, pns2->nodes[1]);
1938 EMIT(build_list, 2);
1939 }
1940 } else {
1941 // list with 1 item
1942 compile_node(comp, pns->nodes[0]);
1943 EMIT(build_list, 1);
1944 }
1945}
1946
1947void compile_atom_brace(compiler_t *comp, py_parse_node_struct_t *pns) {
1948 py_parse_node_t pn = pns->nodes[0];
1949 if (PY_PARSE_NODE_IS_NULL(pn)) {
1950 // empty dict
1951 EMIT(build_map, 0);
1952 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
1953 pns = (py_parse_node_struct_t*)pn;
1954 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
1955 // dict with one element
1956 EMIT(build_map, 1);
1957 compile_node(comp, pn);
1958 EMIT(store_map);
1959 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
1960 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
1961 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
1962 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
1963 // dict/set with multiple elements
1964
1965 // get tail elements (2nd, 3rd, ...)
1966 py_parse_node_t *nodes;
1967 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
1968
1969 // first element sets whether it's a dict or set
1970 bool is_dict;
1971 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
1972 // a dictionary
1973 EMIT(build_map, 1 + n);
1974 compile_node(comp, pns->nodes[0]);
1975 EMIT(store_map);
1976 is_dict = true;
1977 } else {
1978 // a set
1979 compile_node(comp, pns->nodes[0]); // 1st value of set
1980 is_dict = false;
1981 }
1982
1983 // process rest of elements
1984 for (int i = 0; i < n; i++) {
1985 py_parse_node_t pn = nodes[i];
1986 bool is_key_value = PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
1987 compile_node(comp, pn);
1988 if (is_dict) {
1989 if (!is_key_value) {
1990 printf("SyntaxError?: expecting key:value for dictionary");
1991 return;
1992 }
1993 EMIT(store_map);
1994 } else {
1995 if (is_key_value) {
1996 printf("SyntaxError?: expecting just a value for set");
1997 return;
1998 }
1999 }
2000 }
2001
2002 // if it's a set, build it
2003 if (!is_dict) {
2004 EMIT(build_set, 1 + n);
2005 }
2006 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
2007 // dict/set comprehension
2008 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
2009 // a dictionary comprehension
2010 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2011 } else {
2012 // a set comprehension
2013 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2014 }
2015 } else {
2016 // shouldn't happen
2017 assert(0);
2018 }
2019 } else {
2020 // set with one element
2021 goto set_with_one_element;
2022 }
2023 } else {
2024 // set with one element
2025 set_with_one_element:
2026 compile_node(comp, pn);
2027 EMIT(build_set, 1);
2028 }
2029}
2030
2031void compile_trailer_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
2032 compile_trailer_paren_helper(comp, pns, false);
2033}
2034
2035void compile_trailer_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
2036 // object who's index we want is on top of stack
2037 compile_node(comp, pns->nodes[0]); // the index
2038 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
2039}
2040
2041void compile_trailer_period(compiler_t *comp, py_parse_node_struct_t *pns) {
2042 // object who's attribute we want is on top of stack
2043 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
2044}
2045
2046void compile_subscript_3_helper(compiler_t *comp, py_parse_node_struct_t *pns) {
2047 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2048 py_parse_node_t pn = pns->nodes[0];
2049 if (PY_PARSE_NODE_IS_NULL(pn)) {
2050 // [?:]
2051 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2052 EMIT(build_slice, 2);
2053 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
2054 pns = (py_parse_node_struct_t*)pn;
2055 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2056 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2057 pn = pns->nodes[0];
2058 if (PY_PARSE_NODE_IS_NULL(pn)) {
2059 // [?::]
2060 EMIT(build_slice, 2);
2061 } else {
2062 // [?::x]
2063 compile_node(comp, pn);
2064 EMIT(build_slice, 3);
2065 }
2066 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
2067 compile_node(comp, pns->nodes[0]);
2068 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2069 pns = (py_parse_node_struct_t*)pns->nodes[1];
2070 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2071 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2072 // [?:x:]
2073 EMIT(build_slice, 2);
2074 } else {
2075 // [?:x:x]
2076 compile_node(comp, pns->nodes[0]);
2077 EMIT(build_slice, 3);
2078 }
2079 } else {
2080 // [?:x]
2081 compile_node(comp, pn);
2082 EMIT(build_slice, 2);
2083 }
2084 } else {
2085 // [?:x]
2086 compile_node(comp, pn);
2087 EMIT(build_slice, 2);
2088 }
2089}
2090
2091void compile_subscript_2(compiler_t *comp, py_parse_node_struct_t *pns) {
2092 compile_node(comp, pns->nodes[0]); // start of slice
2093 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2094 compile_subscript_3_helper(comp, (py_parse_node_struct_t*)pns->nodes[1]);
2095}
2096
2097void compile_subscript_3(compiler_t *comp, py_parse_node_struct_t *pns) {
2098 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2099 compile_subscript_3_helper(comp, pns);
2100}
2101
2102void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns) {
2103 // if this is called then we are compiling a dict key:value pair
2104 compile_node(comp, pns->nodes[1]); // value
2105 compile_node(comp, pns->nodes[0]); // key
2106}
2107
2108void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002109 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002110 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002111 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002112}
2113
2114void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2115 if (comp->have_star_arg) {
2116 printf("SyntaxError?: can't have multiple *x\n");
2117 return;
2118 }
2119 comp->have_star_arg = true;
2120 compile_node(comp, pns->nodes[0]);
2121}
2122
2123void compile_arglist_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2124 if (comp->have_dbl_star_arg) {
2125 printf("SyntaxError?: can't have multiple **x\n");
2126 return;
2127 }
2128 comp->have_dbl_star_arg = true;
2129 compile_node(comp, pns->nodes[0]);
2130}
2131
2132void compile_argument(compiler_t *comp, py_parse_node_struct_t *pns) {
2133 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2134 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
2135 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2136 if (!PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2137 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2138 return;
2139 }
2140 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
2141 compile_node(comp, pns2->nodes[0]);
2142 comp->n_arg_keyword += 1;
2143 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
2144 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2145 } else {
2146 // shouldn't happen
2147 assert(0);
2148 }
2149}
2150
2151void compile_yield_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
2152 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2153 printf("SyntaxError: 'yield' outside function\n");
2154 return;
2155 }
2156 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2157 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2158 EMIT(yield_value);
2159 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2160 pns = (py_parse_node_struct_t*)pns->nodes[0];
2161 compile_node(comp, pns->nodes[0]);
2162 EMIT(get_iter);
2163 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2164 EMIT(yield_from);
2165 } else {
2166 compile_node(comp, pns->nodes[0]);
2167 EMIT(yield_value);
2168 }
2169}
2170
2171typedef void (*compile_function_t)(compiler_t*, py_parse_node_struct_t*);
2172static compile_function_t compile_function[] = {
2173 NULL,
2174#define nc NULL
2175#define c(f) compile_##f
2176#define DEF_RULE(rule, comp, kind, arg...) comp,
2177#include "grammar.h"
2178#undef nc
2179#undef c
2180#undef DEF_RULE
2181};
2182
2183void compile_node(compiler_t *comp, py_parse_node_t pn) {
2184 if (PY_PARSE_NODE_IS_NULL(pn)) {
2185 // pass
2186 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
2187 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
2188 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
Damien4b03e772013-10-05 14:17:09 +01002189 case PY_PARSE_NODE_ID: EMIT(load_id, arg); break;
Damien429d7192013-10-04 19:53:11 +01002190 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2191 case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2192 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2193 case PY_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2194 case PY_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
2195 case PY_PARSE_NODE_TOKEN: EMIT(load_const_tok, arg); break;
2196 default: assert(0);
2197 }
2198 } else {
2199 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2200 compile_function_t f = compile_function[PY_PARSE_NODE_STRUCT_KIND(pns)];
2201 if (f == NULL) {
2202 printf("node %u cannot be compiled\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
2203 parse_node_show(pn, 0);
2204 assert(0);
2205 } else {
2206 f(comp, pns);
2207 }
2208 }
2209}
2210
2211void 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) {
2212 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002213 qstr param_name = 0;
2214 py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL;
Damienb14de212013-10-06 00:28:28 +01002215 if (PY_PARSE_NODE_IS_ID(pn)) {
2216 param_name = PY_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002217 if (comp->have_bare_star) {
2218 // comes after a bare star, so doesn't count as a parameter
2219 } else {
2220 comp->scope_cur->num_params += 1;
2221 }
Damienb14de212013-10-06 00:28:28 +01002222 } else {
2223 assert(PY_PARSE_NODE_IS_STRUCT(pn));
2224 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2225 if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
Damien429d7192013-10-04 19:53:11 +01002226 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002227 //int node_index = 1; unused
2228 if (allow_annotations) {
2229 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2230 // this parameter has an annotation
2231 pn_annotation = pns->nodes[1];
2232 }
2233 //node_index = 2; unused
2234 }
2235 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
2236 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
2237 // this parameter has a default value
2238 if (comp->have_bare_star) {
2239 comp->scope_cur->num_dict_params += 1;
2240 } else {
2241 comp->scope_cur->num_default_params += 1;
2242 }
2243 }
2244 */
2245 if (comp->have_bare_star) {
2246 // comes after a bare star, so doesn't count as a parameter
2247 } else {
2248 comp->scope_cur->num_params += 1;
2249 }
2250 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2251 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2252 // bare star
2253 // TODO see http://www.python.org/dev/peps/pep-3102/
2254 comp->have_bare_star = true;
2255 //assert(comp->scope_cur->num_dict_params == 0);
2256 } else if (PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2257 // named star
2258 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2259 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2260 } else if (allow_annotations && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2261 // named star with annotation
2262 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2263 pns = (py_parse_node_struct_t*)pns->nodes[0];
2264 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2265 pn_annotation = pns->nodes[1];
2266 } else {
2267 // shouldn't happen
2268 assert(0);
2269 }
2270 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
Damien429d7192013-10-04 19:53:11 +01002271 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002272 if (allow_annotations && !PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2273 // this parameter has an annotation
2274 pn_annotation = pns->nodes[1];
2275 }
2276 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002277 } else {
Damienb14de212013-10-06 00:28:28 +01002278 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002279 assert(0);
2280 }
Damien429d7192013-10-04 19:53:11 +01002281 }
2282
2283 if (param_name != 0) {
2284 if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) {
2285 // TODO this parameter has an annotation
2286 }
2287 bool added;
2288 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2289 if (!added) {
2290 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2291 return;
2292 }
2293 id_info->param = true;
2294 id_info->kind = ID_INFO_KIND_LOCAL;
2295 }
2296}
2297
2298void compile_scope_func_param(compiler_t *comp, py_parse_node_t pn) {
2299 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2300}
2301
2302void compile_scope_lambda_param(compiler_t *comp, py_parse_node_t pn) {
2303 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2304}
2305
2306void 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) {
2307 tail_recursion:
2308 if (PY_PARSE_NODE_IS_NULL(pn_iter)) {
2309 // no more nested if/for; compile inner expression
2310 compile_node(comp, pn_inner_expr);
2311 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2312 EMIT(list_append, for_depth + 2);
2313 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2314 EMIT(map_add, for_depth + 2);
2315 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2316 EMIT(set_add, for_depth + 2);
2317 } else {
2318 EMIT(yield_value);
2319 EMIT(pop_top);
2320 }
2321 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
2322 // if condition
2323 py_parse_node_struct_t *pns_comp_if = (py_parse_node_struct_t*)pn_iter;
2324 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2325 pn_iter = pns_comp_if->nodes[1];
2326 goto tail_recursion;
2327 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
2328 // for loop
2329 py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter;
2330 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002331 int l_end2 = comp_next_label(comp);
2332 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002333 EMIT(get_iter);
2334 EMIT(label_assign, l_top2);
2335 EMIT(for_iter, l_end2);
2336 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2337 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2338 EMIT(jump, l_top2);
2339 EMIT(label_assign, l_end2);
2340 EMIT(for_iter_end);
2341 } else {
2342 // shouldn't happen
2343 assert(0);
2344 }
2345}
2346
2347void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
2348 // see http://www.python.org/dev/peps/pep-0257/
2349
2350 // look for the first statement
2351 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2352 // fall through
2353 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
2354 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2355 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
2356 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2357 } else {
2358 return;
2359 }
2360
2361 // check the first statement for a doc string
2362 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2363 py_parse_node_struct_t* pns = (py_parse_node_struct_t*)pn;
2364 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2365 int kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2366 if (kind == PY_PARSE_NODE_STRING) {
2367 compile_node(comp, pns->nodes[0]); // a doc string
2368 // store doc string
Damien4b03e772013-10-05 14:17:09 +01002369 EMIT(store_id, comp->qstr___doc__);
Damien429d7192013-10-04 19:53:11 +01002370 }
2371 }
2372 }
2373}
2374
2375void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2376 comp->pass = pass;
2377 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002378 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002379 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002380
2381 if (comp->pass == PASS_1) {
2382 scope->stack_size = 0;
2383 }
2384
2385 if (comp->pass == PASS_3) {
2386 //printf("----\n");
2387 scope_print_info(scope);
2388 }
2389
2390 // compile
2391 if (scope->kind == SCOPE_MODULE) {
2392 check_for_doc_string(comp, scope->pn);
2393 compile_node(comp, scope->pn);
2394 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2395 EMIT(return_value);
2396 } else if (scope->kind == SCOPE_FUNCTION) {
2397 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2398 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2399 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
2400
2401 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002402 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002403 if (comp->pass == PASS_1) {
2404 comp->have_bare_star = false;
2405 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2406 }
2407
Damien826005c2013-10-05 23:17:28 +01002408 assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002409
2410 compile_node(comp, pns->nodes[3]); // 3 is function body
2411 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002412 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01002413 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2414 EMIT(return_value);
2415 }
2416 } else if (scope->kind == SCOPE_LAMBDA) {
2417 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2418 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2419 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
2420
2421 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002422 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002423 if (comp->pass == PASS_1) {
2424 comp->have_bare_star = false;
2425 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2426 }
2427
2428 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2429 EMIT(return_value);
2430 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2431 // a bit of a hack at the moment
2432
2433 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2434 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2435 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2436 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2437 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
2438
Damien6cdd3af2013-10-05 18:08:26 +01002439 qstr qstr_arg = qstr_from_str_static(".0");
Damien429d7192013-10-04 19:53:11 +01002440 if (comp->pass == PASS_1) {
2441 bool added;
2442 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2443 assert(added);
2444 id_info->kind = ID_INFO_KIND_LOCAL;
2445 scope->num_params = 1;
2446 }
2447
2448 if (scope->kind == SCOPE_LIST_COMP) {
2449 EMIT(build_list, 0);
2450 } else if (scope->kind == SCOPE_DICT_COMP) {
2451 EMIT(build_map, 0);
2452 } else if (scope->kind == SCOPE_SET_COMP) {
2453 EMIT(build_set, 0);
2454 }
2455
Damienb05d7072013-10-05 13:37:10 +01002456 int l_end = comp_next_label(comp);
2457 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002458 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002459 EMIT(label_assign, l_top);
2460 EMIT(for_iter, l_end);
2461 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2462 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2463 EMIT(jump, l_top);
2464 EMIT(label_assign, l_end);
2465 EMIT(for_iter_end);
2466
2467 if (scope->kind == SCOPE_GEN_EXPR) {
2468 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2469 }
2470 EMIT(return_value);
2471 } else {
2472 assert(scope->kind == SCOPE_CLASS);
2473 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2474 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2475 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
2476
2477 if (comp->pass == PASS_1) {
2478 bool added;
2479 id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added);
2480 assert(added);
2481 id_info->kind = ID_INFO_KIND_LOCAL;
2482 id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added);
2483 assert(added);
2484 id_info->kind = ID_INFO_KIND_LOCAL;
2485 id_info->param = true;
2486 scope->num_params = 1; // __locals__ is the parameter
2487 }
2488
Damien4b03e772013-10-05 14:17:09 +01002489 EMIT(load_id, comp->qstr___locals__);
Damien429d7192013-10-04 19:53:11 +01002490 EMIT(store_locals);
Damien4b03e772013-10-05 14:17:09 +01002491 EMIT(load_id, comp->qstr___name__);
2492 EMIT(store_id, comp->qstr___module__);
Damien429d7192013-10-04 19:53:11 +01002493 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien4b03e772013-10-05 14:17:09 +01002494 EMIT(store_id, comp->qstr___qualname__);
Damien429d7192013-10-04 19:53:11 +01002495
2496 check_for_doc_string(comp, pns->nodes[2]);
2497 compile_node(comp, pns->nodes[2]); // 2 is class body
2498
2499 id_info_t *id = scope_find(scope, comp->qstr___class__);
2500 assert(id != NULL);
2501 if (id->kind == ID_INFO_KIND_LOCAL) {
2502 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2503 } else {
2504 EMIT(load_closure, comp->qstr___class__);
2505 }
2506 EMIT(return_value);
2507 }
2508
Damien415eb6f2013-10-05 12:19:06 +01002509 EMIT(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002510
Damien826005c2013-10-05 23:17:28 +01002511}
2512
2513void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2514 comp->pass = pass;
2515 comp->scope_cur = scope;
2516 comp->next_label = 1;
2517
2518 if (scope->kind != SCOPE_FUNCTION) {
2519 printf("Error: inline assembler must be a function\n");
2520 return;
2521 }
2522
Damiena2f2f7d2013-10-06 00:14:13 +01002523 if (comp->pass > PASS_1) {
2524 EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur);
2525 }
2526
Damien826005c2013-10-05 23:17:28 +01002527 // get the function definition parse node
2528 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2529 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2530 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
2531
Damiena2f2f7d2013-10-06 00:14:13 +01002532 //qstr f_id = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002533
Damiena2f2f7d2013-10-06 00:14:13 +01002534 // parameters are in pns->nodes[1]
2535 if (comp->pass == PASS_2) {
2536 py_parse_node_t *pn_params;
2537 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
2538 scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params);
2539 }
2540
Damien826005c2013-10-05 23:17:28 +01002541 assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
2542
2543 py_parse_node_t pn_body = pns->nodes[3]; // body
2544 py_parse_node_t *nodes;
2545 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2546
Damien826005c2013-10-05 23:17:28 +01002547 if (comp->pass == PASS_3) {
2548 //printf("----\n");
2549 scope_print_info(scope);
2550 }
2551
2552 for (int i = 0; i < num; i++) {
2553 assert(PY_PARSE_NODE_IS_STRUCT(nodes[i]));
2554 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)nodes[i];
2555 assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2556 assert(PY_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2557 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2558 pns2 = (py_parse_node_struct_t*)pns2->nodes[0];
2559 assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2560 assert(PY_PARSE_NODE_IS_ID(pns2->nodes[0]));
2561 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2562 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2563 qstr op = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2564 pns2 = (py_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2565 py_parse_node_t *pn_arg;
2566 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2567
2568 // emit instructions
2569 if (strcmp(qstr_str(op), "label") == 0) {
2570 if (!(n_args == 1 && PY_PARSE_NODE_IS_ID(pn_arg[0]))) {
2571 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2572 return;
2573 }
2574 int lab = comp_next_label(comp);
2575 if (pass > PASS_1) {
2576 EMIT_INLINE_ASM(label, lab, PY_PARSE_NODE_LEAF_ARG(pn_arg[0]));
2577 }
2578 } else {
2579 if (pass > PASS_1) {
2580 EMIT_INLINE_ASM(op, op, n_args, pn_arg);
2581 }
2582 }
2583 }
2584
2585 if (comp->pass > PASS_1) {
2586 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002587 }
Damien429d7192013-10-04 19:53:11 +01002588}
2589
2590void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2591 // in functions, turn implicit globals into explicit globals
2592 // compute num_locals, and the index of each local
2593 scope->num_locals = 0;
2594 for (int i = 0; i < scope->id_info_len; i++) {
2595 id_info_t *id = &scope->id_info[i];
2596 if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) {
2597 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2598 continue;
2599 }
2600 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2601 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2602 }
2603 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2604 id->local_num = scope->num_locals;
2605 scope->num_locals += 1;
2606 }
2607 }
2608
2609 // compute flags
2610 //scope->flags = 0; since we set some things in parameters
2611 if (scope->kind != SCOPE_MODULE) {
2612 scope->flags |= SCOPE_FLAG_NEWLOCALS;
2613 }
2614 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) {
2615 assert(scope->parent != NULL);
2616 scope->flags |= SCOPE_FLAG_OPTIMISED;
2617
2618 // TODO possibly other ways it can be nested
2619 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
2620 scope->flags |= SCOPE_FLAG_NESTED;
2621 }
2622 }
2623 int num_free = 0;
2624 for (int i = 0; i < scope->id_info_len; i++) {
2625 id_info_t *id = &scope->id_info[i];
2626 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2627 num_free += 1;
2628 }
2629 }
2630 if (num_free == 0) {
2631 scope->flags |= SCOPE_FLAG_NOFREE;
2632 }
2633}
2634
2635void py_compile(py_parse_node_t pn) {
2636 compiler_t *comp = m_new(compiler_t, 1);
2637
Damien6cdd3af2013-10-05 18:08:26 +01002638 comp->qstr___class__ = qstr_from_str_static("__class__");
2639 comp->qstr___locals__ = qstr_from_str_static("__locals__");
2640 comp->qstr___name__ = qstr_from_str_static("__name__");
2641 comp->qstr___module__ = qstr_from_str_static("__module__");
2642 comp->qstr___qualname__ = qstr_from_str_static("__qualname__");
2643 comp->qstr___doc__ = qstr_from_str_static("__doc__");
2644 comp->qstr_assertion_error = qstr_from_str_static("AssertionError");
2645 comp->qstr_micropython = qstr_from_str_static("micropython");
2646 comp->qstr_native = qstr_from_str_static("native");
Damien7af3d192013-10-07 00:02:49 +01002647 comp->qstr_viper = qstr_from_str_static("viper");
Damien5bfb7592013-10-05 18:41:24 +01002648 comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb");
Damien429d7192013-10-04 19:53:11 +01002649
2650 comp->break_label = 0;
2651 comp->continue_label = 0;
2652 comp->except_nest_level = 0;
2653 comp->scope_head = NULL;
2654 comp->scope_cur = NULL;
2655
Damien826005c2013-10-05 23:17:28 +01002656 // optimise constants
Damien429d7192013-10-04 19:53:11 +01002657 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01002658
2659 // set the outer scope
Damien6cdd3af2013-10-05 18:08:26 +01002660 scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01002661
Damien826005c2013-10-05 23:17:28 +01002662 // compile pass 1
2663 comp->emit = emit_pass1_new(comp->qstr___class__);
2664 comp->emit_method_table = &emit_pass1_method_table;
2665 comp->emit_inline_asm = NULL;
2666 comp->emit_inline_asm_method_table = NULL;
2667 uint max_num_labels = 0;
Damien429d7192013-10-04 19:53:11 +01002668 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
Damien826005c2013-10-05 23:17:28 +01002669 if (s->emit_options == EMIT_OPT_ASM_THUMB) {
2670 compile_scope_inline_asm(comp, s, PASS_1);
2671 } else {
2672 compile_scope(comp, s, PASS_1);
2673 }
2674
2675 // update maximim number of labels needed
2676 if (comp->next_label > max_num_labels) {
2677 max_num_labels = comp->next_label;
2678 }
Damien429d7192013-10-04 19:53:11 +01002679 }
2680
Damien826005c2013-10-05 23:17:28 +01002681 // compute some things related to scope and identifiers
Damien429d7192013-10-04 19:53:11 +01002682 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
2683 compile_scope_compute_things(comp, s);
2684 }
2685
Damien826005c2013-10-05 23:17:28 +01002686 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01002687 emit_pass1_free(comp->emit);
2688
Damien826005c2013-10-05 23:17:28 +01002689 // compile pass 2 and 3
Damien6cdd3af2013-10-05 18:08:26 +01002690 emit_t *emit_bc = NULL;
Damiendc833822013-10-06 01:01:01 +01002691 emit_t *emit_native = NULL;
Damien7af3d192013-10-07 00:02:49 +01002692 emit_t *emit_viper = NULL;
Damien826005c2013-10-05 23:17:28 +01002693 emit_inline_asm_t *emit_inline_thumb = NULL;
Damien429d7192013-10-04 19:53:11 +01002694 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
Damien826005c2013-10-05 23:17:28 +01002695 if (s->emit_options == EMIT_OPT_ASM_THUMB) {
2696 if (emit_inline_thumb == NULL) {
2697 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
2698 }
2699 comp->emit = NULL;
2700 comp->emit_method_table = NULL;
2701 comp->emit_inline_asm = emit_inline_thumb;
2702 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
2703 compile_scope_inline_asm(comp, s, PASS_2);
2704 compile_scope_inline_asm(comp, s, PASS_3);
2705 } else {
2706 switch (s->emit_options) {
2707 case EMIT_OPT_NATIVE_PYTHON:
Damiendc833822013-10-06 01:01:01 +01002708 if (emit_native == NULL) {
2709 emit_native = emit_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01002710 }
Damiendc833822013-10-06 01:01:01 +01002711 comp->emit = emit_native;
Damien826005c2013-10-05 23:17:28 +01002712 comp->emit_method_table = &emit_x64_method_table;
2713 break;
Damien6cdd3af2013-10-05 18:08:26 +01002714
Damien7af3d192013-10-07 00:02:49 +01002715 case EMIT_OPT_VIPER:
2716 if (emit_viper == NULL) {
2717 emit_viper = emit_viper_x64_new(max_num_labels);
2718 }
2719 comp->emit = emit_viper;
2720 comp->emit_method_table = &emit_viper_x64_method_table;
2721 break;
2722
Damien826005c2013-10-05 23:17:28 +01002723 default:
2724 if (emit_bc == NULL) {
2725 emit_bc = emit_bc_new(max_num_labels);
2726 }
2727 comp->emit = emit_bc;
2728 comp->emit_method_table = &emit_bc_method_table;
2729 break;
2730 }
Damien7af3d192013-10-07 00:02:49 +01002731 //comp->emit = emit_cpython_new(max_num_labels);
2732 //comp->emit_method_table = &emit_cpython_method_table;
Damien826005c2013-10-05 23:17:28 +01002733 compile_scope(comp, s, PASS_2);
2734 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01002735 }
Damien429d7192013-10-04 19:53:11 +01002736 }
2737
2738 m_free(comp);
2739}