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