blob: 673cd405f99bdd475b968760e8c68547ade95e69 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
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 George36db6bc2014-05-07 17:24:22 +0100316 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100317 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000318 }
Damien George3417bc22014-05-10 10:36:38 +0100319 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000320 emit->code_info_offset = 0;
321
Damien George9b7f5832015-03-18 17:47:47 +0000322 // Write local state size and exception stack size.
323 {
324 mp_uint_t n_state = scope->num_locals + scope->stack_size;
325 if (n_state == 0) {
326 // Need at least 1 entry in the state, in the case an exception is
327 // propagated through this function, the exception is returned in
328 // the highest slot in the state (fastn[0], see vm.c).
329 n_state = 1;
330 }
331 emit_write_code_info_uint(emit, n_state);
332 emit_write_code_info_uint(emit, scope->exc_stack_size);
Damien George08335002014-01-18 23:24:36 +0000333 }
334
Damien George3a3db4d2015-10-22 23:45:37 +0100335 // Write scope flags and number of arguments.
336 // TODO check that num args all fit in a byte
337 emit_write_code_info_byte(emit, emit->scope->scope_flags);
338 emit_write_code_info_byte(emit, emit->scope->num_pos_args);
339 emit_write_code_info_byte(emit, emit->scope->num_kwonly_args);
340 emit_write_code_info_byte(emit, emit->scope->num_def_pos_args);
341
Damien George9b7f5832015-03-18 17:47:47 +0000342 // Write size of the rest of the code info. We don't know how big this
343 // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
344 // for it and hope that is enough! TODO assert this or something.
345 if (pass == MP_PASS_EMIT) {
346 emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
347 } else {
348 emit_get_cur_to_write_code_info(emit, 2);
Damien George8dcc0c72014-03-27 10:55:21 +0000349 }
350
Damien George9b7f5832015-03-18 17:47:47 +0000351 // Write the name and source file of this function.
352 emit_write_code_info_qstr(emit, scope->simple_name);
353 emit_write_code_info_qstr(emit, scope->source_file);
354
Damien George8dcc0c72014-03-27 10:55:21 +0000355 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000356 for (int i = 0; i < scope->id_info_len; i++) {
357 id_info_t *id = &scope->id_info[i];
358 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgec9aa1882015-04-07 00:08:17 +0100359 assert(id->local_num < 255);
Damien George3417bc22014-05-10 10:36:38 +0100360 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 +0000361 }
362 }
Damien Georgec9aa1882015-04-07 00:08:17 +0100363 emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George713ea182015-10-23 01:23:11 +0100364
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000365 #if MICROPY_PERSISTENT_CODE
366 emit->ct_cur_obj = 0;
367 emit->ct_cur_raw_code = 0;
368 #endif
369
Damien George713ea182015-10-23 01:23:11 +0100370 if (pass == MP_PASS_EMIT) {
371 // Write argument names (needed to resolve positional args passed as
372 // keywords). We store them as full word-sized objects for efficient access
373 // in mp_setup_code_state this is the start of the prelude and is guaranteed
374 // to be aligned on a word boundary.
375
376 // For a given argument position (indexed by i) we need to find the
377 // corresponding id_info which is a parameter, as it has the correct
378 // qstr name to use as the argument name. Note that it's not a simple
379 // 1-1 mapping (ie i!=j in general) because of possible closed-over
380 // variables. In the case that the argument i has no corresponding
381 // parameter we use "*" as its name (since no argument can ever be named
382 // "*"). We could use a blank qstr but "*" is better for debugging.
383 // Note: there is some wasted RAM here for the case of storing a qstr
384 // for each closed-over variable, and maybe there is a better way to do
385 // it, but that would require changes to mp_setup_code_state.
386 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
387 qstr qst = MP_QSTR__star_;
388 for (int j = 0; j < scope->id_info_len; ++j) {
389 id_info_t *id = &scope->id_info[j];
390 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
391 qst = id->qst;
392 break;
393 }
394 }
395 emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
396 }
397 }
Damien George6baf76e2013-12-30 22:32:17 +0000398}
399
Damien George41125902015-03-26 16:44:14 +0000400void mp_emit_bc_end_pass(emit_t *emit) {
Damien Georgea210c772015-03-26 15:49:53 +0000401 if (emit->pass == MP_PASS_SCOPE) {
402 return;
403 }
404
Damien George6baf76e2013-12-30 22:32:17 +0000405 // check stack is back to zero size
Damien George7385b012016-09-27 15:46:50 +1000406 assert(emit->stack_size == 0);
Damien George6baf76e2013-12-30 22:32:17 +0000407
Damien George9b7f5832015-03-18 17:47:47 +0000408 emit_write_code_info_byte(emit, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000409
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000410 #if MICROPY_PERSISTENT_CODE
411 assert(emit->pass <= MP_PASS_STACK_SIZE || (emit->ct_num_obj == emit->ct_cur_obj));
412 emit->ct_num_obj = emit->ct_cur_obj;
413 #endif
414
Damien George36db6bc2014-05-07 17:24:22 +0100415 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000416 #if !MICROPY_PERSISTENT_CODE
Damien George9b7f5832015-03-18 17:47:47 +0000417 // so bytecode is aligned
Damien George999cedb2015-11-27 17:01:44 +0000418 emit->code_info_offset = (size_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000419 #endif
Damien Georgeb534e1b2014-09-04 14:44:01 +0100420
421 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000422 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100423 emit->bytecode_size = emit->bytecode_offset;
424 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000425
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000426 #if MICROPY_PERSISTENT_CODE
427 emit->const_table = m_new0(mp_uint_t,
428 emit->scope->num_pos_args + emit->scope->num_kwonly_args
429 + emit->ct_cur_obj + emit->ct_cur_raw_code);
430 #else
431 emit->const_table = m_new0(mp_uint_t,
432 emit->scope->num_pos_args + emit->scope->num_kwonly_args);
433 #endif
Damien George713ea182015-10-23 01:23:11 +0100434
Damien George36db6bc2014-05-07 17:24:22 +0100435 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100436 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
Damien George713ea182015-10-23 01:23:11 +0100437 emit->code_info_size + emit->bytecode_size,
Damien Georged8c834c2015-11-02 21:55:42 +0000438 emit->const_table,
439 #if MICROPY_PERSISTENT_CODE_SAVE
440 emit->ct_cur_obj, emit->ct_cur_raw_code,
441 #endif
442 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000443 }
444}
445
Damien George41125902015-03-26 16:44:14 +0000446bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100447 return emit->last_emit_was_return_value;
448}
449
Damien George41125902015-03-26 16:44:14 +0000450void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000451 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100452}
453
Damien George41125902015-03-26 16:44:14 +0000454void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100455 //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 +0000456#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000457 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300458 // If we compile with -O3, don't store line numbers.
459 return;
460 }
Damien George08335002014-01-18 23:24:36 +0000461 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100462 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
463 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000464 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100465 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000466 emit->last_source_line = source_line;
467 }
Damien George3a2171e2015-09-04 16:53:46 +0100468#else
469 (void)emit;
470 (void)source_line;
Damien George62ad1892014-01-29 21:51:51 +0000471#endif
Damien George08335002014-01-18 23:24:36 +0000472}
473
Damien George7ff996c2014-09-08 23:05:16 +0100474STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georgea210c772015-03-26 15:49:53 +0000475 if (emit->pass == MP_PASS_SCOPE) {
476 return;
477 }
Damien George7ff996c2014-09-08 23:05:16 +0100478 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100479 emit->stack_size += stack_size_delta;
480 if (emit->stack_size > emit->scope->stack_size) {
481 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100482 }
483 emit->last_emit_was_return_value = false;
484}
485
Damien George41125902015-03-26 16:44:14 +0000486void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000487 emit_bc_pre(emit, 0);
Damien Georgea210c772015-03-26 15:49:53 +0000488 if (emit->pass == MP_PASS_SCOPE) {
489 return;
490 }
Damienb05d7072013-10-05 13:37:10 +0100491 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100492 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100493 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000494 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100495 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100496 } else {
497 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100498 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
499 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100500 }
501}
502
Damien George41125902015-03-26 16:44:14 +0000503void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000504 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100505 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100506}
507
Damien George41125902015-03-26 16:44:14 +0000508void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000509 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100510 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100511}
512
Damien George41125902015-03-26 16:44:14 +0000513void mp_emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000514 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100515 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100516}
517
Damien George41125902015-03-26 16:44:14 +0000518void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000519 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100520 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100521 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
522 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
523 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien George7385b012016-09-27 15:46:50 +1000524 default:
525 assert(tok == MP_TOKEN_ELLIPSIS);
526 emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
527 break;
Damien429d7192013-10-04 19:53:11 +0100528 }
529}
530
Damien George41125902015-03-26 16:44:14 +0000531void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000532 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100533 if (-16 <= arg && arg <= 47) {
534 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
535 } else {
536 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
537 }
Damien429d7192013-10-04 19:53:11 +0100538}
539
Damien George59fba2d2015-06-25 14:42:13 +0000540void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000541 emit_bc_pre(emit, 1);
Damien George59fba2d2015-06-25 14:42:13 +0000542 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100543}
544
Damien George5d66b422015-11-27 12:41:25 +0000545void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
Damien Georgedab13852015-01-13 15:55:54 +0000546 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000547 emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, obj);
Damien Georgedab13852015-01-13 15:55:54 +0000548}
549
Damien George41125902015-03-26 16:44:14 +0000550void mp_emit_bc_load_null(emit_t *emit) {
Damien George523b5752014-03-31 11:59:23 +0100551 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100552 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100553};
554
Damien George41125902015-03-26 16:44:14 +0000555void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000556 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000557 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100558 if (local_num <= 15) {
559 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
560 } else {
561 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100562 }
563}
564
Damien George41125902015-03-26 16:44:14 +0000565void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000566 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000567 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100568 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000569}
570
Damien George41125902015-03-26 16:44:14 +0000571void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000572 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000573 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100574 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000575 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000576 emit_write_bytecode_byte(emit, 0);
577 }
Damien429d7192013-10-04 19:53:11 +0100578}
579
Damien George41125902015-03-26 16:44:14 +0000580void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000581 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000582 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100583 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000584 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000585 emit_write_bytecode_byte(emit, 0);
586 }
Damien429d7192013-10-04 19:53:11 +0100587}
588
Damien George41125902015-03-26 16:44:14 +0000589void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000590 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100591 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000592 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000593 emit_write_bytecode_byte(emit, 0);
594 }
Damien429d7192013-10-04 19:53:11 +0100595}
596
Damien George41125902015-03-26 16:44:14 +0000597void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000598 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100599 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100600}
601
Damien George41125902015-03-26 16:44:14 +0000602void mp_emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000603 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100604 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Damien George41125902015-03-26 16:44:14 +0000607void mp_emit_bc_load_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +0100608 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100609 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100610}
611
Damien George41125902015-03-26 16:44:14 +0000612void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000613 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000614 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100615 if (local_num <= 15) {
616 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
617 } else {
618 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100619 }
620}
621
Damien George41125902015-03-26 16:44:14 +0000622void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000623 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000624 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100625 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000626}
627
Damien George41125902015-03-26 16:44:14 +0000628void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100630 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100631}
632
Damien George41125902015-03-26 16:44:14 +0000633void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000634 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100635 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Damien George41125902015-03-26 16:44:14 +0000638void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000639 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100640 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000641 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000642 emit_write_bytecode_byte(emit, 0);
643 }
Damien429d7192013-10-04 19:53:11 +0100644}
645
Damien George41125902015-03-26 16:44:14 +0000646void mp_emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000647 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100648 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100649}
650
Damien George41125902015-03-26 16:44:14 +0000651void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000652 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100653 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100654}
655
Damien George41125902015-03-26 16:44:14 +0000656void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000657 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100658 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000659}
660
Damien George41125902015-03-26 16:44:14 +0000661void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000662 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100663 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100664}
665
Damien George41125902015-03-26 16:44:14 +0000666void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000667 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100668 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100669}
670
Damien George41125902015-03-26 16:44:14 +0000671void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
672 mp_emit_bc_load_null(emit);
673 mp_emit_bc_rot_two(emit);
674 mp_emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100675}
676
Damien George41125902015-03-26 16:44:14 +0000677void mp_emit_bc_delete_subscr(emit_t *emit) {
678 mp_emit_bc_load_null(emit);
679 mp_emit_bc_rot_three(emit);
680 mp_emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100681}
682
Damien George41125902015-03-26 16:44:14 +0000683void mp_emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000684 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100685 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100686}
687
Damien George41125902015-03-26 16:44:14 +0000688void mp_emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000689 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100690 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100691}
692
Damien George41125902015-03-26 16:44:14 +0000693void mp_emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000694 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100695 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100696}
697
Damien George41125902015-03-26 16:44:14 +0000698void mp_emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000699 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100700 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100701}
702
Damien George41125902015-03-26 16:44:14 +0000703void mp_emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000704 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100705 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100706}
707
Damien George41125902015-03-26 16:44:14 +0000708void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000709 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100710 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100711}
712
Damien George41125902015-03-26 16:44:14 +0000713void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000714 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000715 if (cond) {
716 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
717 } else {
718 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
719 }
Damien429d7192013-10-04 19:53:11 +0100720}
721
Damien George41125902015-03-26 16:44:14 +0000722void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000723 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000724 if (cond) {
725 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
726 } else {
727 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
728 }
Damien429d7192013-10-04 19:53:11 +0100729}
730
Damien George41125902015-03-26 16:44:14 +0000731void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000732 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000733 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100734 if (label & MP_EMIT_BREAK_FROM_FOR) {
735 // need to pop the iterator if we are breaking out of a for loop
736 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien Georgef4df3aa2016-01-09 23:59:52 +0000737 // also pop the iter_buf
Damien George60656ea2017-03-23 16:36:08 +1100738 for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
Damien Georgef4df3aa2016-01-09 23:59:52 +0000739 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
740 }
Damien George25c84642014-05-30 15:20:41 +0100741 }
742 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
743 } else {
744 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
745 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000746 }
Damien429d7192013-10-04 19:53:11 +0100747}
748
Damien George41125902015-03-26 16:44:14 +0000749void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgef0406852016-09-27 12:37:21 +1000750 // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
751 // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
752 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100753 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100754}
755
Damien Georgece8b4e82016-04-07 08:50:38 +0100756void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
757 mp_emit_bc_pop_block(emit);
758 mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
759 mp_emit_bc_label_assign(emit, label);
Damien Georgef0406852016-09-27 12:37:21 +1000760 emit_bc_pre(emit, 2); // ensure we have enough stack space to call the __exit__ method
Damien George3417bc22014-05-10 10:36:38 +0100761 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien Georgef0406852016-09-27 12:37:21 +1000762 emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_with
Damien429d7192013-10-04 19:53:11 +0100763}
764
Damien George41125902015-03-26 16:44:14 +0000765void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000766 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100767 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100768}
769
Damien George41125902015-03-26 16:44:14 +0000770void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000771 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100772 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100773}
774
Damien George41125902015-03-26 16:44:14 +0000775void mp_emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000776 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100777 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100778}
779
Damien Georgef4df3aa2016-01-09 23:59:52 +0000780void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
Damien George60656ea2017-03-23 16:36:08 +1100781 emit_bc_pre(emit, use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0);
Damien Georgef4df3aa2016-01-09 23:59:52 +0000782 emit_write_bytecode_byte(emit, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100783}
784
Damien George41125902015-03-26 16:44:14 +0000785void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000786 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100787 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100788}
789
Damien George30b42dd2017-01-17 15:30:18 +1100790void mp_emit_bc_for_iter_end(emit_t *emit) {
Damien George60656ea2017-03-23 16:36:08 +1100791 emit_bc_pre(emit, -MP_OBJ_ITER_BUF_NSLOTS);
Damien429d7192013-10-04 19:53:11 +0100792}
793
Damien George41125902015-03-26 16:44:14 +0000794void mp_emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000795 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100796 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100797}
798
Damien George41125902015-03-26 16:44:14 +0000799void mp_emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000800 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100801 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100802}
803
Damien George41125902015-03-26 16:44:14 +0000804void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georgebdbe8c92015-12-08 12:28:11 +0000805 emit_bc_pre(emit, 0);
806 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damien George41125902015-03-26 16:44:14 +0000809void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000810 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100811 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000812 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100813 op = MP_BINARY_OP_IN;
814 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000815 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100816 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000817 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000818 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100819 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000820 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000821 emit_bc_pre(emit, 0);
Damien Georgebdbe8c92015-12-08 12:28:11 +0000822 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000823 }
Damien429d7192013-10-04 19:53:11 +0100824}
825
Damien George41125902015-03-26 16:44:14 +0000826void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000827 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100828 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100829}
830
Damien George41125902015-03-26 16:44:14 +0000831void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000832 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100833 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100834}
835
Damien George41125902015-03-26 16:44:14 +0000836void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000837 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100838 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100839}
840
Damien George41125902015-03-26 16:44:14 +0000841void mp_emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000842 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100843 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100844}
845
Damien Georgee37dcaa2014-12-27 17:07:16 +0000846#if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000847void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000848 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100849 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100850}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000851#endif
Damien429d7192013-10-04 19:53:11 +0100852
Damien George83204f32014-12-27 17:20:41 +0000853#if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000854void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000855 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100856 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100857}
Damien George83204f32014-12-27 17:20:41 +0000858#endif
Damien429d7192013-10-04 19:53:11 +0100859
Damien Georgea5624bf2016-09-18 23:59:47 +1000860void 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 +1000861 int t;
Damien Georgea5624bf2016-09-18 23:59:47 +1000862 int n;
Damien Georgea5624bf2016-09-18 23:59:47 +1000863 if (kind == SCOPE_LIST_COMP) {
Damien Georgeadaf0d82016-09-19 08:46:01 +1000864 n = 0;
865 t = 0;
866 } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
867 n = 1;
868 t = 1;
869 } else if (MICROPY_PY_BUILTINS_SET) {
870 n = 0;
871 t = 2;
Damien Georgea5624bf2016-09-18 23:59:47 +1000872 }
Damien Georgeadaf0d82016-09-19 08:46:01 +1000873 emit_bc_pre(emit, -1 - n);
874 // the lower 2 bits of the opcode argument indicate the collection type
875 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
Damien Georgea5624bf2016-09-18 23:59:47 +1000876}
877
Damien George41125902015-03-26 16:44:14 +0000878void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000879 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100880 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100881}
882
Damien George41125902015-03-26 16:44:14 +0000883void 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 +0000884 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100885 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100886}
887
Damien George41125902015-03-26 16:44:14 +0000888void 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 +0100889 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000890 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000891 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000892 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100893 emit_bc_pre(emit, -1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000894 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200895 }
Damien429d7192013-10-04 19:53:11 +0100896}
897
Damien George41125902015-03-26 16:44:14 +0000898void 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 +0100899 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100900 emit_bc_pre(emit, -n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000901 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100902 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200903 } else {
Damien George3558f622014-04-20 17:50:40 +0100904 assert(n_closed_over <= 255);
905 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000906 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100907 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200908 }
Damien429d7192013-10-04 19:53:11 +0100909}
910
Damien George7ff996c2014-09-08 23:05:16 +0100911STATIC 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 +0100912 if (star_flags) {
Damien George7ff996c2014-09-08 23:05:16 +0100913 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 +0100914 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 +0100915 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100916 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100917 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 +0100918 }
Damien George523b5752014-03-31 11:59:23 +0100919}
920
Damien George41125902015-03-26 16:44:14 +0000921void 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 +0100922 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100923}
924
Damien George41125902015-03-26 16:44:14 +0000925void 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 +0100926 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100927}
928
Damien George41125902015-03-26 16:44:14 +0000929void mp_emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000930 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100931 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100932 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100933}
934
Damien George41125902015-03-26 16:44:14 +0000935void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Pavol Rusnak7ffc9592016-10-25 11:05:33 +0200936 assert(n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000937 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100938 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100939}
940
Damien George41125902015-03-26 16:44:14 +0000941void mp_emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000942 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100943 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100944 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100945}
946
Damien George41125902015-03-26 16:44:14 +0000947void mp_emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000948 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100949 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100950 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100951}
952
Damien George41125902015-03-26 16:44:14 +0000953void mp_emit_bc_start_except_handler(emit_t *emit) {
Damien Georgef0406852016-09-27 12:37:21 +1000954 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 +0100955}
956
Damien George41125902015-03-26 16:44:14 +0000957void mp_emit_bc_end_except_handler(emit_t *emit) {
Damien Georgef0406852016-09-27 12:37:21 +1000958 mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
Damien Georgeb601d952014-06-30 05:17:25 +0100959}
960
Damien George41125902015-03-26 16:44:14 +0000961#if MICROPY_EMIT_NATIVE
Damien6cdd3af2013-10-05 18:08:26 +0100962const emit_method_table_t emit_bc_method_table = {
Damien George7385b012016-09-27 15:46:50 +1000963 NULL, // set_native_type is never called when emitting bytecode
Damien George41125902015-03-26 16:44:14 +0000964 mp_emit_bc_start_pass,
965 mp_emit_bc_end_pass,
966 mp_emit_bc_last_emit_was_return_value,
967 mp_emit_bc_adjust_stack_size,
968 mp_emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100969
Damien George542bd6b2015-03-26 14:42:40 +0000970 {
Damien George41125902015-03-26 16:44:14 +0000971 mp_emit_bc_load_fast,
972 mp_emit_bc_load_deref,
973 mp_emit_bc_load_name,
974 mp_emit_bc_load_global,
Damien George542bd6b2015-03-26 14:42:40 +0000975 },
976 {
Damien George41125902015-03-26 16:44:14 +0000977 mp_emit_bc_store_fast,
978 mp_emit_bc_store_deref,
979 mp_emit_bc_store_name,
980 mp_emit_bc_store_global,
Damien George542bd6b2015-03-26 14:42:40 +0000981 },
982 {
Damien George41125902015-03-26 16:44:14 +0000983 mp_emit_bc_delete_fast,
984 mp_emit_bc_delete_deref,
985 mp_emit_bc_delete_name,
986 mp_emit_bc_delete_global,
Damien George542bd6b2015-03-26 14:42:40 +0000987 },
Damien4b03e772013-10-05 14:17:09 +0100988
Damien George41125902015-03-26 16:44:14 +0000989 mp_emit_bc_label_assign,
990 mp_emit_bc_import_name,
991 mp_emit_bc_import_from,
992 mp_emit_bc_import_star,
993 mp_emit_bc_load_const_tok,
994 mp_emit_bc_load_const_small_int,
995 mp_emit_bc_load_const_str,
996 mp_emit_bc_load_const_obj,
997 mp_emit_bc_load_null,
998 mp_emit_bc_load_attr,
999 mp_emit_bc_load_method,
1000 mp_emit_bc_load_build_class,
1001 mp_emit_bc_load_subscr,
1002 mp_emit_bc_store_attr,
1003 mp_emit_bc_store_subscr,
1004 mp_emit_bc_delete_attr,
1005 mp_emit_bc_delete_subscr,
1006 mp_emit_bc_dup_top,
1007 mp_emit_bc_dup_top_two,
1008 mp_emit_bc_pop_top,
1009 mp_emit_bc_rot_two,
1010 mp_emit_bc_rot_three,
1011 mp_emit_bc_jump,
1012 mp_emit_bc_pop_jump_if,
1013 mp_emit_bc_jump_if_or_pop,
1014 mp_emit_bc_unwind_jump,
1015 mp_emit_bc_unwind_jump,
1016 mp_emit_bc_setup_with,
1017 mp_emit_bc_with_cleanup,
1018 mp_emit_bc_setup_except,
1019 mp_emit_bc_setup_finally,
1020 mp_emit_bc_end_finally,
1021 mp_emit_bc_get_iter,
1022 mp_emit_bc_for_iter,
1023 mp_emit_bc_for_iter_end,
1024 mp_emit_bc_pop_block,
1025 mp_emit_bc_pop_except,
1026 mp_emit_bc_unary_op,
1027 mp_emit_bc_binary_op,
1028 mp_emit_bc_build_tuple,
1029 mp_emit_bc_build_list,
Damien George41125902015-03-26 16:44:14 +00001030 mp_emit_bc_build_map,
1031 mp_emit_bc_store_map,
Damien Georgee37dcaa2014-12-27 17:07:16 +00001032 #if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +00001033 mp_emit_bc_build_set,
Damien Georgee37dcaa2014-12-27 17:07:16 +00001034 #endif
Damien George83204f32014-12-27 17:20:41 +00001035 #if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +00001036 mp_emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +00001037 #endif
Damien Georgea5624bf2016-09-18 23:59:47 +10001038 mp_emit_bc_store_comp,
Damien George41125902015-03-26 16:44:14 +00001039 mp_emit_bc_unpack_sequence,
1040 mp_emit_bc_unpack_ex,
1041 mp_emit_bc_make_function,
1042 mp_emit_bc_make_closure,
1043 mp_emit_bc_call_function,
1044 mp_emit_bc_call_method,
1045 mp_emit_bc_return_value,
1046 mp_emit_bc_raise_varargs,
1047 mp_emit_bc_yield_value,
1048 mp_emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01001049
Damien George41125902015-03-26 16:44:14 +00001050 mp_emit_bc_start_except_handler,
1051 mp_emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +01001052};
Damien George41125902015-03-26 16:44:14 +00001053#else
1054const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
1055 mp_emit_bc_load_fast,
1056 mp_emit_bc_load_deref,
1057 mp_emit_bc_load_name,
1058 mp_emit_bc_load_global,
1059};
1060
1061const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
1062 mp_emit_bc_store_fast,
1063 mp_emit_bc_store_deref,
1064 mp_emit_bc_store_name,
1065 mp_emit_bc_store_global,
1066};
1067
1068const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
1069 mp_emit_bc_delete_fast,
1070 mp_emit_bc_delete_deref,
1071 mp_emit_bc_delete_name,
1072 mp_emit_bc_delete_global,
1073};
1074#endif
Damien Georgedd5353a2015-12-18 12:35:44 +00001075
1076#endif //MICROPY_ENABLE_COMPILER