Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #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" |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 9 | #include "mpyconfig.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 10 | #include "lexer.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 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 |
| 18 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 19 | #define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB) |
| 20 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 21 | typedef enum { |
| 22 | PN_none = 0, |
| 23 | #define DEF_RULE(rule, comp, kind, arg...) PN_##rule, |
| 24 | #include "grammar.h" |
| 25 | #undef DEF_RULE |
| 26 | PN_maximum_number_of, |
| 27 | } pn_kind_t; |
| 28 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 29 | #define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg)) |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 30 | #define EMIT_INLINE_ASM(fun, arg...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, ##arg)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 31 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 32 | #define EMIT_OPT_NONE (0) |
| 33 | #define EMIT_OPT_BYTE_CODE (1) |
| 34 | #define EMIT_OPT_NATIVE_PYTHON (2) |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 35 | #define EMIT_OPT_VIPER (3) |
| 36 | #define EMIT_OPT_ASM_THUMB (4) |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 37 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 38 | typedef struct _compiler_t { |
| 39 | qstr qstr___class__; |
| 40 | qstr qstr___locals__; |
| 41 | qstr qstr___name__; |
| 42 | qstr qstr___module__; |
| 43 | qstr qstr___qualname__; |
| 44 | qstr qstr___doc__; |
| 45 | qstr qstr_assertion_error; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 46 | qstr qstr_micropython; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 47 | qstr qstr_byte_code; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 48 | qstr qstr_native; |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 49 | qstr qstr_viper; |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame] | 50 | qstr qstr_asm_thumb; |
Damien | d793389 | 2013-11-28 19:12:18 +0000 | [diff] [blame] | 51 | qstr qstr_range; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 52 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 53 | bool is_repl; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 54 | pass_kind_t pass; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 55 | bool had_error; // try to keep compiler clean from nlr |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 56 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 57 | int next_label; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 58 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 59 | int break_label; |
| 60 | int continue_label; |
| 61 | int except_nest_level; |
| 62 | |
| 63 | int n_arg_keyword; |
| 64 | bool have_star_arg; |
| 65 | bool have_dbl_star_arg; |
| 66 | bool have_bare_star; |
| 67 | int param_pass; |
| 68 | int param_pass_num_dict_params; |
| 69 | int param_pass_num_default_params; |
| 70 | |
| 71 | scope_t *scope_head; |
| 72 | scope_t *scope_cur; |
| 73 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 74 | emit_t *emit; // current emitter |
| 75 | const emit_method_table_t *emit_method_table; // current emit method table |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 76 | |
| 77 | emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm |
| 78 | const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 79 | } compiler_t; |
| 80 | |
| 81 | py_parse_node_t fold_constants(py_parse_node_t pn) { |
| 82 | if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 83 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 84 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 85 | |
| 86 | // fold arguments first |
| 87 | for (int i = 0; i < n; i++) { |
| 88 | pns->nodes[i] = fold_constants(pns->nodes[i]); |
| 89 | } |
| 90 | |
| 91 | switch (PY_PARSE_NODE_STRUCT_KIND(pns)) { |
| 92 | case PN_shift_expr: |
| 93 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
| 94 | int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 95 | int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 96 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_LESS)) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 97 | #if MICROPY_EMIT_CPYTHON |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 98 | // can overflow; enabled only to compare with CPython |
| 99 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 << arg1); |
| 100 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 101 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_MORE)) { |
| 102 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 >> arg1); |
| 103 | } else { |
| 104 | // shouldn't happen |
| 105 | assert(0); |
| 106 | } |
| 107 | } |
| 108 | break; |
| 109 | |
| 110 | case PN_arith_expr: |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 111 | // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 112 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 113 | machine_int_t arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 114 | machine_int_t arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 115 | machine_int_t res; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 116 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PLUS)) { |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 117 | res = arg0 + arg1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 118 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_MINUS)) { |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 119 | res = arg0 - arg1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 120 | } else { |
| 121 | // shouldn't happen |
| 122 | assert(0); |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 123 | res = 0; |
| 124 | } |
| 125 | if (PY_FIT_SMALL_INT(res)) { |
| 126 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, res); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | break; |
| 130 | |
| 131 | case PN_term: |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 132 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
| 133 | int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 134 | int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 135 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 136 | #if MICROPY_EMIT_CPYTHON |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 137 | // can overflow; enabled only to compare with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 138 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 * arg1); |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 139 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 140 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_SLASH)) { |
| 141 | ; // pass |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 142 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PERCENT)) { |
| 143 | // XXX implement this properly as Python's % operator acts differently to C's |
| 144 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 % arg1); |
| 145 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_SLASH)) { |
| 146 | // XXX implement this properly as Python's // operator acts differently to C's |
| 147 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 / arg1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 148 | } else { |
| 149 | // shouldn't happen |
| 150 | assert(0); |
| 151 | } |
| 152 | } |
| 153 | break; |
| 154 | |
| 155 | case PN_factor_2: |
| 156 | if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) { |
| 157 | machine_int_t arg = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 158 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) { |
| 159 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg); |
| 160 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) { |
| 161 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, -arg); |
| 162 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) { |
| 163 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ~arg); |
| 164 | } else { |
| 165 | // shouldn't happen |
| 166 | assert(0); |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 171 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 172 | case PN_power: |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 173 | // can overflow; enabled only to compare with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 174 | if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_NULL(pns->nodes[1]) && !PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 175 | py_parse_node_struct_t* pns2 = (py_parse_node_struct_t*)pns->nodes[2]; |
| 176 | if (PY_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) { |
| 177 | int power = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]); |
| 178 | if (power >= 0) { |
| 179 | int ans = 1; |
| 180 | int base = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 181 | for (; power > 0; power--) { |
| 182 | ans *= base; |
| 183 | } |
| 184 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ans); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | break; |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 189 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | return pn; |
| 194 | } |
| 195 | |
| 196 | void compile_node(compiler_t *comp, py_parse_node_t pn); |
| 197 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 198 | static int comp_next_label(compiler_t *comp) { |
| 199 | return comp->next_label++; |
| 200 | } |
| 201 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 202 | static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn, uint emit_options) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 203 | scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(kind == SCOPE_MODULE), emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 204 | scope->parent = comp->scope_cur; |
| 205 | scope->next = NULL; |
| 206 | if (comp->scope_head == NULL) { |
| 207 | comp->scope_head = scope; |
| 208 | } else { |
| 209 | scope_t *s = comp->scope_head; |
| 210 | while (s->next != NULL) { |
| 211 | s = s->next; |
| 212 | } |
| 213 | s->next = scope; |
| 214 | } |
| 215 | return scope; |
| 216 | } |
| 217 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 218 | static int list_len(py_parse_node_t pn, int pn_kind) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 219 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 220 | return 0; |
| 221 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 222 | return 1; |
| 223 | } else { |
| 224 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 225 | if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) { |
| 226 | return 1; |
| 227 | } else { |
| 228 | return PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 233 | static 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)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 234 | if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) { |
| 235 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 236 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 237 | for (int i = 0; i < num_nodes; i++) { |
| 238 | f(comp, pns->nodes[i]); |
| 239 | } |
| 240 | } else if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 241 | f(comp, pn); |
| 242 | } |
| 243 | } |
| 244 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 245 | static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 246 | if (PY_PARSE_NODE_IS_NULL(*pn)) { |
| 247 | *nodes = NULL; |
| 248 | return 0; |
| 249 | } else if (PY_PARSE_NODE_IS_LEAF(*pn)) { |
| 250 | *nodes = pn; |
| 251 | return 1; |
| 252 | } else { |
| 253 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)(*pn); |
| 254 | if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) { |
| 255 | *nodes = pn; |
| 256 | return 1; |
| 257 | } else { |
| 258 | *nodes = pns->nodes; |
| 259 | return PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void compile_do_nothing(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 265 | } |
| 266 | |
| 267 | void compile_generic_all_nodes(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 268 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 269 | for (int i = 0; i < num_nodes; i++) { |
| 270 | compile_node(comp, pns->nodes[i]); |
| 271 | } |
| 272 | } |
| 273 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 274 | #if MICROPY_EMIT_CPYTHON |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 275 | static bool cpython_c_tuple_is_const(py_parse_node_t pn) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 276 | if (!PY_PARSE_NODE_IS_LEAF(pn)) { |
| 277 | return false; |
| 278 | } |
| 279 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 280 | return false; |
| 281 | } |
| 282 | return true; |
| 283 | } |
| 284 | |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 285 | static void cpython_c_tuple_emit_const(compiler_t *comp, py_parse_node_t pn) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 286 | assert(PY_PARSE_NODE_IS_LEAF(pn)); |
| 287 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 288 | switch (PY_PARSE_NODE_LEAF_KIND(pn)) { |
| 289 | case PY_PARSE_NODE_ID: assert(0); |
| 290 | case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_verbatim_int, arg); break; |
| 291 | case PY_PARSE_NODE_INTEGER: EMIT(load_const_verbatim_str, qstr_str(arg)); break; |
| 292 | case PY_PARSE_NODE_DECIMAL: EMIT(load_const_verbatim_str, qstr_str(arg)); break; |
| 293 | case PY_PARSE_NODE_STRING: EMIT(load_const_verbatim_quoted_str, arg, false); break; |
| 294 | case PY_PARSE_NODE_BYTES: EMIT(load_const_verbatim_quoted_str, arg, true); break; |
| 295 | case PY_PARSE_NODE_TOKEN: |
| 296 | switch (arg) { |
| 297 | case PY_TOKEN_KW_FALSE: EMIT(load_const_verbatim_str, "False"); break; |
| 298 | case PY_TOKEN_KW_NONE: EMIT(load_const_verbatim_str, "None"); break; |
| 299 | case PY_TOKEN_KW_TRUE: EMIT(load_const_verbatim_str, "True"); break; |
| 300 | default: assert(0); |
| 301 | } |
| 302 | break; |
| 303 | default: assert(0); |
| 304 | } |
| 305 | } |
| 306 | |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 307 | static void cpython_c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 308 | int n = 0; |
| 309 | if (pns_list != NULL) { |
| 310 | n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list); |
| 311 | } |
| 312 | int total = n; |
| 313 | bool is_const = true; |
| 314 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 315 | total += 1; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 316 | if (!cpython_c_tuple_is_const(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 317 | is_const = false; |
| 318 | } |
| 319 | } |
| 320 | for (int i = 0; i < n; i++) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 321 | if (!cpython_c_tuple_is_const(pns_list->nodes[i])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 322 | is_const = false; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | if (total > 0 && is_const) { |
| 327 | bool need_comma = false; |
| 328 | EMIT(load_const_verbatim_start); |
| 329 | EMIT(load_const_verbatim_str, "("); |
| 330 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 331 | cpython_c_tuple_emit_const(comp, pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 332 | need_comma = true; |
| 333 | } |
| 334 | for (int i = 0; i < n; i++) { |
| 335 | if (need_comma) { |
| 336 | EMIT(load_const_verbatim_str, ", "); |
| 337 | } |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 338 | cpython_c_tuple_emit_const(comp, pns_list->nodes[i]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 339 | need_comma = true; |
| 340 | } |
| 341 | if (total == 1) { |
| 342 | EMIT(load_const_verbatim_str, ",)"); |
| 343 | } else { |
| 344 | EMIT(load_const_verbatim_str, ")"); |
| 345 | } |
| 346 | EMIT(load_const_verbatim_end); |
| 347 | } else { |
| 348 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 349 | compile_node(comp, pn); |
| 350 | } |
| 351 | for (int i = 0; i < n; i++) { |
| 352 | compile_node(comp, pns_list->nodes[i]); |
| 353 | } |
| 354 | EMIT(build_tuple, total); |
| 355 | } |
| 356 | } |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 357 | #endif |
| 358 | |
| 359 | // funnelling all tuple creations through this function is purely so we can optionally agree with CPython |
| 360 | void c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 361 | #if MICROPY_EMIT_CPYTHON |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 362 | cpython_c_tuple(comp, pn, pns_list); |
| 363 | #else |
| 364 | int total = 0; |
| 365 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 366 | compile_node(comp, pn); |
| 367 | total += 1; |
| 368 | } |
| 369 | if (pns_list != NULL) { |
| 370 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list); |
| 371 | for (int i = 0; i < n; i++) { |
| 372 | compile_node(comp, pns_list->nodes[i]); |
| 373 | } |
| 374 | total += n; |
| 375 | } |
| 376 | EMIT(build_tuple, total); |
| 377 | #endif |
| 378 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 379 | |
| 380 | void compile_generic_tuple(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 381 | // a simple tuple expression |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 382 | c_tuple(comp, PY_PARSE_NODE_NULL, pns); |
| 383 | } |
| 384 | |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 385 | static bool node_is_const_false(py_parse_node_t pn) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 386 | return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_FALSE); |
| 387 | // untested: || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1); |
| 388 | } |
| 389 | |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 390 | static bool node_is_const_true(py_parse_node_t pn) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 391 | return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_TRUE) || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1); |
| 392 | } |
| 393 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 394 | #if MICROPY_EMIT_CPYTHON |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 395 | // the is_nested variable is purely to match with CPython, which doesn't fully optimise not's |
| 396 | static void cpython_c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, bool is_nested) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 397 | if (node_is_const_false(pn)) { |
| 398 | if (jump_if == false) { |
| 399 | EMIT(jump, label); |
| 400 | } |
| 401 | return; |
| 402 | } else if (node_is_const_true(pn)) { |
| 403 | if (jump_if == true) { |
| 404 | EMIT(jump, label); |
| 405 | } |
| 406 | return; |
| 407 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 408 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 409 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 410 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { |
| 411 | if (jump_if == false) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 412 | int label2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 413 | for (int i = 0; i < n - 1; i++) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 414 | cpython_c_if_cond(comp, pns->nodes[i], true, label2, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 415 | } |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 416 | cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 417 | EMIT(label_assign, label2); |
| 418 | } else { |
| 419 | for (int i = 0; i < n; i++) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 420 | cpython_c_if_cond(comp, pns->nodes[i], true, label, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | return; |
| 424 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) { |
| 425 | if (jump_if == false) { |
| 426 | for (int i = 0; i < n; i++) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 427 | cpython_c_if_cond(comp, pns->nodes[i], false, label, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 428 | } |
| 429 | } else { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 430 | int label2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 431 | for (int i = 0; i < n - 1; i++) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 432 | cpython_c_if_cond(comp, pns->nodes[i], false, label2, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 433 | } |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 434 | cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 435 | EMIT(label_assign, label2); |
| 436 | } |
| 437 | return; |
| 438 | } else if (!is_nested && PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 439 | cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // nothing special, fall back to default compiling for node and jump |
| 445 | compile_node(comp, pn); |
| 446 | if (jump_if == false) { |
| 447 | EMIT(pop_jump_if_false, label); |
| 448 | } else { |
| 449 | EMIT(pop_jump_if_true, label); |
| 450 | } |
| 451 | } |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 452 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 453 | |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 454 | static void c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 455 | #if MICROPY_EMIT_CPYTHON |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 456 | cpython_c_if_cond(comp, pn, jump_if, label, false); |
| 457 | #else |
| 458 | if (node_is_const_false(pn)) { |
| 459 | if (jump_if == false) { |
| 460 | EMIT(jump, label); |
| 461 | } |
| 462 | return; |
| 463 | } else if (node_is_const_true(pn)) { |
| 464 | if (jump_if == true) { |
| 465 | EMIT(jump, label); |
| 466 | } |
| 467 | return; |
| 468 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 469 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 470 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 471 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { |
| 472 | if (jump_if == false) { |
| 473 | int label2 = comp_next_label(comp); |
| 474 | for (int i = 0; i < n - 1; i++) { |
| 475 | c_if_cond(comp, pns->nodes[i], true, label2); |
| 476 | } |
| 477 | c_if_cond(comp, pns->nodes[n - 1], false, label); |
| 478 | EMIT(label_assign, label2); |
| 479 | } else { |
| 480 | for (int i = 0; i < n; i++) { |
| 481 | c_if_cond(comp, pns->nodes[i], true, label); |
| 482 | } |
| 483 | } |
| 484 | return; |
| 485 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) { |
| 486 | if (jump_if == false) { |
| 487 | for (int i = 0; i < n; i++) { |
| 488 | c_if_cond(comp, pns->nodes[i], false, label); |
| 489 | } |
| 490 | } else { |
| 491 | int label2 = comp_next_label(comp); |
| 492 | for (int i = 0; i < n - 1; i++) { |
| 493 | c_if_cond(comp, pns->nodes[i], false, label2); |
| 494 | } |
| 495 | c_if_cond(comp, pns->nodes[n - 1], true, label); |
| 496 | EMIT(label_assign, label2); |
| 497 | } |
| 498 | return; |
| 499 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { |
| 500 | c_if_cond(comp, pns->nodes[0], !jump_if, label); |
| 501 | return; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // nothing special, fall back to default compiling for node and jump |
| 506 | compile_node(comp, pn); |
| 507 | if (jump_if == false) { |
| 508 | EMIT(pop_jump_if_false, label); |
| 509 | } else { |
| 510 | EMIT(pop_jump_if_true, label); |
| 511 | } |
| 512 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t; |
| 516 | void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t kind); |
| 517 | |
| 518 | void c_assign_power(compiler_t *comp, py_parse_node_struct_t *pns, assign_kind_t assign_kind) { |
| 519 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 520 | compile_node(comp, pns->nodes[0]); |
| 521 | } |
| 522 | |
| 523 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 524 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 525 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) { |
| 526 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 527 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 528 | for (int i = 0; i < n - 1; i++) { |
| 529 | compile_node(comp, pns1->nodes[i]); |
| 530 | } |
| 531 | } |
| 532 | assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 533 | pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1]; |
| 534 | } |
| 535 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { |
| 536 | printf("SyntaxError: can't assign to function call\n"); |
| 537 | return; |
| 538 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
| 539 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 540 | EMIT(rot_three); |
| 541 | EMIT(store_subscr); |
| 542 | } else { |
| 543 | compile_node(comp, pns1->nodes[0]); |
| 544 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 545 | EMIT(dup_top_two); |
| 546 | EMIT(binary_op, RT_BINARY_OP_SUBSCR); |
| 547 | } else { |
| 548 | EMIT(store_subscr); |
| 549 | } |
| 550 | } |
| 551 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 552 | assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0])); |
| 553 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 554 | EMIT(dup_top); |
| 555 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 556 | } else { |
| 557 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 558 | EMIT(rot_two); |
| 559 | } |
| 560 | EMIT(store_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 561 | } |
| 562 | } else { |
| 563 | // shouldn't happen |
| 564 | assert(0); |
| 565 | } |
| 566 | } else { |
| 567 | // shouldn't happen |
| 568 | assert(0); |
| 569 | } |
| 570 | |
| 571 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 572 | // SyntaxError, cannot assign |
| 573 | assert(0); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void c_assign_tuple(compiler_t *comp, int n, py_parse_node_t *nodes) { |
| 578 | assert(n >= 0); |
| 579 | int have_star_index = -1; |
| 580 | for (int i = 0; i < n; i++) { |
| 581 | if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) { |
| 582 | if (have_star_index < 0) { |
| 583 | EMIT(unpack_ex, i, n - i - 1); |
| 584 | have_star_index = i; |
| 585 | } else { |
| 586 | printf("SyntaxError: two starred expressions in assignment\n"); |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | if (have_star_index < 0) { |
| 592 | EMIT(unpack_sequence, n); |
| 593 | } |
| 594 | for (int i = 0; i < n; i++) { |
| 595 | if (i == have_star_index) { |
| 596 | c_assign(comp, ((py_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE); |
| 597 | } else { |
| 598 | c_assign(comp, nodes[i], ASSIGN_STORE); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // assigns top of stack to pn |
| 604 | void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) { |
| 605 | tail_recursion: |
| 606 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 607 | assert(0); |
| 608 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 609 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 610 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 611 | switch (assign_kind) { |
| 612 | case ASSIGN_STORE: |
| 613 | case ASSIGN_AUG_STORE: |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 614 | EMIT(store_id, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 615 | break; |
| 616 | case ASSIGN_AUG_LOAD: |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 617 | EMIT(load_id, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 618 | break; |
| 619 | } |
| 620 | } else { |
| 621 | printf("SyntaxError: can't assign to literal\n"); |
| 622 | return; |
| 623 | } |
| 624 | } else { |
| 625 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 626 | switch (PY_PARSE_NODE_STRUCT_KIND(pns)) { |
| 627 | case PN_power: |
| 628 | // lhs is an index or attribute |
| 629 | c_assign_power(comp, pns, assign_kind); |
| 630 | break; |
| 631 | |
| 632 | case PN_testlist_star_expr: |
| 633 | case PN_exprlist: |
| 634 | // lhs is a tuple |
| 635 | if (assign_kind != ASSIGN_STORE) { |
| 636 | goto bad_aug; |
| 637 | } |
| 638 | c_assign_tuple(comp, PY_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes); |
| 639 | break; |
| 640 | |
| 641 | case PN_atom_paren: |
| 642 | // lhs is something in parenthesis |
| 643 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 644 | // empty tuple |
| 645 | printf("SyntaxError: can't assign to ()\n"); |
| 646 | return; |
| 647 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 648 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 649 | goto testlist_comp; |
| 650 | } else { |
| 651 | // parenthesis around 1 item, is just that item |
| 652 | pn = pns->nodes[0]; |
| 653 | goto tail_recursion; |
| 654 | } |
| 655 | break; |
| 656 | |
| 657 | case PN_atom_bracket: |
| 658 | // lhs is something in brackets |
| 659 | if (assign_kind != ASSIGN_STORE) { |
| 660 | goto bad_aug; |
| 661 | } |
| 662 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 663 | // empty list, assignment allowed |
| 664 | c_assign_tuple(comp, 0, NULL); |
| 665 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 666 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 667 | goto testlist_comp; |
| 668 | } else { |
| 669 | // brackets around 1 item |
| 670 | c_assign_tuple(comp, 1, &pns->nodes[0]); |
| 671 | } |
| 672 | break; |
| 673 | |
| 674 | default: |
| 675 | printf("unknown assign, %u\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns)); |
| 676 | assert(0); |
| 677 | } |
| 678 | return; |
| 679 | |
| 680 | testlist_comp: |
| 681 | // lhs is a sequence |
| 682 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 683 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 684 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
| 685 | // sequence of one item, with trailing comma |
| 686 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
| 687 | c_assign_tuple(comp, 1, &pns->nodes[0]); |
| 688 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
| 689 | // sequence of many items |
| 690 | // TODO call c_assign_tuple instead |
| 691 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns2); |
| 692 | EMIT(unpack_sequence, 1 + n); |
| 693 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); |
| 694 | for (int i = 0; i < n; i++) { |
| 695 | c_assign(comp, pns2->nodes[i], ASSIGN_STORE); |
| 696 | } |
| 697 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
| 698 | // TODO not implemented |
| 699 | assert(0); |
| 700 | } else { |
| 701 | // sequence with 2 items |
| 702 | goto sequence_with_2_items; |
| 703 | } |
| 704 | } else { |
| 705 | // sequence with 2 items |
| 706 | sequence_with_2_items: |
| 707 | c_assign_tuple(comp, 2, pns->nodes); |
| 708 | } |
| 709 | return; |
| 710 | } |
| 711 | return; |
| 712 | |
| 713 | bad_aug: |
| 714 | printf("SyntaxError: illegal expression for augmented assignment\n"); |
| 715 | } |
| 716 | |
| 717 | // stuff for lambda and comprehensions and generators |
| 718 | void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) { |
| 719 | // make closed over variables, if any |
| 720 | int nfree = 0; |
| 721 | if (comp->scope_cur->kind != SCOPE_MODULE) { |
| 722 | for (int i = 0; i < this_scope->id_info_len; i++) { |
| 723 | id_info_t *id_info = &this_scope->id_info[i]; |
| 724 | if (id_info->kind == ID_INFO_KIND_FREE) { |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 725 | EMIT(load_closure, id_info->qstr, id_info->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 726 | nfree += 1; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | if (nfree > 0) { |
| 731 | EMIT(build_tuple, nfree); |
| 732 | } |
| 733 | |
| 734 | // make the function/closure |
| 735 | if (nfree == 0) { |
| 736 | EMIT(make_function, this_scope, n_dict_params, n_default_params); |
| 737 | } else { |
| 738 | EMIT(make_closure, this_scope, n_dict_params, n_default_params); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 743 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) { |
| 744 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 745 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 746 | // this parameter has a default value |
| 747 | // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why |
| 748 | if (comp->have_bare_star) { |
| 749 | comp->param_pass_num_dict_params += 1; |
| 750 | if (comp->param_pass == 1) { |
| 751 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 752 | compile_node(comp, pns->nodes[2]); |
| 753 | } |
| 754 | } else { |
| 755 | comp->param_pass_num_default_params += 1; |
| 756 | if (comp->param_pass == 2) { |
| 757 | compile_node(comp, pns->nodes[2]); |
| 758 | } |
| 759 | } |
| 760 | } |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 761 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) { |
| 762 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 763 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 764 | // bare star |
| 765 | comp->have_bare_star = true; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // leaves function object on stack |
| 771 | // returns function name |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 772 | qstr compile_funcdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 773 | if (comp->pass == PASS_1) { |
| 774 | // create a new scope for this function |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 775 | scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (py_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 776 | // store the function scope so the compiling function can use it at each pass |
| 777 | pns->nodes[4] = (py_parse_node_t)s; |
| 778 | } |
| 779 | |
| 780 | // save variables (probably don't need to do this, since we can't have nested definitions..?) |
| 781 | bool old_have_bare_star = comp->have_bare_star; |
| 782 | int old_param_pass = comp->param_pass; |
| 783 | int old_param_pass_num_dict_params = comp->param_pass_num_dict_params; |
| 784 | int old_param_pass_num_default_params = comp->param_pass_num_default_params; |
| 785 | |
| 786 | // compile default parameters |
| 787 | comp->have_bare_star = false; |
| 788 | comp->param_pass = 1; // pass 1 does any default parameters after bare star |
| 789 | comp->param_pass_num_dict_params = 0; |
| 790 | comp->param_pass_num_default_params = 0; |
| 791 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param); |
| 792 | comp->have_bare_star = false; |
| 793 | comp->param_pass = 2; // pass 2 does any default parameters before bare star |
| 794 | comp->param_pass_num_dict_params = 0; |
| 795 | comp->param_pass_num_default_params = 0; |
| 796 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param); |
| 797 | |
| 798 | // get the scope for this function |
| 799 | scope_t *fscope = (scope_t*)pns->nodes[4]; |
| 800 | |
| 801 | // make the function |
| 802 | close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params); |
| 803 | |
| 804 | // restore variables |
| 805 | comp->have_bare_star = old_have_bare_star; |
| 806 | comp->param_pass = old_param_pass; |
| 807 | comp->param_pass_num_dict_params = old_param_pass_num_dict_params; |
| 808 | comp->param_pass_num_default_params = old_param_pass_num_default_params; |
| 809 | |
| 810 | // return its name (the 'f' in "def f(...):") |
| 811 | return fscope->simple_name; |
| 812 | } |
| 813 | |
| 814 | // leaves class object on stack |
| 815 | // returns class name |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 816 | qstr compile_classdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 817 | if (comp->pass == PASS_1) { |
| 818 | // create a new scope for this class |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 819 | scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (py_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 820 | // store the class scope so the compiling function can use it at each pass |
| 821 | pns->nodes[3] = (py_parse_node_t)s; |
| 822 | } |
| 823 | |
| 824 | EMIT(load_build_class); |
| 825 | |
| 826 | // scope for this class |
| 827 | scope_t *cscope = (scope_t*)pns->nodes[3]; |
| 828 | |
| 829 | // compile the class |
| 830 | close_over_variables_etc(comp, cscope, 0, 0); |
| 831 | |
| 832 | // get its name |
| 833 | EMIT(load_const_id, cscope->simple_name); |
| 834 | |
| 835 | // nodes[1] has parent classes, if any |
| 836 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 837 | // no parent classes |
| 838 | EMIT(call_function, 2, 0, false, false); |
| 839 | } else { |
| 840 | // have a parent class or classes |
| 841 | // TODO what if we have, eg, *a or **a in the parent list? |
| 842 | compile_node(comp, pns->nodes[1]); |
| 843 | EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false); |
| 844 | } |
| 845 | |
| 846 | // return its name (the 'C' in class C(...):") |
| 847 | return cscope->simple_name; |
| 848 | } |
| 849 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 850 | // returns true if it was a built-in decorator (even if the built-in had an error) |
| 851 | static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_node_t *name_nodes, uint *emit_options) { |
| 852 | if (PY_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) { |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | if (name_len != 2) { |
| 857 | printf("SyntaxError: invalid micropython decorator\n"); |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 862 | if (attr == comp->qstr_byte_code) { |
| 863 | *emit_options = EMIT_OPT_BYTE_CODE; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 864 | #if MICROPY_EMIT_NATIVE |
| 865 | } else if (attr == comp->qstr_native) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 866 | *emit_options = EMIT_OPT_NATIVE_PYTHON; |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 867 | } else if (attr == comp->qstr_viper) { |
| 868 | *emit_options = EMIT_OPT_VIPER; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 869 | #endif |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 870 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame] | 871 | } else if (attr == comp->qstr_asm_thumb) { |
| 872 | *emit_options = EMIT_OPT_ASM_THUMB; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 873 | #endif |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 874 | } else { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 875 | printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr)); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | return true; |
| 879 | } |
| 880 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 881 | void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 882 | // get the list of decorators |
| 883 | py_parse_node_t *nodes; |
| 884 | int n = list_get(&pns->nodes[0], PN_decorators, &nodes); |
| 885 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 886 | // inherit emit options for this function/class definition |
| 887 | uint emit_options = comp->scope_cur->emit_options; |
| 888 | |
| 889 | // compile each decorator |
| 890 | int num_built_in_decorators = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 891 | for (int i = 0; i < n; i++) { |
| 892 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be |
| 893 | py_parse_node_struct_t *pns_decorator = (py_parse_node_struct_t*)nodes[i]; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 894 | |
| 895 | // nodes[0] contains the decorator function, which is a dotted name |
| 896 | py_parse_node_t *name_nodes; |
| 897 | int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes); |
| 898 | |
| 899 | // check for built-in decorators |
| 900 | if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) { |
| 901 | // this was a built-in |
| 902 | num_built_in_decorators += 1; |
| 903 | |
| 904 | } else { |
| 905 | // not a built-in, compile normally |
| 906 | |
| 907 | // compile the decorator function |
| 908 | compile_node(comp, name_nodes[0]); |
| 909 | for (int i = 1; i < name_len; i++) { |
| 910 | assert(PY_PARSE_NODE_IS_ID(name_nodes[i])); // should be |
| 911 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(name_nodes[i])); |
| 912 | } |
| 913 | |
| 914 | // nodes[1] contains arguments to the decorator function, if any |
| 915 | if (!PY_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) { |
| 916 | // call the decorator function with the arguments in nodes[1] |
| 917 | compile_node(comp, pns_decorator->nodes[1]); |
| 918 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | // compile the body (funcdef or classdef) and get its name |
| 923 | py_parse_node_struct_t *pns_body = (py_parse_node_struct_t*)pns->nodes[1]; |
| 924 | qstr body_name = 0; |
| 925 | if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 926 | body_name = compile_funcdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 927 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 928 | body_name = compile_classdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 929 | } else { |
| 930 | // shouldn't happen |
| 931 | assert(0); |
| 932 | } |
| 933 | |
| 934 | // call each decorator |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 935 | for (int i = 0; i < n - num_built_in_decorators; i++) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 936 | EMIT(call_function, 1, 0, false, false); |
| 937 | } |
| 938 | |
| 939 | // store func/class object into name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 940 | EMIT(store_id, body_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 944 | qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 945 | // store function object into function name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 946 | EMIT(store_id, fname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | void c_del_stmt(compiler_t *comp, py_parse_node_t pn) { |
| 950 | if (PY_PARSE_NODE_IS_ID(pn)) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 951 | EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 952 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) { |
| 953 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 954 | |
| 955 | compile_node(comp, pns->nodes[0]); // base of the power node |
| 956 | |
| 957 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 958 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 959 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) { |
| 960 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 961 | for (int i = 0; i < n - 1; i++) { |
| 962 | compile_node(comp, pns1->nodes[i]); |
| 963 | } |
| 964 | assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 965 | pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1]; |
| 966 | } |
| 967 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { |
| 968 | // SyntaxError: can't delete a function call |
| 969 | assert(0); |
| 970 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
| 971 | compile_node(comp, pns1->nodes[0]); |
| 972 | EMIT(delete_subscr); |
| 973 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 974 | assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0])); |
| 975 | EMIT(delete_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 976 | } else { |
| 977 | // shouldn't happen |
| 978 | assert(0); |
| 979 | } |
| 980 | } else { |
| 981 | // shouldn't happen |
| 982 | assert(0); |
| 983 | } |
| 984 | |
| 985 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 986 | // SyntaxError, cannot delete |
| 987 | assert(0); |
| 988 | } |
| 989 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) { |
| 990 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 991 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) { |
| 992 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 993 | // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp |
| 994 | |
| 995 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 996 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 997 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) { |
| 998 | // sequence of one item, with trailing comma |
| 999 | assert(PY_PARSE_NODE_IS_NULL(pns1->nodes[0])); |
| 1000 | c_del_stmt(comp, pns->nodes[0]); |
| 1001 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) { |
| 1002 | // sequence of many items |
| 1003 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 1004 | c_del_stmt(comp, pns->nodes[0]); |
| 1005 | for (int i = 0; i < n; i++) { |
| 1006 | c_del_stmt(comp, pns1->nodes[i]); |
| 1007 | } |
| 1008 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
| 1009 | // TODO not implemented; can't del comprehension? |
| 1010 | assert(0); |
| 1011 | } else { |
| 1012 | // sequence with 2 items |
| 1013 | goto sequence_with_2_items; |
| 1014 | } |
| 1015 | } else { |
| 1016 | // sequence with 2 items |
| 1017 | sequence_with_2_items: |
| 1018 | c_del_stmt(comp, pns->nodes[0]); |
| 1019 | c_del_stmt(comp, pns->nodes[1]); |
| 1020 | } |
| 1021 | } else { |
| 1022 | // tuple with 1 element |
| 1023 | c_del_stmt(comp, pn); |
| 1024 | } |
| 1025 | } else { |
| 1026 | // not implemented |
| 1027 | assert(0); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | void compile_del_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1032 | apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt); |
| 1033 | } |
| 1034 | |
| 1035 | void compile_break_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1036 | if (comp->break_label == 0) { |
| 1037 | printf("ERROR: cannot break from here\n"); |
| 1038 | } |
| 1039 | EMIT(break_loop, comp->break_label); |
| 1040 | } |
| 1041 | |
| 1042 | void compile_continue_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1043 | if (comp->continue_label == 0) { |
| 1044 | printf("ERROR: cannot continue from here\n"); |
| 1045 | } |
| 1046 | if (comp->except_nest_level > 0) { |
| 1047 | EMIT(continue_loop, comp->continue_label); |
| 1048 | } else { |
| 1049 | EMIT(jump, comp->continue_label); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1054 | if (comp->scope_cur->kind != SCOPE_FUNCTION) { |
| 1055 | printf("SyntaxError: 'return' outside function\n"); |
| 1056 | comp->had_error = true; |
| 1057 | return; |
| 1058 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1059 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1060 | // no argument to 'return', so return None |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1061 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1062 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { |
| 1063 | // special case when returning an if-expression; to match CPython optimisation |
| 1064 | py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1065 | py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1]; |
| 1066 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1067 | int l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1068 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 1069 | compile_node(comp, pns_test_if_expr->nodes[0]); // success value |
| 1070 | EMIT(return_value); |
| 1071 | EMIT(label_assign, l_fail); |
| 1072 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 1073 | } else { |
| 1074 | compile_node(comp, pns->nodes[0]); |
| 1075 | } |
| 1076 | EMIT(return_value); |
| 1077 | } |
| 1078 | |
| 1079 | void compile_yield_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1080 | compile_node(comp, pns->nodes[0]); |
| 1081 | EMIT(pop_top); |
| 1082 | } |
| 1083 | |
| 1084 | void compile_raise_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1085 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 1086 | // raise |
| 1087 | EMIT(raise_varargs, 0); |
| 1088 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) { |
| 1089 | // raise x from y |
| 1090 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1091 | compile_node(comp, pns->nodes[0]); |
| 1092 | compile_node(comp, pns->nodes[1]); |
| 1093 | EMIT(raise_varargs, 2); |
| 1094 | } else { |
| 1095 | // raise x |
| 1096 | compile_node(comp, pns->nodes[0]); |
| 1097 | EMIT(raise_varargs, 1); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | // q1 holds the base, q2 the full name |
| 1102 | // eg a -> q1=q2=a |
| 1103 | // a.b.c -> q1=a, q2=a.b.c |
| 1104 | void do_import_name(compiler_t *comp, py_parse_node_t pn, qstr *q1, qstr *q2) { |
| 1105 | bool is_as = false; |
| 1106 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) { |
| 1107 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 1108 | // a name of the form x as y; unwrap it |
| 1109 | *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 1110 | pn = pns->nodes[0]; |
| 1111 | is_as = true; |
| 1112 | } |
| 1113 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 1114 | // just a simple name |
| 1115 | *q2 = PY_PARSE_NODE_LEAF_ARG(pn); |
| 1116 | if (!is_as) { |
| 1117 | *q1 = *q2; |
| 1118 | } |
| 1119 | EMIT(import_name, *q2); |
| 1120 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 1121 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 1122 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) { |
| 1123 | // a name of the form a.b.c |
| 1124 | if (!is_as) { |
| 1125 | *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 1126 | } |
| 1127 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1128 | int len = n - 1; |
| 1129 | for (int i = 0; i < n; i++) { |
| 1130 | len += strlen(qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]))); |
| 1131 | } |
| 1132 | char *str = m_new(char, len + 1); |
| 1133 | str[0] = 0; |
| 1134 | for (int i = 0; i < n; i++) { |
| 1135 | if (i > 0) { |
| 1136 | strcat(str, "."); |
| 1137 | } |
| 1138 | strcat(str, qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]))); |
| 1139 | } |
| 1140 | *q2 = qstr_from_str_take(str); |
| 1141 | EMIT(import_name, *q2); |
| 1142 | if (is_as) { |
| 1143 | for (int i = 1; i < n; i++) { |
| 1144 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1145 | } |
| 1146 | } |
| 1147 | } else { |
| 1148 | // TODO not implemented |
| 1149 | assert(0); |
| 1150 | } |
| 1151 | } else { |
| 1152 | // TODO not implemented |
| 1153 | assert(0); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) { |
| 1158 | EMIT(load_const_small_int, 0); // ?? |
| 1159 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1160 | qstr q1, q2; |
| 1161 | do_import_name(comp, pn, &q1, &q2); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1162 | EMIT(store_id, q1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1166 | apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name); |
| 1167 | } |
| 1168 | |
| 1169 | void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1170 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) { |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1171 | EMIT(load_const_small_int, 0); // level 0 for __import__ |
| 1172 | |
| 1173 | // build the "fromlist" tuple |
| 1174 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1175 | EMIT(load_const_verbatim_start); |
| 1176 | EMIT(load_const_verbatim_str, "('*',)"); |
| 1177 | EMIT(load_const_verbatim_end); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1178 | #else |
| 1179 | EMIT(load_const_str, qstr_from_str_static("*"), false); |
| 1180 | EMIT(build_tuple, 1); |
| 1181 | #endif |
| 1182 | |
| 1183 | // do the import |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1184 | qstr dummy_q, id1; |
| 1185 | do_import_name(comp, pns->nodes[0], &dummy_q, &id1); |
| 1186 | EMIT(import_star); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1187 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1188 | } else { |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1189 | EMIT(load_const_small_int, 0); // level 0 for __import__ |
| 1190 | |
| 1191 | // build the "fromlist" tuple |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1192 | py_parse_node_t *pn_nodes; |
| 1193 | int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1194 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1195 | EMIT(load_const_verbatim_start); |
| 1196 | EMIT(load_const_verbatim_str, "("); |
| 1197 | for (int i = 0; i < n; i++) { |
| 1198 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1199 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i]; |
| 1200 | qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
| 1201 | if (i > 0) { |
| 1202 | EMIT(load_const_verbatim_str, ", "); |
| 1203 | } |
| 1204 | EMIT(load_const_verbatim_str, "'"); |
| 1205 | EMIT(load_const_verbatim_str, qstr_str(id2)); |
| 1206 | EMIT(load_const_verbatim_str, "'"); |
| 1207 | } |
| 1208 | if (n == 1) { |
| 1209 | EMIT(load_const_verbatim_str, ","); |
| 1210 | } |
| 1211 | EMIT(load_const_verbatim_str, ")"); |
| 1212 | EMIT(load_const_verbatim_end); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1213 | #else |
| 1214 | for (int i = 0; i < n; i++) { |
| 1215 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1216 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i]; |
| 1217 | qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
| 1218 | EMIT(load_const_str, id2, false); |
| 1219 | } |
| 1220 | EMIT(build_tuple, n); |
| 1221 | #endif |
| 1222 | |
| 1223 | // do the import |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1224 | qstr dummy_q, id1; |
| 1225 | do_import_name(comp, pns->nodes[0], &dummy_q, &id1); |
| 1226 | for (int i = 0; i < n; i++) { |
| 1227 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1228 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i]; |
| 1229 | qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
| 1230 | EMIT(import_from, id2); |
| 1231 | if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1232 | EMIT(store_id, id2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1233 | } else { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1234 | EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | EMIT(pop_top); |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1242 | if (comp->pass == PASS_1) { |
| 1243 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 1244 | scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 1245 | } else { |
| 1246 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1247 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1248 | for (int i = 0; i < num_nodes; i++) { |
| 1249 | scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1250 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1256 | if (comp->pass == PASS_1) { |
| 1257 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 1258 | scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 1259 | } else { |
| 1260 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1261 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1262 | for (int i = 0; i < num_nodes; i++) { |
| 1263 | scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1264 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1270 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1271 | c_if_cond(comp, pns->nodes[0], true, l_end); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1272 | EMIT(load_id, comp->qstr_assertion_error); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1273 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 1274 | // assertion message |
| 1275 | compile_node(comp, pns->nodes[1]); |
| 1276 | EMIT(call_function, 1, 0, false, false); |
| 1277 | } |
| 1278 | EMIT(raise_varargs, 1); |
| 1279 | EMIT(label_assign, l_end); |
| 1280 | } |
| 1281 | |
| 1282 | void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1283 | // TODO proper and/or short circuiting |
| 1284 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1285 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1286 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1287 | int l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1288 | c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition |
| 1289 | |
| 1290 | compile_node(comp, pns->nodes[1]); // if block |
| 1291 | //if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython |
| 1292 | // jump over elif/else blocks if they exist |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1293 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1294 | EMIT(jump, l_end); |
| 1295 | } |
| 1296 | //} |
| 1297 | EMIT(label_assign, l_fail); |
| 1298 | |
| 1299 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 1300 | // compile elif blocks |
| 1301 | |
| 1302 | py_parse_node_struct_t *pns_elif = (py_parse_node_struct_t*)pns->nodes[2]; |
| 1303 | |
| 1304 | if (PY_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) { |
| 1305 | // multiple elif blocks |
| 1306 | |
| 1307 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif); |
| 1308 | for (int i = 0; i < n; i++) { |
| 1309 | py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i]; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1310 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1311 | c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition |
| 1312 | |
| 1313 | compile_node(comp, pns_elif2->nodes[1]); // elif block |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1314 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1315 | EMIT(jump, l_end); |
| 1316 | } |
| 1317 | EMIT(label_assign, l_fail); |
| 1318 | } |
| 1319 | |
| 1320 | } else { |
| 1321 | // a single elif block |
| 1322 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1323 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1324 | c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition |
| 1325 | |
| 1326 | compile_node(comp, pns_elif->nodes[1]); // elif block |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1327 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1328 | EMIT(jump, l_end); |
| 1329 | } |
| 1330 | EMIT(label_assign, l_fail); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | // compile else block |
| 1335 | compile_node(comp, pns->nodes[3]); // can be null |
| 1336 | |
| 1337 | EMIT(label_assign, l_end); |
| 1338 | } |
| 1339 | |
| 1340 | void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1341 | int old_break_label = comp->break_label; |
| 1342 | int old_continue_label = comp->continue_label; |
| 1343 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1344 | int break_label = comp_next_label(comp); |
| 1345 | int continue_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1346 | |
| 1347 | comp->break_label = break_label; |
| 1348 | comp->continue_label = continue_label; |
| 1349 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1350 | // compared to CPython, we have an optimised version of while loops |
| 1351 | #if MICROPY_EMIT_CPYTHON |
| 1352 | int done_label = comp_next_label(comp); |
| 1353 | EMIT(setup_loop, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1354 | EMIT(label_assign, continue_label); |
| 1355 | c_if_cond(comp, pns->nodes[0], false, done_label); // condition |
| 1356 | compile_node(comp, pns->nodes[1]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1357 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1358 | EMIT(jump, continue_label); |
| 1359 | } |
| 1360 | EMIT(label_assign, done_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1361 | // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why |
| 1362 | // this is a small hack to agree with CPython |
| 1363 | if (!node_is_const_true(pns->nodes[0])) { |
| 1364 | EMIT(pop_block); |
| 1365 | } |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1366 | #else |
| 1367 | int top_label = comp_next_label(comp); |
| 1368 | EMIT(jump, continue_label); |
| 1369 | EMIT(label_assign, top_label); |
| 1370 | compile_node(comp, pns->nodes[1]); // body |
| 1371 | EMIT(label_assign, continue_label); |
| 1372 | c_if_cond(comp, pns->nodes[0], true, top_label); // condition |
| 1373 | #endif |
| 1374 | |
| 1375 | // break/continue apply to outer loop (if any) in the else block |
| 1376 | comp->break_label = old_break_label; |
| 1377 | comp->continue_label = old_continue_label; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1378 | |
| 1379 | compile_node(comp, pns->nodes[2]); // else |
| 1380 | |
| 1381 | EMIT(label_assign, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1382 | } |
| 1383 | |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1384 | // TODO preload end and step onto stack if they are not constants |
| 1385 | // TODO check if step is negative and do opposite test |
| 1386 | void compile_for_stmt_optimised_range(compiler_t *comp, py_parse_node_t pn_var, py_parse_node_t pn_start, py_parse_node_t pn_end, py_parse_node_t pn_step, py_parse_node_t pn_body, py_parse_node_t pn_else) { |
| 1387 | int old_break_label = comp->break_label; |
| 1388 | int old_continue_label = comp->continue_label; |
| 1389 | |
| 1390 | int break_label = comp_next_label(comp); |
| 1391 | int continue_label = comp_next_label(comp); |
| 1392 | |
| 1393 | comp->break_label = break_label; |
| 1394 | comp->continue_label = continue_label; |
| 1395 | |
| 1396 | int top_label = comp_next_label(comp); |
| 1397 | |
| 1398 | // compile: var = start |
| 1399 | compile_node(comp, pn_start); |
| 1400 | c_assign(comp, pn_var, ASSIGN_STORE); |
| 1401 | |
| 1402 | EMIT(jump, continue_label); |
| 1403 | EMIT(label_assign, top_label); |
| 1404 | |
Damien | f3822fc | 2013-11-09 20:12:03 +0000 | [diff] [blame] | 1405 | // compile body |
| 1406 | compile_node(comp, pn_body); |
| 1407 | |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1408 | // compile: var += step |
| 1409 | c_assign(comp, pn_var, ASSIGN_AUG_LOAD); |
| 1410 | compile_node(comp, pn_step); |
| 1411 | EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); |
| 1412 | c_assign(comp, pn_var, ASSIGN_AUG_STORE); |
| 1413 | |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1414 | EMIT(label_assign, continue_label); |
| 1415 | |
| 1416 | // compile: if var < end: goto top |
| 1417 | compile_node(comp, pn_var); |
| 1418 | compile_node(comp, pn_end); |
| 1419 | EMIT(compare_op, RT_COMPARE_OP_LESS); |
| 1420 | EMIT(pop_jump_if_true, top_label); |
| 1421 | |
| 1422 | // break/continue apply to outer loop (if any) in the else block |
| 1423 | comp->break_label = old_break_label; |
| 1424 | comp->continue_label = old_continue_label; |
| 1425 | |
| 1426 | compile_node(comp, pn_else); |
| 1427 | |
| 1428 | EMIT(label_assign, break_label); |
| 1429 | } |
| 1430 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1431 | void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1432 | #if !MICROPY_EMIT_CPYTHON |
| 1433 | // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable |
| 1434 | // this is actually slower, but uses no heap memory |
| 1435 | // for viper it will be much, much faster |
Damien | d793389 | 2013-11-28 19:12:18 +0000 | [diff] [blame] | 1436 | if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ PY_PARSE_NODE_IS_ID(pns->nodes[0]) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1437 | py_parse_node_struct_t *pns_it = (py_parse_node_struct_t*)pns->nodes[1]; |
Damien | d793389 | 2013-11-28 19:12:18 +0000 | [diff] [blame] | 1438 | if (PY_PARSE_NODE_IS_ID(pns_it->nodes[0]) && PY_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == comp->qstr_range && PY_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && PY_PARSE_NODE_IS_NULL(pns_it->nodes[2])) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1439 | py_parse_node_t pn_range_args = ((py_parse_node_struct_t*)pns_it->nodes[1])->nodes[0]; |
| 1440 | py_parse_node_t *args; |
| 1441 | int n_args = list_get(&pn_range_args, PN_arglist, &args); |
| 1442 | if (1 <= n_args && n_args <= 3) { |
| 1443 | py_parse_node_t pn_range_start; |
| 1444 | py_parse_node_t pn_range_end; |
| 1445 | py_parse_node_t pn_range_step; |
| 1446 | if (n_args == 1) { |
| 1447 | pn_range_start = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 0); |
| 1448 | pn_range_end = args[0]; |
| 1449 | pn_range_step = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 1); |
| 1450 | } else if (n_args == 2) { |
| 1451 | pn_range_start = args[0]; |
| 1452 | pn_range_end = args[1]; |
| 1453 | pn_range_step = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, 1); |
| 1454 | } else { |
| 1455 | pn_range_start = args[0]; |
| 1456 | pn_range_end = args[1]; |
| 1457 | pn_range_step = args[2]; |
| 1458 | } |
| 1459 | compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]); |
| 1460 | return; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | #endif |
| 1465 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1466 | int old_break_label = comp->break_label; |
| 1467 | int old_continue_label = comp->continue_label; |
| 1468 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1469 | int for_label = comp_next_label(comp); |
| 1470 | int pop_label = comp_next_label(comp); |
| 1471 | int end_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1472 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1473 | int break_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1474 | |
| 1475 | comp->continue_label = for_label; |
| 1476 | comp->break_label = break_label; |
| 1477 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1478 | // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements |
| 1479 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1480 | EMIT(setup_loop, end_label); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1481 | #endif |
| 1482 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1483 | compile_node(comp, pns->nodes[1]); // iterator |
| 1484 | EMIT(get_iter); |
| 1485 | EMIT(label_assign, for_label); |
| 1486 | EMIT(for_iter, pop_label); |
| 1487 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1488 | compile_node(comp, pns->nodes[2]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1489 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1490 | EMIT(jump, for_label); |
| 1491 | } |
| 1492 | EMIT(label_assign, pop_label); |
| 1493 | EMIT(for_iter_end); |
| 1494 | |
| 1495 | // break/continue apply to outer loop (if any) in the else block |
| 1496 | comp->break_label = old_break_label; |
| 1497 | comp->continue_label = old_continue_label; |
| 1498 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1499 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1500 | EMIT(pop_block); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1501 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1502 | |
| 1503 | compile_node(comp, pns->nodes[3]); // else (not tested) |
| 1504 | |
| 1505 | EMIT(label_assign, break_label); |
| 1506 | EMIT(label_assign, end_label); |
| 1507 | } |
| 1508 | |
| 1509 | void 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) { |
| 1510 | // this function is a bit of a hack at the moment |
| 1511 | // don't understand how the stack works with exceptions, so we force it to return to the correct value |
| 1512 | |
| 1513 | // setup code |
| 1514 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1515 | int l1 = comp_next_label(comp); |
| 1516 | int success_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1517 | comp->except_nest_level += 1; // for correct handling of continue |
| 1518 | EMIT(setup_except, l1); |
| 1519 | compile_node(comp, pn_body); // body |
| 1520 | EMIT(pop_block); |
| 1521 | EMIT(jump, success_label); |
| 1522 | EMIT(label_assign, l1); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1523 | int l2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1524 | |
| 1525 | for (int i = 0; i < n_except; i++) { |
| 1526 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be |
| 1527 | py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i]; |
| 1528 | |
| 1529 | qstr qstr_exception_local = 0; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1530 | int end_finally_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1531 | |
| 1532 | if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) { |
| 1533 | // this is a catch all exception handler |
| 1534 | if (i + 1 != n_except) { |
| 1535 | printf("SyntaxError: default 'except:' must be last\n"); |
| 1536 | return; |
| 1537 | } |
| 1538 | } else { |
| 1539 | // this exception handler requires a match to a certain type of exception |
| 1540 | py_parse_node_t pns_exception_expr = pns_except->nodes[0]; |
| 1541 | if (PY_PARSE_NODE_IS_STRUCT(pns_exception_expr)) { |
| 1542 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns_exception_expr; |
| 1543 | if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) { |
| 1544 | // handler binds the exception to a local |
| 1545 | pns_exception_expr = pns3->nodes[0]; |
| 1546 | qstr_exception_local = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]); |
| 1547 | } |
| 1548 | } |
| 1549 | EMIT(dup_top); |
| 1550 | compile_node(comp, pns_exception_expr); |
| 1551 | EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH); |
| 1552 | EMIT(pop_jump_if_false, end_finally_label); |
| 1553 | } |
| 1554 | |
| 1555 | EMIT(pop_top); |
| 1556 | |
| 1557 | if (qstr_exception_local == 0) { |
| 1558 | EMIT(pop_top); |
| 1559 | } else { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1560 | EMIT(store_id, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | EMIT(pop_top); |
| 1564 | |
| 1565 | int l3; |
| 1566 | if (qstr_exception_local != 0) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1567 | l3 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1568 | EMIT(setup_finally, l3); |
| 1569 | } |
| 1570 | compile_node(comp, pns_except->nodes[1]); |
| 1571 | if (qstr_exception_local != 0) { |
| 1572 | EMIT(pop_block); |
| 1573 | } |
| 1574 | EMIT(pop_except); |
| 1575 | if (qstr_exception_local != 0) { |
| 1576 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1577 | EMIT(label_assign, l3); |
| 1578 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1579 | EMIT(store_id, qstr_exception_local); |
| 1580 | EMIT(delete_id, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1581 | EMIT(end_finally); |
| 1582 | } |
| 1583 | EMIT(jump, l2); |
| 1584 | EMIT(label_assign, end_finally_label); |
| 1585 | } |
| 1586 | |
| 1587 | EMIT(end_finally); |
| 1588 | EMIT(label_assign, success_label); |
| 1589 | comp->except_nest_level -= 1; |
| 1590 | compile_node(comp, pn_else); // else block, can be null |
| 1591 | EMIT(label_assign, l2); |
| 1592 | EMIT(set_stack_size, stack_size); |
| 1593 | } |
| 1594 | |
| 1595 | void 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) { |
| 1596 | // don't understand how the stack works with exceptions, so we force it to return to the correct value |
| 1597 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1598 | int l_finally_block = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1599 | EMIT(setup_finally, l_finally_block); |
| 1600 | if (n_except == 0) { |
| 1601 | assert(PY_PARSE_NODE_IS_NULL(pn_else)); |
| 1602 | compile_node(comp, pn_body); |
| 1603 | } else { |
| 1604 | compile_try_except(comp, pn_body, n_except, pn_except, pn_else); |
| 1605 | } |
| 1606 | EMIT(pop_block); |
| 1607 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1608 | EMIT(label_assign, l_finally_block); |
| 1609 | compile_node(comp, pn_finally); |
| 1610 | EMIT(end_finally); |
| 1611 | EMIT(set_stack_size, stack_size); |
| 1612 | } |
| 1613 | |
| 1614 | void compile_try_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1615 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 1616 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1617 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) { |
| 1618 | // just try-finally |
| 1619 | compile_try_finally(comp, pns->nodes[0], 0, NULL, PY_PARSE_NODE_NULL, pns2->nodes[0]); |
| 1620 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) { |
| 1621 | // try-except and possibly else and/or finally |
| 1622 | py_parse_node_t *pn_excepts; |
| 1623 | int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts); |
| 1624 | if (PY_PARSE_NODE_IS_NULL(pns2->nodes[2])) { |
| 1625 | // no finally |
| 1626 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]); |
| 1627 | } else { |
| 1628 | // have finally |
| 1629 | compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((py_parse_node_struct_t*)pns2->nodes[2])->nodes[0]); |
| 1630 | } |
| 1631 | } else { |
| 1632 | // just try-except |
| 1633 | py_parse_node_t *pn_excepts; |
| 1634 | int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts); |
| 1635 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, PY_PARSE_NODE_NULL); |
| 1636 | } |
| 1637 | } else { |
| 1638 | // shouldn't happen |
| 1639 | assert(0); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, py_parse_node_t body) { |
| 1644 | if (n == 0) { |
| 1645 | // no more pre-bits, compile the body of the with |
| 1646 | compile_node(comp, body); |
| 1647 | } else { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1648 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1649 | if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
| 1650 | // this pre-bit is of the form "a as b" |
| 1651 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0]; |
| 1652 | compile_node(comp, pns->nodes[0]); |
| 1653 | EMIT(setup_with, l_end); |
| 1654 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1655 | } else { |
| 1656 | // this pre-bit is just an expression |
| 1657 | compile_node(comp, nodes[0]); |
| 1658 | EMIT(setup_with, l_end); |
| 1659 | EMIT(pop_top); |
| 1660 | } |
| 1661 | // compile additional pre-bits and the body |
| 1662 | compile_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1663 | // finish this with block |
| 1664 | EMIT(pop_block); |
| 1665 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1666 | EMIT(label_assign, l_end); |
| 1667 | EMIT(with_cleanup); |
| 1668 | EMIT(end_finally); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void compile_with_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1673 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
| 1674 | py_parse_node_t *nodes; |
| 1675 | int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes); |
| 1676 | assert(n > 0); |
| 1677 | |
| 1678 | // compile in a nested fashion |
| 1679 | compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1680 | } |
| 1681 | |
| 1682 | void compile_expr_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1683 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1684 | if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) { |
| 1685 | // for REPL, evaluate then print the expression |
| 1686 | EMIT(load_id, qstr_from_str_static("__repl_print__")); |
| 1687 | compile_node(comp, pns->nodes[0]); |
| 1688 | EMIT(call_function, 1, 0, false, false); |
| 1689 | EMIT(pop_top); |
| 1690 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1691 | } else { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1692 | // for non-REPL, evaluate then discard the expression |
| 1693 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 1694 | // do nothing with a lonely constant |
| 1695 | } else { |
| 1696 | compile_node(comp, pns->nodes[0]); // just an expression |
| 1697 | EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack |
| 1698 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1699 | } |
| 1700 | } else { |
| 1701 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1702 | int kind = PY_PARSE_NODE_STRUCT_KIND(pns1); |
| 1703 | if (kind == PN_expr_stmt_augassign) { |
| 1704 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign |
| 1705 | compile_node(comp, pns1->nodes[1]); // rhs |
| 1706 | assert(PY_PARSE_NODE_IS_TOKEN(pns1->nodes[0])); |
| 1707 | // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice |
| 1708 | switch (PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) { |
| 1709 | case PY_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break; |
| 1710 | case PY_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break; |
| 1711 | case PY_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break; |
| 1712 | case PY_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break; |
| 1713 | case PY_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break; |
| 1714 | case PY_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break; |
| 1715 | case PY_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break; |
| 1716 | case PY_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break; |
| 1717 | case PY_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break; |
| 1718 | case PY_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break; |
| 1719 | case PY_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break; |
| 1720 | case PY_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break; |
| 1721 | default: assert(0); // shouldn't happen |
| 1722 | } |
| 1723 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign |
| 1724 | } else if (kind == PN_expr_stmt_assign_list) { |
| 1725 | int rhs = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1; |
| 1726 | compile_node(comp, ((py_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs |
| 1727 | // following CPython, we store left-most first |
| 1728 | if (rhs > 0) { |
| 1729 | EMIT(dup_top); |
| 1730 | } |
| 1731 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1732 | for (int i = 0; i < rhs; i++) { |
| 1733 | if (i + 1 < rhs) { |
| 1734 | EMIT(dup_top); |
| 1735 | } |
| 1736 | c_assign(comp, ((py_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store |
| 1737 | } |
| 1738 | } else if (kind == PN_expr_stmt_assign) { |
| 1739 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) |
| 1740 | && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
| 1741 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 2 |
| 1742 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 2) { |
| 1743 | // optimisation for a, b = c, d; to match CPython's optimisation |
| 1744 | py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0]; |
| 1745 | py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1746 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1747 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1748 | EMIT(rot_two); |
| 1749 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1750 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1751 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) |
| 1752 | && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
| 1753 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 3 |
| 1754 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 3) { |
| 1755 | // optimisation for a, b, c = d, e, f; to match CPython's optimisation |
| 1756 | py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0]; |
| 1757 | py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1758 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1759 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1760 | compile_node(comp, pns10->nodes[2]); // rhs |
| 1761 | EMIT(rot_three); |
| 1762 | EMIT(rot_two); |
| 1763 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1764 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1765 | c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store |
| 1766 | } else { |
| 1767 | compile_node(comp, pns1->nodes[0]); // rhs |
| 1768 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1769 | } |
| 1770 | } else { |
| 1771 | // shouldn't happen |
| 1772 | assert(0); |
| 1773 | } |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | void c_binary_op(compiler_t *comp, py_parse_node_struct_t *pns, rt_binary_op_t binary_op) { |
| 1778 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1779 | compile_node(comp, pns->nodes[0]); |
| 1780 | for (int i = 1; i < num_nodes; i += 1) { |
| 1781 | compile_node(comp, pns->nodes[i]); |
| 1782 | EMIT(binary_op, binary_op); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1787 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else)); |
| 1788 | py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1789 | |
| 1790 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1791 | int l_fail = comp_next_label(comp); |
| 1792 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1793 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 1794 | compile_node(comp, pns->nodes[0]); // success value |
| 1795 | EMIT(jump, l_end); |
| 1796 | EMIT(label_assign, l_fail); |
| 1797 | EMIT(set_stack_size, stack_size); // force stack size reset |
| 1798 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 1799 | EMIT(label_assign, l_end); |
| 1800 | } |
| 1801 | |
| 1802 | void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1803 | // TODO default params etc for lambda; possibly just use funcdef code |
| 1804 | //py_parse_node_t pn_params = pns->nodes[0]; |
| 1805 | //py_parse_node_t pn_body = pns->nodes[1]; |
| 1806 | |
| 1807 | if (comp->pass == PASS_1) { |
| 1808 | // create a new scope for this lambda |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 1809 | scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (py_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1810 | // store the lambda scope so the compiling function (this one) can use it at each pass |
| 1811 | pns->nodes[2] = (py_parse_node_t)s; |
| 1812 | } |
| 1813 | |
| 1814 | // get the scope for this lambda |
| 1815 | scope_t *this_scope = (scope_t*)pns->nodes[2]; |
| 1816 | |
| 1817 | // make the lambda |
| 1818 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 1819 | } |
| 1820 | |
| 1821 | void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1822 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1823 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1824 | for (int i = 0; i < n; i += 1) { |
| 1825 | compile_node(comp, pns->nodes[i]); |
| 1826 | if (i + 1 < n) { |
| 1827 | EMIT(jump_if_true_or_pop, l_end); |
| 1828 | } |
| 1829 | } |
| 1830 | EMIT(label_assign, l_end); |
| 1831 | } |
| 1832 | |
| 1833 | void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1834 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1835 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1836 | for (int i = 0; i < n; i += 1) { |
| 1837 | compile_node(comp, pns->nodes[i]); |
| 1838 | if (i + 1 < n) { |
| 1839 | EMIT(jump_if_false_or_pop, l_end); |
| 1840 | } |
| 1841 | } |
| 1842 | EMIT(label_assign, l_end); |
| 1843 | } |
| 1844 | |
| 1845 | void compile_not_test_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1846 | compile_node(comp, pns->nodes[0]); |
| 1847 | EMIT(unary_op, RT_UNARY_OP_NOT); |
| 1848 | } |
| 1849 | |
| 1850 | void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1851 | int stack_size = EMIT(get_stack_size); |
| 1852 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1853 | compile_node(comp, pns->nodes[0]); |
| 1854 | bool multi = (num_nodes > 3); |
| 1855 | int l_fail = 0; |
| 1856 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1857 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1858 | } |
| 1859 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1860 | compile_node(comp, pns->nodes[i + 1]); |
| 1861 | if (i + 2 < num_nodes) { |
| 1862 | EMIT(dup_top); |
| 1863 | EMIT(rot_three); |
| 1864 | } |
| 1865 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS)) { |
| 1866 | EMIT(compare_op, RT_COMPARE_OP_LESS); |
| 1867 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE)) { |
| 1868 | EMIT(compare_op, RT_COMPARE_OP_MORE); |
| 1869 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_EQUAL)) { |
| 1870 | EMIT(compare_op, RT_COMPARE_OP_EQUAL); |
| 1871 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS_EQUAL)) { |
| 1872 | EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL); |
| 1873 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE_EQUAL)) { |
| 1874 | EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL); |
| 1875 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_NOT_EQUAL)) { |
| 1876 | EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL); |
| 1877 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_KW_IN)) { |
| 1878 | EMIT(compare_op, RT_COMPARE_OP_IN); |
| 1879 | } else if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[i])) { |
| 1880 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[i]; |
| 1881 | int kind = PY_PARSE_NODE_STRUCT_KIND(pns2); |
| 1882 | if (kind == PN_comp_op_not_in) { |
| 1883 | EMIT(compare_op, RT_COMPARE_OP_NOT_IN); |
| 1884 | } else if (kind == PN_comp_op_is) { |
| 1885 | if (PY_PARSE_NODE_IS_NULL(pns2->nodes[0])) { |
| 1886 | EMIT(compare_op, RT_COMPARE_OP_IS); |
| 1887 | } else { |
| 1888 | EMIT(compare_op, RT_COMPARE_OP_IS_NOT); |
| 1889 | } |
| 1890 | } else { |
| 1891 | // shouldn't happen |
| 1892 | assert(0); |
| 1893 | } |
| 1894 | } else { |
| 1895 | // shouldn't happen |
| 1896 | assert(0); |
| 1897 | } |
| 1898 | if (i + 2 < num_nodes) { |
| 1899 | EMIT(jump_if_false_or_pop, l_fail); |
| 1900 | } |
| 1901 | } |
| 1902 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1903 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1904 | EMIT(jump, l_end); |
| 1905 | EMIT(label_assign, l_fail); |
| 1906 | EMIT(rot_two); |
| 1907 | EMIT(pop_top); |
| 1908 | EMIT(label_assign, l_end); |
| 1909 | EMIT(set_stack_size, stack_size + 1); // force stack size |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | void compile_star_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1914 | // TODO |
| 1915 | assert(0); |
| 1916 | compile_node(comp, pns->nodes[0]); |
| 1917 | //EMIT(unary_op, "UNARY_STAR"); |
| 1918 | } |
| 1919 | |
| 1920 | void compile_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1921 | c_binary_op(comp, pns, RT_BINARY_OP_OR); |
| 1922 | } |
| 1923 | |
| 1924 | void compile_xor_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1925 | c_binary_op(comp, pns, RT_BINARY_OP_XOR); |
| 1926 | } |
| 1927 | |
| 1928 | void compile_and_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1929 | c_binary_op(comp, pns, RT_BINARY_OP_AND); |
| 1930 | } |
| 1931 | |
| 1932 | void compile_shift_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1933 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1934 | compile_node(comp, pns->nodes[0]); |
| 1935 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1936 | compile_node(comp, pns->nodes[i + 1]); |
| 1937 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_LESS)) { |
| 1938 | EMIT(binary_op, RT_BINARY_OP_LSHIFT); |
| 1939 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_MORE)) { |
| 1940 | EMIT(binary_op, RT_BINARY_OP_RSHIFT); |
| 1941 | } else { |
| 1942 | // shouldn't happen |
| 1943 | assert(0); |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | void compile_arith_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1949 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1950 | compile_node(comp, pns->nodes[0]); |
| 1951 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1952 | compile_node(comp, pns->nodes[i + 1]); |
| 1953 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PLUS)) { |
| 1954 | EMIT(binary_op, RT_BINARY_OP_ADD); |
| 1955 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MINUS)) { |
| 1956 | EMIT(binary_op, RT_BINARY_OP_SUBTRACT); |
| 1957 | } else { |
| 1958 | // shouldn't happen |
| 1959 | assert(0); |
| 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | void compile_term(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1965 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1966 | compile_node(comp, pns->nodes[0]); |
| 1967 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1968 | compile_node(comp, pns->nodes[i + 1]); |
| 1969 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_STAR)) { |
| 1970 | EMIT(binary_op, RT_BINARY_OP_MULTIPLY); |
| 1971 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_SLASH)) { |
| 1972 | EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE); |
| 1973 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_SLASH)) { |
| 1974 | EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE); |
| 1975 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PERCENT)) { |
| 1976 | EMIT(binary_op, RT_BINARY_OP_MODULO); |
| 1977 | } else { |
| 1978 | // shouldn't happen |
| 1979 | assert(0); |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | void compile_factor_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1985 | compile_node(comp, pns->nodes[1]); |
| 1986 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) { |
| 1987 | EMIT(unary_op, RT_UNARY_OP_POSITIVE); |
| 1988 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) { |
| 1989 | EMIT(unary_op, RT_UNARY_OP_NEGATIVE); |
| 1990 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) { |
| 1991 | EMIT(unary_op, RT_UNARY_OP_INVERT); |
| 1992 | } else { |
| 1993 | // shouldn't happen |
| 1994 | assert(0); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | void compile_trailer_paren_helper(compiler_t *comp, py_parse_node_struct_t *pns, bool is_method_call) { |
| 1999 | // function to call is on top of stack |
| 2000 | |
| 2001 | int old_n_arg_keyword = comp->n_arg_keyword; |
| 2002 | bool old_have_star_arg = comp->have_star_arg; |
| 2003 | bool old_have_dbl_star_arg = comp->have_dbl_star_arg; |
| 2004 | comp->n_arg_keyword = 0; |
| 2005 | comp->have_star_arg = false; |
| 2006 | comp->have_dbl_star_arg = false; |
| 2007 | |
| 2008 | compile_node(comp, pns->nodes[0]); // arguments to function call; can be null |
| 2009 | |
| 2010 | // compute number of positional arguments |
| 2011 | int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword; |
| 2012 | if (comp->have_star_arg) { |
| 2013 | n_positional -= 1; |
| 2014 | } |
| 2015 | if (comp->have_dbl_star_arg) { |
| 2016 | n_positional -= 1; |
| 2017 | } |
| 2018 | |
| 2019 | if (is_method_call) { |
| 2020 | EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg); |
| 2021 | } else { |
| 2022 | EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg); |
| 2023 | } |
| 2024 | |
| 2025 | comp->n_arg_keyword = old_n_arg_keyword; |
| 2026 | comp->have_star_arg = old_have_star_arg; |
| 2027 | comp->have_dbl_star_arg = old_have_dbl_star_arg; |
| 2028 | } |
| 2029 | |
| 2030 | void compile_power_trailers(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2031 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 2032 | for (int i = 0; i < num_nodes; i++) { |
| 2033 | 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)) { |
| 2034 | // optimisation for method calls a.f(...), following PyPy |
| 2035 | py_parse_node_struct_t *pns_period = (py_parse_node_struct_t*)pns->nodes[i]; |
| 2036 | py_parse_node_struct_t *pns_paren = (py_parse_node_struct_t*)pns->nodes[i + 1]; |
| 2037 | EMIT(load_method, PY_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method |
| 2038 | compile_trailer_paren_helper(comp, pns_paren, true); |
| 2039 | i += 1; |
| 2040 | } else { |
| 2041 | compile_node(comp, pns->nodes[i]); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2047 | compile_node(comp, pns->nodes[0]); |
| 2048 | EMIT(binary_op, RT_BINARY_OP_POWER); |
| 2049 | } |
| 2050 | |
| 2051 | void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2052 | // a list of strings |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame^] | 2053 | |
| 2054 | // check type of list (string or bytes) and count total number of bytes |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2055 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame^] | 2056 | int n_bytes = 0; |
| 2057 | int string_kind = PY_PARSE_NODE_NULL; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2058 | for (int i = 0; i < n; i++) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2059 | assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i])); |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame^] | 2060 | int pn_kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]); |
| 2061 | assert(pn_kind == PY_PARSE_NODE_STRING || pn_kind == PY_PARSE_NODE_BYTES); |
| 2062 | if (i == 0) { |
| 2063 | string_kind = pn_kind; |
| 2064 | } else if (pn_kind != string_kind) { |
| 2065 | printf("SyntaxError: cannot mix bytes and nonbytes literals\n"); |
| 2066 | return; |
| 2067 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2068 | const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame^] | 2069 | n_bytes += strlen(str); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2070 | } |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame^] | 2071 | |
| 2072 | // allocate memory for concatenated string/bytes |
| 2073 | char *cat_str = m_new(char, n_bytes + 1); |
| 2074 | cat_str[0] = '\0'; |
| 2075 | |
| 2076 | // concatenate string/bytes |
| 2077 | for (int i = 0; i < n; i++) { |
| 2078 | const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 2079 | strcat(cat_str, str); |
| 2080 | } |
| 2081 | |
| 2082 | EMIT(load_const_str, qstr_from_str_take(cat_str), string_kind == PY_PARSE_NODE_BYTES); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node |
| 2086 | void compile_comprehension(compiler_t *comp, py_parse_node_struct_t *pns, scope_kind_t kind) { |
| 2087 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 2088 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 2089 | py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2090 | |
| 2091 | if (comp->pass == PASS_1) { |
| 2092 | // create a new scope for this comprehension |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2093 | scope_t *s = scope_new_and_link(comp, kind, (py_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2094 | // store the comprehension scope so the compiling function (this one) can use it at each pass |
| 2095 | pns_comp_for->nodes[3] = (py_parse_node_t)s; |
| 2096 | } |
| 2097 | |
| 2098 | // get the scope for this comprehension |
| 2099 | scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3]; |
| 2100 | |
| 2101 | // compile the comprehension |
| 2102 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 2103 | |
| 2104 | compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator |
| 2105 | EMIT(get_iter); |
| 2106 | EMIT(call_function, 1, 0, false, false); |
| 2107 | } |
| 2108 | |
| 2109 | void compile_atom_paren(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2110 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2111 | // an empty tuple |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2112 | c_tuple(comp, PY_PARSE_NODE_NULL, NULL); |
| 2113 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 2114 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2115 | assert(!PY_PARSE_NODE_IS_NULL(pns->nodes[1])); |
| 2116 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 2117 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2118 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
| 2119 | // tuple of one item, with trailing comma |
| 2120 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2121 | c_tuple(comp, pns->nodes[0], NULL); |
| 2122 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
| 2123 | // tuple of many items |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2124 | c_tuple(comp, pns->nodes[0], pns2); |
| 2125 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
| 2126 | // generator expression |
| 2127 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 2128 | } else { |
| 2129 | // tuple with 2 items |
| 2130 | goto tuple_with_2_items; |
| 2131 | } |
| 2132 | } else { |
| 2133 | // tuple with 2 items |
| 2134 | tuple_with_2_items: |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2135 | c_tuple(comp, PY_PARSE_NODE_NULL, pns); |
| 2136 | } |
| 2137 | } else { |
| 2138 | // parenthesis around a single item, is just that item |
| 2139 | compile_node(comp, pns->nodes[0]); |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | void compile_atom_bracket(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2144 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2145 | // empty list |
| 2146 | EMIT(build_list, 0); |
| 2147 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 2148 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2149 | if (PY_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) { |
| 2150 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns2->nodes[1]; |
| 2151 | if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) { |
| 2152 | // list of one item, with trailing comma |
| 2153 | assert(PY_PARSE_NODE_IS_NULL(pns3->nodes[0])); |
| 2154 | compile_node(comp, pns2->nodes[0]); |
| 2155 | EMIT(build_list, 1); |
| 2156 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) { |
| 2157 | // list of many items |
| 2158 | compile_node(comp, pns2->nodes[0]); |
| 2159 | compile_generic_all_nodes(comp, pns3); |
| 2160 | EMIT(build_list, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns3)); |
| 2161 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) { |
| 2162 | // list comprehension |
| 2163 | compile_comprehension(comp, pns2, SCOPE_LIST_COMP); |
| 2164 | } else { |
| 2165 | // list with 2 items |
| 2166 | goto list_with_2_items; |
| 2167 | } |
| 2168 | } else { |
| 2169 | // list with 2 items |
| 2170 | list_with_2_items: |
| 2171 | compile_node(comp, pns2->nodes[0]); |
| 2172 | compile_node(comp, pns2->nodes[1]); |
| 2173 | EMIT(build_list, 2); |
| 2174 | } |
| 2175 | } else { |
| 2176 | // list with 1 item |
| 2177 | compile_node(comp, pns->nodes[0]); |
| 2178 | EMIT(build_list, 1); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | void compile_atom_brace(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2183 | py_parse_node_t pn = pns->nodes[0]; |
| 2184 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2185 | // empty dict |
| 2186 | EMIT(build_map, 0); |
| 2187 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 2188 | pns = (py_parse_node_struct_t*)pn; |
| 2189 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) { |
| 2190 | // dict with one element |
| 2191 | EMIT(build_map, 1); |
| 2192 | compile_node(comp, pn); |
| 2193 | EMIT(store_map); |
| 2194 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) { |
| 2195 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed |
| 2196 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2197 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) { |
| 2198 | // dict/set with multiple elements |
| 2199 | |
| 2200 | // get tail elements (2nd, 3rd, ...) |
| 2201 | py_parse_node_t *nodes; |
| 2202 | int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes); |
| 2203 | |
| 2204 | // first element sets whether it's a dict or set |
| 2205 | bool is_dict; |
| 2206 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
| 2207 | // a dictionary |
| 2208 | EMIT(build_map, 1 + n); |
| 2209 | compile_node(comp, pns->nodes[0]); |
| 2210 | EMIT(store_map); |
| 2211 | is_dict = true; |
| 2212 | } else { |
| 2213 | // a set |
| 2214 | compile_node(comp, pns->nodes[0]); // 1st value of set |
| 2215 | is_dict = false; |
| 2216 | } |
| 2217 | |
| 2218 | // process rest of elements |
| 2219 | for (int i = 0; i < n; i++) { |
| 2220 | py_parse_node_t pn = nodes[i]; |
| 2221 | bool is_key_value = PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item); |
| 2222 | compile_node(comp, pn); |
| 2223 | if (is_dict) { |
| 2224 | if (!is_key_value) { |
| 2225 | printf("SyntaxError?: expecting key:value for dictionary"); |
| 2226 | return; |
| 2227 | } |
| 2228 | EMIT(store_map); |
| 2229 | } else { |
| 2230 | if (is_key_value) { |
| 2231 | printf("SyntaxError?: expecting just a value for set"); |
| 2232 | return; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | // if it's a set, build it |
| 2238 | if (!is_dict) { |
| 2239 | EMIT(build_set, 1 + n); |
| 2240 | } |
| 2241 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) { |
| 2242 | // dict/set comprehension |
| 2243 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
| 2244 | // a dictionary comprehension |
| 2245 | compile_comprehension(comp, pns, SCOPE_DICT_COMP); |
| 2246 | } else { |
| 2247 | // a set comprehension |
| 2248 | compile_comprehension(comp, pns, SCOPE_SET_COMP); |
| 2249 | } |
| 2250 | } else { |
| 2251 | // shouldn't happen |
| 2252 | assert(0); |
| 2253 | } |
| 2254 | } else { |
| 2255 | // set with one element |
| 2256 | goto set_with_one_element; |
| 2257 | } |
| 2258 | } else { |
| 2259 | // set with one element |
| 2260 | set_with_one_element: |
| 2261 | compile_node(comp, pn); |
| 2262 | EMIT(build_set, 1); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | void compile_trailer_paren(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2267 | compile_trailer_paren_helper(comp, pns, false); |
| 2268 | } |
| 2269 | |
| 2270 | void compile_trailer_bracket(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2271 | // object who's index we want is on top of stack |
| 2272 | compile_node(comp, pns->nodes[0]); // the index |
| 2273 | EMIT(binary_op, RT_BINARY_OP_SUBSCR); |
| 2274 | } |
| 2275 | |
| 2276 | void compile_trailer_period(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2277 | // object who's attribute we want is on top of stack |
| 2278 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get |
| 2279 | } |
| 2280 | |
| 2281 | void compile_subscript_3_helper(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2282 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be |
| 2283 | py_parse_node_t pn = pns->nodes[0]; |
| 2284 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2285 | // [?:] |
| 2286 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2287 | EMIT(build_slice, 2); |
| 2288 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 2289 | pns = (py_parse_node_struct_t*)pn; |
| 2290 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) { |
| 2291 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2292 | pn = pns->nodes[0]; |
| 2293 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2294 | // [?::] |
| 2295 | EMIT(build_slice, 2); |
| 2296 | } else { |
| 2297 | // [?::x] |
| 2298 | compile_node(comp, pn); |
| 2299 | EMIT(build_slice, 3); |
| 2300 | } |
| 2301 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) { |
| 2302 | compile_node(comp, pns->nodes[0]); |
| 2303 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2304 | pns = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2305 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be |
| 2306 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2307 | // [?:x:] |
| 2308 | EMIT(build_slice, 2); |
| 2309 | } else { |
| 2310 | // [?:x:x] |
| 2311 | compile_node(comp, pns->nodes[0]); |
| 2312 | EMIT(build_slice, 3); |
| 2313 | } |
| 2314 | } else { |
| 2315 | // [?:x] |
| 2316 | compile_node(comp, pn); |
| 2317 | EMIT(build_slice, 2); |
| 2318 | } |
| 2319 | } else { |
| 2320 | // [?:x] |
| 2321 | compile_node(comp, pn); |
| 2322 | EMIT(build_slice, 2); |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | void compile_subscript_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2327 | compile_node(comp, pns->nodes[0]); // start of slice |
| 2328 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2329 | compile_subscript_3_helper(comp, (py_parse_node_struct_t*)pns->nodes[1]); |
| 2330 | } |
| 2331 | |
| 2332 | void compile_subscript_3(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2333 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2334 | compile_subscript_3_helper(comp, pns); |
| 2335 | } |
| 2336 | |
| 2337 | void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2338 | // if this is called then we are compiling a dict key:value pair |
| 2339 | compile_node(comp, pns->nodes[1]); // value |
| 2340 | compile_node(comp, pns->nodes[0]); // key |
| 2341 | } |
| 2342 | |
| 2343 | void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2344 | qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2345 | // store class object into class name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2346 | EMIT(store_id, cname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2350 | if (comp->have_star_arg) { |
| 2351 | printf("SyntaxError?: can't have multiple *x\n"); |
| 2352 | return; |
| 2353 | } |
| 2354 | comp->have_star_arg = true; |
| 2355 | compile_node(comp, pns->nodes[0]); |
| 2356 | } |
| 2357 | |
| 2358 | void compile_arglist_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2359 | if (comp->have_dbl_star_arg) { |
| 2360 | printf("SyntaxError?: can't have multiple **x\n"); |
| 2361 | return; |
| 2362 | } |
| 2363 | comp->have_dbl_star_arg = true; |
| 2364 | compile_node(comp, pns->nodes[0]); |
| 2365 | } |
| 2366 | |
| 2367 | void compile_argument(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2368 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2369 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2370 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) { |
| 2371 | if (!PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 2372 | printf("SyntaxError?: lhs of keyword argument must be an id\n"); |
| 2373 | return; |
| 2374 | } |
| 2375 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 2376 | compile_node(comp, pns2->nodes[0]); |
| 2377 | comp->n_arg_keyword += 1; |
| 2378 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
| 2379 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 2380 | } else { |
| 2381 | // shouldn't happen |
| 2382 | assert(0); |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | void compile_yield_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2387 | if (comp->scope_cur->kind != SCOPE_FUNCTION) { |
| 2388 | printf("SyntaxError: 'yield' outside function\n"); |
| 2389 | return; |
| 2390 | } |
| 2391 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2392 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2393 | EMIT(yield_value); |
| 2394 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) { |
| 2395 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2396 | compile_node(comp, pns->nodes[0]); |
| 2397 | EMIT(get_iter); |
| 2398 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2399 | EMIT(yield_from); |
| 2400 | } else { |
| 2401 | compile_node(comp, pns->nodes[0]); |
| 2402 | EMIT(yield_value); |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | typedef void (*compile_function_t)(compiler_t*, py_parse_node_struct_t*); |
| 2407 | static compile_function_t compile_function[] = { |
| 2408 | NULL, |
| 2409 | #define nc NULL |
| 2410 | #define c(f) compile_##f |
| 2411 | #define DEF_RULE(rule, comp, kind, arg...) comp, |
| 2412 | #include "grammar.h" |
| 2413 | #undef nc |
| 2414 | #undef c |
| 2415 | #undef DEF_RULE |
| 2416 | }; |
| 2417 | |
| 2418 | void compile_node(compiler_t *comp, py_parse_node_t pn) { |
| 2419 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2420 | // pass |
| 2421 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 2422 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 2423 | switch (PY_PARSE_NODE_LEAF_KIND(pn)) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2424 | case PY_PARSE_NODE_ID: EMIT(load_id, arg); break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2425 | case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break; |
| 2426 | case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break; |
| 2427 | case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break; |
| 2428 | case PY_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break; |
| 2429 | case PY_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break; |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2430 | case PY_PARSE_NODE_TOKEN: |
| 2431 | if (arg == PY_TOKEN_NEWLINE) { |
| 2432 | // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline) |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2433 | // or when single_input lets through a NEWLINE (user enters a blank line) |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2434 | // do nothing |
| 2435 | } else { |
| 2436 | EMIT(load_const_tok, arg); |
| 2437 | } |
| 2438 | break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2439 | default: assert(0); |
| 2440 | } |
| 2441 | } else { |
| 2442 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 2443 | compile_function_t f = compile_function[PY_PARSE_NODE_STRUCT_KIND(pns)]; |
| 2444 | if (f == NULL) { |
| 2445 | printf("node %u cannot be compiled\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns)); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2446 | py_parse_node_show(pn, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2447 | assert(0); |
| 2448 | } else { |
| 2449 | f(comp, pns); |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | void 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) { |
| 2455 | // TODO verify that *k and **k are last etc |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2456 | qstr param_name = 0; |
| 2457 | py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL; |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2458 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 2459 | param_name = PY_PARSE_NODE_LEAF_ARG(pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2460 | if (comp->have_bare_star) { |
| 2461 | // comes after a bare star, so doesn't count as a parameter |
| 2462 | } else { |
| 2463 | comp->scope_cur->num_params += 1; |
| 2464 | } |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2465 | } else { |
| 2466 | assert(PY_PARSE_NODE_IS_STRUCT(pn)); |
| 2467 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 2468 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2469 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2470 | //int node_index = 1; unused |
| 2471 | if (allow_annotations) { |
| 2472 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 2473 | // this parameter has an annotation |
| 2474 | pn_annotation = pns->nodes[1]; |
| 2475 | } |
| 2476 | //node_index = 2; unused |
| 2477 | } |
| 2478 | /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param |
| 2479 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[node_index])) { |
| 2480 | // this parameter has a default value |
| 2481 | if (comp->have_bare_star) { |
| 2482 | comp->scope_cur->num_dict_params += 1; |
| 2483 | } else { |
| 2484 | comp->scope_cur->num_default_params += 1; |
| 2485 | } |
| 2486 | } |
| 2487 | */ |
| 2488 | if (comp->have_bare_star) { |
| 2489 | // comes after a bare star, so doesn't count as a parameter |
| 2490 | } else { |
| 2491 | comp->scope_cur->num_params += 1; |
| 2492 | } |
| 2493 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_star) { |
| 2494 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2495 | // bare star |
| 2496 | // TODO see http://www.python.org/dev/peps/pep-3102/ |
| 2497 | comp->have_bare_star = true; |
| 2498 | //assert(comp->scope_cur->num_dict_params == 0); |
| 2499 | } else if (PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 2500 | // named star |
| 2501 | comp->scope_cur->flags |= SCOPE_FLAG_VARARGS; |
| 2502 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2503 | } else if (allow_annotations && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) { |
| 2504 | // named star with annotation |
| 2505 | comp->scope_cur->flags |= SCOPE_FLAG_VARARGS; |
| 2506 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2507 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2508 | pn_annotation = pns->nodes[1]; |
| 2509 | } else { |
| 2510 | // shouldn't happen |
| 2511 | assert(0); |
| 2512 | } |
| 2513 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2514 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2515 | if (allow_annotations && !PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 2516 | // this parameter has an annotation |
| 2517 | pn_annotation = pns->nodes[1]; |
| 2518 | } |
| 2519 | comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2520 | } else { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2521 | // TODO anything to implement? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2522 | assert(0); |
| 2523 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | if (param_name != 0) { |
| 2527 | if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) { |
| 2528 | // TODO this parameter has an annotation |
| 2529 | } |
| 2530 | bool added; |
| 2531 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added); |
| 2532 | if (!added) { |
| 2533 | printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name)); |
| 2534 | return; |
| 2535 | } |
| 2536 | id_info->param = true; |
| 2537 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | void compile_scope_func_param(compiler_t *comp, py_parse_node_t pn) { |
| 2542 | compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true); |
| 2543 | } |
| 2544 | |
| 2545 | void compile_scope_lambda_param(compiler_t *comp, py_parse_node_t pn) { |
| 2546 | compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false); |
| 2547 | } |
| 2548 | |
| 2549 | void 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) { |
| 2550 | tail_recursion: |
| 2551 | if (PY_PARSE_NODE_IS_NULL(pn_iter)) { |
| 2552 | // no more nested if/for; compile inner expression |
| 2553 | compile_node(comp, pn_inner_expr); |
| 2554 | if (comp->scope_cur->kind == SCOPE_LIST_COMP) { |
| 2555 | EMIT(list_append, for_depth + 2); |
| 2556 | } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) { |
| 2557 | EMIT(map_add, for_depth + 2); |
| 2558 | } else if (comp->scope_cur->kind == SCOPE_SET_COMP) { |
| 2559 | EMIT(set_add, for_depth + 2); |
| 2560 | } else { |
| 2561 | EMIT(yield_value); |
| 2562 | EMIT(pop_top); |
| 2563 | } |
| 2564 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) { |
| 2565 | // if condition |
| 2566 | py_parse_node_struct_t *pns_comp_if = (py_parse_node_struct_t*)pn_iter; |
| 2567 | c_if_cond(comp, pns_comp_if->nodes[0], false, l_top); |
| 2568 | pn_iter = pns_comp_if->nodes[1]; |
| 2569 | goto tail_recursion; |
| 2570 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) { |
| 2571 | // for loop |
| 2572 | py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter; |
| 2573 | compile_node(comp, pns_comp_for2->nodes[1]); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2574 | int l_end2 = comp_next_label(comp); |
| 2575 | int l_top2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2576 | EMIT(get_iter); |
| 2577 | EMIT(label_assign, l_top2); |
| 2578 | EMIT(for_iter, l_end2); |
| 2579 | c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE); |
| 2580 | compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1); |
| 2581 | EMIT(jump, l_top2); |
| 2582 | EMIT(label_assign, l_end2); |
| 2583 | EMIT(for_iter_end); |
| 2584 | } else { |
| 2585 | // shouldn't happen |
| 2586 | assert(0); |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) { |
| 2591 | // see http://www.python.org/dev/peps/pep-0257/ |
| 2592 | |
| 2593 | // look for the first statement |
| 2594 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
| 2595 | // fall through |
| 2596 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) { |
| 2597 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 2598 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) { |
| 2599 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 2600 | } else { |
| 2601 | return; |
| 2602 | } |
| 2603 | |
| 2604 | // check the first statement for a doc string |
| 2605 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
| 2606 | py_parse_node_struct_t* pns = (py_parse_node_struct_t*)pn; |
| 2607 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 2608 | int kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[0]); |
| 2609 | if (kind == PY_PARSE_NODE_STRING) { |
| 2610 | compile_node(comp, pns->nodes[0]); // a doc string |
| 2611 | // store doc string |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2612 | EMIT(store_id, comp->qstr___doc__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
| 2619 | comp->pass = pass; |
| 2620 | comp->scope_cur = scope; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2621 | comp->next_label = 1; |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2622 | EMIT(start_pass, pass, scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2623 | |
| 2624 | if (comp->pass == PASS_1) { |
| 2625 | scope->stack_size = 0; |
| 2626 | } |
| 2627 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2628 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2629 | if (comp->pass == PASS_3) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2630 | scope_print_info(scope); |
| 2631 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2632 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2633 | |
| 2634 | // compile |
| 2635 | if (scope->kind == SCOPE_MODULE) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2636 | if (!comp->is_repl) { |
| 2637 | check_for_doc_string(comp, scope->pn); |
| 2638 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2639 | compile_node(comp, scope->pn); |
| 2640 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2641 | EMIT(return_value); |
| 2642 | } else if (scope->kind == SCOPE_FUNCTION) { |
| 2643 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2644 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2645 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
| 2646 | |
| 2647 | // work out number of parameters, keywords and default parameters, and add them to the id_info array |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2648 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2649 | if (comp->pass == PASS_1) { |
| 2650 | comp->have_bare_star = false; |
| 2651 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param); |
| 2652 | } |
| 2653 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2654 | assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something... |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2655 | |
| 2656 | compile_node(comp, pns->nodes[3]); // 3 is function body |
| 2657 | // emit return if it wasn't the last opcode |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2658 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2659 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2660 | EMIT(return_value); |
| 2661 | } |
| 2662 | } else if (scope->kind == SCOPE_LAMBDA) { |
| 2663 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2664 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2665 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3); |
| 2666 | |
| 2667 | // work out number of parameters, keywords and default parameters, and add them to the id_info array |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2668 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2669 | if (comp->pass == PASS_1) { |
| 2670 | comp->have_bare_star = false; |
| 2671 | apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param); |
| 2672 | } |
| 2673 | |
| 2674 | compile_node(comp, pns->nodes[1]); // 1 is lambda body |
| 2675 | EMIT(return_value); |
| 2676 | } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { |
| 2677 | // a bit of a hack at the moment |
| 2678 | |
| 2679 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2680 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2681 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 2682 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 2683 | py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2684 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2685 | qstr qstr_arg = qstr_from_str_static(".0"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2686 | if (comp->pass == PASS_1) { |
| 2687 | bool added; |
| 2688 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added); |
| 2689 | assert(added); |
| 2690 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2691 | scope->num_params = 1; |
| 2692 | } |
| 2693 | |
| 2694 | if (scope->kind == SCOPE_LIST_COMP) { |
| 2695 | EMIT(build_list, 0); |
| 2696 | } else if (scope->kind == SCOPE_DICT_COMP) { |
| 2697 | EMIT(build_map, 0); |
| 2698 | } else if (scope->kind == SCOPE_SET_COMP) { |
| 2699 | EMIT(build_set, 0); |
| 2700 | } |
| 2701 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2702 | int l_end = comp_next_label(comp); |
| 2703 | int l_top = comp_next_label(comp); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2704 | EMIT(load_id, qstr_arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2705 | EMIT(label_assign, l_top); |
| 2706 | EMIT(for_iter, l_end); |
| 2707 | c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE); |
| 2708 | compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0); |
| 2709 | EMIT(jump, l_top); |
| 2710 | EMIT(label_assign, l_end); |
| 2711 | EMIT(for_iter_end); |
| 2712 | |
| 2713 | if (scope->kind == SCOPE_GEN_EXPR) { |
| 2714 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2715 | } |
| 2716 | EMIT(return_value); |
| 2717 | } else { |
| 2718 | assert(scope->kind == SCOPE_CLASS); |
| 2719 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2720 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2721 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef); |
| 2722 | |
| 2723 | if (comp->pass == PASS_1) { |
| 2724 | bool added; |
| 2725 | id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added); |
| 2726 | assert(added); |
| 2727 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2728 | id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added); |
| 2729 | assert(added); |
| 2730 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2731 | id_info->param = true; |
| 2732 | scope->num_params = 1; // __locals__ is the parameter |
| 2733 | } |
| 2734 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2735 | EMIT(load_id, comp->qstr___locals__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2736 | EMIT(store_locals); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2737 | EMIT(load_id, comp->qstr___name__); |
| 2738 | EMIT(store_id, comp->qstr___module__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2739 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2740 | EMIT(store_id, comp->qstr___qualname__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2741 | |
| 2742 | check_for_doc_string(comp, pns->nodes[2]); |
| 2743 | compile_node(comp, pns->nodes[2]); // 2 is class body |
| 2744 | |
| 2745 | id_info_t *id = scope_find(scope, comp->qstr___class__); |
| 2746 | assert(id != NULL); |
| 2747 | if (id->kind == ID_INFO_KIND_LOCAL) { |
| 2748 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2749 | } else { |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 2750 | EMIT(load_closure, comp->qstr___class__, 0); // XXX check this is the correct local num |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2751 | } |
| 2752 | EMIT(return_value); |
| 2753 | } |
| 2754 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2755 | EMIT(end_pass); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2756 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
| 2760 | comp->pass = pass; |
| 2761 | comp->scope_cur = scope; |
| 2762 | comp->next_label = 1; |
| 2763 | |
| 2764 | if (scope->kind != SCOPE_FUNCTION) { |
| 2765 | printf("Error: inline assembler must be a function\n"); |
| 2766 | return; |
| 2767 | } |
| 2768 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 2769 | if (comp->pass > PASS_1) { |
| 2770 | EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur); |
| 2771 | } |
| 2772 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2773 | // get the function definition parse node |
| 2774 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2775 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2776 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
| 2777 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 2778 | //qstr f_id = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2779 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 2780 | // parameters are in pns->nodes[1] |
| 2781 | if (comp->pass == PASS_2) { |
| 2782 | py_parse_node_t *pn_params; |
| 2783 | int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params); |
| 2784 | scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params); |
| 2785 | } |
| 2786 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2787 | assert(PY_PARSE_NODE_IS_NULL(pns->nodes[2])); // type |
| 2788 | |
| 2789 | py_parse_node_t pn_body = pns->nodes[3]; // body |
| 2790 | py_parse_node_t *nodes; |
| 2791 | int num = list_get(&pn_body, PN_suite_block_stmts, &nodes); |
| 2792 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2793 | if (comp->pass == PASS_3) { |
| 2794 | //printf("----\n"); |
| 2795 | scope_print_info(scope); |
| 2796 | } |
| 2797 | |
| 2798 | for (int i = 0; i < num; i++) { |
| 2799 | assert(PY_PARSE_NODE_IS_STRUCT(nodes[i])); |
| 2800 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)nodes[i]; |
| 2801 | assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt); |
| 2802 | assert(PY_PARSE_NODE_IS_STRUCT(pns2->nodes[0])); |
| 2803 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[1])); |
| 2804 | pns2 = (py_parse_node_struct_t*)pns2->nodes[0]; |
| 2805 | assert(PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_power); |
| 2806 | assert(PY_PARSE_NODE_IS_ID(pns2->nodes[0])); |
| 2807 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)); |
| 2808 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[2])); |
| 2809 | qstr op = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]); |
| 2810 | pns2 = (py_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren |
| 2811 | py_parse_node_t *pn_arg; |
| 2812 | int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg); |
| 2813 | |
| 2814 | // emit instructions |
| 2815 | if (strcmp(qstr_str(op), "label") == 0) { |
| 2816 | if (!(n_args == 1 && PY_PARSE_NODE_IS_ID(pn_arg[0]))) { |
| 2817 | printf("SyntaxError: inline assembler 'label' requires 1 argument\n"); |
| 2818 | return; |
| 2819 | } |
| 2820 | int lab = comp_next_label(comp); |
| 2821 | if (pass > PASS_1) { |
| 2822 | EMIT_INLINE_ASM(label, lab, PY_PARSE_NODE_LEAF_ARG(pn_arg[0])); |
| 2823 | } |
| 2824 | } else { |
| 2825 | if (pass > PASS_1) { |
| 2826 | EMIT_INLINE_ASM(op, op, n_args, pn_arg); |
| 2827 | } |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | if (comp->pass > PASS_1) { |
| 2832 | EMIT_INLINE_ASM(end_pass); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2833 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { |
| 2837 | // in functions, turn implicit globals into explicit globals |
| 2838 | // compute num_locals, and the index of each local |
| 2839 | scope->num_locals = 0; |
| 2840 | for (int i = 0; i < scope->id_info_len; i++) { |
| 2841 | id_info_t *id = &scope->id_info[i]; |
| 2842 | if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) { |
| 2843 | // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL |
| 2844 | continue; |
| 2845 | } |
| 2846 | if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 2847 | id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 2848 | } |
| 2849 | if (id->param || id->kind == ID_INFO_KIND_LOCAL) { |
| 2850 | id->local_num = scope->num_locals; |
| 2851 | scope->num_locals += 1; |
| 2852 | } |
| 2853 | } |
| 2854 | |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 2855 | // TODO compute the index of free and cell vars (freevars[idx] in CPython) |
| 2856 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2857 | // compute flags |
| 2858 | //scope->flags = 0; since we set some things in parameters |
| 2859 | if (scope->kind != SCOPE_MODULE) { |
| 2860 | scope->flags |= SCOPE_FLAG_NEWLOCALS; |
| 2861 | } |
| 2862 | 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) { |
| 2863 | assert(scope->parent != NULL); |
| 2864 | scope->flags |= SCOPE_FLAG_OPTIMISED; |
| 2865 | |
| 2866 | // TODO possibly other ways it can be nested |
| 2867 | if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) { |
| 2868 | scope->flags |= SCOPE_FLAG_NESTED; |
| 2869 | } |
| 2870 | } |
| 2871 | int num_free = 0; |
| 2872 | for (int i = 0; i < scope->id_info_len; i++) { |
| 2873 | id_info_t *id = &scope->id_info[i]; |
| 2874 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 2875 | num_free += 1; |
| 2876 | } |
| 2877 | } |
| 2878 | if (num_free == 0) { |
| 2879 | scope->flags |= SCOPE_FLAG_NOFREE; |
| 2880 | } |
| 2881 | } |
| 2882 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2883 | bool py_compile(py_parse_node_t pn, bool is_repl) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2884 | compiler_t *comp = m_new(compiler_t, 1); |
| 2885 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2886 | comp->qstr___class__ = qstr_from_str_static("__class__"); |
| 2887 | comp->qstr___locals__ = qstr_from_str_static("__locals__"); |
| 2888 | comp->qstr___name__ = qstr_from_str_static("__name__"); |
| 2889 | comp->qstr___module__ = qstr_from_str_static("__module__"); |
| 2890 | comp->qstr___qualname__ = qstr_from_str_static("__qualname__"); |
| 2891 | comp->qstr___doc__ = qstr_from_str_static("__doc__"); |
| 2892 | comp->qstr_assertion_error = qstr_from_str_static("AssertionError"); |
| 2893 | comp->qstr_micropython = qstr_from_str_static("micropython"); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2894 | comp->qstr_byte_code = qstr_from_str_static("byte_code"); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2895 | comp->qstr_native = qstr_from_str_static("native"); |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 2896 | comp->qstr_viper = qstr_from_str_static("viper"); |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame] | 2897 | comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb"); |
Damien | d793389 | 2013-11-28 19:12:18 +0000 | [diff] [blame] | 2898 | comp->qstr_range = qstr_from_str_static("range"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2899 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2900 | comp->is_repl = is_repl; |
| 2901 | comp->had_error = false; |
| 2902 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2903 | comp->break_label = 0; |
| 2904 | comp->continue_label = 0; |
| 2905 | comp->except_nest_level = 0; |
| 2906 | comp->scope_head = NULL; |
| 2907 | comp->scope_cur = NULL; |
| 2908 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2909 | // optimise constants |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2910 | pn = fold_constants(pn); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2911 | |
| 2912 | // set the outer scope |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2913 | scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2914 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2915 | // compile pass 1 |
| 2916 | comp->emit = emit_pass1_new(comp->qstr___class__); |
| 2917 | comp->emit_method_table = &emit_pass1_method_table; |
| 2918 | comp->emit_inline_asm = NULL; |
| 2919 | comp->emit_inline_asm_method_table = NULL; |
| 2920 | uint max_num_labels = 0; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2921 | for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2922 | if (false) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2923 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2924 | } else if (s->emit_options == EMIT_OPT_ASM_THUMB) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2925 | compile_scope_inline_asm(comp, s, PASS_1); |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2926 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2927 | } else { |
| 2928 | compile_scope(comp, s, PASS_1); |
| 2929 | } |
| 2930 | |
| 2931 | // update maximim number of labels needed |
| 2932 | if (comp->next_label > max_num_labels) { |
| 2933 | max_num_labels = comp->next_label; |
| 2934 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2935 | } |
| 2936 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2937 | // compute some things related to scope and identifiers |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2938 | for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2939 | compile_scope_compute_things(comp, s); |
| 2940 | } |
| 2941 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2942 | // finish with pass 1 |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2943 | emit_pass1_free(comp->emit); |
| 2944 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2945 | // compile pass 2 and 3 |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2946 | #if !MICROPY_EMIT_CPYTHON |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2947 | emit_t *emit_bc = NULL; |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 2948 | emit_t *emit_native = NULL; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2949 | #endif |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2950 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2951 | emit_inline_asm_t *emit_inline_thumb = NULL; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2952 | #endif |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2953 | for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2954 | if (false) { |
| 2955 | // dummy |
| 2956 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2957 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2958 | } else if (s->emit_options == EMIT_OPT_ASM_THUMB) { |
| 2959 | // inline assembly for thumb |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2960 | if (emit_inline_thumb == NULL) { |
| 2961 | emit_inline_thumb = emit_inline_thumb_new(max_num_labels); |
| 2962 | } |
| 2963 | comp->emit = NULL; |
| 2964 | comp->emit_method_table = NULL; |
| 2965 | comp->emit_inline_asm = emit_inline_thumb; |
| 2966 | comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table; |
| 2967 | compile_scope_inline_asm(comp, s, PASS_2); |
| 2968 | compile_scope_inline_asm(comp, s, PASS_3); |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2969 | #endif |
| 2970 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2971 | } else { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2972 | |
| 2973 | // choose the emit type |
| 2974 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2975 | #if MICROPY_EMIT_CPYTHON |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2976 | comp->emit = emit_cpython_new(max_num_labels); |
| 2977 | comp->emit_method_table = &emit_cpython_method_table; |
| 2978 | #else |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2979 | switch (s->emit_options) { |
| 2980 | case EMIT_OPT_NATIVE_PYTHON: |
Damien | 3410be8 | 2013-10-07 23:09:10 +0100 | [diff] [blame] | 2981 | case EMIT_OPT_VIPER: |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2982 | #if MICROPY_EMIT_X64 |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 2983 | if (emit_native == NULL) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 2984 | emit_native = emit_native_x64_new(max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2985 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 2986 | comp->emit_method_table = &emit_native_x64_method_table; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 2987 | #elif MICROPY_EMIT_THUMB |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 2988 | if (emit_native == NULL) { |
| 2989 | emit_native = emit_native_thumb_new(max_num_labels); |
| 2990 | } |
| 2991 | comp->emit_method_table = &emit_native_thumb_method_table; |
| 2992 | #endif |
| 2993 | comp->emit = emit_native; |
Damien | 3410be8 | 2013-10-07 23:09:10 +0100 | [diff] [blame] | 2994 | comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER); |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 2995 | break; |
| 2996 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 2997 | default: |
| 2998 | if (emit_bc == NULL) { |
| 2999 | emit_bc = emit_bc_new(max_num_labels); |
| 3000 | } |
| 3001 | comp->emit = emit_bc; |
| 3002 | comp->emit_method_table = &emit_bc_method_table; |
| 3003 | break; |
| 3004 | } |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3005 | #endif |
| 3006 | |
| 3007 | // compile pass 2 and pass 3 |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3008 | compile_scope(comp, s, PASS_2); |
| 3009 | compile_scope(comp, s, PASS_3); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 3010 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | m_free(comp); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 3014 | |
| 3015 | return !comp->had_error; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3016 | } |