blob: 4142e892d6e1f935781532fe459e9be7e7c6a494 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien Georgeb4b10fd2015-01-01 23:30:53 +000033#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/emit.h"
35#include "py/bc0.h"
Damien429d7192013-10-04 19:53:11 +010036
Damien Georgedd5353a2015-12-18 12:35:44 +000037#if MICROPY_ENABLE_COMPILER
38
Damien George95977712014-05-10 18:07:08 +010039#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
40#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
41
Damien415eb6f2013-10-05 12:19:06 +010042struct _emit_t {
Damien George999cedb2015-11-27 17:01:44 +000043 // Accessed as mp_obj_t, so must be aligned as such, and we rely on the
44 // memory allocator returning a suitably aligned pointer.
45 // Should work for cases when mp_obj_t is 64-bit on a 32-bit machine.
46 byte dummy_data[DUMMY_DATA_SIZE];
47
Damien George0fb80c32014-05-10 18:16:21 +010048 pass_kind_t pass : 8;
Damien George7ff996c2014-09-08 23:05:16 +010049 mp_uint_t last_emit_was_return_value : 8;
Damien George0fb80c32014-05-10 18:16:21 +010050
Damien429d7192013-10-04 19:53:11 +010051 int stack_size;
Damien429d7192013-10-04 19:53:11 +010052
53 scope_t *scope;
54
Damien George7ff996c2014-09-08 23:05:16 +010055 mp_uint_t last_source_line_offset;
56 mp_uint_t last_source_line;
Damien George08335002014-01-18 23:24:36 +000057
Damien George7ff996c2014-09-08 23:05:16 +010058 mp_uint_t max_num_labels;
59 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010060
Damien George999cedb2015-11-27 17:01:44 +000061 size_t code_info_offset;
62 size_t code_info_size;
63 size_t bytecode_offset;
64 size_t bytecode_size;
Damien George08335002014-01-18 23:24:36 +000065 byte *code_base; // stores both byte code and code info
Damien Georgec8e9c0d2015-11-02 17:27:18 +000066
67 #if MICROPY_PERSISTENT_CODE
68 uint16_t ct_cur_obj;
69 uint16_t ct_num_obj;
70 uint16_t ct_cur_raw_code;
71 #endif
Damien George713ea182015-10-23 01:23:11 +010072 mp_uint_t *const_table;
Damien429d7192013-10-04 19:53:11 +010073};
74
Damien Georgea210c772015-03-26 15:49:53 +000075emit_t *emit_bc_new(void) {
Damien George08335002014-01-18 23:24:36 +000076 emit_t *emit = m_new0(emit_t, 1);
Damien Georgea210c772015-03-26 15:49:53 +000077 return emit;
78}
79
Damien George4dea9222015-04-09 15:29:54 +000080void emit_bc_set_max_num_labels(emit_t *emit, mp_uint_t max_num_labels) {
Damien6cdd3af2013-10-05 18:08:26 +010081 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010082 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010083}
Damien4b03e772013-10-05 14:17:09 +010084
Damien George41d02b62014-01-24 22:42:28 +000085void emit_bc_free(emit_t *emit) {
Damien George7ff996c2014-09-08 23:05:16 +010086 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020087 m_del_obj(emit_t, emit);
88}
89
Damien George91bc32d2015-04-09 15:31:53 +000090typedef byte *(*emit_allocator_t)(emit_t *emit, int nbytes);
91
92STATIC void emit_write_uint(emit_t *emit, emit_allocator_t allocator, mp_uint_t val) {
Damien Georgeb534e1b2014-09-04 14:44:01 +010093 // We store each 7 bits in a separate byte, and that's how many bytes needed
94 byte buf[BYTES_FOR_INT];
95 byte *p = buf + sizeof(buf);
96 // We encode in little-ending order, but store in big-endian, to help decoding
97 do {
98 *--p = val & 0x7f;
99 val >>= 7;
100 } while (val != 0);
Damien George4dea9222015-04-09 15:29:54 +0000101 byte *c = allocator(emit, buf + sizeof(buf) - p);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100102 while (p != buf + sizeof(buf) - 1) {
103 *c++ = *p++ | 0x80;
104 }
105 *c = *p;
106}
107
Damien George08335002014-01-18 23:24:36 +0000108// all functions must go through this one to emit code info
Damien George4dea9222015-04-09 15:29:54 +0000109STATIC byte *emit_get_cur_to_write_code_info(emit_t *emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100110 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100111 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +0000112 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100113 return emit->dummy_data;
114 } else {
Damien George08335002014-01-18 23:24:36 +0000115 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
116 byte *c = emit->code_base + emit->code_info_offset;
117 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100118 return c;
119 }
120}
121
Damien George9b7f5832015-03-18 17:47:47 +0000122STATIC void emit_write_code_info_byte(emit_t* emit, byte val) {
123 *emit_get_cur_to_write_code_info(emit, 1) = val;
124}
125
126STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100127 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
128}
129
Damien George4dea9222015-04-09 15:29:54 +0000130STATIC void emit_write_code_info_qstr(emit_t *emit, qstr qst) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000131 #if MICROPY_PERSISTENT_CODE
132 assert((qst >> 16) == 0);
133 byte *c = emit_get_cur_to_write_code_info(emit, 2);
134 c[0] = qst;
135 c[1] = qst >> 8;
136 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +0100137 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000138 #endif
Damien George08335002014-01-18 23:24:36 +0000139}
140
Damien George73496fb2014-04-13 14:51:56 +0100141#if MICROPY_ENABLE_SOURCE_LINE
Damien George4dea9222015-04-09 15:29:54 +0000142STATIC void emit_write_code_info_bytes_lines(emit_t *emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) {
Damien George73496fb2014-04-13 14:51:56 +0100143 assert(bytes_to_skip > 0 || lines_to_skip > 0);
Damien George4747bec2014-07-31 16:12:01 +0000144 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George73496fb2014-04-13 14:51:56 +0100145 while (bytes_to_skip > 0 || lines_to_skip > 0) {
Damien George4747bec2014-07-31 16:12:01 +0000146 mp_uint_t b, l;
Damien Georgecc2dbdd2017-02-10 11:58:10 +1100147 if (lines_to_skip <= 6 || bytes_to_skip > 0xf) {
Damien George4747bec2014-07-31 16:12:01 +0000148 // use 0b0LLBBBBB encoding
149 b = MIN(bytes_to_skip, 0x1f);
Damien Georgecc2dbdd2017-02-10 11:58:10 +1100150 if (b < bytes_to_skip) {
151 // we can't skip any lines until we skip all the bytes
152 l = 0;
153 } else {
154 l = MIN(lines_to_skip, 0x3);
155 }
Damien George4747bec2014-07-31 16:12:01 +0000156 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
157 } else {
158 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
159 b = MIN(bytes_to_skip, 0xf);
160 l = MIN(lines_to_skip, 0x7ff);
161 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
162 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
163 ci[1] = l;
164 }
Damien George73496fb2014-04-13 14:51:56 +0100165 bytes_to_skip -= b;
166 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000167 }
Damien George08335002014-01-18 23:24:36 +0000168}
Damien George73496fb2014-04-13 14:51:56 +0100169#endif
Damien George08335002014-01-18 23:24:36 +0000170
171// all functions must go through this one to emit byte code
Damien George4dea9222015-04-09 15:29:54 +0000172STATIC byte *emit_get_cur_to_write_bytecode(emit_t *emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000173 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100174 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100175 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000176 return emit->dummy_data;
177 } else {
Damien George3417bc22014-05-10 10:36:38 +0100178 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
179 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
180 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000181 return c;
182 }
183}
184
Damien George4dea9222015-04-09 15:29:54 +0000185STATIC void emit_write_bytecode_byte(emit_t *emit, byte b1) {
186 byte *c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100187 c[0] = b1;
188}
189
Damien George9b7f5832015-03-18 17:47:47 +0000190STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien George4dea9222015-04-09 15:29:54 +0000191 byte *c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100192 c[0] = b1;
193 c[1] = b2;
194}
195
Damien George3417bc22014-05-10 10:36:38 +0100196// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George4dea9222015-04-09 15:29:54 +0000197STATIC void emit_write_bytecode_byte_int(emit_t *emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100198 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200199
200 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100201 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200202 byte *p = buf + sizeof(buf);
203 // We encode in little-ending order, but store in big-endian, to help decoding
204 do {
205 *--p = num & 0x7f;
206 num >>= 7;
207 } while (num != 0 && num != -1);
208 // Make sure that highest bit we stored (mask 0x40) matches sign
209 // of the number. If not, store extra byte just to encode sign
210 if (num == -1 && (*p & 0x40) == 0) {
211 *--p = 0x7f;
212 } else if (num == 0 && (*p & 0x40) != 0) {
213 *--p = 0;
214 }
215
Damien George4dea9222015-04-09 15:29:54 +0000216 byte *c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200217 while (p != buf + sizeof(buf) - 1) {
218 *c++ = *p++ | 0x80;
219 }
220 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100221}
222
Damien George4dea9222015-04-09 15:29:54 +0000223STATIC void emit_write_bytecode_byte_uint(emit_t *emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100224 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100225 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100226}
227
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000228#if MICROPY_PERSISTENT_CODE
229STATIC void emit_write_bytecode_byte_const(emit_t *emit, byte b, mp_uint_t n, mp_uint_t c) {
230 if (emit->pass == MP_PASS_EMIT) {
231 emit->const_table[n] = c;
232 }
233 emit_write_bytecode_byte_uint(emit, b, n);
234}
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000235#endif
Damien Georgedf8127a2014-04-13 11:04:33 +0100236
Damien George9b7f5832015-03-18 17:47:47 +0000237STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000238 #if MICROPY_PERSISTENT_CODE
239 assert((qst >> 16) == 0);
240 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
241 c[0] = b;
242 c[1] = qst;
243 c[2] = qst >> 8;
244 #else
Damien George7ff996c2014-09-08 23:05:16 +0100245 emit_write_bytecode_byte_uint(emit, b, qst);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000246 #endif
247}
248
Damien George999cedb2015-11-27 17:01:44 +0000249STATIC void emit_write_bytecode_byte_obj(emit_t *emit, byte b, mp_obj_t obj) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000250 #if MICROPY_PERSISTENT_CODE
251 emit_write_bytecode_byte_const(emit, b,
252 emit->scope->num_pos_args + emit->scope->num_kwonly_args
Damien George999cedb2015-11-27 17:01:44 +0000253 + emit->ct_cur_obj++, (mp_uint_t)obj);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000254 #else
Damien George999cedb2015-11-27 17:01:44 +0000255 // aligns the pointer so it is friendly to GC
256 emit_write_bytecode_byte(emit, b);
257 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(mp_obj_t));
258 mp_obj_t *c = (mp_obj_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_obj_t));
259 // Verify thar c is already uint-aligned
260 assert(c == MP_ALIGN(c, sizeof(mp_obj_t)));
261 *c = obj;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000262 #endif
263}
264
265STATIC void emit_write_bytecode_byte_raw_code(emit_t *emit, byte b, mp_raw_code_t *rc) {
266 #if MICROPY_PERSISTENT_CODE
267 emit_write_bytecode_byte_const(emit, b,
268 emit->scope->num_pos_args + emit->scope->num_kwonly_args
Damien George999cedb2015-11-27 17:01:44 +0000269 + emit->ct_num_obj + emit->ct_cur_raw_code++, (mp_uint_t)(uintptr_t)rc);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000270 #else
Damien George999cedb2015-11-27 17:01:44 +0000271 // aligns the pointer so it is friendly to GC
272 emit_write_bytecode_byte(emit, b);
273 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(void*));
274 void **c = (void**)emit_get_cur_to_write_bytecode(emit, sizeof(void*));
275 // Verify thar c is already uint-aligned
276 assert(c == MP_ALIGN(c, sizeof(void*)));
277 *c = rc;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000278 #endif
Damien429d7192013-10-04 19:53:11 +0100279}
280
Damien03c9cfb2013-11-05 22:06:08 +0000281// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George4dea9222015-04-09 15:29:54 +0000282STATIC void emit_write_bytecode_byte_unsigned_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George7ff996c2014-09-08 23:05:16 +0100283 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100284 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100285 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100286 } else {
Damien George3417bc22014-05-10 10:36:38 +0100287 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100288 }
Damien George3417bc22014-05-10 10:36:38 +0100289 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000290 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100291 c[1] = bytecode_offset;
292 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000293}
294
295// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George4dea9222015-04-09 15:29:54 +0000296STATIC void emit_write_bytecode_byte_signed_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100297 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100298 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100299 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000300 } else {
Damien George3417bc22014-05-10 10:36:38 +0100301 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000302 }
Damien George4dea9222015-04-09 15:29:54 +0000303 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000304 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100305 c[1] = bytecode_offset;
306 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100307}
308
Damien George41125902015-03-26 16:44:14 +0000309void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000310 emit->pass = pass;
311 emit->stack_size = 0;
312 emit->last_emit_was_return_value = false;
313 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000314 emit->last_source_line_offset = 0;
315 emit->last_source_line = 1;
Damien Georgef935bce2017-12-08 18:23:23 +1100316 #ifndef NDEBUG
317 // With debugging enabled labels are checked for unique assignment
Jeff Eplerbc6c0b22018-05-19 10:52:43 -0500318 if (pass < MP_PASS_EMIT && emit->label_offsets != NULL) {
Damien George7ff996c2014-09-08 23:05:16 +0100319 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000320 }
Damien Georgef935bce2017-12-08 18:23:23 +1100321 #endif
Damien George3417bc22014-05-10 10:36:38 +0100322 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000323 emit->code_info_offset = 0;
324
Damien George9b7f5832015-03-18 17:47:47 +0000325 // Write local state size and exception stack size.
326 {
327 mp_uint_t n_state = scope->num_locals + scope->stack_size;
328 if (n_state == 0) {
329 // Need at least 1 entry in the state, in the case an exception is
330 // propagated through this function, the exception is returned in
331 // the highest slot in the state (fastn[0], see vm.c).
332 n_state = 1;
333 }
Damien George6d199342019-01-04 17:09:41 +1100334 #if MICROPY_DEBUG_VM_STACK_OVERFLOW
335 // An extra slot in the stack is needed to detect VM stack overflow
336 n_state += 1;
337 #endif
Damien George9b7f5832015-03-18 17:47:47 +0000338 emit_write_code_info_uint(emit, n_state);
339 emit_write_code_info_uint(emit, scope->exc_stack_size);
Damien George08335002014-01-18 23:24:36 +0000340 }
341
Damien George3a3db4d2015-10-22 23:45:37 +0100342 // Write scope flags and number of arguments.
343 // TODO check that num args all fit in a byte
344 emit_write_code_info_byte(emit, emit->scope->scope_flags);
345 emit_write_code_info_byte(emit, emit->scope->num_pos_args);
346 emit_write_code_info_byte(emit, emit->scope->num_kwonly_args);
347 emit_write_code_info_byte(emit, emit->scope->num_def_pos_args);
348
Damien George9b7f5832015-03-18 17:47:47 +0000349 // Write size of the rest of the code info. We don't know how big this
350 // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
351 // for it and hope that is enough! TODO assert this or something.
352 if (pass == MP_PASS_EMIT) {
353 emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
354 } else {
355 emit_get_cur_to_write_code_info(emit, 2);
Damien George8dcc0c72014-03-27 10:55:21 +0000356 }
357
Damien George9b7f5832015-03-18 17:47:47 +0000358 // Write the name and source file of this function.
359 emit_write_code_info_qstr(emit, scope->simple_name);
360 emit_write_code_info_qstr(emit, scope->source_file);
361
Damien George8dcc0c72014-03-27 10:55:21 +0000362 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000363 for (int i = 0; i < scope->id_info_len; i++) {
364 id_info_t *id = &scope->id_info[i];
365 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgec9aa1882015-04-07 00:08:17 +0100366 assert(id->local_num < 255);
Damien George3417bc22014-05-10 10:36:38 +0100367 emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
Damien George6baf76e2013-12-30 22:32:17 +0000368 }
369 }
Damien Georgec9aa1882015-04-07 00:08:17 +0100370 emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George713ea182015-10-23 01:23:11 +0100371
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000372 #if MICROPY_PERSISTENT_CODE
373 emit->ct_cur_obj = 0;
374 emit->ct_cur_raw_code = 0;
375 #endif
376
Damien George713ea182015-10-23 01:23:11 +0100377 if (pass == MP_PASS_EMIT) {
378 // Write argument names (needed to resolve positional args passed as
379 // keywords). We store them as full word-sized objects for efficient access
380 // in mp_setup_code_state this is the start of the prelude and is guaranteed
381 // to be aligned on a word boundary.
382
383 // For a given argument position (indexed by i) we need to find the
384 // corresponding id_info which is a parameter, as it has the correct
385 // qstr name to use as the argument name. Note that it's not a simple
386 // 1-1 mapping (ie i!=j in general) because of possible closed-over
387 // variables. In the case that the argument i has no corresponding
388 // parameter we use "*" as its name (since no argument can ever be named
389 // "*"). We could use a blank qstr but "*" is better for debugging.
390 // Note: there is some wasted RAM here for the case of storing a qstr
391 // for each closed-over variable, and maybe there is a better way to do
392 // it, but that would require changes to mp_setup_code_state.
393 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
394 qstr qst = MP_QSTR__star_;
395 for (int j = 0; j < scope->id_info_len; ++j) {
396 id_info_t *id = &scope->id_info[j];
397 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
398 qst = id->qst;
399 break;
400 }
401 }
402 emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
403 }
404 }
Damien George6baf76e2013-12-30 22:32:17 +0000405}
406
Damien George41125902015-03-26 16:44:14 +0000407void mp_emit_bc_end_pass(emit_t *emit) {
Damien Georgea210c772015-03-26 15:49:53 +0000408 if (emit->pass == MP_PASS_SCOPE) {
409 return;
410 }
411
Damien George6baf76e2013-12-30 22:32:17 +0000412 // check stack is back to zero size
Damien George7385b012016-09-27 15:46:50 +1000413 assert(emit->stack_size == 0);
Damien George6baf76e2013-12-30 22:32:17 +0000414
Damien George9b7f5832015-03-18 17:47:47 +0000415 emit_write_code_info_byte(emit, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000416
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000417 #if MICROPY_PERSISTENT_CODE
418 assert(emit->pass <= MP_PASS_STACK_SIZE || (emit->ct_num_obj == emit->ct_cur_obj));
419 emit->ct_num_obj = emit->ct_cur_obj;
420 #endif
421
Damien George36db6bc2014-05-07 17:24:22 +0100422 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000423 #if !MICROPY_PERSISTENT_CODE
Damien George9b7f5832015-03-18 17:47:47 +0000424 // so bytecode is aligned
Damien George999cedb2015-11-27 17:01:44 +0000425 emit->code_info_offset = (size_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000426 #endif
Damien Georgeb534e1b2014-09-04 14:44:01 +0100427
428 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000429 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100430 emit->bytecode_size = emit->bytecode_offset;
431 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000432
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000433 #if MICROPY_PERSISTENT_CODE
434 emit->const_table = m_new0(mp_uint_t,
435 emit->scope->num_pos_args + emit->scope->num_kwonly_args
436 + emit->ct_cur_obj + emit->ct_cur_raw_code);
437 #else
438 emit->const_table = m_new0(mp_uint_t,
439 emit->scope->num_pos_args + emit->scope->num_kwonly_args);
440 #endif
Damien George713ea182015-10-23 01:23:11 +0100441
Damien George36db6bc2014-05-07 17:24:22 +0100442 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100443 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
Damien George5604b712018-02-14 18:41:17 +1100444 #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
Damien George713ea182015-10-23 01:23:11 +0100445 emit->code_info_size + emit->bytecode_size,
Damien George5604b712018-02-14 18:41:17 +1100446 #endif
Damien Georged8c834c2015-11-02 21:55:42 +0000447 emit->const_table,
448 #if MICROPY_PERSISTENT_CODE_SAVE
449 emit->ct_cur_obj, emit->ct_cur_raw_code,
450 #endif
451 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000452 }
453}
454
Damien George41125902015-03-26 16:44:14 +0000455bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100456 return emit->last_emit_was_return_value;
457}
458
Damien George41125902015-03-26 16:44:14 +0000459void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George8f064e42017-05-25 20:42:30 +1000460 if (emit->pass == MP_PASS_SCOPE) {
461 return;
462 }
463 assert((mp_int_t)emit->stack_size + delta >= 0);
Damien Georged66ae182014-04-10 17:28:54 +0000464 emit->stack_size += delta;
Damien George8f064e42017-05-25 20:42:30 +1000465 if (emit->stack_size > emit->scope->stack_size) {
466 emit->scope->stack_size = emit->stack_size;
467 }
468 emit->last_emit_was_return_value = false;
469}
470
471static inline void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
472 mp_emit_bc_adjust_stack_size(emit, stack_size_delta);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Damien George41125902015-03-26 16:44:14 +0000475void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100476 //printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
Damien George62ad1892014-01-29 21:51:51 +0000477#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000478 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300479 // If we compile with -O3, don't store line numbers.
480 return;
481 }
Damien George08335002014-01-18 23:24:36 +0000482 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100483 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
484 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000485 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100486 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000487 emit->last_source_line = source_line;
488 }
Damien George3a2171e2015-09-04 16:53:46 +0100489#else
490 (void)emit;
491 (void)source_line;
Damien George62ad1892014-01-29 21:51:51 +0000492#endif
Damien George08335002014-01-18 23:24:36 +0000493}
494
Damien George41125902015-03-26 16:44:14 +0000495void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000496 emit_bc_pre(emit, 0);
Damien Georgea210c772015-03-26 15:49:53 +0000497 if (emit->pass == MP_PASS_SCOPE) {
498 return;
499 }
Damienb05d7072013-10-05 13:37:10 +0100500 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100501 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100502 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000503 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100504 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100505 } else {
506 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100507 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100508 }
509}
510
Damien Georged97906c2018-05-22 21:58:25 +1000511void mp_emit_bc_import(emit_t *emit, qstr qst, int kind) {
512 MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_NAME == MP_BC_IMPORT_NAME);
513 MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_FROM == MP_BC_IMPORT_FROM);
514 if (kind == MP_EMIT_IMPORT_FROM) {
515 emit_bc_pre(emit, 1);
516 } else {
517 emit_bc_pre(emit, -1);
518 }
519 if (kind == MP_EMIT_IMPORT_STAR) {
520 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
521 } else {
522 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME + kind, qst);
523 }
Damien429d7192013-10-04 19:53:11 +0100524}
525
Damien George41125902015-03-26 16:44:14 +0000526void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000527 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100528 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100529 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
530 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
531 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien George7385b012016-09-27 15:46:50 +1000532 default:
533 assert(tok == MP_TOKEN_ELLIPSIS);
534 emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
535 break;
Damien429d7192013-10-04 19:53:11 +0100536 }
537}
538
Damien George41125902015-03-26 16:44:14 +0000539void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000540 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100541 if (-16 <= arg && arg <= 47) {
542 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
543 } else {
544 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
545 }
Damien429d7192013-10-04 19:53:11 +0100546}
547
Damien George59fba2d2015-06-25 14:42:13 +0000548void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000549 emit_bc_pre(emit, 1);
Damien George59fba2d2015-06-25 14:42:13 +0000550 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100551}
552
Damien George5d66b422015-11-27 12:41:25 +0000553void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
Damien Georgedab13852015-01-13 15:55:54 +0000554 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000555 emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, obj);
Damien Georgedab13852015-01-13 15:55:54 +0000556}
557
Damien George41125902015-03-26 16:44:14 +0000558void mp_emit_bc_load_null(emit_t *emit) {
Damien George523b5752014-03-31 11:59:23 +0100559 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100560 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George280fb4d2017-09-13 20:36:06 +1000561}
Damien George523b5752014-03-31 11:59:23 +0100562
Damien George0a25fff2018-05-19 00:11:04 +1000563void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
564 MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_LOAD_FAST_N);
565 MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_LOAD_DEREF);
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000566 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000567 emit_bc_pre(emit, 1);
Damien George0a25fff2018-05-19 00:11:04 +1000568 if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
Damien George8456cc02014-10-25 16:43:46 +0100569 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
570 } else {
Damien George0a25fff2018-05-19 00:11:04 +1000571 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N + kind, local_num);
Damien429d7192013-10-04 19:53:11 +0100572 }
573}
574
Damien Georged2980132018-05-22 21:16:30 +1000575void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind) {
576 MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_LOAD_NAME);
577 MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_LOAD_GLOBAL);
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000578 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000579 emit_bc_pre(emit, 1);
Damien Georged2980132018-05-22 21:16:30 +1000580 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME + kind, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000581 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000582 emit_write_bytecode_byte(emit, 0);
583 }
Damien429d7192013-10-04 19:53:11 +0100584}
585
Damien Georgedd11af22017-04-19 09:45:59 +1000586void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super) {
587 emit_bc_pre(emit, 1 - 2 * is_super);
588 emit_write_bytecode_byte_qstr(emit, is_super ? MP_BC_LOAD_SUPER_METHOD : MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100589}
590
Damien George41125902015-03-26 16:44:14 +0000591void mp_emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000592 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100593 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100594}
595
Damien Georgea4941a82018-05-22 21:31:56 +1000596void mp_emit_bc_subscr(emit_t *emit, int kind) {
597 if (kind == MP_EMIT_SUBSCR_LOAD) {
598 emit_bc_pre(emit, -1);
599 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
600 } else {
601 if (kind == MP_EMIT_SUBSCR_DELETE) {
602 mp_emit_bc_load_null(emit);
603 mp_emit_bc_rot_three(emit);
604 }
605 emit_bc_pre(emit, -3);
606 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
607 }
Damien George729f7b42014-04-17 22:10:53 +0100608}
609
Damien George6211d972018-05-22 21:43:41 +1000610void mp_emit_bc_attr(emit_t *emit, qstr qst, int kind) {
611 if (kind == MP_EMIT_ATTR_LOAD) {
612 emit_bc_pre(emit, 0);
613 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
614 } else {
615 if (kind == MP_EMIT_ATTR_DELETE) {
616 mp_emit_bc_load_null(emit);
617 mp_emit_bc_rot_two(emit);
618 }
619 emit_bc_pre(emit, -2);
620 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
621 }
622 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
623 emit_write_bytecode_byte(emit, 0);
624 }
625}
626
Damien George0a25fff2018-05-19 00:11:04 +1000627void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
628 MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_STORE_FAST_N);
629 MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_STORE_DEREF);
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000630 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000631 emit_bc_pre(emit, -1);
Damien George0a25fff2018-05-19 00:11:04 +1000632 if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
Damien George8456cc02014-10-25 16:43:46 +0100633 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
634 } else {
Damien George0a25fff2018-05-19 00:11:04 +1000635 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N + kind, local_num);
Damien429d7192013-10-04 19:53:11 +0100636 }
637}
638
Damien Georged2980132018-05-22 21:16:30 +1000639void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind) {
640 MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_STORE_NAME);
641 MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_STORE_GLOBAL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000642 emit_bc_pre(emit, -1);
Damien Georged2980132018-05-22 21:16:30 +1000643 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME + kind, qst);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Damien George0a25fff2018-05-19 00:11:04 +1000646void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
647 MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
648 MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000649 (void)qst;
Damien George0a25fff2018-05-19 00:11:04 +1000650 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST + kind, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000651}
652
Damien Georged2980132018-05-22 21:16:30 +1000653void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind) {
654 MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_DELETE_NAME);
655 MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_DELETE_GLOBAL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000656 emit_bc_pre(emit, 0);
Damien Georged2980132018-05-22 21:16:30 +1000657 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME + kind, qst);
Damien429d7192013-10-04 19:53:11 +0100658}
659
Damien George41125902015-03-26 16:44:14 +0000660void mp_emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000661 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100662 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100663}
664
Damien George41125902015-03-26 16:44:14 +0000665void mp_emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000666 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100667 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100668}
669
Damien George41125902015-03-26 16:44:14 +0000670void mp_emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000671 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100672 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100673}
674
Damien George41125902015-03-26 16:44:14 +0000675void mp_emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000676 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100677 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100678}
679
Damien George41125902015-03-26 16:44:14 +0000680void mp_emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000681 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100682 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien George41125902015-03-26 16:44:14 +0000685void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000686 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100687 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Damien George41125902015-03-26 16:44:14 +0000690void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000691 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000692 if (cond) {
693 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
694 } else {
695 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
696 }
Damien429d7192013-10-04 19:53:11 +0100697}
698
Damien George41125902015-03-26 16:44:14 +0000699void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000700 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000701 if (cond) {
702 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
703 } else {
704 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
705 }
Damien429d7192013-10-04 19:53:11 +0100706}
707
Damien George41125902015-03-26 16:44:14 +0000708void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000709 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000710 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100711 if (label & MP_EMIT_BREAK_FROM_FOR) {
712 // need to pop the iterator if we are breaking out of a for loop
713 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien Georgef4df3aa2016-01-09 23:59:52 +0000714 // also pop the iter_buf
Damien George60656ea2017-03-23 16:36:08 +1100715 for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
Damien Georgef4df3aa2016-01-09 23:59:52 +0000716 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
717 }
Damien George25c84642014-05-30 15:20:41 +0100718 }
719 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
720 } else {
721 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
722 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000723 }
Damien429d7192013-10-04 19:53:11 +0100724}
725
Damien George18e63582018-05-22 22:33:26 +1000726void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind) {
727 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_WITH == MP_BC_SETUP_WITH);
728 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_EXCEPT == MP_BC_SETUP_EXCEPT);
729 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_FINALLY == MP_BC_SETUP_FINALLY);
730 if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
Damien Georgef0406852016-09-27 12:37:21 +1000731 // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
732 // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
Damien George18e63582018-05-22 22:33:26 +1000733 emit_bc_pre(emit, 2);
734 } else {
735 emit_bc_pre(emit, 0);
736 }
737 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH + kind, label);
Damien429d7192013-10-04 19:53:11 +0100738}
739
Damien Georgece8b4e82016-04-07 08:50:38 +0100740void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
Damien Georgece8b4e82016-04-07 08:50:38 +0100741 mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
742 mp_emit_bc_label_assign(emit, label);
Damien Georgef0406852016-09-27 12:37:21 +1000743 emit_bc_pre(emit, 2); // ensure we have enough stack space to call the __exit__ method
Damien George3417bc22014-05-10 10:36:38 +0100744 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien George18e63582018-05-22 22:33:26 +1000745 emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_block(MP_EMIT_SETUP_BLOCK_WITH)
Damien429d7192013-10-04 19:53:11 +0100746}
747
Damien George41125902015-03-26 16:44:14 +0000748void mp_emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000749 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100750 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100751}
752
Damien Georgef4df3aa2016-01-09 23:59:52 +0000753void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
Damien George60656ea2017-03-23 16:36:08 +1100754 emit_bc_pre(emit, use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0);
Damien Georgef4df3aa2016-01-09 23:59:52 +0000755 emit_write_bytecode_byte(emit, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100756}
757
Damien George41125902015-03-26 16:44:14 +0000758void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000759 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100760 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100761}
762
Damien George30b42dd2017-01-17 15:30:18 +1100763void mp_emit_bc_for_iter_end(emit_t *emit) {
Damien George60656ea2017-03-23 16:36:08 +1100764 emit_bc_pre(emit, -MP_OBJ_ITER_BUF_NSLOTS);
Damien429d7192013-10-04 19:53:11 +0100765}
766
Damien George5a2599d2019-02-15 12:18:59 +1100767void mp_emit_bc_pop_except_jump(emit_t *emit, mp_uint_t label, bool within_exc_handler) {
768 (void)within_exc_handler;
Damien Georgece8f07a2014-03-27 23:30:26 +0000769 emit_bc_pre(emit, 0);
Damien George5a2599d2019-02-15 12:18:59 +1100770 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_POP_EXCEPT_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100771}
772
Damien George41125902015-03-26 16:44:14 +0000773void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georgebdbe8c92015-12-08 12:28:11 +0000774 emit_bc_pre(emit, 0);
775 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien429d7192013-10-04 19:53:11 +0100776}
777
Damien George41125902015-03-26 16:44:14 +0000778void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000779 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100780 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000781 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100782 op = MP_BINARY_OP_IN;
783 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000784 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100785 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000786 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000787 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100788 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000789 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000790 emit_bc_pre(emit, 0);
Damien Georgebdbe8c92015-12-08 12:28:11 +0000791 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000792 }
Damien429d7192013-10-04 19:53:11 +0100793}
794
Damien George26b57542018-05-19 00:41:40 +1000795void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
796 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
797 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
798 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
Damien George436e0d42018-05-22 22:18:42 +1000799 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_BC_BUILD_SET);
800 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SLICE == MP_BC_BUILD_SLICE);
Damien George26b57542018-05-19 00:41:40 +1000801 if (kind == MP_EMIT_BUILD_MAP) {
802 emit_bc_pre(emit, 1);
803 } else {
804 emit_bc_pre(emit, 1 - n_args);
805 }
806 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE + kind, n_args);
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damien George41125902015-03-26 16:44:14 +0000809void mp_emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000810 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100811 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100812}
813
Damien Georgea5624bf2016-09-18 23:59:47 +1000814void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
Damien Georgeadaf0d82016-09-19 08:46:01 +1000815 int t;
Damien Georgea5624bf2016-09-18 23:59:47 +1000816 int n;
Damien Georgea5624bf2016-09-18 23:59:47 +1000817 if (kind == SCOPE_LIST_COMP) {
Damien Georgeadaf0d82016-09-19 08:46:01 +1000818 n = 0;
819 t = 0;
820 } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
821 n = 1;
822 t = 1;
823 } else if (MICROPY_PY_BUILTINS_SET) {
824 n = 0;
825 t = 2;
Damien Georgea5624bf2016-09-18 23:59:47 +1000826 }
Damien Georgeadaf0d82016-09-19 08:46:01 +1000827 emit_bc_pre(emit, -1 - n);
828 // the lower 2 bits of the opcode argument indicate the collection type
829 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
Damien Georgea5624bf2016-09-18 23:59:47 +1000830}
831
Damien George41125902015-03-26 16:44:14 +0000832void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000833 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100834 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100835}
836
Damien George41125902015-03-26 16:44:14 +0000837void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000838 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100839 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100840}
841
Damien George41125902015-03-26 16:44:14 +0000842void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100843 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000844 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000845 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000846 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100847 emit_bc_pre(emit, -1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000848 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200849 }
Damien429d7192013-10-04 19:53:11 +0100850}
851
Damien George41125902015-03-26 16:44:14 +0000852void mp_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 Georgee337f1e2014-03-31 15:18:37 +0100853 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100854 emit_bc_pre(emit, -n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000855 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100856 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200857 } else {
Damien George3558f622014-04-20 17:50:40 +0100858 assert(n_closed_over <= 255);
Tom Collins145796f2017-06-30 16:23:29 -0700859 emit_bc_pre(emit, -2 - (mp_int_t)n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000860 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100861 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200862 }
Damien429d7192013-10-04 19:53:11 +0100863}
864
Damien George7ff996c2014-09-08 23:05:16 +0100865STATIC 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 George922ddd62014-04-09 12:43:17 +0100866 if (star_flags) {
Damien George7ff996c2014-09-08 23:05:16 +0100867 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
Damien George3417bc22014-05-10 10:36:38 +0100868 emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100869 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100870 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100871 emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100872 }
Damien George523b5752014-03-31 11:59:23 +0100873}
874
Damien George41125902015-03-26 16:44:14 +0000875void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100876 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100877}
878
Damien George41125902015-03-26 16:44:14 +0000879void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100880 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100881}
882
Damien George41125902015-03-26 16:44:14 +0000883void mp_emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000884 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100885 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100886 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100887}
888
Damien George41125902015-03-26 16:44:14 +0000889void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Pavol Rusnak7ffc9592016-10-25 11:05:33 +0200890 assert(n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000891 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100892 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100893}
894
Damien Georgee686c942018-05-19 00:30:42 +1000895void mp_emit_bc_yield(emit_t *emit, int kind) {
896 MP_STATIC_ASSERT(MP_BC_YIELD_VALUE + 1 == MP_BC_YIELD_FROM);
897 emit_bc_pre(emit, -kind);
Damien George36db6bc2014-05-07 17:24:22 +0100898 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien Georgee686c942018-05-19 00:30:42 +1000899 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE + kind);
Damien429d7192013-10-04 19:53:11 +0100900}
901
Damien George41125902015-03-26 16:44:14 +0000902void mp_emit_bc_start_except_handler(emit_t *emit) {
Damien Georgef0406852016-09-27 12:37:21 +1000903 mp_emit_bc_adjust_stack_size(emit, 4); // stack adjust for the exception instance, +3 for possible UNWIND_JUMP state
Damien Georgeb601d952014-06-30 05:17:25 +0100904}
905
Damien George41125902015-03-26 16:44:14 +0000906void mp_emit_bc_end_except_handler(emit_t *emit) {
Damien Georgef0406852016-09-27 12:37:21 +1000907 mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
Damien Georgeb601d952014-06-30 05:17:25 +0100908}
909
Damien George41125902015-03-26 16:44:14 +0000910#if MICROPY_EMIT_NATIVE
Damien6cdd3af2013-10-05 18:08:26 +0100911const emit_method_table_t emit_bc_method_table = {
Damien George41125902015-03-26 16:44:14 +0000912 mp_emit_bc_start_pass,
913 mp_emit_bc_end_pass,
914 mp_emit_bc_last_emit_was_return_value,
915 mp_emit_bc_adjust_stack_size,
916 mp_emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100917
Damien George542bd6b2015-03-26 14:42:40 +0000918 {
Damien George0a25fff2018-05-19 00:11:04 +1000919 mp_emit_bc_load_local,
Damien George41125902015-03-26 16:44:14 +0000920 mp_emit_bc_load_global,
Damien George542bd6b2015-03-26 14:42:40 +0000921 },
922 {
Damien George0a25fff2018-05-19 00:11:04 +1000923 mp_emit_bc_store_local,
Damien George41125902015-03-26 16:44:14 +0000924 mp_emit_bc_store_global,
Damien George542bd6b2015-03-26 14:42:40 +0000925 },
926 {
Damien George0a25fff2018-05-19 00:11:04 +1000927 mp_emit_bc_delete_local,
Damien George41125902015-03-26 16:44:14 +0000928 mp_emit_bc_delete_global,
Damien George542bd6b2015-03-26 14:42:40 +0000929 },
Damien4b03e772013-10-05 14:17:09 +0100930
Damien George41125902015-03-26 16:44:14 +0000931 mp_emit_bc_label_assign,
Damien Georged97906c2018-05-22 21:58:25 +1000932 mp_emit_bc_import,
Damien George41125902015-03-26 16:44:14 +0000933 mp_emit_bc_load_const_tok,
934 mp_emit_bc_load_const_small_int,
935 mp_emit_bc_load_const_str,
936 mp_emit_bc_load_const_obj,
937 mp_emit_bc_load_null,
Damien George41125902015-03-26 16:44:14 +0000938 mp_emit_bc_load_method,
939 mp_emit_bc_load_build_class,
Damien Georgea4941a82018-05-22 21:31:56 +1000940 mp_emit_bc_subscr,
Damien George6211d972018-05-22 21:43:41 +1000941 mp_emit_bc_attr,
Damien George41125902015-03-26 16:44:14 +0000942 mp_emit_bc_dup_top,
943 mp_emit_bc_dup_top_two,
944 mp_emit_bc_pop_top,
945 mp_emit_bc_rot_two,
946 mp_emit_bc_rot_three,
947 mp_emit_bc_jump,
948 mp_emit_bc_pop_jump_if,
949 mp_emit_bc_jump_if_or_pop,
950 mp_emit_bc_unwind_jump,
Damien George18e63582018-05-22 22:33:26 +1000951 mp_emit_bc_setup_block,
Damien George41125902015-03-26 16:44:14 +0000952 mp_emit_bc_with_cleanup,
Damien George41125902015-03-26 16:44:14 +0000953 mp_emit_bc_end_finally,
954 mp_emit_bc_get_iter,
955 mp_emit_bc_for_iter,
956 mp_emit_bc_for_iter_end,
Damien George5a2599d2019-02-15 12:18:59 +1100957 mp_emit_bc_pop_except_jump,
Damien George41125902015-03-26 16:44:14 +0000958 mp_emit_bc_unary_op,
959 mp_emit_bc_binary_op,
Damien George26b57542018-05-19 00:41:40 +1000960 mp_emit_bc_build,
Damien George41125902015-03-26 16:44:14 +0000961 mp_emit_bc_store_map,
Damien Georgea5624bf2016-09-18 23:59:47 +1000962 mp_emit_bc_store_comp,
Damien George41125902015-03-26 16:44:14 +0000963 mp_emit_bc_unpack_sequence,
964 mp_emit_bc_unpack_ex,
965 mp_emit_bc_make_function,
966 mp_emit_bc_make_closure,
967 mp_emit_bc_call_function,
968 mp_emit_bc_call_method,
969 mp_emit_bc_return_value,
970 mp_emit_bc_raise_varargs,
Damien Georgee686c942018-05-19 00:30:42 +1000971 mp_emit_bc_yield,
Damien Georgeb601d952014-06-30 05:17:25 +0100972
Damien George41125902015-03-26 16:44:14 +0000973 mp_emit_bc_start_except_handler,
974 mp_emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100975};
Damien George41125902015-03-26 16:44:14 +0000976#else
977const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
Damien George0a25fff2018-05-19 00:11:04 +1000978 mp_emit_bc_load_local,
Damien George41125902015-03-26 16:44:14 +0000979 mp_emit_bc_load_global,
980};
981
982const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
Damien George0a25fff2018-05-19 00:11:04 +1000983 mp_emit_bc_store_local,
Damien George41125902015-03-26 16:44:14 +0000984 mp_emit_bc_store_global,
985};
986
987const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
Damien George0a25fff2018-05-19 00:11:04 +1000988 mp_emit_bc_delete_local,
Damien George41125902015-03-26 16:44:14 +0000989 mp_emit_bc_delete_global,
990};
991#endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000992
993#endif //MICROPY_ENABLE_COMPILER