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