blob: d871aa4ce92952d912ceb33fbeee00a87bebb6b5 [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;
147 if (lines_to_skip <= 6) {
148 // use 0b0LLBBBBB encoding
149 b = MIN(bytes_to_skip, 0x1f);
150 l = MIN(lines_to_skip, 0x3);
151 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
152 } else {
153 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
154 b = MIN(bytes_to_skip, 0xf);
155 l = MIN(lines_to_skip, 0x7ff);
156 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
157 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
158 ci[1] = l;
159 }
Damien George73496fb2014-04-13 14:51:56 +0100160 bytes_to_skip -= b;
161 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000162 }
Damien George08335002014-01-18 23:24:36 +0000163}
Damien George73496fb2014-04-13 14:51:56 +0100164#endif
Damien George08335002014-01-18 23:24:36 +0000165
166// all functions must go through this one to emit byte code
Damien George4dea9222015-04-09 15:29:54 +0000167STATIC byte *emit_get_cur_to_write_bytecode(emit_t *emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000168 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100169 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100170 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000171 return emit->dummy_data;
172 } else {
Damien George3417bc22014-05-10 10:36:38 +0100173 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
174 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
175 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000176 return c;
177 }
178}
179
Damien George4dea9222015-04-09 15:29:54 +0000180STATIC void emit_write_bytecode_byte(emit_t *emit, byte b1) {
181 byte *c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100182 c[0] = b1;
183}
184
Damien George9b7f5832015-03-18 17:47:47 +0000185STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100186 assert((b2 & (~0xff)) == 0);
Damien George4dea9222015-04-09 15:29:54 +0000187 byte *c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100188 c[0] = b1;
189 c[1] = b2;
190}
191
Damien George3417bc22014-05-10 10:36:38 +0100192// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George4dea9222015-04-09 15:29:54 +0000193STATIC void emit_write_bytecode_byte_int(emit_t *emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100194 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200195
196 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100197 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200198 byte *p = buf + sizeof(buf);
199 // We encode in little-ending order, but store in big-endian, to help decoding
200 do {
201 *--p = num & 0x7f;
202 num >>= 7;
203 } while (num != 0 && num != -1);
204 // Make sure that highest bit we stored (mask 0x40) matches sign
205 // of the number. If not, store extra byte just to encode sign
206 if (num == -1 && (*p & 0x40) == 0) {
207 *--p = 0x7f;
208 } else if (num == 0 && (*p & 0x40) != 0) {
209 *--p = 0;
210 }
211
Damien George4dea9222015-04-09 15:29:54 +0000212 byte *c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200213 while (p != buf + sizeof(buf) - 1) {
214 *c++ = *p++ | 0x80;
215 }
216 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100217}
218
Damien George4dea9222015-04-09 15:29:54 +0000219STATIC void emit_write_bytecode_byte_uint(emit_t *emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100220 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100221 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100222}
223
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000224#if MICROPY_PERSISTENT_CODE
225STATIC void emit_write_bytecode_byte_const(emit_t *emit, byte b, mp_uint_t n, mp_uint_t c) {
226 if (emit->pass == MP_PASS_EMIT) {
227 emit->const_table[n] = c;
228 }
229 emit_write_bytecode_byte_uint(emit, b, n);
230}
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000231#endif
Damien Georgedf8127a2014-04-13 11:04:33 +0100232
Damien George9b7f5832015-03-18 17:47:47 +0000233STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000234 #if MICROPY_PERSISTENT_CODE
235 assert((qst >> 16) == 0);
236 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
237 c[0] = b;
238 c[1] = qst;
239 c[2] = qst >> 8;
240 #else
Damien George7ff996c2014-09-08 23:05:16 +0100241 emit_write_bytecode_byte_uint(emit, b, qst);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000242 #endif
243}
244
Damien George999cedb2015-11-27 17:01:44 +0000245STATIC void emit_write_bytecode_byte_obj(emit_t *emit, byte b, mp_obj_t obj) {
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000246 #if MICROPY_PERSISTENT_CODE
247 emit_write_bytecode_byte_const(emit, b,
248 emit->scope->num_pos_args + emit->scope->num_kwonly_args
Damien George999cedb2015-11-27 17:01:44 +0000249 + emit->ct_cur_obj++, (mp_uint_t)obj);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000250 #else
Damien George999cedb2015-11-27 17:01:44 +0000251 // aligns the pointer so it is friendly to GC
252 emit_write_bytecode_byte(emit, b);
253 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(mp_obj_t));
254 mp_obj_t *c = (mp_obj_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_obj_t));
255 // Verify thar c is already uint-aligned
256 assert(c == MP_ALIGN(c, sizeof(mp_obj_t)));
257 *c = obj;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000258 #endif
259}
260
261STATIC void emit_write_bytecode_byte_raw_code(emit_t *emit, byte b, mp_raw_code_t *rc) {
262 #if MICROPY_PERSISTENT_CODE
263 emit_write_bytecode_byte_const(emit, b,
264 emit->scope->num_pos_args + emit->scope->num_kwonly_args
Damien George999cedb2015-11-27 17:01:44 +0000265 + emit->ct_num_obj + emit->ct_cur_raw_code++, (mp_uint_t)(uintptr_t)rc);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000266 #else
Damien George999cedb2015-11-27 17:01:44 +0000267 // aligns the pointer so it is friendly to GC
268 emit_write_bytecode_byte(emit, b);
269 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(void*));
270 void **c = (void**)emit_get_cur_to_write_bytecode(emit, sizeof(void*));
271 // Verify thar c is already uint-aligned
272 assert(c == MP_ALIGN(c, sizeof(void*)));
273 *c = rc;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000274 #endif
Damien429d7192013-10-04 19:53:11 +0100275}
276
Damien03c9cfb2013-11-05 22:06:08 +0000277// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George4dea9222015-04-09 15:29:54 +0000278STATIC void emit_write_bytecode_byte_unsigned_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George7ff996c2014-09-08 23:05:16 +0100279 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100280 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100281 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100282 } else {
Damien George3417bc22014-05-10 10:36:38 +0100283 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100284 }
Damien George3417bc22014-05-10 10:36:38 +0100285 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000286 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100287 c[1] = bytecode_offset;
288 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000289}
290
291// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George4dea9222015-04-09 15:29:54 +0000292STATIC void emit_write_bytecode_byte_signed_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100293 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100294 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100295 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000296 } else {
Damien George3417bc22014-05-10 10:36:38 +0100297 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000298 }
Damien George4dea9222015-04-09 15:29:54 +0000299 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000300 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100301 c[1] = bytecode_offset;
302 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100303}
304
Damien George41125902015-03-26 16:44:14 +0000305#if MICROPY_EMIT_NATIVE
306STATIC void mp_emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000307 (void)emit;
308 (void)op;
309 (void)arg1;
310 (void)arg2;
Damien George6baf76e2013-12-30 22:32:17 +0000311}
Damien George41125902015-03-26 16:44:14 +0000312#endif
Damien George6baf76e2013-12-30 22:32:17 +0000313
Damien George41125902015-03-26 16:44:14 +0000314void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000315 emit->pass = pass;
316 emit->stack_size = 0;
317 emit->last_emit_was_return_value = false;
318 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000319 emit->last_source_line_offset = 0;
320 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100321 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100322 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000323 }
Damien George3417bc22014-05-10 10:36:38 +0100324 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000325 emit->code_info_offset = 0;
326
Damien George9b7f5832015-03-18 17:47:47 +0000327 // Write local state size and exception stack size.
328 {
329 mp_uint_t n_state = scope->num_locals + scope->stack_size;
330 if (n_state == 0) {
331 // Need at least 1 entry in the state, in the case an exception is
332 // propagated through this function, the exception is returned in
333 // the highest slot in the state (fastn[0], see vm.c).
334 n_state = 1;
335 }
336 emit_write_code_info_uint(emit, n_state);
337 emit_write_code_info_uint(emit, scope->exc_stack_size);
Damien George08335002014-01-18 23:24:36 +0000338 }
339
Damien George3a3db4d2015-10-22 23:45:37 +0100340 // Write scope flags and number of arguments.
341 // TODO check that num args all fit in a byte
342 emit_write_code_info_byte(emit, emit->scope->scope_flags);
343 emit_write_code_info_byte(emit, emit->scope->num_pos_args);
344 emit_write_code_info_byte(emit, emit->scope->num_kwonly_args);
345 emit_write_code_info_byte(emit, emit->scope->num_def_pos_args);
346
Damien George9b7f5832015-03-18 17:47:47 +0000347 // Write size of the rest of the code info. We don't know how big this
348 // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
349 // for it and hope that is enough! TODO assert this or something.
350 if (pass == MP_PASS_EMIT) {
351 emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
352 } else {
353 emit_get_cur_to_write_code_info(emit, 2);
Damien George8dcc0c72014-03-27 10:55:21 +0000354 }
355
Damien George9b7f5832015-03-18 17:47:47 +0000356 // Write the name and source file of this function.
357 emit_write_code_info_qstr(emit, scope->simple_name);
358 emit_write_code_info_qstr(emit, scope->source_file);
359
Damien George8dcc0c72014-03-27 10:55:21 +0000360 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000361 for (int i = 0; i < scope->id_info_len; i++) {
362 id_info_t *id = &scope->id_info[i];
363 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgec9aa1882015-04-07 00:08:17 +0100364 assert(id->local_num < 255);
Damien George3417bc22014-05-10 10:36:38 +0100365 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 +0000366 }
367 }
Damien Georgec9aa1882015-04-07 00:08:17 +0100368 emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George713ea182015-10-23 01:23:11 +0100369
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000370 #if MICROPY_PERSISTENT_CODE
371 emit->ct_cur_obj = 0;
372 emit->ct_cur_raw_code = 0;
373 #endif
374
Damien George713ea182015-10-23 01:23:11 +0100375 if (pass == MP_PASS_EMIT) {
376 // Write argument names (needed to resolve positional args passed as
377 // keywords). We store them as full word-sized objects for efficient access
378 // in mp_setup_code_state this is the start of the prelude and is guaranteed
379 // to be aligned on a word boundary.
380
381 // For a given argument position (indexed by i) we need to find the
382 // corresponding id_info which is a parameter, as it has the correct
383 // qstr name to use as the argument name. Note that it's not a simple
384 // 1-1 mapping (ie i!=j in general) because of possible closed-over
385 // variables. In the case that the argument i has no corresponding
386 // parameter we use "*" as its name (since no argument can ever be named
387 // "*"). We could use a blank qstr but "*" is better for debugging.
388 // Note: there is some wasted RAM here for the case of storing a qstr
389 // for each closed-over variable, and maybe there is a better way to do
390 // it, but that would require changes to mp_setup_code_state.
391 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
392 qstr qst = MP_QSTR__star_;
393 for (int j = 0; j < scope->id_info_len; ++j) {
394 id_info_t *id = &scope->id_info[j];
395 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
396 qst = id->qst;
397 break;
398 }
399 }
400 emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
401 }
402 }
Damien George6baf76e2013-12-30 22:32:17 +0000403}
404
Damien George41125902015-03-26 16:44:14 +0000405void mp_emit_bc_end_pass(emit_t *emit) {
Damien Georgea210c772015-03-26 15:49:53 +0000406 if (emit->pass == MP_PASS_SCOPE) {
407 return;
408 }
409
Damien George6baf76e2013-12-30 22:32:17 +0000410 // check stack is back to zero size
411 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100412 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien George6baf76e2013-12-30 22:32:17 +0000413 }
414
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 George713ea182015-10-23 01:23:11 +0100444 emit->code_info_size + emit->bytecode_size,
Damien Georged8c834c2015-11-02 21:55:42 +0000445 emit->const_table,
446 #if MICROPY_PERSISTENT_CODE_SAVE
447 emit->ct_cur_obj, emit->ct_cur_raw_code,
448 #endif
449 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000450 }
451}
452
Damien George41125902015-03-26 16:44:14 +0000453bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100454 return emit->last_emit_was_return_value;
455}
456
Damien George41125902015-03-26 16:44:14 +0000457void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000458 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100459}
460
Damien George41125902015-03-26 16:44:14 +0000461void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100462 //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 +0000463#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000464 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300465 // If we compile with -O3, don't store line numbers.
466 return;
467 }
Damien George08335002014-01-18 23:24:36 +0000468 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100469 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
470 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000471 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100472 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000473 emit->last_source_line = source_line;
474 }
Damien George3a2171e2015-09-04 16:53:46 +0100475#else
476 (void)emit;
477 (void)source_line;
Damien George62ad1892014-01-29 21:51:51 +0000478#endif
Damien George08335002014-01-18 23:24:36 +0000479}
480
Damien George7ff996c2014-09-08 23:05:16 +0100481STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georgea210c772015-03-26 15:49:53 +0000482 if (emit->pass == MP_PASS_SCOPE) {
483 return;
484 }
Damien George7ff996c2014-09-08 23:05:16 +0100485 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100486 emit->stack_size += stack_size_delta;
487 if (emit->stack_size > emit->scope->stack_size) {
488 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100489 }
490 emit->last_emit_was_return_value = false;
491}
492
Damien George41125902015-03-26 16:44:14 +0000493void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000494 emit_bc_pre(emit, 0);
Damien Georgea210c772015-03-26 15:49:53 +0000495 if (emit->pass == MP_PASS_SCOPE) {
496 return;
497 }
Damienb05d7072013-10-05 13:37:10 +0100498 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100499 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100500 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000501 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100502 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100503 } else {
504 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100505 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
506 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100507 }
508}
509
Damien George41125902015-03-26 16:44:14 +0000510void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000511 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100512 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100513}
514
Damien George41125902015-03-26 16:44:14 +0000515void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000516 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100517 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100518}
519
Damien George41125902015-03-26 16:44:14 +0000520void mp_emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000521 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100522 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100523}
524
Damien George41125902015-03-26 16:44:14 +0000525void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000526 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100527 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100528 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
529 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
530 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000531 no_other_choice:
Damien George999cedb2015-11-27 17:01:44 +0000532 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj)); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000533 default: assert(0); goto no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100534 }
535}
536
Damien George41125902015-03-26 16:44:14 +0000537void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000538 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100539 if (-16 <= arg && arg <= 47) {
540 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
541 } else {
542 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
543 }
Damien429d7192013-10-04 19:53:11 +0100544}
545
Damien George59fba2d2015-06-25 14:42:13 +0000546void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000547 emit_bc_pre(emit, 1);
Damien George59fba2d2015-06-25 14:42:13 +0000548 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100549}
550
Damien George5d66b422015-11-27 12:41:25 +0000551void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
Damien Georgedab13852015-01-13 15:55:54 +0000552 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000553 emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, obj);
Damien Georgedab13852015-01-13 15:55:54 +0000554}
555
Damien George41125902015-03-26 16:44:14 +0000556void mp_emit_bc_load_null(emit_t *emit) {
Damien George523b5752014-03-31 11:59:23 +0100557 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100558 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100559};
560
Damien George41125902015-03-26 16:44:14 +0000561void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000562 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100563 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000564 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100565 if (local_num <= 15) {
566 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
567 } else {
568 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100569 }
570}
571
Damien George41125902015-03-26 16:44:14 +0000572void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000573 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000574 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100575 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000576}
577
Damien George41125902015-03-26 16:44:14 +0000578void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000579 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000580 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100581 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000582 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000583 emit_write_bytecode_byte(emit, 0);
584 }
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damien George41125902015-03-26 16:44:14 +0000587void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000588 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000589 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100590 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000591 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000592 emit_write_bytecode_byte(emit, 0);
593 }
Damien429d7192013-10-04 19:53:11 +0100594}
595
Damien George41125902015-03-26 16:44:14 +0000596void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000597 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100598 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000599 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000600 emit_write_bytecode_byte(emit, 0);
601 }
Damien429d7192013-10-04 19:53:11 +0100602}
603
Damien George41125902015-03-26 16:44:14 +0000604void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000605 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100606 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100607}
608
Damien George41125902015-03-26 16:44:14 +0000609void mp_emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000610 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100611 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Damien George41125902015-03-26 16:44:14 +0000614void mp_emit_bc_load_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +0100615 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100616 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100617}
618
Damien George41125902015-03-26 16:44:14 +0000619void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000620 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100621 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000622 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100623 if (local_num <= 15) {
624 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
625 } else {
626 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100627 }
628}
629
Damien George41125902015-03-26 16:44:14 +0000630void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000631 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000632 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100633 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000634}
635
Damien George41125902015-03-26 16:44:14 +0000636void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000637 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100638 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100639}
640
Damien George41125902015-03-26 16:44:14 +0000641void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000642 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100643 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Damien George41125902015-03-26 16:44:14 +0000646void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000647 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100648 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien Georgeea235202016-02-11 22:30:53 +0000649 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
Damien George7ee91cf2015-01-06 12:51:39 +0000650 emit_write_bytecode_byte(emit, 0);
651 }
Damien429d7192013-10-04 19:53:11 +0100652}
653
Damien George41125902015-03-26 16:44:14 +0000654void mp_emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000655 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100656 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100657}
658
Damien George41125902015-03-26 16:44:14 +0000659void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000660 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100661 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100662}
663
Damien George41125902015-03-26 16:44:14 +0000664void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000665 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100666 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000667}
668
Damien George41125902015-03-26 16:44:14 +0000669void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000670 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100671 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100672}
673
Damien George41125902015-03-26 16:44:14 +0000674void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000675 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100676 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100677}
678
Damien George41125902015-03-26 16:44:14 +0000679void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
680 mp_emit_bc_load_null(emit);
681 mp_emit_bc_rot_two(emit);
682 mp_emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien George41125902015-03-26 16:44:14 +0000685void mp_emit_bc_delete_subscr(emit_t *emit) {
686 mp_emit_bc_load_null(emit);
687 mp_emit_bc_rot_three(emit);
688 mp_emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100689}
690
Damien George41125902015-03-26 16:44:14 +0000691void mp_emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000692 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100693 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100694}
695
Damien George41125902015-03-26 16:44:14 +0000696void mp_emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000697 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100698 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100699}
700
Damien George41125902015-03-26 16:44:14 +0000701void mp_emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000702 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100703 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100704}
705
Damien George41125902015-03-26 16:44:14 +0000706void mp_emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000707 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100708 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100709}
710
Damien George41125902015-03-26 16:44:14 +0000711void mp_emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000712 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100713 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Damien George41125902015-03-26 16:44:14 +0000716void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000717 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100718 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100719}
720
Damien George41125902015-03-26 16:44:14 +0000721void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000722 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000723 if (cond) {
724 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
725 } else {
726 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
727 }
Damien429d7192013-10-04 19:53:11 +0100728}
729
Damien George41125902015-03-26 16:44:14 +0000730void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000731 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000732 if (cond) {
733 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
734 } else {
735 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
736 }
Damien429d7192013-10-04 19:53:11 +0100737}
738
Damien George41125902015-03-26 16:44:14 +0000739void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000740 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000741 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100742 if (label & MP_EMIT_BREAK_FROM_FOR) {
743 // need to pop the iterator if we are breaking out of a for loop
744 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
745 }
746 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
747 } else {
748 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
749 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000750 }
Damien429d7192013-10-04 19:53:11 +0100751}
752
Damien George41125902015-03-26 16:44:14 +0000753void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien George8c1d23a2015-04-24 01:52:28 +0100754 // TODO We can probably optimise the amount of needed stack space, since
755 // we don't actually need 4 slots during the entire with block, only in
756 // the cleanup handler in certain cases. It needs some thinking.
757 emit_bc_pre(emit, 4);
Damien George3417bc22014-05-10 10:36:38 +0100758 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100759}
760
Damien Georgece8b4e82016-04-07 08:50:38 +0100761void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
762 mp_emit_bc_pop_block(emit);
763 mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
764 mp_emit_bc_label_assign(emit, label);
Damien George8c1d23a2015-04-24 01:52:28 +0100765 emit_bc_pre(emit, -4);
Damien George3417bc22014-05-10 10:36:38 +0100766 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100767}
768
Damien George41125902015-03-26 16:44:14 +0000769void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000770 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100771 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100772}
773
Damien George41125902015-03-26 16:44:14 +0000774void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000775 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100776 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100777}
778
Damien George41125902015-03-26 16:44:14 +0000779void mp_emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000780 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100781 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100782}
783
Damien George41125902015-03-26 16:44:14 +0000784void mp_emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000785 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100786 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100787}
788
Damien George41125902015-03-26 16:44:14 +0000789void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000790 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100791 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100792}
793
Damien George41125902015-03-26 16:44:14 +0000794void mp_emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000795 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100796}
797
Damien George41125902015-03-26 16:44:14 +0000798void mp_emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000799 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100800 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100801}
802
Damien George41125902015-03-26 16:44:14 +0000803void mp_emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000804 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100805 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100806}
807
Damien George41125902015-03-26 16:44:14 +0000808void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georgebdbe8c92015-12-08 12:28:11 +0000809 emit_bc_pre(emit, 0);
810 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien429d7192013-10-04 19:53:11 +0100811}
812
Damien George41125902015-03-26 16:44:14 +0000813void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000814 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100815 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000816 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100817 op = MP_BINARY_OP_IN;
818 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000819 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100820 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000821 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000822 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100823 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000824 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000825 emit_bc_pre(emit, 0);
Damien Georgebdbe8c92015-12-08 12:28:11 +0000826 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000827 }
Damien429d7192013-10-04 19:53:11 +0100828}
829
Damien George41125902015-03-26 16:44:14 +0000830void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000831 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100832 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100833}
834
Damien George41125902015-03-26 16:44:14 +0000835void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000836 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100837 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100838}
839
Damien George41125902015-03-26 16:44:14 +0000840void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000841 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100842 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100843}
844
Damien George41125902015-03-26 16:44:14 +0000845void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000846 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100847 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100848}
849
Damien George41125902015-03-26 16:44:14 +0000850void mp_emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000851 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100852 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100853}
854
Damien George41125902015-03-26 16:44:14 +0000855void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000856 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100857 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100858}
859
Damien Georgee37dcaa2014-12-27 17:07:16 +0000860#if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000861void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000862 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100863 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100864}
865
Damien George41125902015-03-26 16:44:14 +0000866void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000867 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100868 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100869}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000870#endif
Damien429d7192013-10-04 19:53:11 +0100871
Damien George83204f32014-12-27 17:20:41 +0000872#if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000873void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000874 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100875 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100876}
Damien George83204f32014-12-27 17:20:41 +0000877#endif
Damien429d7192013-10-04 19:53:11 +0100878
Damien George41125902015-03-26 16:44:14 +0000879void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000880 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100881 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100882}
883
Damien George41125902015-03-26 16:44:14 +0000884void 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 +0000885 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100886 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100887}
888
Damien George41125902015-03-26 16:44:14 +0000889void 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 +0100890 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000891 emit_bc_pre(emit, 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000892 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000893 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100894 emit_bc_pre(emit, -1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000895 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200896 }
Damien429d7192013-10-04 19:53:11 +0100897}
898
Damien George41125902015-03-26 16:44:14 +0000899void 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 +0100900 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100901 emit_bc_pre(emit, -n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000902 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100903 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200904 } else {
Damien George3558f622014-04-20 17:50:40 +0100905 assert(n_closed_over <= 255);
906 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000907 emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Damien George3417bc22014-05-10 10:36:38 +0100908 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200909 }
Damien429d7192013-10-04 19:53:11 +0100910}
911
Damien George7ff996c2014-09-08 23:05:16 +0100912STATIC 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 +0100913 if (star_flags) {
Damien George7ff996c2014-09-08 23:05:16 +0100914 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 +0100915 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 +0100916 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100917 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100918 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 +0100919 }
Damien George523b5752014-03-31 11:59:23 +0100920}
921
Damien George41125902015-03-26 16:44:14 +0000922void 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 +0100923 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100924}
925
Damien George41125902015-03-26 16:44:14 +0000926void 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 +0100927 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100928}
929
Damien George41125902015-03-26 16:44:14 +0000930void mp_emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000931 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100932 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100933 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100934}
935
Damien George41125902015-03-26 16:44:14 +0000936void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000937 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000938 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100939 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100940}
941
Damien George41125902015-03-26 16:44:14 +0000942void mp_emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000943 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100944 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100945 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100946}
947
Damien George41125902015-03-26 16:44:14 +0000948void mp_emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000949 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100950 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100951 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100952}
953
Damien George41125902015-03-26 16:44:14 +0000954void mp_emit_bc_start_except_handler(emit_t *emit) {
955 mp_emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
Damien Georgeb601d952014-06-30 05:17:25 +0100956}
957
Damien George41125902015-03-26 16:44:14 +0000958void mp_emit_bc_end_except_handler(emit_t *emit) {
959 mp_emit_bc_adjust_stack_size(emit, -5); // stack adjust
Damien Georgeb601d952014-06-30 05:17:25 +0100960}
961
Damien George41125902015-03-26 16:44:14 +0000962#if MICROPY_EMIT_NATIVE
Damien6cdd3af2013-10-05 18:08:26 +0100963const emit_method_table_t emit_bc_method_table = {
Damien George41125902015-03-26 16:44:14 +0000964 mp_emit_bc_set_native_type,
965 mp_emit_bc_start_pass,
966 mp_emit_bc_end_pass,
967 mp_emit_bc_last_emit_was_return_value,
968 mp_emit_bc_adjust_stack_size,
969 mp_emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100970
Damien George542bd6b2015-03-26 14:42:40 +0000971 {
Damien George41125902015-03-26 16:44:14 +0000972 mp_emit_bc_load_fast,
973 mp_emit_bc_load_deref,
974 mp_emit_bc_load_name,
975 mp_emit_bc_load_global,
Damien George542bd6b2015-03-26 14:42:40 +0000976 },
977 {
Damien George41125902015-03-26 16:44:14 +0000978 mp_emit_bc_store_fast,
979 mp_emit_bc_store_deref,
980 mp_emit_bc_store_name,
981 mp_emit_bc_store_global,
Damien George542bd6b2015-03-26 14:42:40 +0000982 },
983 {
Damien George41125902015-03-26 16:44:14 +0000984 mp_emit_bc_delete_fast,
985 mp_emit_bc_delete_deref,
986 mp_emit_bc_delete_name,
987 mp_emit_bc_delete_global,
Damien George542bd6b2015-03-26 14:42:40 +0000988 },
Damien4b03e772013-10-05 14:17:09 +0100989
Damien George41125902015-03-26 16:44:14 +0000990 mp_emit_bc_label_assign,
991 mp_emit_bc_import_name,
992 mp_emit_bc_import_from,
993 mp_emit_bc_import_star,
994 mp_emit_bc_load_const_tok,
995 mp_emit_bc_load_const_small_int,
996 mp_emit_bc_load_const_str,
997 mp_emit_bc_load_const_obj,
998 mp_emit_bc_load_null,
999 mp_emit_bc_load_attr,
1000 mp_emit_bc_load_method,
1001 mp_emit_bc_load_build_class,
1002 mp_emit_bc_load_subscr,
1003 mp_emit_bc_store_attr,
1004 mp_emit_bc_store_subscr,
1005 mp_emit_bc_delete_attr,
1006 mp_emit_bc_delete_subscr,
1007 mp_emit_bc_dup_top,
1008 mp_emit_bc_dup_top_two,
1009 mp_emit_bc_pop_top,
1010 mp_emit_bc_rot_two,
1011 mp_emit_bc_rot_three,
1012 mp_emit_bc_jump,
1013 mp_emit_bc_pop_jump_if,
1014 mp_emit_bc_jump_if_or_pop,
1015 mp_emit_bc_unwind_jump,
1016 mp_emit_bc_unwind_jump,
1017 mp_emit_bc_setup_with,
1018 mp_emit_bc_with_cleanup,
1019 mp_emit_bc_setup_except,
1020 mp_emit_bc_setup_finally,
1021 mp_emit_bc_end_finally,
1022 mp_emit_bc_get_iter,
1023 mp_emit_bc_for_iter,
1024 mp_emit_bc_for_iter_end,
1025 mp_emit_bc_pop_block,
1026 mp_emit_bc_pop_except,
1027 mp_emit_bc_unary_op,
1028 mp_emit_bc_binary_op,
1029 mp_emit_bc_build_tuple,
1030 mp_emit_bc_build_list,
1031 mp_emit_bc_list_append,
1032 mp_emit_bc_build_map,
1033 mp_emit_bc_store_map,
1034 mp_emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00001035 #if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +00001036 mp_emit_bc_build_set,
1037 mp_emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00001038 #endif
Damien George83204f32014-12-27 17:20:41 +00001039 #if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +00001040 mp_emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +00001041 #endif
Damien George41125902015-03-26 16:44:14 +00001042 mp_emit_bc_unpack_sequence,
1043 mp_emit_bc_unpack_ex,
1044 mp_emit_bc_make_function,
1045 mp_emit_bc_make_closure,
1046 mp_emit_bc_call_function,
1047 mp_emit_bc_call_method,
1048 mp_emit_bc_return_value,
1049 mp_emit_bc_raise_varargs,
1050 mp_emit_bc_yield_value,
1051 mp_emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01001052
Damien George41125902015-03-26 16:44:14 +00001053 mp_emit_bc_start_except_handler,
1054 mp_emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +01001055};
Damien George41125902015-03-26 16:44:14 +00001056#else
1057const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
1058 mp_emit_bc_load_fast,
1059 mp_emit_bc_load_deref,
1060 mp_emit_bc_load_name,
1061 mp_emit_bc_load_global,
1062};
1063
1064const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
1065 mp_emit_bc_store_fast,
1066 mp_emit_bc_store_deref,
1067 mp_emit_bc_store_name,
1068 mp_emit_bc_store_global,
1069};
1070
1071const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
1072 mp_emit_bc_delete_fast,
1073 mp_emit_bc_delete_deref,
1074 mp_emit_bc_delete_name,
1075 mp_emit_bc_delete_global,
1076};
1077#endif
Damien Georgedd5353a2015-12-18 12:35:44 +00001078
1079#endif //MICROPY_ENABLE_COMPILER