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 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 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/emit.h" |
| 34 | #include "py/bc0.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | |
Damien George | 5f6a25f | 2014-04-20 18:02:27 +0100 | [diff] [blame] | 36 | #if !MICROPY_EMIT_CPYTHON |
| 37 | |
Damien George | 9597771 | 2014-05-10 18:07:08 +0100 | [diff] [blame] | 38 | #define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) |
| 39 | #define DUMMY_DATA_SIZE (BYTES_FOR_INT) |
| 40 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 41 | struct _emit_t { |
Damien George | 0fb80c3 | 2014-05-10 18:16:21 +0100 | [diff] [blame] | 42 | pass_kind_t pass : 8; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 43 | mp_uint_t last_emit_was_return_value : 8; |
Damien George | 0fb80c3 | 2014-05-10 18:16:21 +0100 | [diff] [blame] | 44 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 45 | int stack_size; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 46 | |
| 47 | scope_t *scope; |
| 48 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 49 | mp_uint_t last_source_line_offset; |
| 50 | mp_uint_t last_source_line; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 51 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 52 | mp_uint_t max_num_labels; |
| 53 | mp_uint_t *label_offsets; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 54 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 55 | mp_uint_t code_info_offset; |
| 56 | mp_uint_t code_info_size; |
| 57 | mp_uint_t bytecode_offset; |
| 58 | mp_uint_t bytecode_size; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 59 | byte *code_base; // stores both byte code and code info |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 60 | // Accessed as mp_uint_t, so must be aligned as such |
Paul Sokolovsky | 58c9586 | 2014-07-12 14:51:48 +0300 | [diff] [blame] | 61 | byte dummy_data[DUMMY_DATA_SIZE]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Damien George | 1d24ea5 | 2014-04-08 21:11:49 +0100 | [diff] [blame] | 64 | STATIC void emit_bc_rot_two(emit_t *emit); |
Damien George | f4c9b33 | 2014-04-08 21:32:29 +0100 | [diff] [blame] | 65 | STATIC void emit_bc_rot_three(emit_t *emit); |
Damien George | 1d24ea5 | 2014-04-08 21:11:49 +0100 | [diff] [blame] | 66 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 67 | emit_t *emit_bc_new(mp_uint_t max_num_labels) { |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 68 | emit_t *emit = m_new0(emit_t, 1); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 69 | emit->max_num_labels = max_num_labels; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 70 | emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 71 | return emit; |
| 72 | } |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 73 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 74 | void emit_bc_free(emit_t *emit) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 75 | m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels); |
Paul Sokolovsky | f46d87a | 2014-01-24 16:20:11 +0200 | [diff] [blame] | 76 | m_del_obj(emit_t, emit); |
| 77 | } |
| 78 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 79 | STATIC void emit_write_uint(emit_t* emit, byte*(*allocator)(emit_t*, int), mp_uint_t val) { |
| 80 | // We store each 7 bits in a separate byte, and that's how many bytes needed |
| 81 | byte buf[BYTES_FOR_INT]; |
| 82 | byte *p = buf + sizeof(buf); |
| 83 | // We encode in little-ending order, but store in big-endian, to help decoding |
| 84 | do { |
| 85 | *--p = val & 0x7f; |
| 86 | val >>= 7; |
| 87 | } while (val != 0); |
| 88 | byte* c = allocator(emit, buf + sizeof(buf) - p); |
| 89 | while (p != buf + sizeof(buf) - 1) { |
| 90 | *c++ = *p++ | 0x80; |
| 91 | } |
| 92 | *c = *p; |
| 93 | } |
| 94 | |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 95 | // all functions must go through this one to emit code info |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 96 | STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 97 | //printf("emit %d\n", num_bytes_to_write); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 98 | if (emit->pass < MP_PASS_EMIT) { |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 99 | emit->code_info_offset += num_bytes_to_write; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 100 | return emit->dummy_data; |
| 101 | } else { |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 102 | assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size); |
| 103 | byte *c = emit->code_base + emit->code_info_offset; |
| 104 | emit->code_info_offset += num_bytes_to_write; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 105 | return c; |
| 106 | } |
| 107 | } |
| 108 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 109 | STATIC void emit_align_code_info_to_machine_word(emit_t* emit) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 110 | emit->code_info_offset = (emit->code_info_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1)); |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 113 | STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) { |
| 114 | emit_write_uint(emit, emit_get_cur_to_write_code_info, val); |
| 115 | } |
| 116 | |
| 117 | STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qst) { |
| 118 | emit_write_uint(emit, emit_get_cur_to_write_code_info, qst); |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 121 | #if MICROPY_ENABLE_SOURCE_LINE |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 122 | STATIC void emit_write_code_info_bytes_lines(emit_t* emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) { |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 123 | assert(bytes_to_skip > 0 || lines_to_skip > 0); |
Damien George | 4747bec | 2014-07-31 16:12:01 +0000 | [diff] [blame] | 124 | //printf(" %d %d\n", bytes_to_skip, lines_to_skip); |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 125 | while (bytes_to_skip > 0 || lines_to_skip > 0) { |
Damien George | 4747bec | 2014-07-31 16:12:01 +0000 | [diff] [blame] | 126 | mp_uint_t b, l; |
| 127 | if (lines_to_skip <= 6) { |
| 128 | // use 0b0LLBBBBB encoding |
| 129 | b = MIN(bytes_to_skip, 0x1f); |
| 130 | l = MIN(lines_to_skip, 0x3); |
| 131 | *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5); |
| 132 | } else { |
| 133 | // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte) |
| 134 | b = MIN(bytes_to_skip, 0xf); |
| 135 | l = MIN(lines_to_skip, 0x7ff); |
| 136 | byte *ci = emit_get_cur_to_write_code_info(emit, 2); |
| 137 | ci[0] = 0x80 | b | ((l >> 4) & 0x70); |
| 138 | ci[1] = l; |
| 139 | } |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 140 | bytes_to_skip -= b; |
| 141 | lines_to_skip -= l; |
Damien George | 28eb577 | 2014-01-25 11:43:20 +0000 | [diff] [blame] | 142 | } |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 143 | } |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 144 | #endif |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 145 | |
| 146 | // all functions must go through this one to emit byte code |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 147 | STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) { |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 148 | //printf("emit %d\n", num_bytes_to_write); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 149 | if (emit->pass < MP_PASS_EMIT) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 150 | emit->bytecode_offset += num_bytes_to_write; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 151 | return emit->dummy_data; |
| 152 | } else { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 153 | assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size); |
| 154 | byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset; |
| 155 | emit->bytecode_offset += num_bytes_to_write; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 156 | return c; |
| 157 | } |
| 158 | } |
| 159 | |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 160 | STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 161 | emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1)); |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 164 | STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) { |
| 165 | byte* c = emit_get_cur_to_write_bytecode(emit, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 166 | c[0] = b1; |
| 167 | } |
| 168 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 169 | STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) { |
| 170 | emit_write_uint(emit, emit_get_cur_to_write_bytecode, val); |
| 171 | } |
| 172 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 173 | STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 174 | assert((b2 & (~0xff)) == 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 175 | byte* c = emit_get_cur_to_write_bytecode(emit, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 176 | c[0] = b1; |
| 177 | c[1] = b2; |
| 178 | } |
| 179 | |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 180 | // Similar to emit_write_bytecode_uint(), just some extra handling to encode sign |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 181 | STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 182 | emit_write_bytecode_byte(emit, b1); |
Paul Sokolovsky | 047cd40 | 2014-02-19 15:47:59 +0200 | [diff] [blame] | 183 | |
| 184 | // We store each 7 bits in a separate byte, and that's how many bytes needed |
Damien George | 9597771 | 2014-05-10 18:07:08 +0100 | [diff] [blame] | 185 | byte buf[BYTES_FOR_INT]; |
Paul Sokolovsky | 047cd40 | 2014-02-19 15:47:59 +0200 | [diff] [blame] | 186 | byte *p = buf + sizeof(buf); |
| 187 | // We encode in little-ending order, but store in big-endian, to help decoding |
| 188 | do { |
| 189 | *--p = num & 0x7f; |
| 190 | num >>= 7; |
| 191 | } while (num != 0 && num != -1); |
| 192 | // Make sure that highest bit we stored (mask 0x40) matches sign |
| 193 | // of the number. If not, store extra byte just to encode sign |
| 194 | if (num == -1 && (*p & 0x40) == 0) { |
| 195 | *--p = 0x7f; |
| 196 | } else if (num == 0 && (*p & 0x40) != 0) { |
| 197 | *--p = 0; |
| 198 | } |
| 199 | |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 200 | byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p); |
Paul Sokolovsky | 047cd40 | 2014-02-19 15:47:59 +0200 | [diff] [blame] | 201 | while (p != buf + sizeof(buf) - 1) { |
| 202 | *c++ = *p++ | 0x80; |
| 203 | } |
| 204 | *c = *p; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 207 | STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 208 | emit_write_bytecode_byte(emit, b); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 209 | emit_write_uint(emit, emit_get_cur_to_write_bytecode, val); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 212 | STATIC void emit_write_bytecode_prealigned_ptr(emit_t* emit, void *ptr) { |
| 213 | mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t)); |
| 214 | // Verify thar c is already uint-aligned |
| 215 | assert(c == MP_ALIGN(c, sizeof(mp_uint_t))); |
| 216 | *c = (mp_uint_t)ptr; |
| 217 | } |
| 218 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 219 | // aligns the pointer so it is friendly to GC |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 220 | STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) { |
| 221 | emit_write_bytecode_byte(emit, b); |
| 222 | emit_align_bytecode_to_machine_word(emit); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 223 | mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t)); |
Paul Sokolovsky | 58c9586 | 2014-07-12 14:51:48 +0300 | [diff] [blame] | 224 | // Verify thar c is already uint-aligned |
| 225 | assert(c == MP_ALIGN(c, sizeof(mp_uint_t))); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 226 | *c = (mp_uint_t)ptr; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Damien George | fb083ea | 2014-02-01 18:29:40 +0000 | [diff] [blame] | 229 | /* currently unused |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 230 | STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, mp_uint_t num1, mp_uint_t num2) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 231 | emit_write_bytecode_byte(emit, b); |
| 232 | emit_write_bytecode_byte_uint(emit, num1); |
| 233 | emit_write_bytecode_byte_uint(emit, num2); |
Damien George | fb083ea | 2014-02-01 18:29:40 +0000 | [diff] [blame] | 234 | } |
| 235 | */ |
| 236 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 237 | STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) { |
| 238 | emit_write_bytecode_byte_uint(emit, b, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 241 | // unsigned labels are relative to ip following this instruction, stored as 16 bits |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 242 | STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) { |
| 243 | mp_uint_t bytecode_offset; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 244 | if (emit->pass < MP_PASS_EMIT) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 245 | bytecode_offset = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 246 | } else { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 247 | bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 248 | } |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 249 | byte *c = emit_get_cur_to_write_bytecode(emit, 3); |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 250 | c[0] = b1; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 251 | c[1] = bytecode_offset; |
| 252 | c[2] = bytecode_offset >> 8; |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // signed labels are relative to ip following this instruction, stored as 16 bits, in excess |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 256 | STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 257 | int bytecode_offset; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 258 | if (emit->pass < MP_PASS_EMIT) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 259 | bytecode_offset = 0; |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 260 | } else { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 261 | bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000; |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 262 | } |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 263 | byte* c = emit_get_cur_to_write_bytecode(emit, 3); |
Damien | 03c9cfb | 2013-11-05 22:06:08 +0000 | [diff] [blame] | 264 | c[0] = b1; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 265 | c[1] = bytecode_offset; |
| 266 | c[2] = bytecode_offset >> 8; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 269 | STATIC void emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 272 | STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 273 | emit->pass = pass; |
| 274 | emit->stack_size = 0; |
| 275 | emit->last_emit_was_return_value = false; |
| 276 | emit->scope = scope; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 277 | emit->last_source_line_offset = 0; |
| 278 | emit->last_source_line = 1; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 279 | if (pass < MP_PASS_EMIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 280 | memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t)); |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 281 | } |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 282 | emit->bytecode_offset = 0; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 283 | emit->code_info_offset = 0; |
| 284 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 285 | // Write code info size as compressed uint. If we are not in the final pass |
| 286 | // then space for this uint is reserved in emit_bc_end_pass. |
| 287 | if (pass == MP_PASS_EMIT) { |
| 288 | emit_write_code_info_uint(emit, emit->code_info_size); |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 291 | // write the name and source file of this function |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 292 | emit_write_code_info_qstr(emit, scope->simple_name); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 293 | emit_write_code_info_qstr(emit, scope->source_file); |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 294 | |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 295 | // bytecode prelude: argument names (needed to resolve positional args passed as keywords) |
| 296 | // we store them as full word-sized objects for efficient access in mp_setup_code_state |
| 297 | // this is the start of the prelude and is guaranteed to be aligned on a word boundary |
| 298 | { |
| 299 | for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) { |
| 300 | emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst)); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // bytecode prelude: local state size and exception stack size |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 305 | { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 306 | mp_uint_t n_state = scope->num_locals + scope->stack_size; |
Damien George | 190d1ba | 2014-04-10 16:59:56 +0000 | [diff] [blame] | 307 | if (n_state == 0) { |
| 308 | // Need at least 1 entry in the state, in the case an exception is |
| 309 | // propagated through this function, the exception is returned in |
| 310 | // the highest slot in the state (fastn[0], see vm.c). |
| 311 | n_state = 1; |
| 312 | } |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 313 | emit_write_bytecode_uint(emit, n_state); |
| 314 | emit_write_bytecode_uint(emit, scope->exc_stack_size); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // bytecode prelude: initialise closed over variables |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 318 | int num_cell = 0; |
| 319 | for (int i = 0; i < scope->id_info_len; i++) { |
| 320 | id_info_t *id = &scope->id_info[i]; |
| 321 | if (id->kind == ID_INFO_KIND_CELL) { |
| 322 | num_cell += 1; |
| 323 | } |
| 324 | } |
| 325 | assert(num_cell <= 255); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 326 | emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 327 | for (int i = 0; i < scope->id_info_len; i++) { |
| 328 | id_info_t *id = &scope->id_info[i]; |
| 329 | if (id->kind == ID_INFO_KIND_CELL) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 330 | emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 335 | STATIC void emit_bc_end_pass(emit_t *emit) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 336 | // check stack is back to zero size |
| 337 | if (emit->stack_size != 0) { |
| 338 | printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size); |
| 339 | } |
| 340 | |
Damien George | 73496fb | 2014-04-13 14:51:56 +0100 | [diff] [blame] | 341 | *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 342 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 343 | if (emit->pass == MP_PASS_CODE_SIZE) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 344 | // Need to make sure we have enough room in the code-info block to write |
| 345 | // the size of the code-info block. Since the size is written as a |
| 346 | // compressed uint, we don't know its size until we write it! Thus, we |
| 347 | // take the biggest possible value it could be and write that here. |
| 348 | // Then there will be enough room to write the value, and any leftover |
| 349 | // space will be absorbed in the alignment at the end of the code-info |
| 350 | // block. |
| 351 | mp_uint_t max_code_info_size = |
| 352 | emit->code_info_offset // current code-info size |
| 353 | + BYTES_FOR_INT // maximum space for compressed uint |
| 354 | + BYTES_PER_WORD - 1; // maximum space for alignment padding |
| 355 | emit_write_code_info_uint(emit, max_code_info_size); |
| 356 | |
| 357 | // Align code-info so that following bytecode is aligned on a machine word. |
| 358 | // We don't need to write anything here, it's just dead space between the |
| 359 | // code-info block and the bytecode block that follows it. |
| 360 | emit_align_code_info_to_machine_word(emit); |
| 361 | |
| 362 | // calculate size of total code-info + bytecode, in bytes |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 363 | emit->code_info_size = emit->code_info_offset; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 364 | emit->bytecode_size = emit->bytecode_offset; |
| 365 | emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size); |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 366 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 367 | } else if (emit->pass == MP_PASS_EMIT) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 368 | mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base, |
| 369 | emit->code_info_size + emit->bytecode_size, |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 370 | emit->scope->num_pos_args, emit->scope->num_kwonly_args, |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 371 | emit->scope->scope_flags); |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 375 | STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 376 | return emit->last_emit_was_return_value; |
| 377 | } |
| 378 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 379 | STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) { |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 380 | emit->stack_size += delta; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 383 | STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 384 | //printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset); |
Damien George | 62ad189 | 2014-01-29 21:51:51 +0000 | [diff] [blame] | 385 | #if MICROPY_ENABLE_SOURCE_LINE |
Paul Sokolovsky | b8f117d | 2014-06-02 19:39:15 +0300 | [diff] [blame] | 386 | if (mp_optimise_value >= 3) { |
| 387 | // If we compile with -O3, don't store line numbers. |
| 388 | return; |
| 389 | } |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 390 | if (source_line > emit->last_source_line) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 391 | mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset; |
| 392 | mp_uint_t lines_to_skip = source_line - emit->last_source_line; |
Damien George | 28eb577 | 2014-01-25 11:43:20 +0000 | [diff] [blame] | 393 | emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 394 | emit->last_source_line_offset = emit->bytecode_offset; |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 395 | emit->last_source_line = source_line; |
| 396 | } |
Damien George | 62ad189 | 2014-01-29 21:51:51 +0000 | [diff] [blame] | 397 | #endif |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 400 | STATIC void emit_bc_load_id(emit_t *emit, qstr qst) { |
| 401 | emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 404 | STATIC void emit_bc_store_id(emit_t *emit, qstr qst) { |
| 405 | emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 408 | STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) { |
| 409 | emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 412 | STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) { |
| 413 | assert((mp_int_t)emit->stack_size + stack_size_delta >= 0); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 414 | emit->stack_size += stack_size_delta; |
| 415 | if (emit->stack_size > emit->scope->stack_size) { |
| 416 | emit->scope->stack_size = emit->stack_size; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 417 | } |
| 418 | emit->last_emit_was_return_value = false; |
| 419 | } |
| 420 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 421 | STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 422 | emit_bc_pre(emit, 0); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 423 | assert(l < emit->max_num_labels); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 424 | if (emit->pass < MP_PASS_EMIT) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 425 | // assign label offset |
| 426 | assert(emit->label_offsets[l] == -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 427 | emit->label_offsets[l] = emit->bytecode_offset; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 428 | } else { |
| 429 | // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 430 | //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]); |
| 431 | assert(emit->label_offsets[l] == emit->bytecode_offset); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 435 | STATIC void emit_bc_import_name(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 436 | emit_bc_pre(emit, -1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 437 | emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 440 | STATIC void emit_bc_import_from(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 441 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 442 | emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 445 | STATIC void emit_bc_import_star(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 446 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 447 | emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 450 | STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 451 | emit_bc_pre(emit, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 452 | switch (tok) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 453 | case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break; |
| 454 | case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break; |
| 455 | case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break; |
| 456 | case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 457 | default: assert(0); |
| 458 | } |
| 459 | } |
| 460 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 461 | STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 462 | emit_bc_pre(emit, 1); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 463 | if (-16 <= arg && arg <= 47) { |
| 464 | emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg); |
| 465 | } else { |
| 466 | emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg); |
| 467 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 470 | STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 471 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 472 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 475 | STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 476 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 477 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 480 | STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 481 | emit_bc_pre(emit, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 482 | if (bytes) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 483 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 484 | } else { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 485 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Damien George | 523b575 | 2014-03-31 11:59:23 +0100 | [diff] [blame] | 489 | STATIC void emit_bc_load_null(emit_t *emit) { |
| 490 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 491 | emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL); |
Damien George | 523b575 | 2014-03-31 11:59:23 +0100 | [diff] [blame] | 492 | }; |
| 493 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 494 | STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 495 | assert(local_num >= 0); |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 496 | emit_bc_pre(emit, 1); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 497 | if (local_num <= 15) { |
| 498 | emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num); |
| 499 | } else { |
| 500 | emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 504 | STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 505 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 506 | emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 509 | STATIC void emit_bc_load_name(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 510 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 511 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 514 | STATIC void emit_bc_load_global(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 515 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 516 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 519 | STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 520 | emit_bc_pre(emit, 0); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 521 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 524 | STATIC void emit_bc_load_method(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 525 | emit_bc_pre(emit, 1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 526 | emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 529 | STATIC void emit_bc_load_build_class(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 530 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 531 | emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 534 | STATIC void emit_bc_load_subscr(emit_t *emit) { |
| 535 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 536 | emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR); |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 539 | STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 540 | assert(local_num >= 0); |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 541 | emit_bc_pre(emit, -1); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 542 | if (local_num <= 15) { |
| 543 | emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num); |
| 544 | } else { |
| 545 | emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 549 | STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 550 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 551 | emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 554 | STATIC void emit_bc_store_name(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 555 | emit_bc_pre(emit, -1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 556 | emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 559 | STATIC void emit_bc_store_global(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 560 | emit_bc_pre(emit, -1); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 561 | emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 564 | STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 565 | emit_bc_pre(emit, -2); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 566 | emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 569 | STATIC void emit_bc_store_subscr(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 570 | emit_bc_pre(emit, -3); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 571 | emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 574 | STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 575 | emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 578 | STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 579 | emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 582 | STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 583 | emit_bc_pre(emit, 0); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 584 | emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 587 | STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 588 | emit_bc_pre(emit, 0); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 589 | emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 592 | STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) { |
Damien George | 1d24ea5 | 2014-04-08 21:11:49 +0100 | [diff] [blame] | 593 | emit_bc_load_null(emit); |
| 594 | emit_bc_rot_two(emit); |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 595 | emit_bc_store_attr(emit, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 598 | STATIC void emit_bc_delete_subscr(emit_t *emit) { |
Damien George | f4c9b33 | 2014-04-08 21:32:29 +0100 | [diff] [blame] | 599 | emit_bc_load_null(emit); |
| 600 | emit_bc_rot_three(emit); |
| 601 | emit_bc_store_subscr(emit); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 604 | STATIC void emit_bc_dup_top(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 605 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 606 | emit_write_bytecode_byte(emit, MP_BC_DUP_TOP); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 609 | STATIC void emit_bc_dup_top_two(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 610 | emit_bc_pre(emit, 2); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 611 | emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 614 | STATIC void emit_bc_pop_top(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 615 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 616 | emit_write_bytecode_byte(emit, MP_BC_POP_TOP); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 619 | STATIC void emit_bc_rot_two(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 620 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 621 | emit_write_bytecode_byte(emit, MP_BC_ROT_TWO); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 624 | STATIC void emit_bc_rot_three(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 625 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 626 | emit_write_bytecode_byte(emit, MP_BC_ROT_THREE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 629 | STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 630 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 631 | emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 634 | STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 635 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 636 | emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 639 | STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 640 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 641 | emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 644 | STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 645 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 646 | emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 649 | STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 650 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 651 | emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 654 | STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 655 | if (except_depth == 0) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 656 | emit_bc_pre(emit, 0); |
Damien George | 25c8464 | 2014-05-30 15:20:41 +0100 | [diff] [blame] | 657 | if (label & MP_EMIT_BREAK_FROM_FOR) { |
| 658 | // need to pop the iterator if we are breaking out of a for loop |
| 659 | emit_write_bytecode_byte(emit, MP_BC_POP_TOP); |
| 660 | } |
| 661 | emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR); |
| 662 | } else { |
| 663 | emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR); |
| 664 | emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 665 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 668 | STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 669 | emit_bc_pre(emit, 7); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 670 | emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 673 | STATIC void emit_bc_with_cleanup(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 674 | emit_bc_pre(emit, -7); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 675 | emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 678 | STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) { |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 679 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 680 | emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 683 | STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) { |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 684 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 685 | emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 688 | STATIC void emit_bc_end_finally(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 689 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 690 | emit_write_bytecode_byte(emit, MP_BC_END_FINALLY); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 693 | STATIC void emit_bc_get_iter(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 694 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 695 | emit_write_bytecode_byte(emit, MP_BC_GET_ITER); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 698 | STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 699 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 700 | emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 703 | STATIC void emit_bc_for_iter_end(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 704 | emit_bc_pre(emit, -1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 707 | STATIC void emit_bc_pop_block(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 708 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 709 | emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 712 | STATIC void emit_bc_pop_except(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 713 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 714 | emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 717 | STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) { |
| 718 | if (op == MP_UNARY_OP_NOT) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 719 | emit_bc_pre(emit, 0); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 720 | emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL); |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 721 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 722 | emit_write_bytecode_byte(emit, MP_BC_NOT); |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 723 | } else { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 724 | emit_bc_pre(emit, 0); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 725 | emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op); |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 726 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 729 | STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) { |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 730 | bool invert = false; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 731 | if (op == MP_BINARY_OP_NOT_IN) { |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 732 | invert = true; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 733 | op = MP_BINARY_OP_IN; |
| 734 | } else if (op == MP_BINARY_OP_IS_NOT) { |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 735 | invert = true; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 736 | op = MP_BINARY_OP_IS; |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 737 | } |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 738 | emit_bc_pre(emit, -1); |
Damien George | 8456cc0 | 2014-10-25 16:43:46 +0100 | [diff] [blame] | 739 | emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op); |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 740 | if (invert) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 741 | emit_bc_pre(emit, 0); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 742 | emit_write_bytecode_byte(emit, MP_BC_NOT); |
Damien George | 9aa2a52 | 2014-02-01 23:04:09 +0000 | [diff] [blame] | 743 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 746 | STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 747 | emit_bc_pre(emit, 1 - n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 748 | emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 751 | STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 752 | emit_bc_pre(emit, 1 - n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 753 | emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 756 | STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 757 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 758 | emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 761 | STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 762 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 763 | emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 766 | STATIC void emit_bc_store_map(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 767 | emit_bc_pre(emit, -2); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 768 | emit_write_bytecode_byte(emit, MP_BC_STORE_MAP); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 771 | STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 772 | emit_bc_pre(emit, -2); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 773 | emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 774 | } |
| 775 | |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 776 | #if MICROPY_PY_BUILTINS_SET |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 777 | STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 778 | emit_bc_pre(emit, 1 - n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 779 | emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 782 | STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 783 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 784 | emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 785 | } |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 786 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 787 | |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 788 | #if MICROPY_PY_BUILTINS_SLICE |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 789 | STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 790 | emit_bc_pre(emit, 1 - n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 791 | emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 792 | } |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 793 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 794 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 795 | STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 796 | emit_bc_pre(emit, -1 + n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 797 | emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 800 | STATIC void emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 801 | emit_bc_pre(emit, -1 + n_left + n_right + 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 802 | emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 805 | STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) { |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 806 | if (n_pos_defaults == 0 && n_kw_defaults == 0) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 807 | emit_bc_pre(emit, 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 808 | emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code); |
Damien George | fb083ea | 2014-02-01 18:29:40 +0000 | [diff] [blame] | 809 | } else { |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 810 | emit_bc_pre(emit, -1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 811 | emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code); |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 812 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 815 | STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) { |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 816 | if (n_pos_defaults == 0 && n_kw_defaults == 0) { |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 817 | emit_bc_pre(emit, -n_closed_over + 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 818 | emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code); |
| 819 | emit_write_bytecode_byte(emit, n_closed_over); |
Paul Sokolovsky | 2447a5b | 2014-03-26 23:14:59 +0200 | [diff] [blame] | 820 | } else { |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 821 | assert(n_closed_over <= 255); |
| 822 | emit_bc_pre(emit, -2 - n_closed_over + 1); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 823 | emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code); |
| 824 | emit_write_bytecode_byte(emit, n_closed_over); |
Paul Sokolovsky | 2447a5b | 2014-03-26 23:14:59 +0200 | [diff] [blame] | 825 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 828 | STATIC void emit_bc_call_function_method_helper(emit_t *emit, mp_int_t stack_adj, mp_uint_t bytecode_base, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) { |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 829 | if (star_flags) { |
| 830 | if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) { |
Damien George | 523b575 | 2014-03-31 11:59:23 +0100 | [diff] [blame] | 831 | // load dummy entry for non-existent pos_seq |
| 832 | emit_bc_load_null(emit); |
| 833 | emit_bc_rot_two(emit); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 834 | } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) { |
Damien George | 523b575 | 2014-03-31 11:59:23 +0100 | [diff] [blame] | 835 | // load dummy entry for non-existent kw_dict |
| 836 | emit_bc_load_null(emit); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 837 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 838 | emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 839 | emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 840 | } else { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 841 | emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 842 | emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 843 | } |
Damien George | 523b575 | 2014-03-31 11:59:23 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 846 | STATIC void emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) { |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 847 | emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 850 | STATIC void emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) { |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 851 | emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 852 | } |
| 853 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 854 | STATIC void emit_bc_return_value(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 855 | emit_bc_pre(emit, -1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 856 | emit->last_emit_was_return_value = true; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 857 | emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 860 | STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) { |
Damien George | 25042b1 | 2014-01-11 09:33:39 +0000 | [diff] [blame] | 861 | assert(0 <= n_args && n_args <= 2); |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 862 | emit_bc_pre(emit, -n_args); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 863 | emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 866 | STATIC void emit_bc_yield_value(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 867 | emit_bc_pre(emit, 0); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 868 | emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 869 | emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 872 | STATIC void emit_bc_yield_from(emit_t *emit) { |
Damien George | ce8f07a | 2014-03-27 23:30:26 +0000 | [diff] [blame] | 873 | emit_bc_pre(emit, -1); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 874 | emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 875 | emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 878 | STATIC void emit_bc_start_except_handler(emit_t *emit) { |
| 879 | emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state |
| 880 | } |
| 881 | |
| 882 | STATIC void emit_bc_end_except_handler(emit_t *emit) { |
| 883 | emit_bc_adjust_stack_size(emit, -5); // stack adjust |
| 884 | } |
| 885 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 886 | const emit_method_table_t emit_bc_method_table = { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 887 | emit_bc_set_native_type, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 888 | emit_bc_start_pass, |
| 889 | emit_bc_end_pass, |
| 890 | emit_bc_last_emit_was_return_value, |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 891 | emit_bc_adjust_stack_size, |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 892 | emit_bc_set_source_line, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 893 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 894 | emit_bc_load_id, |
| 895 | emit_bc_store_id, |
| 896 | emit_bc_delete_id, |
| 897 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 898 | emit_bc_label_assign, |
| 899 | emit_bc_import_name, |
| 900 | emit_bc_import_from, |
| 901 | emit_bc_import_star, |
| 902 | emit_bc_load_const_tok, |
| 903 | emit_bc_load_const_small_int, |
| 904 | emit_bc_load_const_int, |
| 905 | emit_bc_load_const_dec, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 906 | emit_bc_load_const_str, |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 907 | emit_bc_load_null, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 908 | emit_bc_load_fast, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 909 | emit_bc_load_deref, |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 910 | emit_bc_load_name, |
| 911 | emit_bc_load_global, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 912 | emit_bc_load_attr, |
| 913 | emit_bc_load_method, |
| 914 | emit_bc_load_build_class, |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 915 | emit_bc_load_subscr, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 916 | emit_bc_store_fast, |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 917 | emit_bc_store_deref, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 918 | emit_bc_store_name, |
| 919 | emit_bc_store_global, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 920 | emit_bc_store_attr, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 921 | emit_bc_store_subscr, |
| 922 | emit_bc_delete_fast, |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 923 | emit_bc_delete_deref, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 924 | emit_bc_delete_name, |
| 925 | emit_bc_delete_global, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 926 | emit_bc_delete_attr, |
| 927 | emit_bc_delete_subscr, |
| 928 | emit_bc_dup_top, |
| 929 | emit_bc_dup_top_two, |
| 930 | emit_bc_pop_top, |
| 931 | emit_bc_rot_two, |
| 932 | emit_bc_rot_three, |
| 933 | emit_bc_jump, |
| 934 | emit_bc_pop_jump_if_true, |
| 935 | emit_bc_pop_jump_if_false, |
| 936 | emit_bc_jump_if_true_or_pop, |
| 937 | emit_bc_jump_if_false_or_pop, |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 938 | emit_bc_unwind_jump, |
| 939 | emit_bc_unwind_jump, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 940 | emit_bc_setup_with, |
| 941 | emit_bc_with_cleanup, |
| 942 | emit_bc_setup_except, |
| 943 | emit_bc_setup_finally, |
| 944 | emit_bc_end_finally, |
| 945 | emit_bc_get_iter, |
| 946 | emit_bc_for_iter, |
| 947 | emit_bc_for_iter_end, |
| 948 | emit_bc_pop_block, |
| 949 | emit_bc_pop_except, |
| 950 | emit_bc_unary_op, |
| 951 | emit_bc_binary_op, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 952 | emit_bc_build_tuple, |
| 953 | emit_bc_build_list, |
| 954 | emit_bc_list_append, |
| 955 | emit_bc_build_map, |
| 956 | emit_bc_store_map, |
| 957 | emit_bc_map_add, |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 958 | #if MICROPY_PY_BUILTINS_SET |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 959 | emit_bc_build_set, |
| 960 | emit_bc_set_add, |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 961 | #endif |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 962 | #if MICROPY_PY_BUILTINS_SLICE |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 963 | emit_bc_build_slice, |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 964 | #endif |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 965 | emit_bc_unpack_sequence, |
| 966 | emit_bc_unpack_ex, |
| 967 | emit_bc_make_function, |
| 968 | emit_bc_make_closure, |
| 969 | emit_bc_call_function, |
| 970 | emit_bc_call_method, |
| 971 | emit_bc_return_value, |
| 972 | emit_bc_raise_varargs, |
| 973 | emit_bc_yield_value, |
| 974 | emit_bc_yield_from, |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 975 | |
| 976 | emit_bc_start_except_handler, |
| 977 | emit_bc_end_except_handler, |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 978 | }; |
Damien George | 5f6a25f | 2014-04-20 18:02:27 +0100 | [diff] [blame] | 979 | |
| 980 | #endif // !MICROPY_EMIT_CPYTHON |