blob: 3914a9dcbfcd969ca6af8d7b501dcccf591f2864 [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 George51dfcb42015-01-01 20:27:54 +000033#include "py/emit.h"
34#include "py/bc0.h"
Damien429d7192013-10-04 19:53:11 +010035
Damien George5f6a25f2014-04-20 18:02:27 +010036#if !MICROPY_EMIT_CPYTHON
37
Damien George95977712014-05-10 18:07:08 +010038#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
39#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
40
Damien415eb6f2013-10-05 12:19:06 +010041struct _emit_t {
Damien George0fb80c32014-05-10 18:16:21 +010042 pass_kind_t pass : 8;
Damien George7ff996c2014-09-08 23:05:16 +010043 mp_uint_t last_emit_was_return_value : 8;
Damien George0fb80c32014-05-10 18:16:21 +010044
Damien429d7192013-10-04 19:53:11 +010045 int stack_size;
Damien429d7192013-10-04 19:53:11 +010046
47 scope_t *scope;
48
Damien George7ff996c2014-09-08 23:05:16 +010049 mp_uint_t last_source_line_offset;
50 mp_uint_t last_source_line;
Damien George08335002014-01-18 23:24:36 +000051
Damien George7ff996c2014-09-08 23:05:16 +010052 mp_uint_t max_num_labels;
53 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010054
Damien George7ff996c2014-09-08 23:05:16 +010055 mp_uint_t code_info_offset;
56 mp_uint_t code_info_size;
57 mp_uint_t bytecode_offset;
58 mp_uint_t bytecode_size;
Damien George08335002014-01-18 23:24:36 +000059 byte *code_base; // stores both byte code and code info
Damien George7ff996c2014-09-08 23:05:16 +010060 // Accessed as mp_uint_t, so must be aligned as such
Paul Sokolovsky58c95862014-07-12 14:51:48 +030061 byte dummy_data[DUMMY_DATA_SIZE];
Damien429d7192013-10-04 19:53:11 +010062};
63
Damien George1d24ea52014-04-08 21:11:49 +010064STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010065STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010066
Damien George7ff996c2014-09-08 23:05:16 +010067emit_t *emit_bc_new(mp_uint_t max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000068 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010069 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010070 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010071 return emit;
72}
Damien4b03e772013-10-05 14:17:09 +010073
Damien George41d02b62014-01-24 22:42:28 +000074void emit_bc_free(emit_t *emit) {
Damien George7ff996c2014-09-08 23:05:16 +010075 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020076 m_del_obj(emit_t, emit);
77}
78
Damien Georgeb534e1b2014-09-04 14:44:01 +010079STATIC void emit_write_uint(emit_t* emit, byte*(*allocator)(emit_t*, int), mp_uint_t val) {
80 // We store each 7 bits in a separate byte, and that's how many bytes needed
81 byte buf[BYTES_FOR_INT];
82 byte *p = buf + sizeof(buf);
83 // We encode in little-ending order, but store in big-endian, to help decoding
84 do {
85 *--p = val & 0x7f;
86 val >>= 7;
87 } while (val != 0);
88 byte* c = allocator(emit, buf + sizeof(buf) - p);
89 while (p != buf + sizeof(buf) - 1) {
90 *c++ = *p++ | 0x80;
91 }
92 *c = *p;
93}
94
Damien George08335002014-01-18 23:24:36 +000095// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020096STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010097 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010098 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +000099 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100100 return emit->dummy_data;
101 } else {
Damien George08335002014-01-18 23:24:36 +0000102 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
103 byte *c = emit->code_base + emit->code_info_offset;
104 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100105 return c;
106 }
107}
108
Damien Georgedf8127a2014-04-13 11:04:33 +0100109STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100110 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 +0100111}
112
Damien Georgeb534e1b2014-09-04 14:44:01 +0100113STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
114 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
115}
116
117STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qst) {
118 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien George08335002014-01-18 23:24:36 +0000119}
120
Damien George73496fb2014-04-13 14:51:56 +0100121#if MICROPY_ENABLE_SOURCE_LINE
Damien George7ff996c2014-09-08 23:05:16 +0100122STATIC 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 +0100123 assert(bytes_to_skip > 0 || lines_to_skip > 0);
Damien George4747bec2014-07-31 16:12:01 +0000124 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George73496fb2014-04-13 14:51:56 +0100125 while (bytes_to_skip > 0 || lines_to_skip > 0) {
Damien George4747bec2014-07-31 16:12:01 +0000126 mp_uint_t b, l;
127 if (lines_to_skip <= 6) {
128 // use 0b0LLBBBBB encoding
129 b = MIN(bytes_to_skip, 0x1f);
130 l = MIN(lines_to_skip, 0x3);
131 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
132 } else {
133 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
134 b = MIN(bytes_to_skip, 0xf);
135 l = MIN(lines_to_skip, 0x7ff);
136 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
137 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
138 ci[1] = l;
139 }
Damien George73496fb2014-04-13 14:51:56 +0100140 bytes_to_skip -= b;
141 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000142 }
Damien George08335002014-01-18 23:24:36 +0000143}
Damien George73496fb2014-04-13 14:51:56 +0100144#endif
Damien George08335002014-01-18 23:24:36 +0000145
146// all functions must go through this one to emit byte code
Damien George3417bc22014-05-10 10:36:38 +0100147STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000148 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100149 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100150 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000151 return emit->dummy_data;
152 } else {
Damien George3417bc22014-05-10 10:36:38 +0100153 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
154 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
155 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000156 return c;
157 }
158}
159
Damien George3417bc22014-05-10 10:36:38 +0100160STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100161 emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100162}
163
Damien George3417bc22014-05-10 10:36:38 +0100164STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
165 byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100166 c[0] = b1;
167}
168
Damien Georgeb534e1b2014-09-04 14:44:01 +0100169STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) {
170 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
171}
172
Damien George7ff996c2014-09-08 23:05:16 +0100173STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100174 assert((b2 & (~0xff)) == 0);
Damien George3417bc22014-05-10 10:36:38 +0100175 byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100176 c[0] = b1;
177 c[1] = b2;
178}
179
Damien George3417bc22014-05-10 10:36:38 +0100180// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George40f3c022014-07-03 13:25:24 +0100181STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100182 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200183
184 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100185 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200186 byte *p = buf + sizeof(buf);
187 // We encode in little-ending order, but store in big-endian, to help decoding
188 do {
189 *--p = num & 0x7f;
190 num >>= 7;
191 } while (num != 0 && num != -1);
192 // Make sure that highest bit we stored (mask 0x40) matches sign
193 // of the number. If not, store extra byte just to encode sign
194 if (num == -1 && (*p & 0x40) == 0) {
195 *--p = 0x7f;
196 } else if (num == 0 && (*p & 0x40) != 0) {
197 *--p = 0;
198 }
199
Damien George3417bc22014-05-10 10:36:38 +0100200 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200201 while (p != buf + sizeof(buf) - 1) {
202 *c++ = *p++ | 0x80;
203 }
204 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100205}
206
Damien Georgeb534e1b2014-09-04 14:44:01 +0100207STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100208 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100209 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100210}
211
Damien George1084b0f2014-10-25 15:07:02 +0100212STATIC void emit_write_bytecode_prealigned_ptr(emit_t* emit, void *ptr) {
213 mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
214 // Verify thar c is already uint-aligned
215 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
216 *c = (mp_uint_t)ptr;
217}
218
Damien Georgedf8127a2014-04-13 11:04:33 +0100219// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100220STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
221 emit_write_bytecode_byte(emit, b);
222 emit_align_bytecode_to_machine_word(emit);
Damien George40f3c022014-07-03 13:25:24 +0100223 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 +0300224 // Verify thar c is already uint-aligned
225 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
Damien George40f3c022014-07-03 13:25:24 +0100226 *c = (mp_uint_t)ptr;
Damien Georgedf8127a2014-04-13 11:04:33 +0100227}
228
Damien Georgefb083ea2014-02-01 18:29:40 +0000229/* currently unused
Damien George7ff996c2014-09-08 23:05:16 +0100230STATIC 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 +0100231 emit_write_bytecode_byte(emit, b);
232 emit_write_bytecode_byte_uint(emit, num1);
233 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000234}
235*/
236
Damien George7ff996c2014-09-08 23:05:16 +0100237STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
238 emit_write_bytecode_byte_uint(emit, b, qst);
Damien429d7192013-10-04 19:53:11 +0100239}
240
Damien03c9cfb2013-11-05 22:06:08 +0000241// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George7ff996c2014-09-08 23:05:16 +0100242STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) {
243 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100244 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100245 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100246 } else {
Damien George3417bc22014-05-10 10:36:38 +0100247 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100248 }
Damien George3417bc22014-05-10 10:36:38 +0100249 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000250 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100251 c[1] = bytecode_offset;
252 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000253}
254
255// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George7ff996c2014-09-08 23:05:16 +0100256STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100257 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100258 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100259 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000260 } else {
Damien George3417bc22014-05-10 10:36:38 +0100261 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000262 }
Damien George3417bc22014-05-10 10:36:38 +0100263 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000264 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100265 c[1] = bytecode_offset;
266 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100267}
268
Damien George2ac4af62014-08-15 16:45:41 +0100269STATIC void emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien George6baf76e2013-12-30 22:32:17 +0000270}
271
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200272STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000273 emit->pass = pass;
274 emit->stack_size = 0;
275 emit->last_emit_was_return_value = false;
276 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000277 emit->last_source_line_offset = 0;
278 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100279 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100280 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000281 }
Damien George3417bc22014-05-10 10:36:38 +0100282 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000283 emit->code_info_offset = 0;
284
Damien Georgeb534e1b2014-09-04 14:44:01 +0100285 // Write code info size as compressed uint. If we are not in the final pass
286 // then space for this uint is reserved in emit_bc_end_pass.
287 if (pass == MP_PASS_EMIT) {
288 emit_write_code_info_uint(emit, emit->code_info_size);
Damien George08335002014-01-18 23:24:36 +0000289 }
290
Damien Georgeb534e1b2014-09-04 14:44:01 +0100291 // write the name and source file of this function
Damien Georgecbd2f742014-01-19 11:48:48 +0000292 emit_write_code_info_qstr(emit, scope->simple_name);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100293 emit_write_code_info_qstr(emit, scope->source_file);
Damien George6baf76e2013-12-30 22:32:17 +0000294
Damien George1084b0f2014-10-25 15:07:02 +0100295 // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
296 // we store them as full word-sized objects for efficient access in mp_setup_code_state
297 // this is the start of the prelude and is guaranteed to be aligned on a word boundary
298 {
299 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
300 emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst));
301 }
302 }
303
304 // bytecode prelude: local state size and exception stack size
Damien George8dcc0c72014-03-27 10:55:21 +0000305 {
Damien George7ff996c2014-09-08 23:05:16 +0100306 mp_uint_t n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000307 if (n_state == 0) {
308 // Need at least 1 entry in the state, in the case an exception is
309 // propagated through this function, the exception is returned in
310 // the highest slot in the state (fastn[0], see vm.c).
311 n_state = 1;
312 }
Damien Georgeb534e1b2014-09-04 14:44:01 +0100313 emit_write_bytecode_uint(emit, n_state);
314 emit_write_bytecode_uint(emit, scope->exc_stack_size);
Damien George8dcc0c72014-03-27 10:55:21 +0000315 }
316
317 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000318 int num_cell = 0;
319 for (int i = 0; i < scope->id_info_len; i++) {
320 id_info_t *id = &scope->id_info[i];
321 if (id->kind == ID_INFO_KIND_CELL) {
322 num_cell += 1;
323 }
324 }
325 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100326 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000327 for (int i = 0; i < scope->id_info_len; i++) {
328 id_info_t *id = &scope->id_info[i];
329 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100330 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 +0000331 }
332 }
333}
334
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200335STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000336 // check stack is back to zero size
337 if (emit->stack_size != 0) {
338 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
339 }
340
Damien George73496fb2014-04-13 14:51:56 +0100341 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George08335002014-01-18 23:24:36 +0000342
Damien George36db6bc2014-05-07 17:24:22 +0100343 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100344 // Need to make sure we have enough room in the code-info block to write
345 // the size of the code-info block. Since the size is written as a
346 // compressed uint, we don't know its size until we write it! Thus, we
347 // take the biggest possible value it could be and write that here.
348 // Then there will be enough room to write the value, and any leftover
349 // space will be absorbed in the alignment at the end of the code-info
350 // block.
351 mp_uint_t max_code_info_size =
352 emit->code_info_offset // current code-info size
353 + BYTES_FOR_INT // maximum space for compressed uint
354 + BYTES_PER_WORD - 1; // maximum space for alignment padding
355 emit_write_code_info_uint(emit, max_code_info_size);
356
357 // Align code-info so that following bytecode is aligned on a machine word.
358 // We don't need to write anything here, it's just dead space between the
359 // code-info block and the bytecode block that follows it.
360 emit_align_code_info_to_machine_word(emit);
361
362 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000363 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100364 emit->bytecode_size = emit->bytecode_offset;
365 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000366
Damien George36db6bc2014-05-07 17:24:22 +0100367 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100368 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
369 emit->code_info_size + emit->bytecode_size,
Damien George1084b0f2014-10-25 15:07:02 +0100370 emit->scope->num_pos_args, emit->scope->num_kwonly_args,
Damien George2827d622014-04-27 15:50:52 +0100371 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000372 }
373}
374
Damien George069a35e2014-04-10 17:22:19 +0000375STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100376 return emit->last_emit_was_return_value;
377}
378
Damien George7ff996c2014-09-08 23:05:16 +0100379STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000380 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100381}
382
Damien George7ff996c2014-09-08 23:05:16 +0100383STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100384 //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 +0000385#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300386 if (mp_optimise_value >= 3) {
387 // If we compile with -O3, don't store line numbers.
388 return;
389 }
Damien George08335002014-01-18 23:24:36 +0000390 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100391 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
392 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000393 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100394 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000395 emit->last_source_line = source_line;
396 }
Damien George62ad1892014-01-29 21:51:51 +0000397#endif
Damien George08335002014-01-18 23:24:36 +0000398}
399
Damien George7ff996c2014-09-08 23:05:16 +0100400STATIC void emit_bc_load_id(emit_t *emit, qstr qst) {
401 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100402}
403
Damien George7ff996c2014-09-08 23:05:16 +0100404STATIC void emit_bc_store_id(emit_t *emit, qstr qst) {
405 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100406}
407
Damien George7ff996c2014-09-08 23:05:16 +0100408STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) {
409 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100410}
411
Damien George7ff996c2014-09-08 23:05:16 +0100412STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
413 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100414 emit->stack_size += stack_size_delta;
415 if (emit->stack_size > emit->scope->stack_size) {
416 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100417 }
418 emit->last_emit_was_return_value = false;
419}
420
Damien George7ff996c2014-09-08 23:05:16 +0100421STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000422 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100423 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100424 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100425 // assign label offset
426 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100427 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100428 } else {
429 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100430 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
431 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100432 }
433}
434
Damien George7ff996c2014-09-08 23:05:16 +0100435STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000436 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100437 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100438}
439
Damien George7ff996c2014-09-08 23:05:16 +0100440STATIC void emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000441 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100442 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100443}
444
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200445STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000446 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100447 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100448}
449
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200450STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000451 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100452 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100453 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
454 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
455 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
456 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100457 default: assert(0);
458 }
459}
460
Damien George40f3c022014-07-03 13:25:24 +0100461STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000462 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100463 if (-16 <= arg && arg <= 47) {
464 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
465 } else {
466 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
467 }
Damien429d7192013-10-04 19:53:11 +0100468}
469
Damien George7ff996c2014-09-08 23:05:16 +0100470STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000471 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100472 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Damien George7ff996c2014-09-08 23:05:16 +0100475STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000476 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100477 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Damien George7ff996c2014-09-08 23:05:16 +0100480STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000481 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100482 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100483 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100484 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100485 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100486 }
487}
488
Damien George523b5752014-03-31 11:59:23 +0100489STATIC void emit_bc_load_null(emit_t *emit) {
490 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100491 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100492};
493
Damien George7ff996c2014-09-08 23:05:16 +0100494STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100495 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000496 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100497 if (local_num <= 15) {
498 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
499 } else {
500 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100501 }
502}
503
Damien George7ff996c2014-09-08 23:05:16 +0100504STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000505 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100506 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000507}
508
Damien George7ff996c2014-09-08 23:05:16 +0100509STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000510 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100511 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100512}
513
Damien George7ff996c2014-09-08 23:05:16 +0100514STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000515 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100516 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100517}
518
Damien George7ff996c2014-09-08 23:05:16 +0100519STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000520 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100521 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100522}
523
Damien George7ff996c2014-09-08 23:05:16 +0100524STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000525 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100526 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100527}
528
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200529STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000530 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100531 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100532}
533
Damien George729f7b42014-04-17 22:10:53 +0100534STATIC void emit_bc_load_subscr(emit_t *emit) {
535 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100536 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100537}
538
Damien George7ff996c2014-09-08 23:05:16 +0100539STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100540 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000541 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100542 if (local_num <= 15) {
543 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
544 } else {
545 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100546 }
547}
548
Damien George7ff996c2014-09-08 23:05:16 +0100549STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000550 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100551 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000552}
553
Damien George7ff996c2014-09-08 23:05:16 +0100554STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000555 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100556 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100557}
558
Damien George7ff996c2014-09-08 23:05:16 +0100559STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000560 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100561 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100562}
563
Damien George7ff996c2014-09-08 23:05:16 +0100564STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000565 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100566 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100567}
568
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200569STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000570 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100571 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100572}
573
Damien George7ff996c2014-09-08 23:05:16 +0100574STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100575 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100576}
577
Damien George7ff996c2014-09-08 23:05:16 +0100578STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100579 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000580}
581
Damien George7ff996c2014-09-08 23:05:16 +0100582STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000583 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100584 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damien George7ff996c2014-09-08 23:05:16 +0100587STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000588 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100589 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100590}
591
Damien George7ff996c2014-09-08 23:05:16 +0100592STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
Damien George1d24ea52014-04-08 21:11:49 +0100593 emit_bc_load_null(emit);
594 emit_bc_rot_two(emit);
Damien George7ff996c2014-09-08 23:05:16 +0100595 emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100599 emit_bc_load_null(emit);
600 emit_bc_rot_three(emit);
601 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100602}
603
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200604STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000605 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100606 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100607}
608
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200609STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000610 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100611 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200614STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000615 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100616 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100617}
618
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200619STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000620 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100621 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100622}
623
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200624STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000625 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100626 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100627}
628
Damien George7ff996c2014-09-08 23:05:16 +0100629STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000630 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100631 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100632}
633
Damien George7ff996c2014-09-08 23:05:16 +0100634STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000635 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100636 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100637}
638
Damien George7ff996c2014-09-08 23:05:16 +0100639STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000640 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100641 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100642}
643
Damien George7ff996c2014-09-08 23:05:16 +0100644STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000645 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100646 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100647}
648
Damien George7ff996c2014-09-08 23:05:16 +0100649STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000650 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100651 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100652}
653
Damien George7ff996c2014-09-08 23:05:16 +0100654STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000655 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000656 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100657 if (label & MP_EMIT_BREAK_FROM_FOR) {
658 // need to pop the iterator if we are breaking out of a for loop
659 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
660 }
661 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
662 } else {
663 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
664 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000665 }
Damien429d7192013-10-04 19:53:11 +0100666}
667
Damien George7ff996c2014-09-08 23:05:16 +0100668STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000669 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100670 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100671}
672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200673STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000674 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100675 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100676}
677
Damien George7ff996c2014-09-08 23:05:16 +0100678STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000679 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100680 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100681}
682
Damien George7ff996c2014-09-08 23:05:16 +0100683STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000684 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100685 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100686}
687
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200688STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000689 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100690 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100691}
692
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200693STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000694 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100695 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100696}
697
Damien George7ff996c2014-09-08 23:05:16 +0100698STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000699 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100700 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100701}
702
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200703STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000704 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100705}
706
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200707STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000708 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100709 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100710}
711
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200712STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000713 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100714 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100715}
716
Damien Georged17926d2014-03-30 13:35:08 +0100717STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
718 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000719 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100720 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000721 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100722 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000723 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000724 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100725 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000726 }
Damien429d7192013-10-04 19:53:11 +0100727}
728
Damien Georged17926d2014-03-30 13:35:08 +0100729STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000730 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100731 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000732 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100733 op = MP_BINARY_OP_IN;
734 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000735 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100736 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000737 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000738 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100739 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000740 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000741 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100742 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000743 }
Damien429d7192013-10-04 19:53:11 +0100744}
745
Damien George7ff996c2014-09-08 23:05:16 +0100746STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000747 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100748 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100749}
750
Damien George7ff996c2014-09-08 23:05:16 +0100751STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000752 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100753 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100754}
755
Damien George7ff996c2014-09-08 23:05:16 +0100756STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000757 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100758 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100759}
760
Damien George7ff996c2014-09-08 23:05:16 +0100761STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000762 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100763 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100764}
765
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200766STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000767 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100768 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100769}
770
Damien George7ff996c2014-09-08 23:05:16 +0100771STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000772 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100773 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100774}
775
Damien Georgee37dcaa2014-12-27 17:07:16 +0000776#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +0100777STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000778 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100779 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100780}
781
Damien George7ff996c2014-09-08 23:05:16 +0100782STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000783 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100784 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100785}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000786#endif
Damien429d7192013-10-04 19:53:11 +0100787
Damien George83204f32014-12-27 17:20:41 +0000788#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +0100789STATIC void emit_bc_build_slice(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_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100792}
Damien George83204f32014-12-27 17:20:41 +0000793#endif
Damien429d7192013-10-04 19:53:11 +0100794
Damien George7ff996c2014-09-08 23:05:16 +0100795STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000796 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100797 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100798}
799
Damien George7ff996c2014-09-08 23:05:16 +0100800STATIC void emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000801 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100802 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100803}
804
Damien George7ff996c2014-09-08 23:05:16 +0100805STATIC void 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 +0100806 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000807 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100808 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000809 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100810 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100811 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200812 }
Damien429d7192013-10-04 19:53:11 +0100813}
814
Damien George7ff996c2014-09-08 23:05:16 +0100815STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100816 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100817 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100818 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
819 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200820 } else {
Damien George3558f622014-04-20 17:50:40 +0100821 assert(n_closed_over <= 255);
822 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100823 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
824 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200825 }
Damien429d7192013-10-04 19:53:11 +0100826}
827
Damien George7ff996c2014-09-08 23:05:16 +0100828STATIC 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 +0100829 if (star_flags) {
830 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100831 // load dummy entry for non-existent pos_seq
832 emit_bc_load_null(emit);
833 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100834 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100835 // load dummy entry for non-existent kw_dict
836 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100837 }
Damien George7ff996c2014-09-08 23:05:16 +0100838 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 +0100839 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 +0100840 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100841 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100842 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 +0100843 }
Damien George523b5752014-03-31 11:59:23 +0100844}
845
Damien George7ff996c2014-09-08 23:05:16 +0100846STATIC void 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 +0100847 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100848}
849
Damien George7ff996c2014-09-08 23:05:16 +0100850STATIC void 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 +0100851 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100852}
853
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200854STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000855 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100856 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100857 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100858}
859
Damien George7ff996c2014-09-08 23:05:16 +0100860STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000861 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000862 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100863 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100864}
865
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200866STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000867 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100868 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100869 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100870}
871
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200872STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000873 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100874 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100875 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100876}
877
Damien Georgeb601d952014-06-30 05:17:25 +0100878STATIC void emit_bc_start_except_handler(emit_t *emit) {
879 emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
880}
881
882STATIC void emit_bc_end_except_handler(emit_t *emit) {
883 emit_bc_adjust_stack_size(emit, -5); // stack adjust
884}
885
Damien6cdd3af2013-10-05 18:08:26 +0100886const emit_method_table_t emit_bc_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100887 emit_bc_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100888 emit_bc_start_pass,
889 emit_bc_end_pass,
890 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000891 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000892 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100893
Damien4b03e772013-10-05 14:17:09 +0100894 emit_bc_load_id,
895 emit_bc_store_id,
896 emit_bc_delete_id,
897
Damien415eb6f2013-10-05 12:19:06 +0100898 emit_bc_label_assign,
899 emit_bc_import_name,
900 emit_bc_import_from,
901 emit_bc_import_star,
902 emit_bc_load_const_tok,
903 emit_bc_load_const_small_int,
904 emit_bc_load_const_int,
905 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100906 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100907 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100908 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100909 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000910 emit_bc_load_name,
911 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100912 emit_bc_load_attr,
913 emit_bc_load_method,
914 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100915 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100916 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000917 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100918 emit_bc_store_name,
919 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100920 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100921 emit_bc_store_subscr,
922 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000923 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100924 emit_bc_delete_name,
925 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100926 emit_bc_delete_attr,
927 emit_bc_delete_subscr,
928 emit_bc_dup_top,
929 emit_bc_dup_top_two,
930 emit_bc_pop_top,
931 emit_bc_rot_two,
932 emit_bc_rot_three,
933 emit_bc_jump,
934 emit_bc_pop_jump_if_true,
935 emit_bc_pop_jump_if_false,
936 emit_bc_jump_if_true_or_pop,
937 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000938 emit_bc_unwind_jump,
939 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100940 emit_bc_setup_with,
941 emit_bc_with_cleanup,
942 emit_bc_setup_except,
943 emit_bc_setup_finally,
944 emit_bc_end_finally,
945 emit_bc_get_iter,
946 emit_bc_for_iter,
947 emit_bc_for_iter_end,
948 emit_bc_pop_block,
949 emit_bc_pop_except,
950 emit_bc_unary_op,
951 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100952 emit_bc_build_tuple,
953 emit_bc_build_list,
954 emit_bc_list_append,
955 emit_bc_build_map,
956 emit_bc_store_map,
957 emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000958 #if MICROPY_PY_BUILTINS_SET
Damien415eb6f2013-10-05 12:19:06 +0100959 emit_bc_build_set,
960 emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000961 #endif
Damien George83204f32014-12-27 17:20:41 +0000962 #if MICROPY_PY_BUILTINS_SLICE
Damien415eb6f2013-10-05 12:19:06 +0100963 emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +0000964 #endif
Damien415eb6f2013-10-05 12:19:06 +0100965 emit_bc_unpack_sequence,
966 emit_bc_unpack_ex,
967 emit_bc_make_function,
968 emit_bc_make_closure,
969 emit_bc_call_function,
970 emit_bc_call_method,
971 emit_bc_return_value,
972 emit_bc_raise_varargs,
973 emit_bc_yield_value,
974 emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100975
976 emit_bc_start_except_handler,
977 emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100978};
Damien George5f6a25f2014-04-20 18:02:27 +0100979
980#endif // !MICROPY_EMIT_CPYTHON