Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 6 | * Copyright (c) 2013-2015 Damien P. George |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 33 | #include "py/scope.h" |
| 34 | #include "py/emit.h" |
| 35 | #include "py/compile.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 36 | #include "py/runtime.h" |
Damien George | dd53b12 | 2016-12-09 20:54:54 +1100 | [diff] [blame] | 37 | #include "py/asmbase.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 38 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 39 | #if MICROPY_ENABLE_COMPILER |
| 40 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 41 | // TODO need to mangle __attr names |
| 42 | |
| 43 | typedef enum { |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 44 | // define rules with a compile function |
Damien George | 00208ce | 2014-01-23 00:00:53 +0000 | [diff] [blame] | 45 | #define DEF_RULE(rule, comp, kind, ...) PN_##rule, |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 46 | #define DEF_RULE_NC(rule, kind, ...) |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 47 | #include "py/grammar.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | #undef DEF_RULE |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 49 | #undef DEF_RULE_NC |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 50 | PN_const_object, // special node for a constant, generic Python object |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 51 | // define rules without a compile function |
| 52 | #define DEF_RULE(rule, comp, kind, ...) |
| 53 | #define DEF_RULE_NC(rule, kind, ...) PN_##rule, |
| 54 | #include "py/grammar.h" |
| 55 | #undef DEF_RULE |
| 56 | #undef DEF_RULE_NC |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 57 | } pn_kind_t; |
| 58 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 59 | #define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 60 | |
| 61 | #if NEED_METHOD_TABLE |
| 62 | |
| 63 | // we need a method table to do the lookup for the emitter functions |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 64 | #define EMIT(fun) (comp->emit_method_table->fun(comp->emit)) |
| 65 | #define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__)) |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 66 | #define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num)) |
| 67 | #define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 68 | |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 69 | #else |
| 70 | |
| 71 | // if we only have the bytecode emitter enabled then we can do a direct call to the functions |
| 72 | #define EMIT(fun) (mp_emit_bc_##fun(comp->emit)) |
| 73 | #define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__)) |
| 74 | #define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num)) |
| 75 | #define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst)) |
| 76 | |
| 77 | #endif |
| 78 | |
Damien George | 080a78b | 2016-12-07 11:17:17 +1100 | [diff] [blame] | 79 | #if MICROPY_EMIT_NATIVE |
| 80 | // define a macro to access external native emitter |
| 81 | #if MICROPY_EMIT_X64 |
| 82 | #define NATIVE_EMITTER(f) emit_native_x64_##f |
| 83 | #elif MICROPY_EMIT_X86 |
| 84 | #define NATIVE_EMITTER(f) emit_native_x86_##f |
| 85 | #elif MICROPY_EMIT_THUMB |
| 86 | #define NATIVE_EMITTER(f) emit_native_thumb_##f |
| 87 | #elif MICROPY_EMIT_ARM |
| 88 | #define NATIVE_EMITTER(f) emit_native_arm_##f |
Damien George | 8e5aced | 2016-12-09 16:39:39 +1100 | [diff] [blame] | 89 | #elif MICROPY_EMIT_XTENSA |
| 90 | #define NATIVE_EMITTER(f) emit_native_xtensa_##f |
Damien George | 080a78b | 2016-12-07 11:17:17 +1100 | [diff] [blame] | 91 | #else |
| 92 | #error "unknown native emitter" |
| 93 | #endif |
| 94 | #endif |
| 95 | |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 96 | #if MICROPY_EMIT_INLINE_ASM |
| 97 | // define macros for inline assembler |
| 98 | #if MICROPY_EMIT_INLINE_THUMB |
| 99 | #define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb |
| 100 | #define ASM_EMITTER(f) emit_inline_thumb_##f |
Damien George | f76b1bf | 2016-12-09 17:03:33 +1100 | [diff] [blame] | 101 | #elif MICROPY_EMIT_INLINE_XTENSA |
| 102 | #define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa |
| 103 | #define ASM_EMITTER(f) emit_inline_xtensa_##f |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 104 | #else |
| 105 | #error "unknown asm emitter" |
| 106 | #endif |
| 107 | #endif |
| 108 | |
Damien George | 0496de2 | 2015-10-03 17:07:54 +0100 | [diff] [blame] | 109 | #define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm)) |
| 110 | #define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__)) |
| 111 | |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 112 | // elements in this struct are ordered to make it compact |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 113 | typedef struct _compiler_t { |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 114 | qstr source_file; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 115 | |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 116 | uint8_t is_repl; |
| 117 | uint8_t pass; // holds enum type pass_kind_t |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 118 | uint8_t have_star; |
| 119 | |
| 120 | // try to keep compiler clean from nlr |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 121 | mp_obj_t compile_error; // set to an exception object if there's an error |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 122 | size_t compile_error_line; // set to best guess of line of error |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 123 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 124 | uint next_label; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 125 | |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 126 | uint16_t num_dict_params; |
| 127 | uint16_t num_default_params; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 128 | |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 129 | uint16_t break_label; // highest bit set indicates we are breaking out of a for loop |
| 130 | uint16_t continue_label; |
| 131 | uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 132 | uint16_t break_continue_except_level; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 133 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 134 | scope_t *scope_head; |
| 135 | scope_t *scope_cur; |
| 136 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 137 | emit_t *emit; // current emitter |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 138 | #if NEED_METHOD_TABLE |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 139 | const emit_method_table_t *emit_method_table; // current emit method table |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 140 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 141 | |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 142 | #if MICROPY_EMIT_INLINE_ASM |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 143 | emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm |
| 144 | const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm |
Damien George | a77ffe6 | 2015-03-14 12:59:31 +0000 | [diff] [blame] | 145 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 146 | } compiler_t; |
| 147 | |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 148 | STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) { |
| 149 | // if the line of the error is unknown then try to update it from the pn |
| 150 | if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 151 | comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line; |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 152 | } |
Damien George | 84d59c2 | 2015-07-27 22:20:00 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) { |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 156 | // only register the error if there has been no other error |
| 157 | if (comp->compile_error == MP_OBJ_NULL) { |
| 158 | comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg); |
| 159 | compile_error_set_line(comp, pn); |
| 160 | } |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 163 | STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 164 | STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 165 | STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 166 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 167 | STATIC uint comp_next_label(compiler_t *comp) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 168 | return comp->next_label++; |
| 169 | } |
| 170 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 171 | STATIC void compile_increase_except_level(compiler_t *comp) { |
| 172 | comp->cur_except_level += 1; |
| 173 | if (comp->cur_except_level > comp->scope_cur->exc_stack_size) { |
| 174 | comp->scope_cur->exc_stack_size = comp->cur_except_level; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | STATIC void compile_decrease_except_level(compiler_t *comp) { |
| 179 | assert(comp->cur_except_level > 0); |
| 180 | comp->cur_except_level -= 1; |
| 181 | } |
| 182 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 183 | STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) { |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 184 | scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 185 | scope->parent = comp->scope_cur; |
| 186 | scope->next = NULL; |
| 187 | if (comp->scope_head == NULL) { |
| 188 | comp->scope_head = scope; |
| 189 | } else { |
| 190 | scope_t *s = comp->scope_head; |
| 191 | while (s->next != NULL) { |
| 192 | s = s->next; |
| 193 | } |
| 194 | s->next = scope; |
| 195 | } |
| 196 | return scope; |
| 197 | } |
| 198 | |
Damien George | 91bc32d | 2015-04-09 15:31:53 +0000 | [diff] [blame] | 199 | typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn); |
| 200 | |
| 201 | STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 202 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 203 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 204 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 205 | for (int i = 0; i < num_nodes; i++) { |
| 206 | f(comp, pns->nodes[i]); |
| 207 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 208 | } else if (!MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 209 | f(comp, pn); |
| 210 | } |
| 211 | } |
| 212 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 213 | STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 214 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 215 | for (int i = 0; i < num_nodes; i++) { |
| 216 | compile_node(comp, pns->nodes[i]); |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 217 | if (comp->compile_error != MP_OBJ_NULL) { |
| 218 | // add line info for the error in case it didn't have a line number |
| 219 | compile_error_set_line(comp, pns->nodes[i]); |
| 220 | return; |
| 221 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 225 | STATIC void compile_load_id(compiler_t *comp, qstr qst) { |
| 226 | if (comp->pass == MP_PASS_SCOPE) { |
| 227 | mp_emit_common_get_id_for_load(comp->scope_cur, qst); |
| 228 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 229 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 230 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 231 | #else |
| 232 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst); |
| 233 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | STATIC void compile_store_id(compiler_t *comp, qstr qst) { |
| 238 | if (comp->pass == MP_PASS_SCOPE) { |
| 239 | mp_emit_common_get_id_for_modification(comp->scope_cur, qst); |
| 240 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 241 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 242 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 243 | #else |
| 244 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst); |
| 245 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | STATIC void compile_delete_id(compiler_t *comp, qstr qst) { |
| 250 | if (comp->pass == MP_PASS_SCOPE) { |
| 251 | mp_emit_common_get_id_for_modification(comp->scope_cur, qst); |
| 252 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 253 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 254 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 255 | #else |
| 256 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst); |
| 257 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 261 | STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 262 | int total = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 263 | if (!MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 264 | compile_node(comp, pn); |
| 265 | total += 1; |
| 266 | } |
| 267 | if (pns_list != NULL) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 268 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 269 | for (int i = 0; i < n; i++) { |
| 270 | compile_node(comp, pns_list->nodes[i]); |
| 271 | } |
| 272 | total += n; |
| 273 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 274 | EMIT_ARG(build_tuple, total); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 275 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 276 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 277 | STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 278 | // a simple tuple expression |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 279 | c_tuple(comp, MP_PARSE_NODE_NULL, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 282 | STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) { |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 283 | if (mp_parse_node_is_const_false(pn)) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 284 | if (jump_if == false) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 285 | EMIT_ARG(jump, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 286 | } |
| 287 | return; |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 288 | } else if (mp_parse_node_is_const_true(pn)) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 289 | if (jump_if == true) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 290 | EMIT_ARG(jump, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 291 | } |
| 292 | return; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 293 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 294 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 295 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 296 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 297 | if (jump_if == false) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 298 | and_or_logic1:; |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 299 | uint label2 = comp_next_label(comp); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 300 | for (int i = 0; i < n - 1; i++) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 301 | c_if_cond(comp, pns->nodes[i], !jump_if, label2); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 302 | } |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 303 | c_if_cond(comp, pns->nodes[n - 1], jump_if, label); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 304 | EMIT_ARG(label_assign, label2); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 305 | } else { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 306 | and_or_logic2: |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 307 | for (int i = 0; i < n; i++) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 308 | c_if_cond(comp, pns->nodes[i], jump_if, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | return; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 312 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 313 | if (jump_if == false) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 314 | goto and_or_logic2; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 315 | } else { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 316 | goto and_or_logic1; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 317 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 318 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 319 | c_if_cond(comp, pns->nodes[0], !jump_if, label); |
| 320 | return; |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 321 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) { |
| 322 | // cond is something in parenthesis |
| 323 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 324 | // empty tuple, acts as false for the condition |
| 325 | if (jump_if == false) { |
| 326 | EMIT_ARG(jump, label); |
| 327 | } |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 328 | } else { |
| 329 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 330 | // non-empty tuple, acts as true for the condition |
| 331 | if (jump_if == true) { |
| 332 | EMIT_ARG(jump, label); |
| 333 | } |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 334 | } |
| 335 | return; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | // nothing special, fall back to default compiling for node and jump |
| 340 | compile_node(comp, pn); |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 341 | EMIT_ARG(pop_jump_if, jump_if, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t; |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 345 | STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 346 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 347 | STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 348 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 349 | compile_node(comp, pns->nodes[0]); |
| 350 | } |
| 351 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 352 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 353 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 354 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 355 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 356 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 357 | for (int i = 0; i < n - 1; i++) { |
| 358 | compile_node(comp, pns1->nodes[i]); |
| 359 | } |
| 360 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 361 | assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 362 | pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 363 | } |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 364 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 365 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 366 | EMIT(rot_three); |
| 367 | EMIT(store_subscr); |
| 368 | } else { |
| 369 | compile_node(comp, pns1->nodes[0]); |
| 370 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 371 | EMIT(dup_top_two); |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 372 | EMIT(load_subscr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 373 | } else { |
| 374 | EMIT(store_subscr); |
| 375 | } |
| 376 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 377 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 378 | assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 379 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 380 | EMIT(dup_top); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 381 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 382 | } else { |
| 383 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 384 | EMIT(rot_two); |
| 385 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 386 | EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 387 | } |
| 388 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 389 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 390 | } |
| 391 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 392 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 395 | return; |
| 396 | |
| 397 | cannot_assign: |
| 398 | compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 401 | // we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail) |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 402 | STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 403 | uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1; |
| 404 | |
| 405 | // look for star expression |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 406 | uint have_star_index = -1; |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 407 | if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) { |
| 408 | EMIT_ARG(unpack_ex, 0, num_tail); |
| 409 | have_star_index = 0; |
| 410 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 411 | for (uint i = 0; i < num_tail; i++) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 412 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 413 | if (have_star_index == (uint)-1) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 414 | EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1); |
| 415 | have_star_index = num_head + i; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 416 | } else { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 417 | compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | } |
| 421 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 422 | if (have_star_index == (uint)-1) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 423 | EMIT_ARG(unpack_sequence, num_head + num_tail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 424 | } |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 425 | if (num_head != 0) { |
| 426 | if (0 == have_star_index) { |
| 427 | c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 428 | } else { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 429 | c_assign(comp, node_head, ASSIGN_STORE); |
| 430 | } |
| 431 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 432 | for (uint i = 0; i < num_tail; i++) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 433 | if (num_head + i == have_star_index) { |
| 434 | c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE); |
| 435 | } else { |
| 436 | c_assign(comp, nodes_tail[i], ASSIGN_STORE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // assigns top of stack to pn |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 442 | STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) { |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 443 | assert(!MP_PARSE_NODE_IS_NULL(pn)); |
| 444 | if (MP_PARSE_NODE_IS_LEAF(pn)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 445 | if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 446 | qstr arg = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 447 | switch (assign_kind) { |
| 448 | case ASSIGN_STORE: |
| 449 | case ASSIGN_AUG_STORE: |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 450 | compile_store_id(comp, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 451 | break; |
| 452 | case ASSIGN_AUG_LOAD: |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 453 | default: |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 454 | compile_load_id(comp, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | } else { |
Damien George | f55a059 | 2017-03-29 12:28:33 +1100 | [diff] [blame] | 458 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 459 | } |
| 460 | } else { |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 461 | // pn must be a struct |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 462 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 463 | switch (MP_PARSE_NODE_STRUCT_KIND(pns)) { |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 464 | case PN_atom_expr_normal: |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 465 | // lhs is an index or attribute |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 466 | c_assign_atom_expr(comp, pns, assign_kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case PN_testlist_star_expr: |
| 470 | case PN_exprlist: |
| 471 | // lhs is a tuple |
| 472 | if (assign_kind != ASSIGN_STORE) { |
Damien George | f55a059 | 2017-03-29 12:28:33 +1100 | [diff] [blame] | 473 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 474 | } |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 475 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | case PN_atom_paren: |
| 479 | // lhs is something in parenthesis |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 480 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 481 | // empty tuple |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 482 | goto cannot_assign; |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 483 | } else { |
| 484 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien George | 44f65c0 | 2015-03-25 23:06:48 +0000 | [diff] [blame] | 485 | if (assign_kind != ASSIGN_STORE) { |
Damien George | f55a059 | 2017-03-29 12:28:33 +1100 | [diff] [blame] | 486 | goto cannot_assign; |
Damien George | 44f65c0 | 2015-03-25 23:06:48 +0000 | [diff] [blame] | 487 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 488 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 489 | goto testlist_comp; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 490 | } |
| 491 | break; |
| 492 | |
| 493 | case PN_atom_bracket: |
| 494 | // lhs is something in brackets |
| 495 | if (assign_kind != ASSIGN_STORE) { |
Damien George | f55a059 | 2017-03-29 12:28:33 +1100 | [diff] [blame] | 496 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 497 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 498 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 499 | // empty list, assignment allowed |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 500 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 501 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 502 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 503 | goto testlist_comp; |
| 504 | } else { |
| 505 | // brackets around 1 item |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 506 | c_assign_tuple(comp, pns->nodes[0], 0, NULL); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 507 | } |
| 508 | break; |
| 509 | |
| 510 | default: |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 511 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 512 | } |
| 513 | return; |
| 514 | |
| 515 | testlist_comp: |
| 516 | // lhs is a sequence |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 517 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 518 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 519 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 520 | // sequence of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 521 | assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 522 | c_assign_tuple(comp, pns->nodes[0], 0, NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 523 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 524 | // sequence of many items |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 525 | uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2); |
| 526 | c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes); |
Damien George | 216a711 | 2016-09-30 14:48:06 +1000 | [diff] [blame] | 527 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 528 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 529 | } else { |
| 530 | // sequence with 2 items |
| 531 | goto sequence_with_2_items; |
| 532 | } |
| 533 | } else { |
| 534 | // sequence with 2 items |
| 535 | sequence_with_2_items: |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 536 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 537 | } |
| 538 | return; |
| 539 | } |
| 540 | return; |
| 541 | |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 542 | cannot_assign: |
| 543 | compile_syntax_error(comp, pn, "can't assign to expression"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 546 | // stuff for lambda and comprehensions and generators: |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 547 | // if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults |
| 548 | // if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults |
| 549 | // if both exist, the tuple is above the dictionary (ie the first pop gets the tuple) |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 550 | STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) { |
Damien George | 3056509 | 2014-03-31 11:30:17 +0100 | [diff] [blame] | 551 | assert(n_pos_defaults >= 0); |
| 552 | assert(n_kw_defaults >= 0); |
| 553 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 554 | // set flags |
| 555 | if (n_kw_defaults > 0) { |
| 556 | this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS; |
| 557 | } |
| 558 | this_scope->num_def_pos_args = n_pos_defaults; |
| 559 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 560 | // make closed over variables, if any |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 561 | // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 562 | int nfree = 0; |
| 563 | if (comp->scope_cur->kind != SCOPE_MODULE) { |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 564 | for (int i = 0; i < comp->scope_cur->id_info_len; i++) { |
| 565 | id_info_t *id = &comp->scope_cur->id_info[i]; |
| 566 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 567 | for (int j = 0; j < this_scope->id_info_len; j++) { |
| 568 | id_info_t *id2 = &this_scope->id_info[j]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 569 | if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 570 | // in Micro Python we load closures using LOAD_FAST |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 571 | EMIT_LOAD_FAST(id->qst, id->local_num); |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 572 | nfree += 1; |
| 573 | } |
| 574 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 578 | |
| 579 | // make the function/closure |
| 580 | if (nfree == 0) { |
Damien George | 3056509 | 2014-03-31 11:30:17 +0100 | [diff] [blame] | 581 | EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 582 | } else { |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 583 | EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 587 | STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 40b40ff | 2017-04-22 14:23:47 +1000 | [diff] [blame] | 588 | // For efficiency of the code below we extract the parse-node kind here |
| 589 | int pn_kind; |
| 590 | if (MP_PARSE_NODE_IS_ID(pn)) { |
| 591 | pn_kind = -1; |
| 592 | } else { |
| 593 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 594 | pn_kind = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn); |
| 595 | } |
| 596 | |
| 597 | if (pn_kind == PN_typedargslist_star || pn_kind == PN_varargslist_star) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 598 | comp->have_star = true; |
| 599 | /* don't need to distinguish bare from named star |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 600 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 601 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 602 | // bare star |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 603 | } else { |
| 604 | // named star |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 605 | } |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 606 | */ |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 607 | |
Damien George | 40b40ff | 2017-04-22 14:23:47 +1000 | [diff] [blame] | 608 | } else if (pn_kind == PN_typedargslist_dbl_star || pn_kind == PN_varargslist_dbl_star) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 609 | // named double star |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 610 | // TODO do we need to do anything with this? |
| 611 | |
| 612 | } else { |
| 613 | mp_parse_node_t pn_id; |
| 614 | mp_parse_node_t pn_colon; |
| 615 | mp_parse_node_t pn_equal; |
Damien George | 40b40ff | 2017-04-22 14:23:47 +1000 | [diff] [blame] | 616 | if (pn_kind == -1) { |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 617 | // this parameter is just an id |
| 618 | |
| 619 | pn_id = pn; |
| 620 | pn_colon = MP_PARSE_NODE_NULL; |
| 621 | pn_equal = MP_PARSE_NODE_NULL; |
| 622 | |
Damien George | 40b40ff | 2017-04-22 14:23:47 +1000 | [diff] [blame] | 623 | } else if (pn_kind == PN_typedargslist_name) { |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 624 | // this parameter has a colon and/or equal specifier |
| 625 | |
| 626 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 627 | pn_id = pns->nodes[0]; |
| 628 | pn_colon = pns->nodes[1]; |
| 629 | pn_equal = pns->nodes[2]; |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 630 | |
| 631 | } else { |
Damien George | 40b40ff | 2017-04-22 14:23:47 +1000 | [diff] [blame] | 632 | assert(pn_kind == PN_varargslist_name); // should be |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 633 | // this parameter has an equal specifier |
| 634 | |
| 635 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 636 | pn_id = pns->nodes[0]; |
| 637 | pn_equal = pns->nodes[1]; |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (MP_PARSE_NODE_IS_NULL(pn_equal)) { |
| 641 | // this parameter does not have a default value |
| 642 | |
| 643 | // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid) |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 644 | if (!comp->have_star && comp->num_default_params != 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 645 | compile_syntax_error(comp, pn, "non-default argument follows default argument"); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
| 649 | } else { |
| 650 | // this parameter has a default value |
| 651 | // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why |
| 652 | |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 653 | if (comp->have_star) { |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 654 | comp->num_dict_params += 1; |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 655 | // in Micro Python we put the default dict parameters into a dictionary using the bytecode |
| 656 | if (comp->num_dict_params == 1) { |
Damien George | 7b43301 | 2014-04-12 00:05:49 +0100 | [diff] [blame] | 657 | // in Micro Python we put the default positional parameters into a tuple using the bytecode |
| 658 | // we need to do this here before we start building the map for the default keywords |
| 659 | if (comp->num_default_params > 0) { |
| 660 | EMIT_ARG(build_tuple, comp->num_default_params); |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 661 | } else { |
| 662 | EMIT(load_null); // sentinel indicating empty default positional args |
Damien George | 7b43301 | 2014-04-12 00:05:49 +0100 | [diff] [blame] | 663 | } |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 664 | // first default dict param, so make the map |
| 665 | EMIT_ARG(build_map, 0); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 666 | } |
Damien George | f0778a7 | 2014-06-07 22:01:00 +0100 | [diff] [blame] | 667 | |
| 668 | // compile value then key, then store it to the dict |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 669 | compile_node(comp, pn_equal); |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 670 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id)); |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 671 | EMIT(store_map); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 672 | } else { |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 673 | comp->num_default_params += 1; |
| 674 | compile_node(comp, pn_equal); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | // TODO pn_colon not implemented |
| 679 | (void)pn_colon; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 683 | STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) { |
Damien George | 29e9db0 | 2015-12-12 13:42:51 +0000 | [diff] [blame] | 684 | // When we call compile_funcdef_lambdef_param below it can compile an arbitrary |
| 685 | // expression for default arguments, which may contain a lambda. The lambda will |
| 686 | // call here in a nested way, so we must save and restore the relevant state. |
| 687 | bool orig_have_star = comp->have_star; |
| 688 | uint16_t orig_num_dict_params = comp->num_dict_params; |
| 689 | uint16_t orig_num_default_params = comp->num_default_params; |
| 690 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 691 | // compile default parameters |
| 692 | comp->have_star = false; |
| 693 | comp->num_dict_params = 0; |
| 694 | comp->num_default_params = 0; |
| 695 | apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param); |
| 696 | |
| 697 | if (comp->compile_error != MP_OBJ_NULL) { |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | // in Micro Python we put the default positional parameters into a tuple using the bytecode |
| 702 | // the default keywords args may have already made the tuple; if not, do it now |
| 703 | if (comp->num_default_params > 0 && comp->num_dict_params == 0) { |
| 704 | EMIT_ARG(build_tuple, comp->num_default_params); |
| 705 | EMIT(load_null); // sentinel indicating empty default keyword args |
| 706 | } |
| 707 | |
| 708 | // make the function |
| 709 | close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params); |
Damien George | 29e9db0 | 2015-12-12 13:42:51 +0000 | [diff] [blame] | 710 | |
| 711 | // restore state |
| 712 | comp->have_star = orig_have_star; |
| 713 | comp->num_dict_params = orig_num_dict_params; |
| 714 | comp->num_default_params = orig_num_default_params; |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 717 | // leaves function object on stack |
| 718 | // returns function name |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 719 | STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 720 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 721 | // create a new scope for this function |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 722 | scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 723 | // store the function scope so the compiling function can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 724 | pns->nodes[4] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 727 | // get the scope for this function |
| 728 | scope_t *fscope = (scope_t*)pns->nodes[4]; |
| 729 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 730 | // compile the function definition |
| 731 | compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 732 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 733 | // return its name (the 'f' in "def f(...):") |
| 734 | return fscope->simple_name; |
| 735 | } |
| 736 | |
| 737 | // leaves class object on stack |
| 738 | // returns class name |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 739 | STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 740 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 741 | // create a new scope for this class |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 742 | scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 743 | // store the class scope so the compiling function can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 744 | pns->nodes[3] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | EMIT(load_build_class); |
| 748 | |
| 749 | // scope for this class |
| 750 | scope_t *cscope = (scope_t*)pns->nodes[3]; |
| 751 | |
| 752 | // compile the class |
| 753 | close_over_variables_etc(comp, cscope, 0, 0); |
| 754 | |
| 755 | // get its name |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 756 | EMIT_ARG(load_const_str, cscope->simple_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 757 | |
| 758 | // nodes[1] has parent classes, if any |
Damien George | 804760b | 2014-03-30 23:06:37 +0100 | [diff] [blame] | 759 | // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling |
| 760 | mp_parse_node_t parents = pns->nodes[1]; |
| 761 | if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) { |
| 762 | parents = MP_PARSE_NODE_NULL; |
| 763 | } |
Damien George | 804760b | 2014-03-30 23:06:37 +0100 | [diff] [blame] | 764 | compile_trailer_paren_helper(comp, parents, false, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 765 | |
| 766 | // return its name (the 'C' in class C(...):") |
| 767 | return cscope->simple_name; |
| 768 | } |
| 769 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 770 | // returns true if it was a built-in decorator (even if the built-in had an error) |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 771 | STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 772 | if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 773 | return false; |
| 774 | } |
| 775 | |
| 776 | if (name_len != 2) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 777 | compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator"); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 778 | return true; |
| 779 | } |
| 780 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 781 | qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 782 | if (attr == MP_QSTR_bytecode) { |
Damien George | 96f137b | 2014-05-12 22:35:37 +0100 | [diff] [blame] | 783 | *emit_options = MP_EMIT_OPT_BYTECODE; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 784 | #if MICROPY_EMIT_NATIVE |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 785 | } else if (attr == MP_QSTR_native) { |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 786 | *emit_options = MP_EMIT_OPT_NATIVE_PYTHON; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 787 | } else if (attr == MP_QSTR_viper) { |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 788 | *emit_options = MP_EMIT_OPT_VIPER; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 789 | #endif |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 790 | #if MICROPY_EMIT_INLINE_ASM |
| 791 | } else if (attr == ASM_DECORATOR_QSTR) { |
| 792 | *emit_options = MP_EMIT_OPT_ASM; |
| 793 | #endif |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 794 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 795 | compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator"); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | return true; |
| 799 | } |
| 800 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 801 | STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 802 | // get the list of decorators |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 803 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 804 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 805 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 806 | // inherit emit options for this function/class definition |
| 807 | uint emit_options = comp->scope_cur->emit_options; |
| 808 | |
| 809 | // compile each decorator |
| 810 | int num_built_in_decorators = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 811 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 812 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be |
| 813 | mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i]; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 814 | |
| 815 | // nodes[0] contains the decorator function, which is a dotted name |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 816 | mp_parse_node_t *name_nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 817 | int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 818 | |
| 819 | // check for built-in decorators |
| 820 | if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) { |
| 821 | // this was a built-in |
| 822 | num_built_in_decorators += 1; |
| 823 | |
| 824 | } else { |
| 825 | // not a built-in, compile normally |
| 826 | |
| 827 | // compile the decorator function |
| 828 | compile_node(comp, name_nodes[0]); |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 829 | for (int j = 1; j < name_len; j++) { |
| 830 | assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be |
| 831 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j])); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // nodes[1] contains arguments to the decorator function, if any |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 835 | if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 836 | // call the decorator function with the arguments in nodes[1] |
| 837 | compile_node(comp, pns_decorator->nodes[1]); |
| 838 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 842 | // compile the body (funcdef, async funcdef or classdef) and get its name |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 843 | mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 844 | qstr body_name = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 845 | if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 846 | body_name = compile_funcdef_helper(comp, pns_body, emit_options); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 847 | #if MICROPY_PY_ASYNC_AWAIT |
| 848 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) { |
| 849 | assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0])); |
| 850 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0]; |
| 851 | body_name = compile_funcdef_helper(comp, pns0, emit_options); |
| 852 | scope_t *fscope = (scope_t*)pns0->nodes[4]; |
| 853 | fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
| 854 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 855 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 856 | assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be |
| 857 | body_name = compile_classdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | // call each decorator |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 861 | for (int i = 0; i < n - num_built_in_decorators; i++) { |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 862 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | // store func/class object into name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 866 | compile_store_id(comp, body_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 867 | } |
| 868 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 869 | STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 870 | qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 871 | // store function object into function name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 872 | compile_store_id(comp, fname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 875 | STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 876 | if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 877 | compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn)); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 878 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 879 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 880 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 881 | compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 882 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 883 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 884 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 885 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 886 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 887 | for (int i = 0; i < n - 1; i++) { |
| 888 | compile_node(comp, pns1->nodes[i]); |
| 889 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 890 | assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 891 | pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 892 | } |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 893 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 894 | compile_node(comp, pns1->nodes[0]); |
| 895 | EMIT(delete_subscr); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 896 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 897 | assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0])); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 898 | EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 899 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 900 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 901 | } |
| 902 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 903 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 904 | } |
| 905 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 906 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) { |
| 907 | pn = ((mp_parse_node_struct_t*)pn)->nodes[0]; |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 908 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
| 909 | goto cannot_delete; |
| 910 | } else { |
| 911 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 912 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 913 | // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp |
| 914 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 915 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 916 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 917 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 918 | // sequence of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 919 | assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 920 | c_del_stmt(comp, pns->nodes[0]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 921 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 922 | // sequence of many items |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 923 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 924 | c_del_stmt(comp, pns->nodes[0]); |
| 925 | for (int i = 0; i < n; i++) { |
| 926 | c_del_stmt(comp, pns1->nodes[i]); |
| 927 | } |
Damien George | 216a711 | 2016-09-30 14:48:06 +1000 | [diff] [blame] | 928 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 929 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 930 | } else { |
| 931 | // sequence with 2 items |
| 932 | goto sequence_with_2_items; |
| 933 | } |
| 934 | } else { |
| 935 | // sequence with 2 items |
| 936 | sequence_with_2_items: |
| 937 | c_del_stmt(comp, pns->nodes[0]); |
| 938 | c_del_stmt(comp, pns->nodes[1]); |
| 939 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 940 | } |
| 941 | } else { |
Ville Skyttä | ca16c38 | 2017-05-29 10:08:14 +0300 | [diff] [blame] | 942 | // some arbitrary statement that we can't delete (eg del 1) |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 943 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 944 | } |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 945 | |
| 946 | return; |
| 947 | |
| 948 | cannot_delete: |
| 949 | compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 952 | STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 953 | apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt); |
| 954 | } |
| 955 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 956 | STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 957 | if (comp->break_label == 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 958 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 959 | } |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 960 | assert(comp->cur_except_level >= comp->break_continue_except_level); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 961 | EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 964 | STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 965 | if (comp->continue_label == 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 966 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 967 | } |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 968 | assert(comp->cur_except_level >= comp->break_continue_except_level); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 969 | EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 972 | STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 973 | if (comp->scope_cur->kind != SCOPE_FUNCTION) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 974 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function"); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 975 | return; |
| 976 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 977 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 978 | // no argument to 'return', so return None |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 979 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien George | ae54fbf | 2017-04-22 14:58:01 +1000 | [diff] [blame] | 980 | } else if (MICROPY_COMP_RETURN_IF_EXPR |
| 981 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 982 | // special case when returning an if-expression; to match CPython optimisation |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 983 | mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 984 | mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 985 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 986 | uint l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 987 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 988 | compile_node(comp, pns_test_if_expr->nodes[0]); // success value |
| 989 | EMIT(return_value); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 990 | EMIT_ARG(label_assign, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 991 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 992 | } else { |
| 993 | compile_node(comp, pns->nodes[0]); |
| 994 | } |
| 995 | EMIT(return_value); |
| 996 | } |
| 997 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 998 | STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 999 | compile_node(comp, pns->nodes[0]); |
| 1000 | EMIT(pop_top); |
| 1001 | } |
| 1002 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1003 | STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1004 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1005 | // raise |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1006 | EMIT_ARG(raise_varargs, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1007 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1008 | // raise x from y |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1009 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1010 | compile_node(comp, pns->nodes[0]); |
| 1011 | compile_node(comp, pns->nodes[1]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1012 | EMIT_ARG(raise_varargs, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1013 | } else { |
| 1014 | // raise x |
| 1015 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1016 | EMIT_ARG(raise_varargs, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1020 | // q_base holds the base of the name |
| 1021 | // eg a -> q_base=a |
| 1022 | // a.b.c -> q_base=a |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1023 | STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1024 | bool is_as = false; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1025 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) { |
| 1026 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1027 | // a name of the form x as y; unwrap it |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1028 | *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1029 | pn = pns->nodes[0]; |
| 1030 | is_as = true; |
| 1031 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1032 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
| 1033 | // empty name (eg, from . import x) |
| 1034 | *q_base = MP_QSTR_; |
| 1035 | EMIT_ARG(import_name, MP_QSTR_); // import the empty string |
| 1036 | } else if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1037 | // just a simple name |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1038 | qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1039 | if (!is_as) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1040 | *q_base = q_full; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1041 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1042 | EMIT_ARG(import_name, q_full); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1043 | } else { |
| 1044 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1045 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1046 | { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1047 | // a name of the form a.b.c |
| 1048 | if (!is_as) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1049 | *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1050 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1051 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1052 | int len = n - 1; |
| 1053 | for (int i = 0; i < n; i++) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1054 | len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1055 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1056 | byte *q_ptr; |
| 1057 | byte *str_dest = qstr_build_start(len, &q_ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1058 | for (int i = 0; i < n; i++) { |
| 1059 | if (i > 0) { |
Damien George | fe8fb91 | 2014-01-02 16:36:09 +0000 | [diff] [blame] | 1060 | *str_dest++ = '.'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1061 | } |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 1062 | size_t str_src_len; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1063 | const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len); |
Damien George | fe8fb91 | 2014-01-02 16:36:09 +0000 | [diff] [blame] | 1064 | memcpy(str_dest, str_src, str_src_len); |
| 1065 | str_dest += str_src_len; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1066 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1067 | qstr q_full = qstr_build_end(q_ptr); |
| 1068 | EMIT_ARG(import_name, q_full); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1069 | if (is_as) { |
| 1070 | for (int i = 1; i < n; i++) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1071 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1072 | } |
| 1073 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1074 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1078 | STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1079 | EMIT_ARG(load_const_small_int, 0); // level 0 import |
| 1080 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything |
| 1081 | qstr q_base; |
| 1082 | do_import_name(comp, pn, &q_base); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1083 | compile_store_id(comp, q_base); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1086 | STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1087 | apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name); |
| 1088 | } |
| 1089 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1090 | STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1091 | mp_parse_node_t pn_import_source = pns->nodes[0]; |
| 1092 | |
Ville Skyttä | ca16c38 | 2017-05-29 10:08:14 +0300 | [diff] [blame] | 1093 | // extract the preceding .'s (if any) for a relative import, to compute the import level |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1094 | uint import_level = 0; |
| 1095 | do { |
| 1096 | mp_parse_node_t pn_rel; |
| 1097 | if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) { |
| 1098 | // This covers relative imports with dots only like "from .. import" |
| 1099 | pn_rel = pn_import_source; |
| 1100 | pn_import_source = MP_PARSE_NODE_NULL; |
| 1101 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) { |
| 1102 | // This covers relative imports starting with dot(s) like "from .foo import" |
| 1103 | mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source; |
| 1104 | pn_rel = pns_2b->nodes[0]; |
| 1105 | pn_import_source = pns_2b->nodes[1]; |
| 1106 | assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be |
| 1107 | } else { |
| 1108 | // Not a relative import |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | // get the list of . and/or ...'s |
| 1113 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1114 | int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes); |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1115 | |
| 1116 | // count the total number of .'s |
| 1117 | for (int i = 0; i < n; i++) { |
| 1118 | if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) { |
| 1119 | import_level++; |
| 1120 | } else { |
| 1121 | // should be an MP_TOKEN_ELLIPSIS |
| 1122 | import_level += 3; |
| 1123 | } |
| 1124 | } |
| 1125 | } while (0); |
| 1126 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1127 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1128 | EMIT_ARG(load_const_small_int, import_level); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1129 | |
| 1130 | // build the "fromlist" tuple |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 1131 | EMIT_ARG(load_const_str, MP_QSTR__star_); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1132 | EMIT_ARG(build_tuple, 1); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1133 | |
| 1134 | // do the import |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1135 | qstr dummy_q; |
| 1136 | do_import_name(comp, pn_import_source, &dummy_q); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1137 | EMIT(import_star); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1138 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1139 | } else { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1140 | EMIT_ARG(load_const_small_int, import_level); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1141 | |
| 1142 | // build the "fromlist" tuple |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1143 | mp_parse_node_t *pn_nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1144 | int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1145 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1146 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1147 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i]; |
| 1148 | qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 1149 | EMIT_ARG(load_const_str, id2); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1150 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1151 | EMIT_ARG(build_tuple, n); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1152 | |
| 1153 | // do the import |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1154 | qstr dummy_q; |
| 1155 | do_import_name(comp, pn_import_source, &dummy_q); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1156 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1157 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1158 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i]; |
| 1159 | qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1160 | EMIT_ARG(import_from, id2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1161 | if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1162 | compile_store_id(comp, id2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1163 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1164 | compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | EMIT(pop_top); |
| 1168 | } |
| 1169 | } |
| 1170 | |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1171 | STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) { |
| 1172 | bool added; |
| 1173 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); |
Damien George | 558a016 | 2015-09-07 16:55:02 +0100 | [diff] [blame] | 1174 | if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) { |
| 1175 | compile_syntax_error(comp, pn, "identifier redefined as global"); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1176 | return; |
| 1177 | } |
| 1178 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 1179 | |
| 1180 | // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL |
| 1181 | id_info = scope_find_global(comp->scope_cur, qst); |
| 1182 | if (id_info != NULL) { |
| 1183 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1187 | STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 1188 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1189 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1190 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1191 | for (int i = 0; i < n; i++) { |
| 1192 | compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1197 | STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) { |
| 1198 | bool added; |
| 1199 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 1200 | if (added) { |
| 1201 | scope_find_local_and_close_over(comp->scope_cur, id_info, qst); |
| 1202 | if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 1203 | compile_syntax_error(comp, pn, "no binding for nonlocal found"); |
| 1204 | } |
| 1205 | } else if (id_info->kind != ID_INFO_KIND_FREE) { |
Damien George | 558a016 | 2015-09-07 16:55:02 +0100 | [diff] [blame] | 1206 | compile_syntax_error(comp, pn, "identifier redefined as nonlocal"); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1207 | } |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1210 | STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 1211 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1212 | if (comp->scope_cur->kind == SCOPE_MODULE) { |
| 1213 | compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code"); |
| 1214 | return; |
| 1215 | } |
| 1216 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1217 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1218 | for (int i = 0; i < n; i++) { |
| 1219 | compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1224 | STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 24df30c | 2016-08-26 22:28:22 +1000 | [diff] [blame] | 1225 | // with optimisations enabled we don't compile assertions |
| 1226 | if (MP_STATE_VM(mp_optimise_value) != 0) { |
| 1227 | return; |
| 1228 | } |
| 1229 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1230 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1231 | c_if_cond(comp, pns->nodes[0], true, l_end); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1232 | EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1233 | if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1234 | // assertion message |
| 1235 | compile_node(comp, pns->nodes[1]); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 1236 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1237 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1238 | EMIT_ARG(raise_varargs, 1); |
| 1239 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1242 | STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1243 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1244 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1245 | // optimisation: don't emit anything when "if False" |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1246 | if (!mp_parse_node_is_const_false(pns->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1247 | uint l_fail = comp_next_label(comp); |
| 1248 | c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1249 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1250 | compile_node(comp, pns->nodes[1]); // if block |
Damien George | af6edc6 | 2014-04-02 16:12:28 +0100 | [diff] [blame] | 1251 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1252 | // optimisation: skip everything else when "if True" |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1253 | if (mp_parse_node_is_const_true(pns->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1254 | goto done; |
| 1255 | } |
| 1256 | |
| 1257 | if ( |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1258 | // optimisation: don't jump over non-existent elif/else blocks |
| 1259 | !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1260 | // optimisation: don't jump if last instruction was return |
| 1261 | && !EMIT(last_emit_was_return_value) |
| 1262 | ) { |
| 1263 | // jump over elif/else blocks |
| 1264 | EMIT_ARG(jump, l_end); |
| 1265 | } |
| 1266 | |
| 1267 | EMIT_ARG(label_assign, l_fail); |
Damien George | af6edc6 | 2014-04-02 16:12:28 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Damien George | 235f9b3 | 2014-10-17 17:30:16 +0000 | [diff] [blame] | 1270 | // compile elif blocks (if any) |
| 1271 | mp_parse_node_t *pn_elif; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1272 | int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif); |
Damien George | 235f9b3 | 2014-10-17 17:30:16 +0000 | [diff] [blame] | 1273 | for (int i = 0; i < n_elif; i++) { |
| 1274 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be |
| 1275 | mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1276 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1277 | // optimisation: don't emit anything when "if False" |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1278 | if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1279 | uint l_fail = comp_next_label(comp); |
| 1280 | c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1281 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1282 | compile_node(comp, pns_elif->nodes[1]); // elif block |
| 1283 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1284 | // optimisation: skip everything else when "elif True" |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1285 | if (mp_parse_node_is_const_true(pns_elif->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1286 | goto done; |
| 1287 | } |
| 1288 | |
| 1289 | // optimisation: don't jump if last instruction was return |
| 1290 | if (!EMIT(last_emit_was_return_value)) { |
| 1291 | EMIT_ARG(jump, l_end); |
| 1292 | } |
| 1293 | EMIT_ARG(label_assign, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // compile else block |
| 1298 | compile_node(comp, pns->nodes[3]); // can be null |
| 1299 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1300 | done: |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1301 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1304 | #define START_BREAK_CONTINUE_BLOCK \ |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 1305 | uint16_t old_break_label = comp->break_label; \ |
| 1306 | uint16_t old_continue_label = comp->continue_label; \ |
| 1307 | uint16_t old_break_continue_except_level = comp->break_continue_except_level; \ |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1308 | uint break_label = comp_next_label(comp); \ |
| 1309 | uint continue_label = comp_next_label(comp); \ |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1310 | comp->break_label = break_label; \ |
| 1311 | comp->continue_label = continue_label; \ |
| 1312 | comp->break_continue_except_level = comp->cur_except_level; |
| 1313 | |
| 1314 | #define END_BREAK_CONTINUE_BLOCK \ |
| 1315 | comp->break_label = old_break_label; \ |
| 1316 | comp->continue_label = old_continue_label; \ |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 1317 | comp->break_continue_except_level = old_break_continue_except_level; |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1318 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1319 | STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1320 | START_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1321 | |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1322 | if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False" |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1323 | uint top_label = comp_next_label(comp); |
Damien George | b0cbfb0 | 2016-11-13 15:32:05 +1100 | [diff] [blame] | 1324 | if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True" |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1325 | EMIT_ARG(jump, continue_label); |
| 1326 | } |
| 1327 | EMIT_ARG(label_assign, top_label); |
| 1328 | compile_node(comp, pns->nodes[1]); // body |
| 1329 | EMIT_ARG(label_assign, continue_label); |
| 1330 | c_if_cond(comp, pns->nodes[0], true, top_label); // condition |
| 1331 | } |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1332 | |
| 1333 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1334 | END_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1335 | |
| 1336 | compile_node(comp, pns->nodes[2]); // else |
| 1337 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1338 | EMIT_ARG(label_assign, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1341 | // This function compiles an optimised for-loop of the form: |
| 1342 | // for <var> in range(<start>, <end>, <step>): |
| 1343 | // <body> |
| 1344 | // else: |
| 1345 | // <else> |
| 1346 | // <var> must be an identifier and <step> must be a small-int. |
| 1347 | // |
| 1348 | // Semantics of for-loop require: |
| 1349 | // - final failing value should not be stored in the loop variable |
| 1350 | // - if the loop never runs, the loop variable should never be assigned |
| 1351 | // - assignments to <var>, <end> or <step> in the body do not alter the loop |
| 1352 | // (<step> is a constant for us, so no need to worry about it changing) |
| 1353 | // |
| 1354 | // If <end> is a small-int, then the stack during the for-loop contains just |
| 1355 | // the current value of <var>. Otherwise, the stack contains <end> then the |
| 1356 | // current value of <var>. |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1357 | STATIC void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1358 | START_BREAK_CONTINUE_BLOCK |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1359 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1360 | uint top_label = comp_next_label(comp); |
| 1361 | uint entry_label = comp_next_label(comp); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1362 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1363 | // put the end value on the stack if it's not a small-int constant |
| 1364 | bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end); |
| 1365 | if (end_on_stack) { |
| 1366 | compile_node(comp, pn_end); |
| 1367 | } |
| 1368 | |
| 1369 | // compile: start |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1370 | compile_node(comp, pn_start); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1371 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1372 | EMIT_ARG(jump, entry_label); |
| 1373 | EMIT_ARG(label_assign, top_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1374 | |
Damien George | c33ce60 | 2014-12-11 17:35:23 +0000 | [diff] [blame] | 1375 | // duplicate next value and store it to var |
| 1376 | EMIT(dup_top); |
Damien George | 3ff2d03 | 2014-03-31 18:02:22 +0100 | [diff] [blame] | 1377 | c_assign(comp, pn_var, ASSIGN_STORE); |
| 1378 | |
Damien | f3822fc | 2013-11-09 20:12:03 +0000 | [diff] [blame] | 1379 | // compile body |
| 1380 | compile_node(comp, pn_body); |
| 1381 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1382 | EMIT_ARG(label_assign, continue_label); |
Damien George | 600ae73 | 2014-01-21 23:48:04 +0000 | [diff] [blame] | 1383 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1384 | // compile: var + step |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1385 | compile_node(comp, pn_step); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1386 | EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1387 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1388 | EMIT_ARG(label_assign, entry_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1389 | |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1390 | // compile: if var <cond> end: goto top |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1391 | if (end_on_stack) { |
| 1392 | EMIT(dup_top_two); |
| 1393 | EMIT(rot_two); |
| 1394 | } else { |
| 1395 | EMIT(dup_top); |
| 1396 | compile_node(comp, pn_end); |
| 1397 | } |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 1398 | assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step)); |
| 1399 | if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1400 | EMIT_ARG(binary_op, MP_BINARY_OP_LESS); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1401 | } else { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1402 | EMIT_ARG(binary_op, MP_BINARY_OP_MORE); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1403 | } |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 1404 | EMIT_ARG(pop_jump_if, true, top_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1405 | |
| 1406 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1407 | END_BREAK_CONTINUE_BLOCK |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1408 | |
Damien George | 4c5f108 | 2017-06-22 13:50:33 +1000 | [diff] [blame^] | 1409 | // Compile the else block. We must pop the iterator variables before |
| 1410 | // executing the else code because it may contain break/continue statements. |
| 1411 | uint end_label = 0; |
| 1412 | if (!MP_PARSE_NODE_IS_NULL(pn_else)) { |
| 1413 | // discard final value of "var", and possible "end" value |
| 1414 | EMIT(pop_top); |
| 1415 | if (end_on_stack) { |
| 1416 | EMIT(pop_top); |
| 1417 | } |
| 1418 | compile_node(comp, pn_else); |
| 1419 | end_label = comp_next_label(comp); |
| 1420 | EMIT_ARG(jump, end_label); |
| 1421 | EMIT_ARG(adjust_stack_size, 1 + end_on_stack); |
| 1422 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1423 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1424 | EMIT_ARG(label_assign, break_label); |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1425 | |
| 1426 | // discard final value of var that failed the loop condition |
| 1427 | EMIT(pop_top); |
| 1428 | |
| 1429 | // discard <end> value if it's on the stack |
| 1430 | if (end_on_stack) { |
| 1431 | EMIT(pop_top); |
| 1432 | } |
Damien George | 4c5f108 | 2017-06-22 13:50:33 +1000 | [diff] [blame^] | 1433 | |
| 1434 | if (!MP_PARSE_NODE_IS_NULL(pn_else)) { |
| 1435 | EMIT_ARG(label_assign, end_label); |
| 1436 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1439 | STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1440 | // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable |
| 1441 | // this is actually slower, but uses no heap memory |
| 1442 | // for viper it will be much, much faster |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1443 | if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_atom_expr_normal)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1444 | mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1445 | if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1446 | && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range |
Damien George | fa03bbf | 2017-04-22 14:13:37 +1000 | [diff] [blame] | 1447 | && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pns_it->nodes[1]) == PN_trailer_paren) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1448 | mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0]; |
| 1449 | mp_parse_node_t *args; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1450 | int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1451 | mp_parse_node_t pn_range_start; |
| 1452 | mp_parse_node_t pn_range_end; |
| 1453 | mp_parse_node_t pn_range_step; |
| 1454 | bool optimize = false; |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1455 | if (1 <= n_args && n_args <= 3) { |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1456 | optimize = true; |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1457 | if (n_args == 1) { |
Damien George | ed9c93f | 2016-11-13 15:35:11 +1100 | [diff] [blame] | 1458 | pn_range_start = mp_parse_node_new_small_int(0); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1459 | pn_range_end = args[0]; |
Damien George | ed9c93f | 2016-11-13 15:35:11 +1100 | [diff] [blame] | 1460 | pn_range_step = mp_parse_node_new_small_int(1); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1461 | } else if (n_args == 2) { |
| 1462 | pn_range_start = args[0]; |
| 1463 | pn_range_end = args[1]; |
Damien George | ed9c93f | 2016-11-13 15:35:11 +1100 | [diff] [blame] | 1464 | pn_range_step = mp_parse_node_new_small_int(1); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | pn_range_start = args[0]; |
| 1467 | pn_range_end = args[1]; |
| 1468 | pn_range_step = args[2]; |
Damien George | de9b536 | 2017-04-05 10:50:26 +1000 | [diff] [blame] | 1469 | // the step must be a non-zero constant integer to do the optimisation |
| 1470 | if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step) |
| 1471 | || MP_PARSE_NODE_LEAF_SMALL_INT(pn_range_step) == 0) { |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1472 | optimize = false; |
| 1473 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1474 | } |
Damien George | 33ac0fd | 2015-12-08 21:05:14 +0000 | [diff] [blame] | 1475 | // arguments must be able to be compiled as standard expressions |
| 1476 | if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) { |
| 1477 | int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start); |
| 1478 | if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { |
| 1479 | optimize = false; |
| 1480 | } |
| 1481 | } |
| 1482 | if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) { |
| 1483 | int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end); |
| 1484 | if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { |
| 1485 | optimize = false; |
| 1486 | } |
| 1487 | } |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1488 | } |
| 1489 | if (optimize) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1490 | compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]); |
| 1491 | return; |
| 1492 | } |
| 1493 | } |
| 1494 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1495 | |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1496 | START_BREAK_CONTINUE_BLOCK |
Damien George | 25c8464 | 2014-05-30 15:20:41 +0100 | [diff] [blame] | 1497 | comp->break_label |= MP_EMIT_BREAK_FROM_FOR; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1498 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1499 | uint pop_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1500 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1501 | compile_node(comp, pns->nodes[1]); // iterator |
Damien George | f4df3aa | 2016-01-09 23:59:52 +0000 | [diff] [blame] | 1502 | EMIT_ARG(get_iter, true); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1503 | EMIT_ARG(label_assign, continue_label); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1504 | EMIT_ARG(for_iter, pop_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1505 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1506 | compile_node(comp, pns->nodes[2]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1507 | if (!EMIT(last_emit_was_return_value)) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1508 | EMIT_ARG(jump, continue_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1509 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1510 | EMIT_ARG(label_assign, pop_label); |
Damien George | 30b42dd | 2017-01-17 15:30:18 +1100 | [diff] [blame] | 1511 | EMIT(for_iter_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1512 | |
| 1513 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1514 | END_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1515 | |
Damien George | 4c5f108 | 2017-06-22 13:50:33 +1000 | [diff] [blame^] | 1516 | compile_node(comp, pns->nodes[3]); // else (may be empty) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1517 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1518 | EMIT_ARG(label_assign, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1521 | STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1522 | // setup code |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1523 | uint l1 = comp_next_label(comp); |
| 1524 | uint success_label = comp_next_label(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1525 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1526 | EMIT_ARG(setup_except, l1); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1527 | compile_increase_except_level(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1528 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1529 | compile_node(comp, pn_body); // body |
| 1530 | EMIT(pop_block); |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 1531 | EMIT_ARG(jump, success_label); // jump over exception handler |
| 1532 | |
| 1533 | EMIT_ARG(label_assign, l1); // start of exception handler |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 1534 | EMIT(start_except_handler); |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 1535 | |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1536 | // at this point the top of the stack contains the exception instance that was raised |
| 1537 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1538 | uint l2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1539 | |
| 1540 | for (int i = 0; i < n_except; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1541 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be |
| 1542 | mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1543 | |
| 1544 | qstr qstr_exception_local = 0; |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1545 | uint end_finally_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1546 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1547 | if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1548 | // this is a catch all exception handler |
| 1549 | if (i + 1 != n_except) { |
Damien George | 18c059f | 2017-03-29 12:36:46 +1100 | [diff] [blame] | 1550 | compile_syntax_error(comp, pn_excepts[i], "default 'except' must be last"); |
Damien George | c935d69 | 2015-01-13 23:33:16 +0000 | [diff] [blame] | 1551 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | } else { |
| 1555 | // this exception handler requires a match to a certain type of exception |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1556 | mp_parse_node_t pns_exception_expr = pns_except->nodes[0]; |
| 1557 | if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) { |
| 1558 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr; |
| 1559 | if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1560 | // handler binds the exception to a local |
| 1561 | pns_exception_expr = pns3->nodes[0]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1562 | qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | EMIT(dup_top); |
| 1566 | compile_node(comp, pns_exception_expr); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1567 | EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 1568 | EMIT_ARG(pop_jump_if, false, end_finally_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1571 | // either discard or store the exception instance |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1572 | if (qstr_exception_local == 0) { |
| 1573 | EMIT(pop_top); |
| 1574 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1575 | compile_store_id(comp, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1576 | } |
| 1577 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1578 | uint l3 = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1579 | if (qstr_exception_local != 0) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1580 | l3 = comp_next_label(comp); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1581 | EMIT_ARG(setup_finally, l3); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1582 | compile_increase_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1583 | } |
| 1584 | compile_node(comp, pns_except->nodes[1]); |
| 1585 | if (qstr_exception_local != 0) { |
| 1586 | EMIT(pop_block); |
| 1587 | } |
| 1588 | EMIT(pop_except); |
| 1589 | if (qstr_exception_local != 0) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1590 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1591 | EMIT_ARG(label_assign, l3); |
| 1592 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1593 | compile_store_id(comp, qstr_exception_local); |
| 1594 | compile_delete_id(comp, qstr_exception_local); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1595 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1596 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1597 | EMIT(end_finally); |
| 1598 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1599 | EMIT_ARG(jump, l2); |
| 1600 | EMIT_ARG(label_assign, end_finally_label); |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1601 | EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1604 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1605 | EMIT(end_finally); |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 1606 | EMIT(end_except_handler); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1607 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1608 | EMIT_ARG(label_assign, success_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1609 | compile_node(comp, pn_else); // else block, can be null |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1610 | EMIT_ARG(label_assign, l2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1613 | STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1614 | uint l_finally_block = comp_next_label(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1615 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1616 | EMIT_ARG(setup_finally, l_finally_block); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1617 | compile_increase_except_level(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1618 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1619 | if (n_except == 0) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1620 | assert(MP_PARSE_NODE_IS_NULL(pn_else)); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 1621 | EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1622 | compile_node(comp, pn_body); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 1623 | EMIT_ARG(adjust_stack_size, -3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1624 | } else { |
| 1625 | compile_try_except(comp, pn_body, n_except, pn_except, pn_else); |
| 1626 | } |
| 1627 | EMIT(pop_block); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1628 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1629 | EMIT_ARG(label_assign, l_finally_block); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1630 | compile_node(comp, pn_finally); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1631 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1632 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1633 | EMIT(end_finally); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1634 | } |
| 1635 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1636 | STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1637 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be |
| 1638 | { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1639 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 1640 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1641 | // just try-finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1642 | compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]); |
| 1643 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1644 | // try-except and possibly else and/or finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1645 | mp_parse_node_t *pn_excepts; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1646 | int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1647 | if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1648 | // no finally |
| 1649 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]); |
| 1650 | } else { |
| 1651 | // have finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1652 | compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1653 | } |
| 1654 | } else { |
| 1655 | // just try-except |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1656 | mp_parse_node_t *pn_excepts; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1657 | int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1658 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1659 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1663 | STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1664 | if (n == 0) { |
| 1665 | // no more pre-bits, compile the body of the with |
| 1666 | compile_node(comp, body); |
| 1667 | } else { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1668 | uint l_end = comp_next_label(comp); |
Damien George | 2c915e1 | 2016-04-07 08:53:24 +0100 | [diff] [blame] | 1669 | if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) { |
| 1670 | // we need to allocate an extra label for the native emitter |
| 1671 | // it will use l_end+1 as an auxiliary label |
| 1672 | comp_next_label(comp); |
| 1673 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1674 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1675 | // this pre-bit is of the form "a as b" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1676 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1677 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1678 | EMIT_ARG(setup_with, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1679 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1680 | } else { |
| 1681 | // this pre-bit is just an expression |
| 1682 | compile_node(comp, nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1683 | EMIT_ARG(setup_with, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1684 | EMIT(pop_top); |
| 1685 | } |
Paul Sokolovsky | 44307d5 | 2014-03-29 04:10:11 +0200 | [diff] [blame] | 1686 | compile_increase_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1687 | // compile additional pre-bits and the body |
| 1688 | compile_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1689 | // finish this with block |
Damien George | ce8b4e8 | 2016-04-07 08:50:38 +0100 | [diff] [blame] | 1690 | EMIT_ARG(with_cleanup, l_end); |
Paul Sokolovsky | 44307d5 | 2014-03-29 04:10:11 +0200 | [diff] [blame] | 1691 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1692 | EMIT(end_finally); |
| 1693 | } |
| 1694 | } |
| 1695 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1696 | STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1697 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1698 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1699 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1700 | assert(n > 0); |
| 1701 | |
| 1702 | // compile in a nested fashion |
| 1703 | compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1704 | } |
| 1705 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1706 | STATIC void compile_yield_from(compiler_t *comp) { |
Damien George | f4df3aa | 2016-01-09 23:59:52 +0000 | [diff] [blame] | 1707 | EMIT_ARG(get_iter, false); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1708 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1709 | EMIT(yield_from); |
| 1710 | } |
| 1711 | |
| 1712 | #if MICROPY_PY_ASYNC_AWAIT |
| 1713 | STATIC void compile_await_object_method(compiler_t *comp, qstr method) { |
Damien George | dd11af2 | 2017-04-19 09:45:59 +1000 | [diff] [blame] | 1714 | EMIT_ARG(load_method, method, false); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1715 | EMIT_ARG(call_method, 0, 0, 0); |
| 1716 | compile_yield_from(comp); |
| 1717 | } |
| 1718 | |
| 1719 | STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1720 | // comp->break_label |= MP_EMIT_BREAK_FROM_FOR; |
| 1721 | |
| 1722 | qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 1723 | uint while_else_label = comp_next_label(comp); |
| 1724 | uint try_exception_label = comp_next_label(comp); |
| 1725 | uint try_else_label = comp_next_label(comp); |
| 1726 | uint try_finally_label = comp_next_label(comp); |
| 1727 | |
| 1728 | compile_node(comp, pns->nodes[1]); // iterator |
| 1729 | compile_await_object_method(comp, MP_QSTR___aiter__); |
| 1730 | compile_store_id(comp, context); |
| 1731 | |
| 1732 | START_BREAK_CONTINUE_BLOCK |
| 1733 | |
| 1734 | EMIT_ARG(label_assign, continue_label); |
| 1735 | |
| 1736 | EMIT_ARG(setup_except, try_exception_label); |
| 1737 | compile_increase_except_level(comp); |
| 1738 | |
| 1739 | compile_load_id(comp, context); |
| 1740 | compile_await_object_method(comp, MP_QSTR___anext__); |
| 1741 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1742 | EMIT(pop_block); |
| 1743 | EMIT_ARG(jump, try_else_label); |
| 1744 | |
| 1745 | EMIT_ARG(label_assign, try_exception_label); |
| 1746 | EMIT(start_except_handler); |
| 1747 | EMIT(dup_top); |
| 1748 | EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration); |
| 1749 | EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); |
| 1750 | EMIT_ARG(pop_jump_if, false, try_finally_label); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1751 | EMIT(pop_top); // pop exception instance |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1752 | EMIT(pop_except); |
| 1753 | EMIT_ARG(jump, while_else_label); |
| 1754 | |
| 1755 | EMIT_ARG(label_assign, try_finally_label); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1756 | EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1757 | compile_decrease_except_level(comp); |
| 1758 | EMIT(end_finally); |
| 1759 | EMIT(end_except_handler); |
| 1760 | |
| 1761 | EMIT_ARG(label_assign, try_else_label); |
| 1762 | compile_node(comp, pns->nodes[2]); // body |
| 1763 | |
| 1764 | EMIT_ARG(jump, continue_label); |
| 1765 | // break/continue apply to outer loop (if any) in the else block |
| 1766 | END_BREAK_CONTINUE_BLOCK |
| 1767 | |
| 1768 | EMIT_ARG(label_assign, while_else_label); |
| 1769 | compile_node(comp, pns->nodes[3]); // else |
| 1770 | |
| 1771 | EMIT_ARG(label_assign, break_label); |
| 1772 | } |
| 1773 | |
| 1774 | STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) { |
| 1775 | if (n == 0) { |
| 1776 | // no more pre-bits, compile the body of the with |
| 1777 | compile_node(comp, body); |
| 1778 | } else { |
| 1779 | uint try_exception_label = comp_next_label(comp); |
| 1780 | uint no_reraise_label = comp_next_label(comp); |
| 1781 | uint try_else_label = comp_next_label(comp); |
| 1782 | uint end_label = comp_next_label(comp); |
| 1783 | qstr context; |
| 1784 | |
| 1785 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
| 1786 | // this pre-bit is of the form "a as b" |
| 1787 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0]; |
| 1788 | compile_node(comp, pns->nodes[0]); |
| 1789 | context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 1790 | compile_store_id(comp, context); |
| 1791 | compile_load_id(comp, context); |
| 1792 | compile_await_object_method(comp, MP_QSTR___aenter__); |
| 1793 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1794 | } else { |
| 1795 | // this pre-bit is just an expression |
| 1796 | compile_node(comp, nodes[0]); |
| 1797 | context = MP_PARSE_NODE_LEAF_ARG(nodes[0]); |
| 1798 | compile_store_id(comp, context); |
| 1799 | compile_load_id(comp, context); |
| 1800 | compile_await_object_method(comp, MP_QSTR___aenter__); |
| 1801 | EMIT(pop_top); |
| 1802 | } |
| 1803 | |
| 1804 | compile_load_id(comp, context); |
Damien George | dd11af2 | 2017-04-19 09:45:59 +1000 | [diff] [blame] | 1805 | EMIT_ARG(load_method, MP_QSTR___aexit__, false); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1806 | |
| 1807 | EMIT_ARG(setup_except, try_exception_label); |
| 1808 | compile_increase_except_level(comp); |
| 1809 | // compile additional pre-bits and the body |
| 1810 | compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1811 | // finish this with block |
| 1812 | EMIT(pop_block); |
| 1813 | EMIT_ARG(jump, try_else_label); // jump over exception handler |
| 1814 | |
| 1815 | EMIT_ARG(label_assign, try_exception_label); // start of exception handler |
| 1816 | EMIT(start_except_handler); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1817 | |
| 1818 | // at this point the stack contains: ..., __aexit__, self, exc |
| 1819 | EMIT(dup_top); |
| 1820 | #if MICROPY_CPYTHON_COMPAT |
| 1821 | EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc) |
| 1822 | #else |
| 1823 | compile_load_id(comp, MP_QSTR_type); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1824 | EMIT(rot_two); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1825 | EMIT_ARG(call_function, 1, 0, 0); // get type(exc) |
| 1826 | #endif |
| 1827 | EMIT(rot_two); |
| 1828 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value |
| 1829 | // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1830 | EMIT_ARG(call_method, 3, 0, 0); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1831 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1832 | compile_yield_from(comp); |
| 1833 | EMIT_ARG(pop_jump_if, true, no_reraise_label); |
| 1834 | EMIT_ARG(raise_varargs, 0); |
| 1835 | |
| 1836 | EMIT_ARG(label_assign, no_reraise_label); |
| 1837 | EMIT(pop_except); |
| 1838 | EMIT_ARG(jump, end_label); |
| 1839 | |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1840 | EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1841 | compile_decrease_except_level(comp); |
| 1842 | EMIT(end_finally); |
| 1843 | EMIT(end_except_handler); |
| 1844 | |
| 1845 | EMIT_ARG(label_assign, try_else_label); // start of try-else handler |
| 1846 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1847 | EMIT(dup_top); |
| 1848 | EMIT(dup_top); |
| 1849 | EMIT_ARG(call_method, 3, 0, 0); |
| 1850 | compile_yield_from(comp); |
| 1851 | EMIT(pop_top); |
| 1852 | |
| 1853 | EMIT_ARG(label_assign, end_label); |
| 1854 | |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1859 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
| 1860 | mp_parse_node_t *nodes; |
| 1861 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes); |
| 1862 | assert(n > 0); |
| 1863 | |
| 1864 | // compile in a nested fashion |
| 1865 | compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1866 | } |
| 1867 | |
| 1868 | STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1869 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0])); |
| 1870 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 1871 | if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) { |
| 1872 | // async def |
| 1873 | compile_funcdef(comp, pns0); |
| 1874 | scope_t *fscope = (scope_t*)pns0->nodes[4]; |
| 1875 | fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
| 1876 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) { |
| 1877 | // async for |
| 1878 | compile_async_for_stmt(comp, pns0); |
| 1879 | } else { |
| 1880 | // async with |
| 1881 | assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt); |
| 1882 | compile_async_with_stmt(comp, pns0); |
| 1883 | } |
| 1884 | } |
| 1885 | #endif |
| 1886 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1887 | STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1888 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1889 | if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) { |
| 1890 | // for REPL, evaluate then print the expression |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1891 | compile_load_id(comp, MP_QSTR___repl_print__); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1892 | compile_node(comp, pns->nodes[0]); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 1893 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1894 | EMIT(pop_top); |
| 1895 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1896 | } else { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1897 | // for non-REPL, evaluate then discard the expression |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 1898 | if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 1899 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1900 | // do nothing with a lonely constant |
| 1901 | } else { |
| 1902 | compile_node(comp, pns->nodes[0]); // just an expression |
| 1903 | EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack |
| 1904 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1905 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1906 | } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1907 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 1908 | int kind = MP_PARSE_NODE_STRUCT_KIND(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1909 | if (kind == PN_expr_stmt_augassign) { |
| 1910 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign |
| 1911 | compile_node(comp, pns1->nodes[1]); // rhs |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1912 | assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0])); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1913 | mp_binary_op_t op; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1914 | switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1915 | case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break; |
| 1916 | case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break; |
| 1917 | case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break; |
| 1918 | case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break; |
| 1919 | case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break; |
| 1920 | case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break; |
| 1921 | case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break; |
| 1922 | case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break; |
| 1923 | case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break; |
| 1924 | case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break; |
| 1925 | case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 1926 | case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1927 | } |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 1928 | EMIT_ARG(binary_op, op); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1929 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign |
| 1930 | } else if (kind == PN_expr_stmt_assign_list) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1931 | int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1; |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1932 | compile_node(comp, pns1->nodes[rhs]); // rhs |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1933 | // following CPython, we store left-most first |
| 1934 | if (rhs > 0) { |
| 1935 | EMIT(dup_top); |
| 1936 | } |
| 1937 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1938 | for (int i = 0; i < rhs; i++) { |
| 1939 | if (i + 1 < rhs) { |
| 1940 | EMIT(dup_top); |
| 1941 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1942 | c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1943 | } |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1944 | } else { |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1945 | plain_assign: |
Damien George | 42e0c59 | 2015-03-14 13:11:35 +0000 | [diff] [blame] | 1946 | if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1947 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1948 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1949 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2 |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1950 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1951 | // optimisation for a, b = c, d |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1952 | mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 1953 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1954 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr) |
| 1955 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) { |
| 1956 | // can't optimise when it's a star expression on the lhs |
| 1957 | goto no_optimisation; |
| 1958 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1959 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1960 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1961 | EMIT(rot_two); |
| 1962 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1963 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
Damien George | 42e0c59 | 2015-03-14 13:11:35 +0000 | [diff] [blame] | 1964 | } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1965 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1966 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1967 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3 |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1968 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1969 | // optimisation for a, b, c = d, e, f |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1970 | mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 1971 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1972 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr) |
| 1973 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr) |
| 1974 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) { |
| 1975 | // can't optimise when it's a star expression on the lhs |
| 1976 | goto no_optimisation; |
| 1977 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1978 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1979 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1980 | compile_node(comp, pns10->nodes[2]); // rhs |
| 1981 | EMIT(rot_three); |
| 1982 | EMIT(rot_two); |
| 1983 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1984 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1985 | c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store |
| 1986 | } else { |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1987 | no_optimisation: |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1988 | compile_node(comp, pns->nodes[1]); // rhs |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1989 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1990 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1991 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1992 | } else { |
| 1993 | goto plain_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1997 | STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1998 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1999 | compile_node(comp, pns->nodes[0]); |
| 2000 | for (int i = 1; i < num_nodes; i += 1) { |
| 2001 | compile_node(comp, pns->nodes[i]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2002 | EMIT_ARG(binary_op, binary_op); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2006 | STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2007 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else)); |
| 2008 | mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2009 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2010 | uint l_fail = comp_next_label(comp); |
| 2011 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2012 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 2013 | compile_node(comp, pns->nodes[0]); // success value |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2014 | EMIT_ARG(jump, l_end); |
| 2015 | EMIT_ARG(label_assign, l_fail); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 2016 | EMIT_ARG(adjust_stack_size, -1); // adjust stack size |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2017 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2018 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2021 | STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2022 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2023 | // create a new scope for this lambda |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2024 | scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2025 | // store the lambda scope so the compiling function (this one) can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2026 | pns->nodes[2] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | // get the scope for this lambda |
| 2030 | scope_t *this_scope = (scope_t*)pns->nodes[2]; |
| 2031 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 2032 | // compile the lambda definition |
| 2033 | compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2034 | } |
| 2035 | |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2036 | STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2037 | uint l_end = comp_next_label(comp); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2038 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2039 | for (int i = 0; i < n; i += 1) { |
| 2040 | compile_node(comp, pns->nodes[i]); |
| 2041 | if (i + 1 < n) { |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2042 | EMIT_ARG(jump_if_or_pop, cond, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2043 | } |
| 2044 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2045 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2046 | } |
| 2047 | |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2048 | STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2049 | compile_or_and_test(comp, pns, true); |
| 2050 | } |
| 2051 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2052 | STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2053 | compile_or_and_test(comp, pns, false); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2054 | } |
| 2055 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2056 | STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2057 | compile_node(comp, pns->nodes[0]); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2058 | EMIT_ARG(unary_op, MP_UNARY_OP_NOT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2061 | STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2062 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2063 | compile_node(comp, pns->nodes[0]); |
| 2064 | bool multi = (num_nodes > 3); |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2065 | uint l_fail = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2066 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2067 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2068 | } |
| 2069 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2070 | compile_node(comp, pns->nodes[i + 1]); |
| 2071 | if (i + 2 < num_nodes) { |
| 2072 | EMIT(dup_top); |
| 2073 | EMIT(rot_three); |
| 2074 | } |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2075 | if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2076 | mp_binary_op_t op; |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2077 | switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2078 | case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break; |
| 2079 | case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break; |
| 2080 | case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break; |
| 2081 | case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break; |
| 2082 | case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break; |
| 2083 | case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 2084 | case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break; |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2085 | } |
| 2086 | EMIT_ARG(binary_op, op); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2087 | } else { |
| 2088 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2089 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i]; |
| 2090 | int kind = MP_PARSE_NODE_STRUCT_KIND(pns2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2091 | if (kind == PN_comp_op_not_in) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2092 | EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2093 | } else { |
| 2094 | assert(kind == PN_comp_op_is); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2095 | if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2096 | EMIT_ARG(binary_op, MP_BINARY_OP_IS); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2097 | } else { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2098 | EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2099 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2100 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2101 | } |
| 2102 | if (i + 2 < num_nodes) { |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 2103 | EMIT_ARG(jump_if_or_pop, false, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | if (multi) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2107 | uint l_end = comp_next_label(comp); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2108 | EMIT_ARG(jump, l_end); |
| 2109 | EMIT_ARG(label_assign, l_fail); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 2110 | EMIT_ARG(adjust_stack_size, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2111 | EMIT(rot_two); |
| 2112 | EMIT(pop_top); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2113 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2117 | STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 2118 | compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2119 | } |
| 2120 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2121 | STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2122 | c_binary_op(comp, pns, MP_BINARY_OP_OR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2125 | STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2126 | c_binary_op(comp, pns, MP_BINARY_OP_XOR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2127 | } |
| 2128 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2129 | STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2130 | c_binary_op(comp, pns, MP_BINARY_OP_AND); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2133 | STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2134 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2135 | compile_node(comp, pns->nodes[0]); |
| 2136 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2137 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2138 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2139 | EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2140 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2141 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be |
| 2142 | EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2147 | STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2148 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2149 | compile_node(comp, pns->nodes[0]); |
| 2150 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2151 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2152 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2153 | EMIT_ARG(binary_op, MP_BINARY_OP_ADD); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2154 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2155 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be |
| 2156 | EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2161 | STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2162 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2163 | compile_node(comp, pns->nodes[0]); |
| 2164 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2165 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2166 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2167 | EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2168 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2169 | EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2170 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2171 | EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2172 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2173 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be |
| 2174 | EMIT_ARG(binary_op, MP_BINARY_OP_MODULO); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2179 | STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2180 | compile_node(comp, pns->nodes[1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2181 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2182 | EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2183 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2184 | EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2185 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2186 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be |
| 2187 | EMIT_ARG(unary_op, MP_UNARY_OP_INVERT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2191 | STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 5335942 | 2017-04-18 22:52:18 +1000 | [diff] [blame] | 2192 | // compile the subject of the expression |
| 2193 | compile_node(comp, pns->nodes[0]); |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2194 | |
Damien George | 5335942 | 2017-04-18 22:52:18 +1000 | [diff] [blame] | 2195 | // compile_atom_expr_await may call us with a NULL node |
| 2196 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 2197 | return; |
| 2198 | } |
| 2199 | |
| 2200 | // get the array of trailers (known to be an array of PARSE_NODE_STRUCT) |
| 2201 | size_t num_trail = 1; |
| 2202 | mp_parse_node_struct_t **pns_trail = (mp_parse_node_struct_t**)&pns->nodes[1]; |
| 2203 | if (MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_atom_expr_trailers) { |
| 2204 | num_trail = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_trail[0]); |
| 2205 | pns_trail = (mp_parse_node_struct_t**)&pns_trail[0]->nodes[0]; |
| 2206 | } |
| 2207 | |
| 2208 | // the current index into the array of trailers |
| 2209 | size_t i = 0; |
| 2210 | |
| 2211 | // handle special super() call |
| 2212 | if (comp->scope_cur->kind == SCOPE_FUNCTION |
| 2213 | && MP_PARSE_NODE_IS_ID(pns->nodes[0]) |
| 2214 | && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super |
| 2215 | && MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_trailer_paren |
| 2216 | && MP_PARSE_NODE_IS_NULL(pns_trail[0]->nodes[0])) { |
| 2217 | // at this point we have matched "super()" within a function |
| 2218 | |
| 2219 | // load the class for super to search for a parent |
| 2220 | compile_load_id(comp, MP_QSTR___class__); |
| 2221 | |
| 2222 | // look for first argument to function (assumes it's "self") |
| 2223 | bool found = false; |
| 2224 | id_info_t *id = &comp->scope_cur->id_info[0]; |
| 2225 | for (size_t n = comp->scope_cur->id_info_len; n > 0; --n, ++id) { |
| 2226 | if (id->flags & ID_FLAG_IS_PARAM) { |
| 2227 | // first argument found; load it |
| 2228 | compile_load_id(comp, id->qst); |
| 2229 | found = true; |
| 2230 | break; |
| 2231 | } |
| 2232 | } |
| 2233 | if (!found) { |
| 2234 | compile_syntax_error(comp, (mp_parse_node_t)pns_trail[0], |
| 2235 | "super() can't find self"); // really a TypeError |
| 2236 | return; |
| 2237 | } |
| 2238 | |
Damien George | dd11af2 | 2017-04-19 09:45:59 +1000 | [diff] [blame] | 2239 | if (num_trail >= 3 |
| 2240 | && MP_PARSE_NODE_STRUCT_KIND(pns_trail[1]) == PN_trailer_period |
| 2241 | && MP_PARSE_NODE_STRUCT_KIND(pns_trail[2]) == PN_trailer_paren) { |
| 2242 | // optimisation for method calls super().f(...), to eliminate heap allocation |
| 2243 | mp_parse_node_struct_t *pns_period = pns_trail[1]; |
| 2244 | mp_parse_node_struct_t *pns_paren = pns_trail[2]; |
| 2245 | EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0]), true); |
| 2246 | compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0); |
| 2247 | i = 3; |
| 2248 | } else { |
| 2249 | // a super() call |
| 2250 | EMIT_ARG(call_function, 2, 0, 0); |
| 2251 | i = 1; |
| 2252 | } |
Damien George | 5335942 | 2017-04-18 22:52:18 +1000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | // compile the remaining trailers |
| 2256 | for (; i < num_trail; i++) { |
| 2257 | if (i + 1 < num_trail |
| 2258 | && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i]) == PN_trailer_period |
| 2259 | && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i + 1]) == PN_trailer_paren) { |
| 2260 | // optimisation for method calls a.f(...), following PyPy |
| 2261 | mp_parse_node_struct_t *pns_period = pns_trail[i]; |
| 2262 | mp_parse_node_struct_t *pns_paren = pns_trail[i + 1]; |
Damien George | dd11af2 | 2017-04-19 09:45:59 +1000 | [diff] [blame] | 2263 | EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0]), false); |
Damien George | 5335942 | 2017-04-18 22:52:18 +1000 | [diff] [blame] | 2264 | compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0); |
| 2265 | i += 1; |
| 2266 | } else { |
| 2267 | // node is one of: trailer_paren, trailer_bracket, trailer_period |
| 2268 | compile_node(comp, (mp_parse_node_t)pns_trail[i]); |
| 2269 | } |
| 2270 | } |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2271 | } |
Damien George | 3acaa28 | 2016-03-16 13:04:51 +0000 | [diff] [blame] | 2272 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2273 | STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2274 | compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power |
| 2275 | EMIT_ARG(binary_op, MP_BINARY_OP_POWER); |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 2278 | STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2279 | // function to call is on top of stack |
| 2280 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2281 | // get the list of arguments |
| 2282 | mp_parse_node_t *args; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 2283 | int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2284 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2285 | // compile the arguments |
| 2286 | // Rather than calling compile_node on the list, we go through the list of args |
| 2287 | // explicitly here so that we can count the number of arguments and give sensible |
| 2288 | // error messages. |
| 2289 | int n_positional = n_positional_extra; |
| 2290 | uint n_keyword = 0; |
| 2291 | uint star_flags = 0; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2292 | mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2293 | for (int i = 0; i < n_args; i++) { |
| 2294 | if (MP_PARSE_NODE_IS_STRUCT(args[i])) { |
| 2295 | mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i]; |
| 2296 | if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) { |
| 2297 | if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) { |
| 2298 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x"); |
| 2299 | return; |
| 2300 | } |
| 2301 | star_flags |= MP_EMIT_STAR_FLAG_SINGLE; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2302 | star_args_node = pns_arg; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2303 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) { |
| 2304 | if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) { |
| 2305 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x"); |
| 2306 | return; |
| 2307 | } |
| 2308 | star_flags |= MP_EMIT_STAR_FLAG_DOUBLE; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2309 | dblstar_args_node = pns_arg; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2310 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) { |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 2311 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2312 | if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) { |
| 2313 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id"); |
| 2314 | return; |
| 2315 | } |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2316 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0])); |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 2317 | compile_node(comp, pns_arg->nodes[1]); |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2318 | n_keyword += 1; |
| 2319 | } else { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2320 | compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR); |
| 2321 | n_positional++; |
| 2322 | } |
| 2323 | } else { |
| 2324 | goto normal_argument; |
| 2325 | } |
| 2326 | } else { |
| 2327 | normal_argument: |
Damien George | 1e70fda | 2017-06-14 18:18:01 +1000 | [diff] [blame] | 2328 | if (star_flags) { |
| 2329 | compile_syntax_error(comp, args[i], "non-keyword arg after */**"); |
| 2330 | return; |
| 2331 | } |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2332 | if (n_keyword > 0) { |
| 2333 | compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg"); |
| 2334 | return; |
| 2335 | } |
| 2336 | compile_node(comp, args[i]); |
| 2337 | n_positional++; |
| 2338 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2339 | } |
| 2340 | |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2341 | // compile the star/double-star arguments if we had them |
Damien George | fbcaf0e | 2015-09-23 11:47:01 +0100 | [diff] [blame] | 2342 | // if we had one but not the other then we load "null" as a place holder |
| 2343 | if (star_flags != 0) { |
| 2344 | if (star_args_node == NULL) { |
| 2345 | EMIT(load_null); |
| 2346 | } else { |
| 2347 | compile_node(comp, star_args_node->nodes[0]); |
| 2348 | } |
| 2349 | if (dblstar_args_node == NULL) { |
| 2350 | EMIT(load_null); |
| 2351 | } else { |
| 2352 | compile_node(comp, dblstar_args_node->nodes[0]); |
| 2353 | } |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2356 | // emit the function/method call |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2357 | if (is_method_call) { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2358 | EMIT_ARG(call_method, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2359 | } else { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2360 | EMIT_ARG(call_function, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2361 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2364 | // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 2365 | STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2366 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 2367 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 2368 | mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2369 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2370 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2371 | // create a new scope for this comprehension |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2372 | scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2373 | // store the comprehension scope so the compiling function (this one) can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2374 | pns_comp_for->nodes[3] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | // get the scope for this comprehension |
| 2378 | scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3]; |
| 2379 | |
| 2380 | // compile the comprehension |
| 2381 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 2382 | |
| 2383 | compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator |
Damien George | 4d2bab1 | 2017-02-10 15:39:33 +1100 | [diff] [blame] | 2384 | if (kind == SCOPE_GEN_EXPR) { |
| 2385 | EMIT_ARG(get_iter, false); |
| 2386 | } |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 2387 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2388 | } |
| 2389 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2390 | STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2391 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2392 | // an empty tuple |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2393 | c_tuple(comp, MP_PARSE_NODE_NULL, NULL); |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 2394 | } else { |
| 2395 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2396 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2397 | assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1])); |
| 2398 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 2399 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2400 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2401 | // tuple of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2402 | assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2403 | c_tuple(comp, pns->nodes[0], NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2404 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2405 | // tuple of many items |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2406 | c_tuple(comp, pns->nodes[0], pns2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2407 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2408 | // generator expression |
| 2409 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 2410 | } else { |
| 2411 | // tuple with 2 items |
| 2412 | goto tuple_with_2_items; |
| 2413 | } |
| 2414 | } else { |
| 2415 | // tuple with 2 items |
| 2416 | tuple_with_2_items: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2417 | c_tuple(comp, MP_PARSE_NODE_NULL, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2418 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2422 | STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2423 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2424 | // empty list |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2425 | EMIT_ARG(build_list, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2426 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 2427 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2428 | if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) { |
| 2429 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1]; |
| 2430 | if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2431 | // list of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2432 | assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2433 | compile_node(comp, pns2->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2434 | EMIT_ARG(build_list, 1); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2435 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2436 | // list of many items |
| 2437 | compile_node(comp, pns2->nodes[0]); |
| 2438 | compile_generic_all_nodes(comp, pns3); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2439 | EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2440 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2441 | // list comprehension |
| 2442 | compile_comprehension(comp, pns2, SCOPE_LIST_COMP); |
| 2443 | } else { |
| 2444 | // list with 2 items |
| 2445 | goto list_with_2_items; |
| 2446 | } |
| 2447 | } else { |
| 2448 | // list with 2 items |
| 2449 | list_with_2_items: |
| 2450 | compile_node(comp, pns2->nodes[0]); |
| 2451 | compile_node(comp, pns2->nodes[1]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2452 | EMIT_ARG(build_list, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2453 | } |
| 2454 | } else { |
| 2455 | // list with 1 item |
| 2456 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2457 | EMIT_ARG(build_list, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2461 | STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2462 | mp_parse_node_t pn = pns->nodes[0]; |
| 2463 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2464 | // empty dict |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2465 | EMIT_ARG(build_map, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2466 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2467 | pns = (mp_parse_node_struct_t*)pn; |
| 2468 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2469 | // dict with one element |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2470 | EMIT_ARG(build_map, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2471 | compile_node(comp, pn); |
| 2472 | EMIT(store_map); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2473 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) { |
| 2474 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed |
| 2475 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2476 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2477 | // dict/set with multiple elements |
| 2478 | |
| 2479 | // get tail elements (2nd, 3rd, ...) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2480 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 2481 | int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2482 | |
| 2483 | // first element sets whether it's a dict or set |
| 2484 | bool is_dict; |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2485 | if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2486 | // a dictionary |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2487 | EMIT_ARG(build_map, 1 + n); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2488 | compile_node(comp, pns->nodes[0]); |
| 2489 | EMIT(store_map); |
| 2490 | is_dict = true; |
| 2491 | } else { |
| 2492 | // a set |
| 2493 | compile_node(comp, pns->nodes[0]); // 1st value of set |
| 2494 | is_dict = false; |
| 2495 | } |
| 2496 | |
| 2497 | // process rest of elements |
| 2498 | for (int i = 0; i < n; i++) { |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 2499 | mp_parse_node_t pn_i = nodes[i]; |
| 2500 | bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item); |
| 2501 | compile_node(comp, pn_i); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2502 | if (is_dict) { |
| 2503 | if (!is_key_value) { |
Damien George | f9b0e64 | 2017-03-29 12:44:27 +1100 | [diff] [blame] | 2504 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 2505 | compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax"); |
| 2506 | } else { |
| 2507 | compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dict"); |
| 2508 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2509 | return; |
| 2510 | } |
| 2511 | EMIT(store_map); |
| 2512 | } else { |
| 2513 | if (is_key_value) { |
Damien George | f9b0e64 | 2017-03-29 12:44:27 +1100 | [diff] [blame] | 2514 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 2515 | compile_syntax_error(comp, (mp_parse_node_t)pns, "invalid syntax"); |
| 2516 | } else { |
| 2517 | compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set"); |
| 2518 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2519 | return; |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2524 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2525 | // if it's a set, build it |
| 2526 | if (!is_dict) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2527 | EMIT_ARG(build_set, 1 + n); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2528 | } |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2529 | #endif |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2530 | } else { |
| 2531 | assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2532 | // dict/set comprehension |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2533 | if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2534 | // a dictionary comprehension |
| 2535 | compile_comprehension(comp, pns, SCOPE_DICT_COMP); |
| 2536 | } else { |
| 2537 | // a set comprehension |
| 2538 | compile_comprehension(comp, pns, SCOPE_SET_COMP); |
| 2539 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2540 | } |
| 2541 | } else { |
| 2542 | // set with one element |
| 2543 | goto set_with_one_element; |
| 2544 | } |
| 2545 | } else { |
| 2546 | // set with one element |
| 2547 | set_with_one_element: |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2548 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2549 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2550 | EMIT_ARG(build_set, 1); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2551 | #else |
| 2552 | assert(0); |
| 2553 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2557 | STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | bbcd49a | 2014-02-06 20:30:16 +0000 | [diff] [blame] | 2558 | compile_trailer_paren_helper(comp, pns->nodes[0], false, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2559 | } |
| 2560 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2561 | STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2562 | // object who's index we want is on top of stack |
| 2563 | compile_node(comp, pns->nodes[0]); // the index |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 2564 | EMIT(load_subscr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2567 | STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2568 | // object who's attribute we want is on top of stack |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2569 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 2572 | #if MICROPY_PY_BUILTINS_SLICE |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2573 | STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2574 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be |
| 2575 | mp_parse_node_t pn = pns->nodes[0]; |
| 2576 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2577 | // [?:] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2578 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 2579 | EMIT_ARG(build_slice, 2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2580 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2581 | pns = (mp_parse_node_struct_t*)pn; |
| 2582 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2583 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2584 | pn = pns->nodes[0]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2585 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2586 | // [?::] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2587 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2588 | } else { |
| 2589 | // [?::x] |
| 2590 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2591 | EMIT_ARG(build_slice, 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2592 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2593 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2594 | compile_node(comp, pns->nodes[0]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2595 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2596 | pns = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2597 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be |
| 2598 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2599 | // [?:x:] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2600 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2601 | } else { |
| 2602 | // [?:x:x] |
| 2603 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2604 | EMIT_ARG(build_slice, 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2605 | } |
| 2606 | } else { |
| 2607 | // [?:x] |
| 2608 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2609 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2610 | } |
| 2611 | } else { |
| 2612 | // [?:x] |
| 2613 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2614 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2615 | } |
| 2616 | } |
| 2617 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2618 | STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2619 | compile_node(comp, pns->nodes[0]); // start of slice |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2620 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2621 | compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2624 | STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2625 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2626 | compile_subscript_3_helper(comp, pns); |
| 2627 | } |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 2628 | #endif // MICROPY_PY_BUILTINS_SLICE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2629 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2630 | STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2631 | // if this is called then we are compiling a dict key:value pair |
| 2632 | compile_node(comp, pns->nodes[1]); // value |
| 2633 | compile_node(comp, pns->nodes[0]); // key |
| 2634 | } |
| 2635 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2636 | STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2637 | qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2638 | // store class object into class name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2639 | compile_store_id(comp, cname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2642 | STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 0e3329a | 2014-04-11 13:10:21 +0000 | [diff] [blame] | 2643 | if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 2644 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2645 | return; |
| 2646 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2647 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2648 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2649 | EMIT(yield_value); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2650 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) { |
| 2651 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2652 | compile_node(comp, pns->nodes[0]); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2653 | compile_yield_from(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2654 | } else { |
| 2655 | compile_node(comp, pns->nodes[0]); |
| 2656 | EMIT(yield_value); |
| 2657 | } |
| 2658 | } |
| 2659 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2660 | #if MICROPY_PY_ASYNC_AWAIT |
| 2661 | STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2662 | if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) { |
| 2663 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function"); |
| 2664 | return; |
| 2665 | } |
| 2666 | compile_atom_expr_normal(comp, pns); |
| 2667 | compile_yield_from(comp); |
| 2668 | } |
| 2669 | #endif |
| 2670 | |
Damien George | 5255255 | 2017-02-24 13:43:43 +1100 | [diff] [blame] | 2671 | STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) { |
| 2672 | #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D |
| 2673 | // nodes are 32-bit pointers, but need to extract 64-bit object |
| 2674 | return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32); |
| 2675 | #else |
| 2676 | return (mp_obj_t)pns->nodes[0]; |
| 2677 | #endif |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2680 | STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 5255255 | 2017-02-24 13:43:43 +1100 | [diff] [blame] | 2681 | EMIT_ARG(load_const_obj, get_const_object(pns)); |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2684 | typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*); |
Damien George | 3ff16ff | 2016-05-20 12:38:15 +0100 | [diff] [blame] | 2685 | STATIC const compile_function_t compile_function[] = { |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 2686 | // only define rules with a compile function |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2687 | #define c(f) compile_##f |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 2688 | #define DEF_RULE(rule, comp, kind, ...) comp, |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 2689 | #define DEF_RULE_NC(rule, kind, ...) |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 2690 | #include "py/grammar.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2691 | #undef c |
| 2692 | #undef DEF_RULE |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 2693 | #undef DEF_RULE_NC |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2694 | compile_const_object, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2695 | }; |
| 2696 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 2697 | STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2698 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2699 | // pass |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 2700 | } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 2701 | mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn); |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 2702 | #if MICROPY_DYNAMIC_COMPILER |
| 2703 | mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1)); |
| 2704 | if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) { |
| 2705 | // integer fits in target runtime's small-int |
| 2706 | EMIT_ARG(load_const_small_int, arg); |
| 2707 | } else { |
| 2708 | // integer doesn't fit, so create a multi-precision int object |
| 2709 | // (but only create the actual object on the last pass) |
| 2710 | if (comp->pass != MP_PASS_EMIT) { |
| 2711 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2712 | } else { |
| 2713 | EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg)); |
| 2714 | } |
| 2715 | } |
| 2716 | #else |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 2717 | EMIT_ARG(load_const_small_int, arg); |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 2718 | #endif |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2719 | } else if (MP_PARSE_NODE_IS_LEAF(pn)) { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2720 | uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2721 | switch (MP_PARSE_NODE_LEAF_KIND(pn)) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2722 | case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break; |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2723 | case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break; |
| 2724 | case MP_PARSE_NODE_BYTES: |
| 2725 | // only create and load the actual bytes object on the last pass |
| 2726 | if (comp->pass != MP_PASS_EMIT) { |
| 2727 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2728 | } else { |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 2729 | size_t len; |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2730 | const byte *data = qstr_data(arg, &len); |
| 2731 | EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len)); |
| 2732 | } |
| 2733 | break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 2734 | case MP_PARSE_NODE_TOKEN: default: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2735 | if (arg == MP_TOKEN_NEWLINE) { |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2736 | // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline) |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2737 | // or when single_input lets through a NEWLINE (user enters a blank line) |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2738 | // do nothing |
| 2739 | } else { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2740 | EMIT_ARG(load_const_tok, arg); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2741 | } |
| 2742 | break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2743 | } |
| 2744 | } else { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2745 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 2746 | EMIT_ARG(set_source_line, pns->source_line); |
Damien George | 71019ae | 2017-02-15 10:58:05 +1100 | [diff] [blame] | 2747 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object); |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2748 | compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)]; |
Damien George | deaa57a | 2016-10-12 10:20:48 +1100 | [diff] [blame] | 2749 | f(comp, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2753 | STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star) { |
Damien George | 9a56912 | 2015-11-23 16:50:42 +0000 | [diff] [blame] | 2754 | // check that **kw is last |
| 2755 | if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { |
| 2756 | compile_syntax_error(comp, pn, "invalid syntax"); |
| 2757 | return; |
| 2758 | } |
| 2759 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2760 | qstr param_name = MP_QSTR_NULL; |
| 2761 | uint param_flag = ID_FLAG_IS_PARAM; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2762 | if (MP_PARSE_NODE_IS_ID(pn)) { |
| 2763 | param_name = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2764 | if (comp->have_star) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2765 | // comes after a star, so counts as a keyword-only parameter |
| 2766 | comp->scope_cur->num_kwonly_args += 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2767 | } else { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2768 | // comes before a star, so counts as a positional parameter |
| 2769 | comp->scope_cur->num_pos_args += 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2770 | } |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2771 | } else { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2772 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 2773 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2774 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) { |
| 2775 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2776 | if (comp->have_star) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2777 | // comes after a star, so counts as a keyword-only parameter |
| 2778 | comp->scope_cur->num_kwonly_args += 1; |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2779 | } else { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2780 | // comes before a star, so counts as a positional parameter |
| 2781 | comp->scope_cur->num_pos_args += 1; |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2782 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2783 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) { |
Damien George | 9a56912 | 2015-11-23 16:50:42 +0000 | [diff] [blame] | 2784 | if (comp->have_star) { |
| 2785 | // more than one star |
| 2786 | compile_syntax_error(comp, pn, "invalid syntax"); |
| 2787 | return; |
| 2788 | } |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2789 | comp->have_star = true; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2790 | param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2791 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2792 | // bare star |
| 2793 | // TODO see http://www.python.org/dev/peps/pep-3102/ |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2794 | //assert(comp->scope_cur->num_dict_params == 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2795 | } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2796 | // named star |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2797 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2798 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2799 | } else { |
| 2800 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2801 | // named star with possible annotation |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2802 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2803 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2804 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2805 | } |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2806 | } else { |
| 2807 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2808 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2809 | param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM; |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2810 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2811 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2812 | } |
| 2813 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2814 | if (param_name != MP_QSTR_NULL) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2815 | bool added; |
| 2816 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added); |
| 2817 | if (!added) { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 2818 | compile_syntax_error(comp, pn, "name reused for argument"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2819 | return; |
| 2820 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2821 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2822 | id_info->flags = param_flag; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2826 | STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2827 | compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2828 | } |
| 2829 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2830 | STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2831 | compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star); |
| 2832 | } |
| 2833 | |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2834 | #if MICROPY_EMIT_NATIVE |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2835 | STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) { |
| 2836 | if (!MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2837 | // no annotation |
| 2838 | return; |
| 2839 | } |
| 2840 | |
| 2841 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2842 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) { |
| 2843 | // named parameter with possible annotation |
| 2844 | // fallthrough |
| 2845 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) { |
| 2846 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) { |
| 2847 | // named star with possible annotation |
| 2848 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2849 | // fallthrough |
| 2850 | } else { |
| 2851 | // no annotation |
| 2852 | return; |
| 2853 | } |
Damien George | e49153f | 2016-10-11 12:29:54 +1100 | [diff] [blame] | 2854 | } else { |
| 2855 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2856 | // double star with possible annotation |
| 2857 | // fallthrough |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | mp_parse_node_t pn_annotation = pns->nodes[1]; |
| 2861 | |
| 2862 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2863 | qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2864 | id_info_t *id_info = scope_find(comp->scope_cur, param_name); |
| 2865 | assert(id_info != NULL); |
| 2866 | |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2867 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 2868 | qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 2869 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type); |
| 2870 | } else { |
| 2871 | compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier"); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2872 | } |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2873 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2874 | } |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2875 | #endif // MICROPY_EMIT_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2876 | |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2877 | STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pns_comp_for, mp_parse_node_t pn_inner_expr, int for_depth) { |
| 2878 | uint l_top = comp_next_label(comp); |
| 2879 | uint l_end = comp_next_label(comp); |
| 2880 | EMIT_ARG(label_assign, l_top); |
| 2881 | EMIT_ARG(for_iter, l_end); |
| 2882 | c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE); |
| 2883 | mp_parse_node_t pn_iter = pns_comp_for->nodes[2]; |
| 2884 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2885 | tail_recursion: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2886 | if (MP_PARSE_NODE_IS_NULL(pn_iter)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2887 | // no more nested if/for; compile inner expression |
| 2888 | compile_node(comp, pn_inner_expr); |
Damien George | a5624bf | 2016-09-18 23:59:47 +1000 | [diff] [blame] | 2889 | if (comp->scope_cur->kind == SCOPE_GEN_EXPR) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2890 | EMIT(yield_value); |
| 2891 | EMIT(pop_top); |
Damien George | a5624bf | 2016-09-18 23:59:47 +1000 | [diff] [blame] | 2892 | } else { |
Damien George | 088740e | 2017-01-17 15:27:37 +1100 | [diff] [blame] | 2893 | EMIT_ARG(store_comp, comp->scope_cur->kind, 4 * for_depth + 5); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2894 | } |
Damien George | 0dd6a59 | 2017-04-22 21:43:42 +1000 | [diff] [blame] | 2895 | } else if (MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_iter) == PN_comp_if) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2896 | // if condition |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2897 | mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2898 | c_if_cond(comp, pns_comp_if->nodes[0], false, l_top); |
| 2899 | pn_iter = pns_comp_if->nodes[1]; |
| 2900 | goto tail_recursion; |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2901 | } else { |
Damien George | 0dd6a59 | 2017-04-22 21:43:42 +1000 | [diff] [blame] | 2902 | assert(MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_iter) == PN_comp_for); // should be |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2903 | // for loop |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2904 | mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2905 | compile_node(comp, pns_comp_for2->nodes[1]); |
Damien George | 6e769da | 2017-01-17 14:32:50 +1100 | [diff] [blame] | 2906 | EMIT_ARG(get_iter, true); |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2907 | compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2908 | } |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2909 | |
| 2910 | EMIT_ARG(jump, l_top); |
| 2911 | EMIT_ARG(label_assign, l_end); |
Damien George | 30b42dd | 2017-01-17 15:30:18 +1100 | [diff] [blame] | 2912 | EMIT(for_iter_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2913 | } |
| 2914 | |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2915 | STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 2916 | #if MICROPY_ENABLE_DOC_STRING |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2917 | // see http://www.python.org/dev/peps/pep-0257/ |
| 2918 | |
| 2919 | // look for the first statement |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2920 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2921 | // a statement; fall through |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2922 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2923 | // file input; find the first non-newline node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2924 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2925 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2926 | for (int i = 0; i < num_nodes; i++) { |
| 2927 | pn = pns->nodes[i]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2928 | if (!(MP_PARSE_NODE_IS_LEAF(pn) && MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN && MP_PARSE_NODE_LEAF_ARG(pn) == MP_TOKEN_NEWLINE)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2929 | // not a newline, so this is the first statement; finish search |
| 2930 | break; |
| 2931 | } |
| 2932 | } |
| 2933 | // if we didn't find a non-newline then it's okay to fall through; pn will be a newline and so doc-string test below will fail gracefully |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2934 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2935 | // a list of statements; get the first one |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2936 | pn = ((mp_parse_node_struct_t*)pn)->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2937 | } else { |
| 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | // check the first statement for a doc string |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2942 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 2943 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2944 | if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) |
| 2945 | && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING) |
Damien George | 5255255 | 2017-02-24 13:43:43 +1100 | [diff] [blame] | 2946 | || (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object) |
| 2947 | && MP_OBJ_IS_STR(get_const_object((mp_parse_node_struct_t*)pns->nodes[0])))) { |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2948 | // compile the doc string |
| 2949 | compile_node(comp, pns->nodes[0]); |
| 2950 | // store the doc string |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2951 | compile_store_id(comp, MP_QSTR___doc__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2952 | } |
| 2953 | } |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 2954 | #else |
| 2955 | (void)comp; |
| 2956 | (void)pn; |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2957 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2958 | } |
| 2959 | |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2960 | STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2961 | comp->pass = pass; |
| 2962 | comp->scope_cur = scope; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2963 | comp->next_label = 1; |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2964 | EMIT_ARG(start_pass, pass, scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2965 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2966 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 2967 | // reset maximum stack sizes in scope |
| 2968 | // they will be computed in this first pass |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2969 | scope->stack_size = 0; |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 2970 | scope->exc_stack_size = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2971 | } |
| 2972 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2973 | // compile |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 2974 | if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) { |
| 2975 | assert(scope->kind == SCOPE_MODULE); |
| 2976 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 2977 | compile_node(comp, pns->nodes[0]); // compile the expression |
| 2978 | EMIT(return_value); |
| 2979 | } else if (scope->kind == SCOPE_MODULE) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2980 | if (!comp->is_repl) { |
| 2981 | check_for_doc_string(comp, scope->pn); |
| 2982 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2983 | compile_node(comp, scope->pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2984 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2985 | EMIT(return_value); |
| 2986 | } else if (scope->kind == SCOPE_FUNCTION) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2987 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2988 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 2989 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2990 | |
| 2991 | // 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] | 2992 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2993 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2994 | comp->have_star = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2995 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param); |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2996 | } |
| 2997 | #if MICROPY_EMIT_NATIVE |
| 2998 | else if (scope->emit_options == MP_EMIT_OPT_VIPER) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2999 | // compile annotations; only needed on latter compiler passes |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 3000 | // only needed for viper emitter |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3001 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 3002 | // argument annotations |
| 3003 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations); |
| 3004 | |
| 3005 | // pns->nodes[2] is return/whole function annotation |
Damien George | a5190a7 | 2014-08-15 22:39:08 +0100 | [diff] [blame] | 3006 | mp_parse_node_t pn_annotation = pns->nodes[2]; |
| 3007 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 3008 | // nodes[2] can be null or a test-expr |
| 3009 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 3010 | qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 3011 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type); |
| 3012 | } else { |
| 3013 | compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier"); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 3014 | } |
| 3015 | } |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 3016 | } |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 3017 | #endif // MICROPY_EMIT_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3018 | |
| 3019 | compile_node(comp, pns->nodes[3]); // 3 is function body |
| 3020 | // emit return if it wasn't the last opcode |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 3021 | if (!EMIT(last_emit_was_return_value)) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3022 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3023 | EMIT(return_value); |
| 3024 | } |
| 3025 | } else if (scope->kind == SCOPE_LAMBDA) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3026 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3027 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3028 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3029 | |
| 3030 | // 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] | 3031 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3032 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 3033 | comp->have_star = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3034 | apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param); |
| 3035 | } |
| 3036 | |
| 3037 | compile_node(comp, pns->nodes[1]); // 1 is lambda body |
Damien George | 0e3329a | 2014-04-11 13:10:21 +0000 | [diff] [blame] | 3038 | |
| 3039 | // if the lambda is a generator, then we return None, not the result of the expression of the lambda |
| 3040 | if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) { |
| 3041 | EMIT(pop_top); |
| 3042 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 3043 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3044 | EMIT(return_value); |
| 3045 | } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { |
| 3046 | // a bit of a hack at the moment |
| 3047 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3048 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3049 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3050 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 3051 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 3052 | mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3053 | |
Damien George | 708c073 | 2014-04-27 19:23:46 +0100 | [diff] [blame] | 3054 | // We need a unique name for the comprehension argument (the iterator). |
| 3055 | // CPython uses .0, but we should be able to use anything that won't |
| 3056 | // clash with a user defined variable. Best to use an existing qstr, |
| 3057 | // so we use the blank qstr. |
Damien George | 708c073 | 2014-04-27 19:23:46 +0100 | [diff] [blame] | 3058 | qstr qstr_arg = MP_QSTR_; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3059 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3060 | bool added; |
| 3061 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added); |
| 3062 | assert(added); |
| 3063 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3064 | scope->num_pos_args = 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | if (scope->kind == SCOPE_LIST_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3068 | EMIT_ARG(build_list, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3069 | } else if (scope->kind == SCOPE_DICT_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3070 | EMIT_ARG(build_map, 0); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 3071 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3072 | } else if (scope->kind == SCOPE_SET_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3073 | EMIT_ARG(build_set, 0); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 3074 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3075 | } |
| 3076 | |
Damien George | 088740e | 2017-01-17 15:27:37 +1100 | [diff] [blame] | 3077 | // There are 4 slots on the stack for the iterator, and the first one is |
| 3078 | // NULL to indicate that the second one points to the iterator object. |
Damien George | 4d2bab1 | 2017-02-10 15:39:33 +1100 | [diff] [blame] | 3079 | if (scope->kind == SCOPE_GEN_EXPR) { |
Damien George | 60656ea | 2017-03-23 16:36:08 +1100 | [diff] [blame] | 3080 | // TODO static assert that MP_OBJ_ITER_BUF_NSLOTS == 4 |
Damien George | 4d2bab1 | 2017-02-10 15:39:33 +1100 | [diff] [blame] | 3081 | EMIT(load_null); |
| 3082 | compile_load_id(comp, qstr_arg); |
| 3083 | EMIT(load_null); |
| 3084 | EMIT(load_null); |
| 3085 | } else { |
| 3086 | compile_load_id(comp, qstr_arg); |
| 3087 | EMIT_ARG(get_iter, true); |
| 3088 | } |
Damien George | 6e769da | 2017-01-17 14:32:50 +1100 | [diff] [blame] | 3089 | |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 3090 | compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3091 | |
| 3092 | if (scope->kind == SCOPE_GEN_EXPR) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3093 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3094 | } |
| 3095 | EMIT(return_value); |
| 3096 | } else { |
| 3097 | assert(scope->kind == SCOPE_CLASS); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3098 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3099 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3100 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3101 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3102 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3103 | bool added; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 3104 | id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3105 | assert(added); |
| 3106 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3109 | compile_load_id(comp, MP_QSTR___name__); |
| 3110 | compile_store_id(comp, MP_QSTR___module__); |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 3111 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3112 | compile_store_id(comp, MP_QSTR___qualname__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3113 | |
| 3114 | check_for_doc_string(comp, pns->nodes[2]); |
| 3115 | compile_node(comp, pns->nodes[2]); // 2 is class body |
| 3116 | |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 3117 | id_info_t *id = scope_find(scope, MP_QSTR___class__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3118 | assert(id != NULL); |
| 3119 | if (id->kind == ID_INFO_KIND_LOCAL) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3120 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3121 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3122 | EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3123 | } |
| 3124 | EMIT(return_value); |
| 3125 | } |
| 3126 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 3127 | EMIT(end_pass); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 3128 | |
| 3129 | // make sure we match all the exception levels |
| 3130 | assert(comp->cur_except_level == 0); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3131 | } |
| 3132 | |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3133 | #if MICROPY_EMIT_INLINE_ASM |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3134 | // requires 3 passes: SCOPE, CODE_SIZE, EMIT |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 3135 | STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3136 | comp->pass = pass; |
| 3137 | comp->scope_cur = scope; |
| 3138 | comp->next_label = 1; |
| 3139 | |
| 3140 | if (scope->kind != SCOPE_FUNCTION) { |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 3141 | compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function"); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3142 | return; |
| 3143 | } |
| 3144 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3145 | if (comp->pass > MP_PASS_SCOPE) { |
Damien George | e920bab | 2016-12-09 21:23:17 +1100 | [diff] [blame] | 3146 | EMIT_INLINE_ASM_ARG(start_pass, comp->pass, &comp->compile_error); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3147 | } |
| 3148 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3149 | // get the function definition parse node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3150 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3151 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3152 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3153 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3154 | //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3155 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3156 | // parameters are in pns->nodes[1] |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3157 | if (comp->pass == MP_PASS_CODE_SIZE) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3158 | mp_parse_node_t *pn_params; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3159 | int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3160 | scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params); |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3161 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3162 | goto inline_asm_error; |
| 3163 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
Damien George | 8f54c08 | 2016-01-15 15:20:43 +0000 | [diff] [blame] | 3166 | // pns->nodes[2] is function return annotation |
| 3167 | mp_uint_t type_sig = MP_NATIVE_TYPE_INT; |
| 3168 | mp_parse_node_t pn_annotation = pns->nodes[2]; |
| 3169 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
| 3170 | // nodes[2] can be null or a test-expr |
| 3171 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 3172 | qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 3173 | switch (ret_type) { |
| 3174 | case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break; |
| 3175 | case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break; |
| 3176 | case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break; |
| 3177 | case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break; |
| 3178 | default: compile_syntax_error(comp, pn_annotation, "unknown type"); return; |
| 3179 | } |
| 3180 | } else { |
| 3181 | compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier"); |
| 3182 | } |
| 3183 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3184 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3185 | mp_parse_node_t pn_body = pns->nodes[3]; // body |
| 3186 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3187 | int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3188 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3189 | for (int i = 0; i < num; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3190 | assert(MP_PARSE_NODE_IS_STRUCT(nodes[i])); |
| 3191 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i]; |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3192 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) { |
| 3193 | // no instructions |
| 3194 | continue; |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3195 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3196 | // not an instruction; error |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3197 | not_an_instruction: |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3198 | compile_syntax_error(comp, nodes[i], "expecting an assembler instruction"); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3199 | return; |
| 3200 | } |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3201 | |
| 3202 | // check structure of parse node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3203 | assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0])); |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3204 | if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) { |
| 3205 | goto not_an_instruction; |
| 3206 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3207 | pns2 = (mp_parse_node_struct_t*)pns2->nodes[0]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 3208 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) { |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3209 | goto not_an_instruction; |
| 3210 | } |
| 3211 | if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) { |
| 3212 | goto not_an_instruction; |
| 3213 | } |
| 3214 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) { |
| 3215 | goto not_an_instruction; |
| 3216 | } |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3217 | |
| 3218 | // parse node looks like an instruction |
| 3219 | // get instruction name and args |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3220 | qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]); |
| 3221 | pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren |
| 3222 | mp_parse_node_t *pn_arg; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3223 | int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3224 | |
| 3225 | // emit instructions |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3226 | if (op == MP_QSTR_label) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3227 | if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3228 | compile_syntax_error(comp, nodes[i], "'label' requires 1 argument"); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3229 | return; |
| 3230 | } |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 3231 | uint lab = comp_next_label(comp); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3232 | if (pass > MP_PASS_SCOPE) { |
Damien George | 9c5cabb | 2015-03-03 17:08:02 +0000 | [diff] [blame] | 3233 | if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) { |
| 3234 | compile_syntax_error(comp, nodes[i], "label redefined"); |
| 3235 | return; |
| 3236 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3237 | } |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3238 | } else if (op == MP_QSTR_align) { |
| 3239 | if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3240 | compile_syntax_error(comp, nodes[i], "'align' requires 1 argument"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3241 | return; |
| 3242 | } |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3243 | if (pass > MP_PASS_SCOPE) { |
Damien George | dd53b12 | 2016-12-09 20:54:54 +1100 | [diff] [blame] | 3244 | mp_asm_base_align((mp_asm_base_t*)comp->emit_inline_asm, |
| 3245 | MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0])); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3246 | } |
| 3247 | } else if (op == MP_QSTR_data) { |
| 3248 | if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3249 | compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3250 | return; |
| 3251 | } |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3252 | if (pass > MP_PASS_SCOPE) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 3253 | mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]); |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 3254 | for (uint j = 1; j < n_args; j++) { |
| 3255 | if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3256 | compile_syntax_error(comp, nodes[i], "'data' requires integer arguments"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3257 | return; |
| 3258 | } |
Damien George | dd53b12 | 2016-12-09 20:54:54 +1100 | [diff] [blame] | 3259 | mp_asm_base_data((mp_asm_base_t*)comp->emit_inline_asm, |
| 3260 | bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j])); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3261 | } |
| 3262 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3263 | } else { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3264 | if (pass > MP_PASS_SCOPE) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3265 | EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3266 | } |
| 3267 | } |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3268 | |
| 3269 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3270 | pns = pns2; // this is the parse node that had the error |
| 3271 | goto inline_asm_error; |
| 3272 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3275 | if (comp->pass > MP_PASS_SCOPE) { |
Damien George | 8f54c08 | 2016-01-15 15:20:43 +0000 | [diff] [blame] | 3276 | EMIT_INLINE_ASM_ARG(end_pass, type_sig); |
Damien George | e920bab | 2016-12-09 21:23:17 +1100 | [diff] [blame] | 3277 | |
| 3278 | if (comp->pass == MP_PASS_EMIT) { |
| 3279 | void *f = mp_asm_base_get_code((mp_asm_base_t*)comp->emit_inline_asm); |
| 3280 | mp_emit_glue_assign_native(comp->scope_cur->raw_code, MP_CODE_NATIVE_ASM, |
| 3281 | f, mp_asm_base_get_code_size((mp_asm_base_t*)comp->emit_inline_asm), |
| 3282 | NULL, comp->scope_cur->num_pos_args, 0, type_sig); |
| 3283 | } |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | if (comp->compile_error != MP_OBJ_NULL) { |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3287 | // inline assembler had an error; set line for its exception |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3288 | inline_asm_error: |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3289 | comp->compile_error_line = pns->source_line; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 3290 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3291 | } |
Damien George | 094d450 | 2014-04-02 17:31:27 +0100 | [diff] [blame] | 3292 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3293 | |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 3294 | STATIC void scope_compute_things(scope_t *scope) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3295 | // in Micro Python we put the *x parameter after all other parameters (except **y) |
| 3296 | if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) { |
| 3297 | id_info_t *id_param = NULL; |
| 3298 | for (int i = scope->id_info_len - 1; i >= 0; i--) { |
| 3299 | id_info_t *id = &scope->id_info[i]; |
| 3300 | if (id->flags & ID_FLAG_IS_STAR_PARAM) { |
| 3301 | if (id_param != NULL) { |
| 3302 | // swap star param with last param |
| 3303 | id_info_t temp = *id_param; *id_param = *id; *id = temp; |
| 3304 | } |
| 3305 | break; |
| 3306 | } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) { |
| 3307 | id_param = id; |
| 3308 | } |
| 3309 | } |
| 3310 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3311 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3312 | // in functions, turn implicit globals into explicit globals |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3313 | // compute the index of each local |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3314 | scope->num_locals = 0; |
| 3315 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3316 | id_info_t *id = &scope->id_info[i]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 3317 | if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3318 | // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL |
| 3319 | continue; |
| 3320 | } |
Damien George | 3dea8c9 | 2016-09-30 12:34:05 +1000 | [diff] [blame] | 3321 | if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3322 | id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 3323 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3324 | // params always count for 1 local, even if they are a cell |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3325 | if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3326 | id->local_num = scope->num_locals++; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 3330 | // compute the index of cell vars |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3331 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3332 | id_info_t *id = &scope->id_info[i]; |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3333 | // in Micro Python the cells come right after the fast locals |
| 3334 | // parameters are not counted here, since they remain at the start |
| 3335 | // of the locals, even if they are cell vars |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3336 | if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3337 | id->local_num = scope->num_locals; |
| 3338 | scope->num_locals += 1; |
| 3339 | } |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3340 | } |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3341 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 3342 | // compute the index of free vars |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3343 | // make sure they are in the order of the parent scope |
| 3344 | if (scope->parent != NULL) { |
| 3345 | int num_free = 0; |
| 3346 | for (int i = 0; i < scope->parent->id_info_len; i++) { |
| 3347 | id_info_t *id = &scope->parent->id_info[i]; |
| 3348 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 3349 | for (int j = 0; j < scope->id_info_len; j++) { |
| 3350 | id_info_t *id2 = &scope->id_info[j]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 3351 | if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) { |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3352 | assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3353 | // in Micro Python the frees come first, before the params |
| 3354 | id2->local_num = num_free; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3355 | num_free += 1; |
| 3356 | } |
| 3357 | } |
| 3358 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3359 | } |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3360 | // in Micro Python shift all other locals after the free locals |
| 3361 | if (num_free > 0) { |
| 3362 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3363 | id_info_t *id = &scope->id_info[i]; |
Damien George | 2bf7c09 | 2014-04-09 15:26:46 +0100 | [diff] [blame] | 3364 | if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3365 | id->local_num += num_free; |
| 3366 | } |
| 3367 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3368 | scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3369 | scope->num_locals += num_free; |
| 3370 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3371 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3372 | } |
| 3373 | |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3374 | #if !MICROPY_PERSISTENT_CODE_SAVE |
| 3375 | STATIC |
| 3376 | #endif |
| 3377 | mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) { |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 3378 | // put compiler state on the stack, it's relatively small |
| 3379 | compiler_t comp_state = {0}; |
| 3380 | compiler_t *comp = &comp_state; |
| 3381 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 3382 | comp->source_file = source_file; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 3383 | comp->is_repl = is_repl; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3384 | |
Damien George | 62a3a28 | 2015-03-01 12:04:05 +0000 | [diff] [blame] | 3385 | // create the module scope |
Damien George | 58e0f4a | 2015-09-23 10:50:43 +0100 | [diff] [blame] | 3386 | scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt); |
Damien George | 62a3a28 | 2015-03-01 12:04:05 +0000 | [diff] [blame] | 3387 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3388 | // create standard emitter; it's used at least for MP_PASS_SCOPE |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3389 | emit_t *emit_bc = emit_bc_new(); |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3390 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3391 | // compile pass 1 |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3392 | comp->emit = emit_bc; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3393 | #if MICROPY_EMIT_NATIVE |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3394 | comp->emit_method_table = &emit_bc_method_table; |
| 3395 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3396 | uint max_num_labels = 0; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3397 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3398 | if (false) { |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3399 | #if MICROPY_EMIT_INLINE_ASM |
| 3400 | } else if (s->emit_options == MP_EMIT_OPT_ASM) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3401 | compile_scope_inline_asm(comp, s, MP_PASS_SCOPE); |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3402 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3403 | } else { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3404 | compile_scope(comp, s, MP_PASS_SCOPE); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | // update maximim number of labels needed |
| 3408 | if (comp->next_label > max_num_labels) { |
| 3409 | max_num_labels = comp->next_label; |
| 3410 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3413 | // compute some things related to scope and identifiers |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3414 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 3415 | scope_compute_things(s); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3416 | } |
| 3417 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3418 | // set max number of labels now that it's calculated |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3419 | emit_bc_set_max_num_labels(emit_bc, max_num_labels); |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3420 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3421 | // compile pass 2 and 3 |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3422 | #if MICROPY_EMIT_NATIVE |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 3423 | emit_t *emit_native = NULL; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3424 | #endif |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3425 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3426 | if (false) { |
| 3427 | // dummy |
| 3428 | |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3429 | #if MICROPY_EMIT_INLINE_ASM |
| 3430 | } else if (s->emit_options == MP_EMIT_OPT_ASM) { |
| 3431 | // inline assembly |
| 3432 | if (comp->emit_inline_asm == NULL) { |
| 3433 | comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3434 | } |
| 3435 | comp->emit = NULL; |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3436 | comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3437 | compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE); |
Damien George | de9cd00 | 2016-12-19 17:42:25 +1100 | [diff] [blame] | 3438 | #if MICROPY_EMIT_INLINE_XTENSA |
| 3439 | // Xtensa requires an extra pass to compute size of l32r const table |
| 3440 | // TODO this can be improved by calculating it during SCOPE pass |
| 3441 | // but that requires some other structural changes to the asm emitters |
| 3442 | compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE); |
| 3443 | #endif |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3444 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3445 | compile_scope_inline_asm(comp, s, MP_PASS_EMIT); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3446 | } |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3447 | #endif |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3448 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3449 | } else { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3450 | |
| 3451 | // choose the emit type |
| 3452 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3453 | switch (s->emit_options) { |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3454 | |
| 3455 | #if MICROPY_EMIT_NATIVE |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 3456 | case MP_EMIT_OPT_NATIVE_PYTHON: |
| 3457 | case MP_EMIT_OPT_VIPER: |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 3458 | if (emit_native == NULL) { |
Damien George | 080a78b | 2016-12-07 11:17:17 +1100 | [diff] [blame] | 3459 | emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3460 | } |
Damien George | 080a78b | 2016-12-07 11:17:17 +1100 | [diff] [blame] | 3461 | comp->emit_method_table = &NATIVE_EMITTER(method_table); |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3462 | comp->emit = emit_native; |
Damien George | a5190a7 | 2014-08-15 22:39:08 +0100 | [diff] [blame] | 3463 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0); |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 3464 | break; |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3465 | #endif // MICROPY_EMIT_NATIVE |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 3466 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3467 | default: |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3468 | comp->emit = emit_bc; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3469 | #if MICROPY_EMIT_NATIVE |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3470 | comp->emit_method_table = &emit_bc_method_table; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3471 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3472 | break; |
| 3473 | } |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3474 | |
Damien George | 1e1779e | 2015-01-14 00:20:28 +0000 | [diff] [blame] | 3475 | // need a pass to compute stack size |
| 3476 | compile_scope(comp, s, MP_PASS_STACK_SIZE); |
| 3477 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3478 | // second last pass: compute code size |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3479 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3480 | compile_scope(comp, s, MP_PASS_CODE_SIZE); |
| 3481 | } |
| 3482 | |
| 3483 | // final pass: emit code |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3484 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3485 | compile_scope(comp, s, MP_PASS_EMIT); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3486 | } |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 3487 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3490 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3491 | // if there is no line number for the error then use the line |
| 3492 | // number for the start of this scope |
| 3493 | compile_error_set_line(comp, comp->scope_cur->pn); |
| 3494 | // add a traceback to the exception using relevant source info |
| 3495 | mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, |
| 3496 | comp->compile_error_line, comp->scope_cur->simple_name); |
| 3497 | } |
| 3498 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3499 | // free the emitters |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3500 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3501 | emit_bc_free(emit_bc); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3502 | #if MICROPY_EMIT_NATIVE |
| 3503 | if (emit_native != NULL) { |
Damien George | 080a78b | 2016-12-07 11:17:17 +1100 | [diff] [blame] | 3504 | NATIVE_EMITTER(free)(emit_native); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3505 | } |
| 3506 | #endif |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3507 | #if MICROPY_EMIT_INLINE_ASM |
| 3508 | if (comp->emit_inline_asm != NULL) { |
| 3509 | ASM_EMITTER(free)(comp->emit_inline_asm); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3510 | } |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 3511 | #endif |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3512 | |
Damien George | 52b5d76 | 2014-09-23 15:31:56 +0000 | [diff] [blame] | 3513 | // free the parse tree |
Damien George | 58e0f4a | 2015-09-23 10:50:43 +0100 | [diff] [blame] | 3514 | mp_parse_tree_clear(parse_tree); |
Damien George | 52b5d76 | 2014-09-23 15:31:56 +0000 | [diff] [blame] | 3515 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3516 | // free the scopes |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 3517 | mp_raw_code_t *outer_raw_code = module_scope->raw_code; |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 3518 | for (scope_t *s = module_scope; s;) { |
| 3519 | scope_t *next = s->next; |
| 3520 | scope_free(s); |
| 3521 | s = next; |
| 3522 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 3523 | |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 3524 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3525 | nlr_raise(comp->compile_error); |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 3526 | } else { |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3527 | return outer_raw_code; |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 3528 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3529 | } |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3530 | |
| 3531 | mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) { |
| 3532 | mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl); |
| 3533 | // return function that executes the outer module |
| 3534 | return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); |
| 3535 | } |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 3536 | |
| 3537 | #endif // MICROPY_ENABLE_COMPILER |