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