blob: 53437a8f86891d9a7bfb6ec7cdf9e2d5e94ded2c [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 George1d24ea52014-04-08 21:11:49 +010065STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010066STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010067
Damien George7ff996c2014-09-08 23:05:16 +010068emit_t *emit_bc_new(mp_uint_t max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000069 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010070 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010071 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010072 return emit;
73}
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 George2ac4af62014-08-15 16:45:41 +0100270STATIC void 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 +0000271 (void)emit;
272 (void)op;
273 (void)arg1;
274 (void)arg2;
Damien George6baf76e2013-12-30 22:32:17 +0000275}
276
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200277STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000278 emit->pass = pass;
279 emit->stack_size = 0;
280 emit->last_emit_was_return_value = false;
281 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000282 emit->last_source_line_offset = 0;
283 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100284 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100285 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000286 }
Damien George3417bc22014-05-10 10:36:38 +0100287 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000288 emit->code_info_offset = 0;
289
Damien Georgeb534e1b2014-09-04 14:44:01 +0100290 // Write code info size as compressed uint. If we are not in the final pass
291 // then space for this uint is reserved in emit_bc_end_pass.
292 if (pass == MP_PASS_EMIT) {
293 emit_write_code_info_uint(emit, emit->code_info_size);
Damien George08335002014-01-18 23:24:36 +0000294 }
295
Damien Georgeb534e1b2014-09-04 14:44:01 +0100296 // write the name and source file of this function
Damien Georgecbd2f742014-01-19 11:48:48 +0000297 emit_write_code_info_qstr(emit, scope->simple_name);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100298 emit_write_code_info_qstr(emit, scope->source_file);
Damien George6baf76e2013-12-30 22:32:17 +0000299
Damien George1084b0f2014-10-25 15:07:02 +0100300 // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
301 // we store them as full word-sized objects for efficient access in mp_setup_code_state
302 // this is the start of the prelude and is guaranteed to be aligned on a word boundary
303 {
304 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
305 emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst));
306 }
307 }
308
309 // bytecode prelude: local state size and exception stack size
Damien George8dcc0c72014-03-27 10:55:21 +0000310 {
Damien George7ff996c2014-09-08 23:05:16 +0100311 mp_uint_t n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000312 if (n_state == 0) {
313 // Need at least 1 entry in the state, in the case an exception is
314 // propagated through this function, the exception is returned in
315 // the highest slot in the state (fastn[0], see vm.c).
316 n_state = 1;
317 }
Damien Georgeb534e1b2014-09-04 14:44:01 +0100318 emit_write_bytecode_uint(emit, n_state);
319 emit_write_bytecode_uint(emit, scope->exc_stack_size);
Damien George8dcc0c72014-03-27 10:55:21 +0000320 }
321
322 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000323 int num_cell = 0;
324 for (int i = 0; i < scope->id_info_len; i++) {
325 id_info_t *id = &scope->id_info[i];
326 if (id->kind == ID_INFO_KIND_CELL) {
327 num_cell += 1;
328 }
329 }
330 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100331 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000332 for (int i = 0; i < scope->id_info_len; i++) {
333 id_info_t *id = &scope->id_info[i];
334 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100335 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 +0000336 }
337 }
338}
339
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200340STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000341 // check stack is back to zero size
342 if (emit->stack_size != 0) {
343 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
344 }
345
Damien George73496fb2014-04-13 14:51:56 +0100346 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George08335002014-01-18 23:24:36 +0000347
Damien George36db6bc2014-05-07 17:24:22 +0100348 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100349 // Need to make sure we have enough room in the code-info block to write
350 // the size of the code-info block. Since the size is written as a
351 // compressed uint, we don't know its size until we write it! Thus, we
352 // take the biggest possible value it could be and write that here.
353 // Then there will be enough room to write the value, and any leftover
354 // space will be absorbed in the alignment at the end of the code-info
355 // block.
356 mp_uint_t max_code_info_size =
357 emit->code_info_offset // current code-info size
358 + BYTES_FOR_INT // maximum space for compressed uint
359 + BYTES_PER_WORD - 1; // maximum space for alignment padding
360 emit_write_code_info_uint(emit, max_code_info_size);
361
362 // Align code-info so that following bytecode is aligned on a machine word.
363 // We don't need to write anything here, it's just dead space between the
364 // code-info block and the bytecode block that follows it.
365 emit_align_code_info_to_machine_word(emit);
366
367 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000368 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100369 emit->bytecode_size = emit->bytecode_offset;
370 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000371
Damien George36db6bc2014-05-07 17:24:22 +0100372 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100373 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
374 emit->code_info_size + emit->bytecode_size,
Damien George1084b0f2014-10-25 15:07:02 +0100375 emit->scope->num_pos_args, emit->scope->num_kwonly_args,
Damien George2827d622014-04-27 15:50:52 +0100376 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000377 }
378}
379
Damien George069a35e2014-04-10 17:22:19 +0000380STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100381 return emit->last_emit_was_return_value;
382}
383
Damien George7ff996c2014-09-08 23:05:16 +0100384STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000385 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100386}
387
Damien George7ff996c2014-09-08 23:05:16 +0100388STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100389 //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 +0000390#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000391 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300392 // If we compile with -O3, don't store line numbers.
393 return;
394 }
Damien George08335002014-01-18 23:24:36 +0000395 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100396 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
397 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000398 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100399 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000400 emit->last_source_line = source_line;
401 }
Damien George62ad1892014-01-29 21:51:51 +0000402#endif
Damien George08335002014-01-18 23:24:36 +0000403}
404
Damien George7ff996c2014-09-08 23:05:16 +0100405STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
406 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100407 emit->stack_size += stack_size_delta;
408 if (emit->stack_size > emit->scope->stack_size) {
409 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100410 }
411 emit->last_emit_was_return_value = false;
412}
413
Damien George7ff996c2014-09-08 23:05:16 +0100414STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000415 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100416 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100417 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100418 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000419 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100420 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100421 } else {
422 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100423 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
424 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100425 }
426}
427
Damien George7ff996c2014-09-08 23:05:16 +0100428STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000429 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100430 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100431}
432
Damien George7ff996c2014-09-08 23:05:16 +0100433STATIC void emit_bc_import_from(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_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100436}
437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000439 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100440 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200443STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000444 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100445 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100446 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
447 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
448 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000449 no_other_choice:
Damien George3417bc22014-05-10 10:36:38 +0100450 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000451 default: assert(0); goto no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100452 }
453}
454
Damien George40f3c022014-07-03 13:25:24 +0100455STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000456 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100457 if (-16 <= arg && arg <= 47) {
458 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
459 } else {
460 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
461 }
Damien429d7192013-10-04 19:53:11 +0100462}
463
Damien George7ff996c2014-09-08 23:05:16 +0100464STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000465 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100466 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100467 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100468 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100469 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100470 }
471}
472
Damien Georgedab13852015-01-13 15:55:54 +0000473STATIC void emit_bc_load_const_obj(emit_t *emit, void *obj) {
474 emit_bc_pre(emit, 1);
475 emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
476}
477
Damien George523b5752014-03-31 11:59:23 +0100478STATIC void emit_bc_load_null(emit_t *emit) {
479 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100480 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100481};
482
Damien George0abb5602015-01-16 12:24:49 +0000483STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000484 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100485 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000486 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100487 if (local_num <= 15) {
488 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
489 } else {
490 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100491 }
492}
493
Damien George7ff996c2014-09-08 23:05:16 +0100494STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000495 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000496 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100497 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000498}
499
Damien George7ff996c2014-09-08 23:05:16 +0100500STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000501 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000502 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100503 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000504 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
505 emit_write_bytecode_byte(emit, 0);
506 }
Damien429d7192013-10-04 19:53:11 +0100507}
508
Damien George7ff996c2014-09-08 23:05:16 +0100509STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000510 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000511 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100512 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000513 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
514 emit_write_bytecode_byte(emit, 0);
515 }
Damien429d7192013-10-04 19:53:11 +0100516}
517
Damien George7ff996c2014-09-08 23:05:16 +0100518STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000519 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100520 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000521 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
522 emit_write_bytecode_byte(emit, 0);
523 }
Damien429d7192013-10-04 19:53:11 +0100524}
525
Damien George7ff996c2014-09-08 23:05:16 +0100526STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000527 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100528 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100529}
530
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200531STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000532 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100533 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100534}
535
Damien George729f7b42014-04-17 22:10:53 +0100536STATIC void emit_bc_load_subscr(emit_t *emit) {
537 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100538 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100539}
540
Damien George7ff996c2014-09-08 23:05:16 +0100541STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000542 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100543 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000544 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100545 if (local_num <= 15) {
546 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
547 } else {
548 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100549 }
550}
551
Damien George7ff996c2014-09-08 23:05:16 +0100552STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000553 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000554 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100555 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000556}
557
Damien George7ff996c2014-09-08 23:05:16 +0100558STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000559 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100560 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100561}
562
Damien George7ff996c2014-09-08 23:05:16 +0100563STATIC void emit_bc_store_global(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_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Damien George7ff996c2014-09-08 23:05:16 +0100568STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000569 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100570 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000571 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
572 emit_write_bytecode_byte(emit, 0);
573 }
Damien429d7192013-10-04 19:53:11 +0100574}
575
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200576STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000577 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100578 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100579}
580
Damien George7ff996c2014-09-08 23:05:16 +0100581STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000582 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100583 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100584}
585
Damien George7ff996c2014-09-08 23:05:16 +0100586STATIC void emit_bc_delete_deref(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_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000589}
590
Damien George7ff996c2014-09-08 23:05:16 +0100591STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000592 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100593 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100594}
595
Damien George7ff996c2014-09-08 23:05:16 +0100596STATIC void emit_bc_delete_global(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_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Damien George7ff996c2014-09-08 23:05:16 +0100601STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
Damien George1d24ea52014-04-08 21:11:49 +0100602 emit_bc_load_null(emit);
603 emit_bc_rot_two(emit);
Damien George7ff996c2014-09-08 23:05:16 +0100604 emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100608 emit_bc_load_null(emit);
609 emit_bc_rot_three(emit);
610 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200613STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000614 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100615 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200618STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000619 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100620 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100621}
622
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200623STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000624 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100625 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100626}
627
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200628STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100630 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100631}
632
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200633STATIC void emit_bc_rot_three(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_THREE);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Damien George7ff996c2014-09-08 23:05:16 +0100638STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
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_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100641}
642
Damien George63f38322015-02-28 15:04:06 +0000643STATIC void emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000644 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000645 if (cond) {
646 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
647 } else {
648 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
649 }
Damien429d7192013-10-04 19:53:11 +0100650}
651
Damien George63f38322015-02-28 15:04:06 +0000652STATIC void emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000653 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000654 if (cond) {
655 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
656 } else {
657 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
658 }
Damien429d7192013-10-04 19:53:11 +0100659}
660
Damien George7ff996c2014-09-08 23:05:16 +0100661STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000662 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000663 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100664 if (label & MP_EMIT_BREAK_FROM_FOR) {
665 // need to pop the iterator if we are breaking out of a for loop
666 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
667 }
668 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
669 } else {
670 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
671 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000672 }
Damien429d7192013-10-04 19:53:11 +0100673}
674
Damien George7ff996c2014-09-08 23:05:16 +0100675STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000676 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100677 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100678}
679
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200680STATIC void emit_bc_with_cleanup(emit_t *emit) {
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(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien George7ff996c2014-09-08 23:05:16 +0100685STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000686 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100687 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Damien George7ff996c2014-09-08 23:05:16 +0100690STATIC void emit_bc_setup_finally(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_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100693}
694
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200695STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000696 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100697 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100698}
699
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200700STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000701 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100702 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100703}
704
Damien George7ff996c2014-09-08 23:05:16 +0100705STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000706 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100707 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100708}
709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200710STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000711 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200714STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000715 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100716 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100717}
718
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200719STATIC void emit_bc_pop_except(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_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100722}
723
Damien Georged17926d2014-03-30 13:35:08 +0100724STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
725 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000726 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100727 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000728 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100729 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000730 } else {
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 + op);
Damien George9aa2a522014-02-01 23:04:09 +0000733 }
Damien429d7192013-10-04 19:53:11 +0100734}
735
Damien Georged17926d2014-03-30 13:35:08 +0100736STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000737 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100738 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000739 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100740 op = MP_BINARY_OP_IN;
741 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000742 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100743 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000744 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000745 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100746 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000747 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000748 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100749 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000750 }
Damien429d7192013-10-04 19:53:11 +0100751}
752
Damien George7ff996c2014-09-08 23:05:16 +0100753STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000754 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100755 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100756}
757
Damien George7ff996c2014-09-08 23:05:16 +0100758STATIC void emit_bc_build_list(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_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100761}
762
Damien George7ff996c2014-09-08 23:05:16 +0100763STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000764 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100765 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100766}
767
Damien George7ff996c2014-09-08 23:05:16 +0100768STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
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_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100771}
772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000774 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100775 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100776}
777
Damien George7ff996c2014-09-08 23:05:16 +0100778STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
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_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100781}
782
Damien Georgee37dcaa2014-12-27 17:07:16 +0000783#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +0100784STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000785 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100786 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100787}
788
Damien George7ff996c2014-09-08 23:05:16 +0100789STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000790 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100791 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100792}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000793#endif
Damien429d7192013-10-04 19:53:11 +0100794
Damien George83204f32014-12-27 17:20:41 +0000795#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +0100796STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000797 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100798 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100799}
Damien George83204f32014-12-27 17:20:41 +0000800#endif
Damien429d7192013-10-04 19:53:11 +0100801
Damien George7ff996c2014-09-08 23:05:16 +0100802STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000803 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100804 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100805}
806
Damien George7ff996c2014-09-08 23:05:16 +0100807STATIC 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 +0000808 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100809 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100810}
811
Damien George7ff996c2014-09-08 23:05:16 +0100812STATIC 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 +0100813 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000814 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100815 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000816 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100817 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100818 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200819 }
Damien429d7192013-10-04 19:53:11 +0100820}
821
Damien George7ff996c2014-09-08 23:05:16 +0100822STATIC 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 +0100823 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100824 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100825 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
826 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200827 } else {
Damien George3558f622014-04-20 17:50:40 +0100828 assert(n_closed_over <= 255);
829 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100830 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
831 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200832 }
Damien429d7192013-10-04 19:53:11 +0100833}
834
Damien George7ff996c2014-09-08 23:05:16 +0100835STATIC 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 +0100836 if (star_flags) {
837 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100838 // load dummy entry for non-existent pos_seq
839 emit_bc_load_null(emit);
840 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100841 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100842 // load dummy entry for non-existent kw_dict
843 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100844 }
Damien George7ff996c2014-09-08 23:05:16 +0100845 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 +0100846 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 +0100847 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100848 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100849 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 +0100850 }
Damien George523b5752014-03-31 11:59:23 +0100851}
852
Damien George7ff996c2014-09-08 23:05:16 +0100853STATIC 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 +0100854 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100855}
856
Damien George7ff996c2014-09-08 23:05:16 +0100857STATIC 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 +0100858 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100859}
860
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200861STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000862 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100863 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100864 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100865}
866
Damien George7ff996c2014-09-08 23:05:16 +0100867STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000868 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000869 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100870 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100871}
872
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200873STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000874 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100875 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100876 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100877}
878
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200879STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000880 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100881 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100882 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100883}
884
Damien Georgeb601d952014-06-30 05:17:25 +0100885STATIC void emit_bc_start_except_handler(emit_t *emit) {
886 emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
887}
888
889STATIC void emit_bc_end_except_handler(emit_t *emit) {
890 emit_bc_adjust_stack_size(emit, -5); // stack adjust
891}
892
Damien6cdd3af2013-10-05 18:08:26 +0100893const emit_method_table_t emit_bc_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100894 emit_bc_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100895 emit_bc_start_pass,
896 emit_bc_end_pass,
897 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000898 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000899 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100900
Damien George542bd6b2015-03-26 14:42:40 +0000901 {
902 emit_bc_load_fast,
903 emit_bc_load_deref,
904 emit_bc_load_name,
905 emit_bc_load_global,
906 },
907 {
908 emit_bc_store_fast,
909 emit_bc_store_deref,
910 emit_bc_store_name,
911 emit_bc_store_global,
912 },
913 {
914 emit_bc_delete_fast,
915 emit_bc_delete_deref,
916 emit_bc_delete_name,
917 emit_bc_delete_global,
918 },
Damien4b03e772013-10-05 14:17:09 +0100919
Damien415eb6f2013-10-05 12:19:06 +0100920 emit_bc_label_assign,
921 emit_bc_import_name,
922 emit_bc_import_from,
923 emit_bc_import_star,
924 emit_bc_load_const_tok,
925 emit_bc_load_const_small_int,
Damien415eb6f2013-10-05 12:19:06 +0100926 emit_bc_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +0000927 emit_bc_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +0100928 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100929 emit_bc_load_attr,
930 emit_bc_load_method,
931 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100932 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100933 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100934 emit_bc_store_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100935 emit_bc_delete_attr,
936 emit_bc_delete_subscr,
937 emit_bc_dup_top,
938 emit_bc_dup_top_two,
939 emit_bc_pop_top,
940 emit_bc_rot_two,
941 emit_bc_rot_three,
942 emit_bc_jump,
Damien George63f38322015-02-28 15:04:06 +0000943 emit_bc_pop_jump_if,
944 emit_bc_jump_if_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000945 emit_bc_unwind_jump,
946 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100947 emit_bc_setup_with,
948 emit_bc_with_cleanup,
949 emit_bc_setup_except,
950 emit_bc_setup_finally,
951 emit_bc_end_finally,
952 emit_bc_get_iter,
953 emit_bc_for_iter,
954 emit_bc_for_iter_end,
955 emit_bc_pop_block,
956 emit_bc_pop_except,
957 emit_bc_unary_op,
958 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100959 emit_bc_build_tuple,
960 emit_bc_build_list,
961 emit_bc_list_append,
962 emit_bc_build_map,
963 emit_bc_store_map,
964 emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000965 #if MICROPY_PY_BUILTINS_SET
Damien415eb6f2013-10-05 12:19:06 +0100966 emit_bc_build_set,
967 emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000968 #endif
Damien George83204f32014-12-27 17:20:41 +0000969 #if MICROPY_PY_BUILTINS_SLICE
Damien415eb6f2013-10-05 12:19:06 +0100970 emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +0000971 #endif
Damien415eb6f2013-10-05 12:19:06 +0100972 emit_bc_unpack_sequence,
973 emit_bc_unpack_ex,
974 emit_bc_make_function,
975 emit_bc_make_closure,
976 emit_bc_call_function,
977 emit_bc_call_method,
978 emit_bc_return_value,
979 emit_bc_raise_varargs,
980 emit_bc_yield_value,
981 emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100982
983 emit_bc_start_except_handler,
984 emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100985};
Damien George5f6a25f2014-04-20 18:02:27 +0100986
987#endif // !MICROPY_EMIT_CPYTHON