blob: f55fa964be2c8e0a760a65bb9bb3c0bdc7845e70 [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 George5f6a25f2014-04-20 18:02:27 +010037#if !MICROPY_EMIT_CPYTHON
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 George0fb80c32014-05-10 18:16:21 +010043 pass_kind_t pass : 8;
Damien George7ff996c2014-09-08 23:05:16 +010044 mp_uint_t last_emit_was_return_value : 8;
Damien George0fb80c32014-05-10 18:16:21 +010045
Damien429d7192013-10-04 19:53:11 +010046 int stack_size;
Damien429d7192013-10-04 19:53:11 +010047
48 scope_t *scope;
49
Damien George7ff996c2014-09-08 23:05:16 +010050 mp_uint_t last_source_line_offset;
51 mp_uint_t last_source_line;
Damien George08335002014-01-18 23:24:36 +000052
Damien George7ff996c2014-09-08 23:05:16 +010053 mp_uint_t max_num_labels;
54 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010055
Damien George7ff996c2014-09-08 23:05:16 +010056 mp_uint_t code_info_offset;
57 mp_uint_t code_info_size;
58 mp_uint_t bytecode_offset;
59 mp_uint_t bytecode_size;
Damien George08335002014-01-18 23:24:36 +000060 byte *code_base; // stores both byte code and code info
Damien George7ff996c2014-09-08 23:05:16 +010061 // Accessed as mp_uint_t, so must be aligned as such
Paul Sokolovsky58c95862014-07-12 14:51:48 +030062 byte dummy_data[DUMMY_DATA_SIZE];
Damien429d7192013-10-04 19:53:11 +010063};
64
Damien Georgea210c772015-03-26 15:49:53 +000065emit_t *emit_bc_new(void) {
Damien George08335002014-01-18 23:24:36 +000066 emit_t *emit = m_new0(emit_t, 1);
Damien Georgea210c772015-03-26 15:49:53 +000067 return emit;
68}
69
70void emit_bc_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels) {
Damien6cdd3af2013-10-05 18:08:26 +010071 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010072 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010073}
Damien4b03e772013-10-05 14:17:09 +010074
Damien George41d02b62014-01-24 22:42:28 +000075void emit_bc_free(emit_t *emit) {
Damien George7ff996c2014-09-08 23:05:16 +010076 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020077 m_del_obj(emit_t, emit);
78}
79
Damien Georgeb534e1b2014-09-04 14:44:01 +010080STATIC void emit_write_uint(emit_t* emit, byte*(*allocator)(emit_t*, int), mp_uint_t val) {
81 // We store each 7 bits in a separate byte, and that's how many bytes needed
82 byte buf[BYTES_FOR_INT];
83 byte *p = buf + sizeof(buf);
84 // We encode in little-ending order, but store in big-endian, to help decoding
85 do {
86 *--p = val & 0x7f;
87 val >>= 7;
88 } while (val != 0);
89 byte* c = allocator(emit, buf + sizeof(buf) - p);
90 while (p != buf + sizeof(buf) - 1) {
91 *c++ = *p++ | 0x80;
92 }
93 *c = *p;
94}
95
Damien George08335002014-01-18 23:24:36 +000096// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020097STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010098 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010099 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +0000100 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100101 return emit->dummy_data;
102 } else {
Damien George08335002014-01-18 23:24:36 +0000103 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
104 byte *c = emit->code_base + emit->code_info_offset;
105 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100106 return c;
107 }
108}
109
Damien Georgedf8127a2014-04-13 11:04:33 +0100110STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100111 emit->code_info_offset = (emit->code_info_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100112}
113
Damien Georgeb534e1b2014-09-04 14:44:01 +0100114STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
115 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
116}
117
118STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qst) {
119 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien George08335002014-01-18 23:24:36 +0000120}
121
Damien George73496fb2014-04-13 14:51:56 +0100122#if MICROPY_ENABLE_SOURCE_LINE
Damien George7ff996c2014-09-08 23:05:16 +0100123STATIC 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 +0100124 assert(bytes_to_skip > 0 || lines_to_skip > 0);
Damien George4747bec2014-07-31 16:12:01 +0000125 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George73496fb2014-04-13 14:51:56 +0100126 while (bytes_to_skip > 0 || lines_to_skip > 0) {
Damien George4747bec2014-07-31 16:12:01 +0000127 mp_uint_t b, l;
128 if (lines_to_skip <= 6) {
129 // use 0b0LLBBBBB encoding
130 b = MIN(bytes_to_skip, 0x1f);
131 l = MIN(lines_to_skip, 0x3);
132 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
133 } else {
134 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
135 b = MIN(bytes_to_skip, 0xf);
136 l = MIN(lines_to_skip, 0x7ff);
137 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
138 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
139 ci[1] = l;
140 }
Damien George73496fb2014-04-13 14:51:56 +0100141 bytes_to_skip -= b;
142 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000143 }
Damien George08335002014-01-18 23:24:36 +0000144}
Damien George73496fb2014-04-13 14:51:56 +0100145#endif
Damien George08335002014-01-18 23:24:36 +0000146
147// all functions must go through this one to emit byte code
Damien George3417bc22014-05-10 10:36:38 +0100148STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000149 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100150 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100151 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000152 return emit->dummy_data;
153 } else {
Damien George3417bc22014-05-10 10:36:38 +0100154 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
155 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
156 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000157 return c;
158 }
159}
160
Damien George3417bc22014-05-10 10:36:38 +0100161STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100162 emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100163}
164
Damien George3417bc22014-05-10 10:36:38 +0100165STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
166 byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100167 c[0] = b1;
168}
169
Damien Georgeb534e1b2014-09-04 14:44:01 +0100170STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) {
171 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
172}
173
Damien George7ff996c2014-09-08 23:05:16 +0100174STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100175 assert((b2 & (~0xff)) == 0);
Damien George3417bc22014-05-10 10:36:38 +0100176 byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100177 c[0] = b1;
178 c[1] = b2;
179}
180
Damien George3417bc22014-05-10 10:36:38 +0100181// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George40f3c022014-07-03 13:25:24 +0100182STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100183 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200184
185 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100186 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200187 byte *p = buf + sizeof(buf);
188 // We encode in little-ending order, but store in big-endian, to help decoding
189 do {
190 *--p = num & 0x7f;
191 num >>= 7;
192 } while (num != 0 && num != -1);
193 // Make sure that highest bit we stored (mask 0x40) matches sign
194 // of the number. If not, store extra byte just to encode sign
195 if (num == -1 && (*p & 0x40) == 0) {
196 *--p = 0x7f;
197 } else if (num == 0 && (*p & 0x40) != 0) {
198 *--p = 0;
199 }
200
Damien George3417bc22014-05-10 10:36:38 +0100201 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200202 while (p != buf + sizeof(buf) - 1) {
203 *c++ = *p++ | 0x80;
204 }
205 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100206}
207
Damien Georgeb534e1b2014-09-04 14:44:01 +0100208STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100209 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100210 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100211}
212
Damien George1084b0f2014-10-25 15:07:02 +0100213STATIC void emit_write_bytecode_prealigned_ptr(emit_t* emit, void *ptr) {
214 mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
215 // Verify thar c is already uint-aligned
216 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
217 *c = (mp_uint_t)ptr;
218}
219
Damien Georgedf8127a2014-04-13 11:04:33 +0100220// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100221STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
222 emit_write_bytecode_byte(emit, b);
223 emit_align_bytecode_to_machine_word(emit);
Damien George40f3c022014-07-03 13:25:24 +0100224 mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
Paul Sokolovsky58c95862014-07-12 14:51:48 +0300225 // Verify thar c is already uint-aligned
226 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
Damien George40f3c022014-07-03 13:25:24 +0100227 *c = (mp_uint_t)ptr;
Damien Georgedf8127a2014-04-13 11:04:33 +0100228}
229
Damien Georgefb083ea2014-02-01 18:29:40 +0000230/* currently unused
Damien George7ff996c2014-09-08 23:05:16 +0100231STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, mp_uint_t num1, mp_uint_t num2) {
Damien George3417bc22014-05-10 10:36:38 +0100232 emit_write_bytecode_byte(emit, b);
233 emit_write_bytecode_byte_uint(emit, num1);
234 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000235}
236*/
237
Damien George7ff996c2014-09-08 23:05:16 +0100238STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
239 emit_write_bytecode_byte_uint(emit, b, qst);
Damien429d7192013-10-04 19:53:11 +0100240}
241
Damien03c9cfb2013-11-05 22:06:08 +0000242// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George7ff996c2014-09-08 23:05:16 +0100243STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) {
244 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100245 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100246 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100247 } else {
Damien George3417bc22014-05-10 10:36:38 +0100248 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100249 }
Damien George3417bc22014-05-10 10:36:38 +0100250 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000251 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100252 c[1] = bytecode_offset;
253 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000254}
255
256// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George7ff996c2014-09-08 23:05:16 +0100257STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100258 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100259 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100260 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000261 } else {
Damien George3417bc22014-05-10 10:36:38 +0100262 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000263 }
Damien George3417bc22014-05-10 10:36:38 +0100264 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000265 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100266 c[1] = bytecode_offset;
267 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100268}
269
Damien George41125902015-03-26 16:44:14 +0000270#if MICROPY_EMIT_NATIVE
271STATIC 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 +0000272 (void)emit;
273 (void)op;
274 (void)arg1;
275 (void)arg2;
Damien George6baf76e2013-12-30 22:32:17 +0000276}
Damien George41125902015-03-26 16:44:14 +0000277#endif
Damien George6baf76e2013-12-30 22:32:17 +0000278
Damien George41125902015-03-26 16:44:14 +0000279void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000280 emit->pass = pass;
281 emit->stack_size = 0;
282 emit->last_emit_was_return_value = false;
283 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000284 emit->last_source_line_offset = 0;
285 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100286 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100287 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000288 }
Damien George3417bc22014-05-10 10:36:38 +0100289 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000290 emit->code_info_offset = 0;
291
Damien Georgeb534e1b2014-09-04 14:44:01 +0100292 // Write code info size as compressed uint. If we are not in the final pass
293 // then space for this uint is reserved in emit_bc_end_pass.
294 if (pass == MP_PASS_EMIT) {
295 emit_write_code_info_uint(emit, emit->code_info_size);
Damien George08335002014-01-18 23:24:36 +0000296 }
297
Damien Georgeb534e1b2014-09-04 14:44:01 +0100298 // write the name and source file of this function
Damien Georgecbd2f742014-01-19 11:48:48 +0000299 emit_write_code_info_qstr(emit, scope->simple_name);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100300 emit_write_code_info_qstr(emit, scope->source_file);
Damien George6baf76e2013-12-30 22:32:17 +0000301
Damien George1084b0f2014-10-25 15:07:02 +0100302 // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
303 // we store them as full word-sized objects for efficient access in mp_setup_code_state
304 // this is the start of the prelude and is guaranteed to be aligned on a word boundary
305 {
306 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
307 emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst));
308 }
309 }
310
311 // bytecode prelude: local state size and exception stack size
Damien George8dcc0c72014-03-27 10:55:21 +0000312 {
Damien George7ff996c2014-09-08 23:05:16 +0100313 mp_uint_t n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000314 if (n_state == 0) {
315 // Need at least 1 entry in the state, in the case an exception is
316 // propagated through this function, the exception is returned in
317 // the highest slot in the state (fastn[0], see vm.c).
318 n_state = 1;
319 }
Damien Georgeb534e1b2014-09-04 14:44:01 +0100320 emit_write_bytecode_uint(emit, n_state);
321 emit_write_bytecode_uint(emit, scope->exc_stack_size);
Damien George8dcc0c72014-03-27 10:55:21 +0000322 }
323
324 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000325 for (int i = 0; i < scope->id_info_len; i++) {
326 id_info_t *id = &scope->id_info[i];
327 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgec9aa1882015-04-07 00:08:17 +0100328 assert(id->local_num < 255);
Damien George3417bc22014-05-10 10:36:38 +0100329 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 +0000330 }
331 }
Damien Georgec9aa1882015-04-07 00:08:17 +0100332 emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George6baf76e2013-12-30 22:32:17 +0000333}
334
Damien George41125902015-03-26 16:44:14 +0000335void mp_emit_bc_end_pass(emit_t *emit) {
Damien Georgea210c772015-03-26 15:49:53 +0000336 if (emit->pass == MP_PASS_SCOPE) {
337 return;
338 }
339
Damien George6baf76e2013-12-30 22:32:17 +0000340 // check stack is back to zero size
341 if (emit->stack_size != 0) {
342 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
343 }
344
Damien George73496fb2014-04-13 14:51:56 +0100345 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George08335002014-01-18 23:24:36 +0000346
Damien George36db6bc2014-05-07 17:24:22 +0100347 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100348 // Need to make sure we have enough room in the code-info block to write
349 // the size of the code-info block. Since the size is written as a
350 // compressed uint, we don't know its size until we write it! Thus, we
351 // take the biggest possible value it could be and write that here.
352 // Then there will be enough room to write the value, and any leftover
353 // space will be absorbed in the alignment at the end of the code-info
354 // block.
355 mp_uint_t max_code_info_size =
356 emit->code_info_offset // current code-info size
357 + BYTES_FOR_INT // maximum space for compressed uint
358 + BYTES_PER_WORD - 1; // maximum space for alignment padding
359 emit_write_code_info_uint(emit, max_code_info_size);
360
361 // Align code-info so that following bytecode is aligned on a machine word.
362 // We don't need to write anything here, it's just dead space between the
363 // code-info block and the bytecode block that follows it.
364 emit_align_code_info_to_machine_word(emit);
365
366 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000367 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100368 emit->bytecode_size = emit->bytecode_offset;
369 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000370
Damien George36db6bc2014-05-07 17:24:22 +0100371 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100372 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
373 emit->code_info_size + emit->bytecode_size,
Damien George1084b0f2014-10-25 15:07:02 +0100374 emit->scope->num_pos_args, emit->scope->num_kwonly_args,
Damien George2827d622014-04-27 15:50:52 +0100375 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000376 }
377}
378
Damien George41125902015-03-26 16:44:14 +0000379bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100380 return emit->last_emit_was_return_value;
381}
382
Damien George41125902015-03-26 16:44:14 +0000383void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000384 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100385}
386
Damien George41125902015-03-26 16:44:14 +0000387void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100388 //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 +0000389#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000390 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300391 // If we compile with -O3, don't store line numbers.
392 return;
393 }
Damien George08335002014-01-18 23:24:36 +0000394 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100395 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
396 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000397 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100398 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000399 emit->last_source_line = source_line;
400 }
Damien George62ad1892014-01-29 21:51:51 +0000401#endif
Damien George08335002014-01-18 23:24:36 +0000402}
403
Damien George7ff996c2014-09-08 23:05:16 +0100404STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georgea210c772015-03-26 15:49:53 +0000405 if (emit->pass == MP_PASS_SCOPE) {
406 return;
407 }
Damien George7ff996c2014-09-08 23:05:16 +0100408 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100409 emit->stack_size += stack_size_delta;
410 if (emit->stack_size > emit->scope->stack_size) {
411 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100412 }
413 emit->last_emit_was_return_value = false;
414}
415
Damien George41125902015-03-26 16:44:14 +0000416void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000417 emit_bc_pre(emit, 0);
Damien Georgea210c772015-03-26 15:49:53 +0000418 if (emit->pass == MP_PASS_SCOPE) {
419 return;
420 }
Damienb05d7072013-10-05 13:37:10 +0100421 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100422 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100423 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000424 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100425 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100426 } else {
427 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100428 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
429 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100430 }
431}
432
Damien George41125902015-03-26 16:44:14 +0000433void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000434 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100435 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100436}
437
Damien George41125902015-03-26 16:44:14 +0000438void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000439 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100440 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Damien George41125902015-03-26 16:44:14 +0000443void mp_emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000444 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100445 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100446}
447
Damien George41125902015-03-26 16:44:14 +0000448void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000449 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100450 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100451 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
452 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
453 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000454 no_other_choice:
Damien George3417bc22014-05-10 10:36:38 +0100455 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000456 default: assert(0); goto no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100457 }
458}
459
Damien George41125902015-03-26 16:44:14 +0000460void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000461 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100462 if (-16 <= arg && arg <= 47) {
463 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
464 } else {
465 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
466 }
Damien429d7192013-10-04 19:53:11 +0100467}
468
Damien George41125902015-03-26 16:44:14 +0000469void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000470 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100471 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100472 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100473 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100474 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100475 }
476}
477
Damien George41125902015-03-26 16:44:14 +0000478void mp_emit_bc_load_const_obj(emit_t *emit, void *obj) {
Damien Georgedab13852015-01-13 15:55:54 +0000479 emit_bc_pre(emit, 1);
480 emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
481}
482
Damien George41125902015-03-26 16:44:14 +0000483void mp_emit_bc_load_null(emit_t *emit) {
Damien George523b5752014-03-31 11:59:23 +0100484 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100485 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100486};
487
Damien George41125902015-03-26 16:44:14 +0000488void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000489 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100490 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000491 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100492 if (local_num <= 15) {
493 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
494 } else {
495 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100496 }
497}
498
Damien George41125902015-03-26 16:44:14 +0000499void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000500 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000501 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100502 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000503}
504
Damien George41125902015-03-26 16:44:14 +0000505void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000506 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000507 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100508 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000509 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
510 emit_write_bytecode_byte(emit, 0);
511 }
Damien429d7192013-10-04 19:53:11 +0100512}
513
Damien George41125902015-03-26 16:44:14 +0000514void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000515 (void)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_LOAD_GLOBAL, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000518 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
519 emit_write_bytecode_byte(emit, 0);
520 }
Damien429d7192013-10-04 19:53:11 +0100521}
522
Damien George41125902015-03-26 16:44:14 +0000523void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000524 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100525 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000526 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
527 emit_write_bytecode_byte(emit, 0);
528 }
Damien429d7192013-10-04 19:53:11 +0100529}
530
Damien George41125902015-03-26 16:44:14 +0000531void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000532 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100533 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100534}
535
Damien George41125902015-03-26 16:44:14 +0000536void mp_emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000537 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100538 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100539}
540
Damien George41125902015-03-26 16:44:14 +0000541void mp_emit_bc_load_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +0100542 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100543 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100544}
545
Damien George41125902015-03-26 16:44:14 +0000546void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000547 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100548 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000549 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100550 if (local_num <= 15) {
551 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
552 } else {
553 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100554 }
555}
556
Damien George41125902015-03-26 16:44:14 +0000557void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000558 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000559 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100560 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000561}
562
Damien George41125902015-03-26 16:44:14 +0000563void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000564 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100565 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Damien George41125902015-03-26 16:44:14 +0000568void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000569 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100570 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Damien George41125902015-03-26 16:44:14 +0000573void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000574 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100575 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000576 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
577 emit_write_bytecode_byte(emit, 0);
578 }
Damien429d7192013-10-04 19:53:11 +0100579}
580
Damien George41125902015-03-26 16:44:14 +0000581void mp_emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000582 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100583 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100584}
585
Damien George41125902015-03-26 16:44:14 +0000586void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000587 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100588 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100589}
590
Damien George41125902015-03-26 16:44:14 +0000591void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000592 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100593 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000594}
595
Damien George41125902015-03-26 16:44:14 +0000596void mp_emit_bc_delete_name(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_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Damien George41125902015-03-26 16:44:14 +0000601void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000602 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100603 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100604}
605
Damien George41125902015-03-26 16:44:14 +0000606void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
607 mp_emit_bc_load_null(emit);
608 mp_emit_bc_rot_two(emit);
609 mp_emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Damien George41125902015-03-26 16:44:14 +0000612void mp_emit_bc_delete_subscr(emit_t *emit) {
613 mp_emit_bc_load_null(emit);
614 mp_emit_bc_rot_three(emit);
615 mp_emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Damien George41125902015-03-26 16:44:14 +0000618void mp_emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000619 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100620 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100621}
622
Damien George41125902015-03-26 16:44:14 +0000623void mp_emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000624 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100625 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100626}
627
Damien George41125902015-03-26 16:44:14 +0000628void mp_emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100630 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100631}
632
Damien George41125902015-03-26 16:44:14 +0000633void mp_emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000634 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100635 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Damien George41125902015-03-26 16:44:14 +0000638void mp_emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000639 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100640 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100641}
642
Damien George41125902015-03-26 16:44:14 +0000643void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000644 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100645 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100646}
647
Damien George41125902015-03-26 16:44:14 +0000648void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000649 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000650 if (cond) {
651 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
652 } else {
653 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
654 }
Damien429d7192013-10-04 19:53:11 +0100655}
656
Damien George41125902015-03-26 16:44:14 +0000657void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000658 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000659 if (cond) {
660 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
661 } else {
662 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
663 }
Damien429d7192013-10-04 19:53:11 +0100664}
665
Damien George41125902015-03-26 16:44:14 +0000666void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000667 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000668 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100669 if (label & MP_EMIT_BREAK_FROM_FOR) {
670 // need to pop the iterator if we are breaking out of a for loop
671 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
672 }
673 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
674 } else {
675 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
676 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000677 }
Damien429d7192013-10-04 19:53:11 +0100678}
679
Damien George41125902015-03-26 16:44:14 +0000680void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000681 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100682 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien George41125902015-03-26 16:44:14 +0000685void mp_emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000686 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100687 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Damien George41125902015-03-26 16:44:14 +0000690void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000691 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100692 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100693}
694
Damien George41125902015-03-26 16:44:14 +0000695void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000696 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100697 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100698}
699
Damien George41125902015-03-26 16:44:14 +0000700void mp_emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000701 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100702 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100703}
704
Damien George41125902015-03-26 16:44:14 +0000705void mp_emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000706 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100707 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100708}
709
Damien George41125902015-03-26 16:44:14 +0000710void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000711 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100712 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100713}
714
Damien George41125902015-03-26 16:44:14 +0000715void mp_emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000716 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100717}
718
Damien George41125902015-03-26 16:44:14 +0000719void mp_emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000720 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100721 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100722}
723
Damien George41125902015-03-26 16:44:14 +0000724void mp_emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000725 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100726 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100727}
728
Damien George41125902015-03-26 16:44:14 +0000729void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged17926d2014-03-30 13:35:08 +0100730 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000731 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100732 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000733 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100734 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000735 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000736 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100737 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000738 }
Damien429d7192013-10-04 19:53:11 +0100739}
740
Damien George41125902015-03-26 16:44:14 +0000741void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000742 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100743 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000744 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100745 op = MP_BINARY_OP_IN;
746 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000747 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100748 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000749 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000750 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100751 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000752 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000753 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100754 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000755 }
Damien429d7192013-10-04 19:53:11 +0100756}
757
Damien George41125902015-03-26 16:44:14 +0000758void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000759 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100760 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100761}
762
Damien George41125902015-03-26 16:44:14 +0000763void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000764 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100765 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100766}
767
Damien George41125902015-03-26 16:44:14 +0000768void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000769 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100770 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100771}
772
Damien George41125902015-03-26 16:44:14 +0000773void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000774 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100775 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100776}
777
Damien George41125902015-03-26 16:44:14 +0000778void mp_emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000779 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100780 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100781}
782
Damien George41125902015-03-26 16:44:14 +0000783void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000784 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100785 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100786}
787
Damien Georgee37dcaa2014-12-27 17:07:16 +0000788#if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000789void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000790 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100791 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100792}
793
Damien George41125902015-03-26 16:44:14 +0000794void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000795 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100796 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100797}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000798#endif
Damien429d7192013-10-04 19:53:11 +0100799
Damien George83204f32014-12-27 17:20:41 +0000800#if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000801void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000802 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100803 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100804}
Damien George83204f32014-12-27 17:20:41 +0000805#endif
Damien429d7192013-10-04 19:53:11 +0100806
Damien George41125902015-03-26 16:44:14 +0000807void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000808 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100809 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100810}
811
Damien George41125902015-03-26 16:44:14 +0000812void 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 +0000813 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100814 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100815}
816
Damien George41125902015-03-26 16:44:14 +0000817void 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 +0100818 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000819 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100820 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000821 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100822 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100823 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200824 }
Damien429d7192013-10-04 19:53:11 +0100825}
826
Damien George41125902015-03-26 16:44:14 +0000827void 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 +0100828 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100829 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100830 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
831 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200832 } else {
Damien George3558f622014-04-20 17:50:40 +0100833 assert(n_closed_over <= 255);
834 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100835 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
836 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200837 }
Damien429d7192013-10-04 19:53:11 +0100838}
839
Damien George7ff996c2014-09-08 23:05:16 +0100840STATIC 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 +0100841 if (star_flags) {
842 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100843 // load dummy entry for non-existent pos_seq
Damien George41125902015-03-26 16:44:14 +0000844 mp_emit_bc_load_null(emit);
845 mp_emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100846 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100847 // load dummy entry for non-existent kw_dict
Damien George41125902015-03-26 16:44:14 +0000848 mp_emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100849 }
Damien George7ff996c2014-09-08 23:05:16 +0100850 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 +0100851 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 +0100852 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100853 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100854 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 +0100855 }
Damien George523b5752014-03-31 11:59:23 +0100856}
857
Damien George41125902015-03-26 16:44:14 +0000858void 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 +0100859 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100860}
861
Damien George41125902015-03-26 16:44:14 +0000862void 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 +0100863 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100864}
865
Damien George41125902015-03-26 16:44:14 +0000866void mp_emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000867 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100868 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100869 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100870}
871
Damien George41125902015-03-26 16:44:14 +0000872void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000873 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000874 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100875 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100876}
877
Damien George41125902015-03-26 16:44:14 +0000878void mp_emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000879 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100880 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100881 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100882}
883
Damien George41125902015-03-26 16:44:14 +0000884void mp_emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000885 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100886 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100887 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100888}
889
Damien George41125902015-03-26 16:44:14 +0000890void mp_emit_bc_start_except_handler(emit_t *emit) {
891 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 +0100892}
893
Damien George41125902015-03-26 16:44:14 +0000894void mp_emit_bc_end_except_handler(emit_t *emit) {
895 mp_emit_bc_adjust_stack_size(emit, -5); // stack adjust
Damien Georgeb601d952014-06-30 05:17:25 +0100896}
897
Damien George41125902015-03-26 16:44:14 +0000898#if MICROPY_EMIT_NATIVE
Damien6cdd3af2013-10-05 18:08:26 +0100899const emit_method_table_t emit_bc_method_table = {
Damien George41125902015-03-26 16:44:14 +0000900 mp_emit_bc_set_native_type,
901 mp_emit_bc_start_pass,
902 mp_emit_bc_end_pass,
903 mp_emit_bc_last_emit_was_return_value,
904 mp_emit_bc_adjust_stack_size,
905 mp_emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100906
Damien George542bd6b2015-03-26 14:42:40 +0000907 {
Damien George41125902015-03-26 16:44:14 +0000908 mp_emit_bc_load_fast,
909 mp_emit_bc_load_deref,
910 mp_emit_bc_load_name,
911 mp_emit_bc_load_global,
Damien George542bd6b2015-03-26 14:42:40 +0000912 },
913 {
Damien George41125902015-03-26 16:44:14 +0000914 mp_emit_bc_store_fast,
915 mp_emit_bc_store_deref,
916 mp_emit_bc_store_name,
917 mp_emit_bc_store_global,
Damien George542bd6b2015-03-26 14:42:40 +0000918 },
919 {
Damien George41125902015-03-26 16:44:14 +0000920 mp_emit_bc_delete_fast,
921 mp_emit_bc_delete_deref,
922 mp_emit_bc_delete_name,
923 mp_emit_bc_delete_global,
Damien George542bd6b2015-03-26 14:42:40 +0000924 },
Damien4b03e772013-10-05 14:17:09 +0100925
Damien George41125902015-03-26 16:44:14 +0000926 mp_emit_bc_label_assign,
927 mp_emit_bc_import_name,
928 mp_emit_bc_import_from,
929 mp_emit_bc_import_star,
930 mp_emit_bc_load_const_tok,
931 mp_emit_bc_load_const_small_int,
932 mp_emit_bc_load_const_str,
933 mp_emit_bc_load_const_obj,
934 mp_emit_bc_load_null,
935 mp_emit_bc_load_attr,
936 mp_emit_bc_load_method,
937 mp_emit_bc_load_build_class,
938 mp_emit_bc_load_subscr,
939 mp_emit_bc_store_attr,
940 mp_emit_bc_store_subscr,
941 mp_emit_bc_delete_attr,
942 mp_emit_bc_delete_subscr,
943 mp_emit_bc_dup_top,
944 mp_emit_bc_dup_top_two,
945 mp_emit_bc_pop_top,
946 mp_emit_bc_rot_two,
947 mp_emit_bc_rot_three,
948 mp_emit_bc_jump,
949 mp_emit_bc_pop_jump_if,
950 mp_emit_bc_jump_if_or_pop,
951 mp_emit_bc_unwind_jump,
952 mp_emit_bc_unwind_jump,
953 mp_emit_bc_setup_with,
954 mp_emit_bc_with_cleanup,
955 mp_emit_bc_setup_except,
956 mp_emit_bc_setup_finally,
957 mp_emit_bc_end_finally,
958 mp_emit_bc_get_iter,
959 mp_emit_bc_for_iter,
960 mp_emit_bc_for_iter_end,
961 mp_emit_bc_pop_block,
962 mp_emit_bc_pop_except,
963 mp_emit_bc_unary_op,
964 mp_emit_bc_binary_op,
965 mp_emit_bc_build_tuple,
966 mp_emit_bc_build_list,
967 mp_emit_bc_list_append,
968 mp_emit_bc_build_map,
969 mp_emit_bc_store_map,
970 mp_emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000971 #if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000972 mp_emit_bc_build_set,
973 mp_emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000974 #endif
Damien George83204f32014-12-27 17:20:41 +0000975 #if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000976 mp_emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +0000977 #endif
Damien George41125902015-03-26 16:44:14 +0000978 mp_emit_bc_unpack_sequence,
979 mp_emit_bc_unpack_ex,
980 mp_emit_bc_make_function,
981 mp_emit_bc_make_closure,
982 mp_emit_bc_call_function,
983 mp_emit_bc_call_method,
984 mp_emit_bc_return_value,
985 mp_emit_bc_raise_varargs,
986 mp_emit_bc_yield_value,
987 mp_emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100988
Damien George41125902015-03-26 16:44:14 +0000989 mp_emit_bc_start_except_handler,
990 mp_emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100991};
Damien George41125902015-03-26 16:44:14 +0000992#else
993const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
994 mp_emit_bc_load_fast,
995 mp_emit_bc_load_deref,
996 mp_emit_bc_load_name,
997 mp_emit_bc_load_global,
998};
999
1000const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
1001 mp_emit_bc_store_fast,
1002 mp_emit_bc_store_deref,
1003 mp_emit_bc_store_name,
1004 mp_emit_bc_store_global,
1005};
1006
1007const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
1008 mp_emit_bc_delete_fast,
1009 mp_emit_bc_delete_deref,
1010 mp_emit_bc_delete_name,
1011 mp_emit_bc_delete_global,
1012};
1013#endif
Damien George5f6a25f2014-04-20 18:02:27 +01001014
1015#endif // !MICROPY_EMIT_CPYTHON