blob: 0ed7828f9f300e2f0329cba7348428581b2a65ef [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 George95977712014-05-10 18:07:08 +010037#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
38#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
39
Damien415eb6f2013-10-05 12:19:06 +010040struct _emit_t {
Damien George0fb80c32014-05-10 18:16:21 +010041 pass_kind_t pass : 8;
Damien George7ff996c2014-09-08 23:05:16 +010042 mp_uint_t last_emit_was_return_value : 8;
Damien George0fb80c32014-05-10 18:16:21 +010043
Damien429d7192013-10-04 19:53:11 +010044 int stack_size;
Damien429d7192013-10-04 19:53:11 +010045
46 scope_t *scope;
47
Damien George7ff996c2014-09-08 23:05:16 +010048 mp_uint_t last_source_line_offset;
49 mp_uint_t last_source_line;
Damien George08335002014-01-18 23:24:36 +000050
Damien George7ff996c2014-09-08 23:05:16 +010051 mp_uint_t max_num_labels;
52 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010053
Damien George7ff996c2014-09-08 23:05:16 +010054 mp_uint_t code_info_offset;
55 mp_uint_t code_info_size;
56 mp_uint_t bytecode_offset;
57 mp_uint_t bytecode_size;
Damien George08335002014-01-18 23:24:36 +000058 byte *code_base; // stores both byte code and code info
Damien George7ff996c2014-09-08 23:05:16 +010059 // Accessed as mp_uint_t, so must be aligned as such
Paul Sokolovsky58c95862014-07-12 14:51:48 +030060 byte dummy_data[DUMMY_DATA_SIZE];
Damien429d7192013-10-04 19:53:11 +010061};
62
Damien Georgea210c772015-03-26 15:49:53 +000063emit_t *emit_bc_new(void) {
Damien George08335002014-01-18 23:24:36 +000064 emit_t *emit = m_new0(emit_t, 1);
Damien Georgea210c772015-03-26 15:49:53 +000065 return emit;
66}
67
Damien George4dea9222015-04-09 15:29:54 +000068void emit_bc_set_max_num_labels(emit_t *emit, mp_uint_t max_num_labels) {
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}
Damien4b03e772013-10-05 14:17:09 +010072
Damien George41d02b62014-01-24 22:42:28 +000073void emit_bc_free(emit_t *emit) {
Damien George7ff996c2014-09-08 23:05:16 +010074 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020075 m_del_obj(emit_t, emit);
76}
77
Damien George91bc32d2015-04-09 15:31:53 +000078typedef byte *(*emit_allocator_t)(emit_t *emit, int nbytes);
79
80STATIC void emit_write_uint(emit_t *emit, emit_allocator_t allocator, mp_uint_t val) {
Damien Georgeb534e1b2014-09-04 14:44:01 +010081 // 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);
Damien George4dea9222015-04-09 15:29:54 +000089 byte *c = allocator(emit, buf + sizeof(buf) - p);
Damien Georgeb534e1b2014-09-04 14:44:01 +010090 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
Damien George4dea9222015-04-09 15:29:54 +000097STATIC 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 George4dea9222015-04-09 15:29:54 +0000110STATIC 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 George9b7f5832015-03-18 17:47:47 +0000114STATIC void emit_write_code_info_byte(emit_t* emit, byte val) {
115 *emit_get_cur_to_write_code_info(emit, 1) = val;
116}
117
118STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100119 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
120}
121
Damien George4dea9222015-04-09 15:29:54 +0000122STATIC void emit_write_code_info_qstr(emit_t *emit, qstr qst) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100123 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien George08335002014-01-18 23:24:36 +0000124}
125
Damien George9b7f5832015-03-18 17:47:47 +0000126STATIC void emit_write_code_info_prealigned_ptr(emit_t* emit, void *ptr) {
127 mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_code_info(emit, sizeof(mp_uint_t));
128 // Verify thar c is already uint-aligned
129 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
130 *c = (mp_uint_t)ptr;
131}
132
Damien George73496fb2014-04-13 14:51:56 +0100133#if MICROPY_ENABLE_SOURCE_LINE
Damien George4dea9222015-04-09 15:29:54 +0000134STATIC 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 +0100135 assert(bytes_to_skip > 0 || lines_to_skip > 0);
Damien George4747bec2014-07-31 16:12:01 +0000136 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George73496fb2014-04-13 14:51:56 +0100137 while (bytes_to_skip > 0 || lines_to_skip > 0) {
Damien George4747bec2014-07-31 16:12:01 +0000138 mp_uint_t b, l;
139 if (lines_to_skip <= 6) {
140 // use 0b0LLBBBBB encoding
141 b = MIN(bytes_to_skip, 0x1f);
142 l = MIN(lines_to_skip, 0x3);
143 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
144 } else {
145 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
146 b = MIN(bytes_to_skip, 0xf);
147 l = MIN(lines_to_skip, 0x7ff);
148 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
149 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
150 ci[1] = l;
151 }
Damien George73496fb2014-04-13 14:51:56 +0100152 bytes_to_skip -= b;
153 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000154 }
Damien George08335002014-01-18 23:24:36 +0000155}
Damien George73496fb2014-04-13 14:51:56 +0100156#endif
Damien George08335002014-01-18 23:24:36 +0000157
158// all functions must go through this one to emit byte code
Damien George4dea9222015-04-09 15:29:54 +0000159STATIC byte *emit_get_cur_to_write_bytecode(emit_t *emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000160 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100161 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100162 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000163 return emit->dummy_data;
164 } else {
Damien George3417bc22014-05-10 10:36:38 +0100165 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
166 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
167 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000168 return c;
169 }
170}
171
Damien George4dea9222015-04-09 15:29:54 +0000172STATIC void emit_align_bytecode_to_machine_word(emit_t *emit) {
Damien George40f3c022014-07-03 13:25:24 +0100173 emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100174}
175
Damien George4dea9222015-04-09 15:29:54 +0000176STATIC void emit_write_bytecode_byte(emit_t *emit, byte b1) {
177 byte *c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100178 c[0] = b1;
179}
180
Damien George9b7f5832015-03-18 17:47:47 +0000181STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100182 assert((b2 & (~0xff)) == 0);
Damien George4dea9222015-04-09 15:29:54 +0000183 byte *c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100184 c[0] = b1;
185 c[1] = b2;
186}
187
Damien George3417bc22014-05-10 10:36:38 +0100188// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George4dea9222015-04-09 15:29:54 +0000189STATIC void emit_write_bytecode_byte_int(emit_t *emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100190 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200191
192 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100193 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200194 byte *p = buf + sizeof(buf);
195 // We encode in little-ending order, but store in big-endian, to help decoding
196 do {
197 *--p = num & 0x7f;
198 num >>= 7;
199 } while (num != 0 && num != -1);
200 // Make sure that highest bit we stored (mask 0x40) matches sign
201 // of the number. If not, store extra byte just to encode sign
202 if (num == -1 && (*p & 0x40) == 0) {
203 *--p = 0x7f;
204 } else if (num == 0 && (*p & 0x40) != 0) {
205 *--p = 0;
206 }
207
Damien George4dea9222015-04-09 15:29:54 +0000208 byte *c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200209 while (p != buf + sizeof(buf) - 1) {
210 *c++ = *p++ | 0x80;
211 }
212 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100213}
214
Damien George4dea9222015-04-09 15:29:54 +0000215STATIC void emit_write_bytecode_byte_uint(emit_t *emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100216 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100217 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100218}
219
Damien Georgedf8127a2014-04-13 11:04:33 +0100220// aligns the pointer so it is friendly to GC
Damien George4dea9222015-04-09 15:29:54 +0000221STATIC void emit_write_bytecode_byte_ptr(emit_t *emit, byte b, void *ptr) {
Damien George3417bc22014-05-10 10:36:38 +0100222 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 George9b7f5832015-03-18 17:47:47 +0000230STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
Damien George7ff996c2014-09-08 23:05:16 +0100231 emit_write_bytecode_byte_uint(emit, b, qst);
Damien429d7192013-10-04 19:53:11 +0100232}
233
Damien03c9cfb2013-11-05 22:06:08 +0000234// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George4dea9222015-04-09 15:29:54 +0000235STATIC void emit_write_bytecode_byte_unsigned_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George7ff996c2014-09-08 23:05:16 +0100236 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100237 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100238 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100239 } else {
Damien George3417bc22014-05-10 10:36:38 +0100240 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100241 }
Damien George3417bc22014-05-10 10:36:38 +0100242 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000243 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100244 c[1] = bytecode_offset;
245 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000246}
247
248// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George4dea9222015-04-09 15:29:54 +0000249STATIC void emit_write_bytecode_byte_signed_label(emit_t *emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100250 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100251 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100252 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000253 } else {
Damien George3417bc22014-05-10 10:36:38 +0100254 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000255 }
Damien George4dea9222015-04-09 15:29:54 +0000256 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000257 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100258 c[1] = bytecode_offset;
259 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100260}
261
Damien George41125902015-03-26 16:44:14 +0000262#if MICROPY_EMIT_NATIVE
263STATIC void mp_emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000264 (void)emit;
265 (void)op;
266 (void)arg1;
267 (void)arg2;
Damien George6baf76e2013-12-30 22:32:17 +0000268}
Damien George41125902015-03-26 16:44:14 +0000269#endif
Damien George6baf76e2013-12-30 22:32:17 +0000270
Damien George41125902015-03-26 16:44:14 +0000271void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000272 emit->pass = pass;
273 emit->stack_size = 0;
274 emit->last_emit_was_return_value = false;
275 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000276 emit->last_source_line_offset = 0;
277 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100278 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100279 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000280 }
Damien George3417bc22014-05-10 10:36:38 +0100281 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000282 emit->code_info_offset = 0;
283
Damien George9b7f5832015-03-18 17:47:47 +0000284 // Write local state size and exception stack size.
285 {
286 mp_uint_t n_state = scope->num_locals + scope->stack_size;
287 if (n_state == 0) {
288 // Need at least 1 entry in the state, in the case an exception is
289 // propagated through this function, the exception is returned in
290 // the highest slot in the state (fastn[0], see vm.c).
291 n_state = 1;
292 }
293 emit_write_code_info_uint(emit, n_state);
294 emit_write_code_info_uint(emit, scope->exc_stack_size);
Damien George08335002014-01-18 23:24:36 +0000295 }
296
Damien George9b7f5832015-03-18 17:47:47 +0000297 // Align code-info so that following pointers are aligned on a machine word.
298 emit_align_code_info_to_machine_word(emit);
Damien George6baf76e2013-12-30 22:32:17 +0000299
Damien George9b7f5832015-03-18 17:47:47 +0000300 // Write argument names (needed to resolve positional args passed as
301 // keywords). We store them as full word-sized objects for efficient access
302 // in mp_setup_code_state this is the start of the prelude and is guaranteed
303 // to be aligned on a word boundary.
Damien George1084b0f2014-10-25 15:07:02 +0100304 {
Damien George9a42eb52015-05-06 13:55:33 +0100305 // For a given argument position (indexed by i) we need to find the
306 // corresponding id_info which is a parameter, as it has the correct
307 // qstr name to use as the argument name. Note that it's not a simple
308 // 1-1 mapping (ie i!=j in general) because of possible closed-over
309 // variables. In the case that the argument i has no corresponding
310 // parameter we use "*" as its name (since no argument can ever be named
311 // "*"). We could use a blank qstr but "*" is better for debugging.
312 // Note: there is some wasted RAM here for the case of storing a qstr
313 // for each closed-over variable, and maybe there is a better way to do
314 // it, but that would require changes to mp_setup_code_state.
Damien George1084b0f2014-10-25 15:07:02 +0100315 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100316 qstr qst = MP_QSTR__star_;
317 for (int j = 0; j < scope->id_info_len; ++j) {
318 id_info_t *id = &scope->id_info[j];
319 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
320 qst = id->qst;
321 break;
322 }
323 }
Damien George9b7f5832015-03-18 17:47:47 +0000324 emit_write_code_info_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(qst));
Damien George1084b0f2014-10-25 15:07:02 +0100325 }
326 }
327
Damien George9b7f5832015-03-18 17:47:47 +0000328 // Write size of the rest of the code info. We don't know how big this
329 // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
330 // for it and hope that is enough! TODO assert this or something.
331 if (pass == MP_PASS_EMIT) {
332 emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
333 } else {
334 emit_get_cur_to_write_code_info(emit, 2);
Damien George8dcc0c72014-03-27 10:55:21 +0000335 }
336
Damien George9b7f5832015-03-18 17:47:47 +0000337 // Write the name and source file of this function.
338 emit_write_code_info_qstr(emit, scope->simple_name);
339 emit_write_code_info_qstr(emit, scope->source_file);
340
Damien George8dcc0c72014-03-27 10:55:21 +0000341 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000342 for (int i = 0; i < scope->id_info_len; i++) {
343 id_info_t *id = &scope->id_info[i];
344 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgec9aa1882015-04-07 00:08:17 +0100345 assert(id->local_num < 255);
Damien George3417bc22014-05-10 10:36:38 +0100346 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 +0000347 }
348 }
Damien Georgec9aa1882015-04-07 00:08:17 +0100349 emit_write_bytecode_byte(emit, 255); // end of list sentinel
Damien George6baf76e2013-12-30 22:32:17 +0000350}
351
Damien George41125902015-03-26 16:44:14 +0000352void mp_emit_bc_end_pass(emit_t *emit) {
Damien Georgea210c772015-03-26 15:49:53 +0000353 if (emit->pass == MP_PASS_SCOPE) {
354 return;
355 }
356
Damien George6baf76e2013-12-30 22:32:17 +0000357 // check stack is back to zero size
358 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100359 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien George6baf76e2013-12-30 22:32:17 +0000360 }
361
Damien George9b7f5832015-03-18 17:47:47 +0000362 emit_write_code_info_byte(emit, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000363
Damien George36db6bc2014-05-07 17:24:22 +0100364 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien George9b7f5832015-03-18 17:47:47 +0000365 // so bytecode is aligned
Damien Georgeb534e1b2014-09-04 14:44:01 +0100366 emit_align_code_info_to_machine_word(emit);
367
368 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000369 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100370 emit->bytecode_size = emit->bytecode_offset;
371 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000372
Damien George36db6bc2014-05-07 17:24:22 +0100373 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100374 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
375 emit->code_info_size + emit->bytecode_size,
Damien George1084b0f2014-10-25 15:07:02 +0100376 emit->scope->num_pos_args, emit->scope->num_kwonly_args,
Damien George2827d622014-04-27 15:50:52 +0100377 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000378 }
379}
380
Damien George41125902015-03-26 16:44:14 +0000381bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100382 return emit->last_emit_was_return_value;
383}
384
Damien George41125902015-03-26 16:44:14 +0000385void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000386 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100387}
388
Damien George41125902015-03-26 16:44:14 +0000389void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100390 //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 +0000391#if MICROPY_ENABLE_SOURCE_LINE
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000392 if (MP_STATE_VM(mp_optimise_value) >= 3) {
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300393 // If we compile with -O3, don't store line numbers.
394 return;
395 }
Damien George08335002014-01-18 23:24:36 +0000396 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100397 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
398 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000399 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100400 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000401 emit->last_source_line = source_line;
402 }
Damien George3a2171e2015-09-04 16:53:46 +0100403#else
404 (void)emit;
405 (void)source_line;
Damien George62ad1892014-01-29 21:51:51 +0000406#endif
Damien George08335002014-01-18 23:24:36 +0000407}
408
Damien George7ff996c2014-09-08 23:05:16 +0100409STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georgea210c772015-03-26 15:49:53 +0000410 if (emit->pass == MP_PASS_SCOPE) {
411 return;
412 }
Damien George7ff996c2014-09-08 23:05:16 +0100413 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 George41125902015-03-26 16:44:14 +0000421void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000422 emit_bc_pre(emit, 0);
Damien Georgea210c772015-03-26 15:49:53 +0000423 if (emit->pass == MP_PASS_SCOPE) {
424 return;
425 }
Damienb05d7072013-10-05 13:37:10 +0100426 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100427 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100428 // assign label offset
Damien George963a5a32015-01-16 17:47:07 +0000429 assert(emit->label_offsets[l] == (mp_uint_t)-1);
Damien George3417bc22014-05-10 10:36:38 +0100430 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100431 } else {
432 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100433 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
434 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100435 }
436}
437
Damien George41125902015-03-26 16:44:14 +0000438void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000439 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100440 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Damien George41125902015-03-26 16:44:14 +0000443void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000444 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100445 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100446}
447
Damien George41125902015-03-26 16:44:14 +0000448void mp_emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000449 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100450 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Damien George41125902015-03-26 16:44:14 +0000453void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000454 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100455 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100456 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
457 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
458 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000459 no_other_choice:
Damien George8872abc2015-05-05 22:15:42 +0100460 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, (void*)&mp_const_ellipsis_obj); break;
Damien Georged2d64f02015-01-14 21:32:42 +0000461 default: assert(0); goto no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100462 }
463}
464
Damien George41125902015-03-26 16:44:14 +0000465void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000466 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100467 if (-16 <= arg && arg <= 47) {
468 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
469 } else {
470 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
471 }
Damien429d7192013-10-04 19:53:11 +0100472}
473
Damien George59fba2d2015-06-25 14:42:13 +0000474void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000475 emit_bc_pre(emit, 1);
Damien George59fba2d2015-06-25 14:42:13 +0000476 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100477}
478
Damien George41125902015-03-26 16:44:14 +0000479void mp_emit_bc_load_const_obj(emit_t *emit, void *obj) {
Damien Georgedab13852015-01-13 15:55:54 +0000480 emit_bc_pre(emit, 1);
481 emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
482}
483
Damien George41125902015-03-26 16:44:14 +0000484void mp_emit_bc_load_null(emit_t *emit) {
Damien George523b5752014-03-31 11:59:23 +0100485 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100486 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100487};
488
Damien George41125902015-03-26 16:44:14 +0000489void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000490 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100491 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000492 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100493 if (local_num <= 15) {
494 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
495 } else {
496 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100497 }
498}
499
Damien George41125902015-03-26 16:44:14 +0000500void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000501 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000502 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100503 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000504}
505
Damien George41125902015-03-26 16:44:14 +0000506void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000507 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000508 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100509 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000510 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
511 emit_write_bytecode_byte(emit, 0);
512 }
Damien429d7192013-10-04 19:53:11 +0100513}
514
Damien George41125902015-03-26 16:44:14 +0000515void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000516 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000517 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100518 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000519 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
520 emit_write_bytecode_byte(emit, 0);
521 }
Damien429d7192013-10-04 19:53:11 +0100522}
523
Damien George41125902015-03-26 16:44:14 +0000524void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000525 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100526 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000527 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
528 emit_write_bytecode_byte(emit, 0);
529 }
Damien429d7192013-10-04 19:53:11 +0100530}
531
Damien George41125902015-03-26 16:44:14 +0000532void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000533 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100534 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100535}
536
Damien George41125902015-03-26 16:44:14 +0000537void mp_emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000538 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100539 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100540}
541
Damien George41125902015-03-26 16:44:14 +0000542void mp_emit_bc_load_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +0100543 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100544 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100545}
546
Damien George41125902015-03-26 16:44:14 +0000547void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000548 (void)qst;
Damien429d7192013-10-04 19:53:11 +0100549 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000550 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100551 if (local_num <= 15) {
552 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
553 } else {
554 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100555 }
556}
557
Damien George41125902015-03-26 16:44:14 +0000558void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000559 (void)qst;
Damien Georgece8f07a2014-03-27 23:30:26 +0000560 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100561 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000562}
563
Damien George41125902015-03-26 16:44:14 +0000564void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000565 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100566 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100567}
568
Damien George41125902015-03-26 16:44:14 +0000569void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000570 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100571 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100572}
573
Damien George41125902015-03-26 16:44:14 +0000574void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000575 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100576 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien George7ee91cf2015-01-06 12:51:39 +0000577 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
578 emit_write_bytecode_byte(emit, 0);
579 }
Damien429d7192013-10-04 19:53:11 +0100580}
581
Damien George41125902015-03-26 16:44:14 +0000582void mp_emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000583 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100584 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damien George41125902015-03-26 16:44:14 +0000587void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000588 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100589 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100590}
591
Damien George41125902015-03-26 16:44:14 +0000592void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000593 (void)qst;
Damien George3417bc22014-05-10 10:36:38 +0100594 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000595}
596
Damien George41125902015-03-26 16:44:14 +0000597void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000598 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100599 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100600}
601
Damien George41125902015-03-26 16:44:14 +0000602void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000603 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100604 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Damien George41125902015-03-26 16:44:14 +0000607void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
608 mp_emit_bc_load_null(emit);
609 mp_emit_bc_rot_two(emit);
610 mp_emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Damien George41125902015-03-26 16:44:14 +0000613void mp_emit_bc_delete_subscr(emit_t *emit) {
614 mp_emit_bc_load_null(emit);
615 mp_emit_bc_rot_three(emit);
616 mp_emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100617}
618
Damien George41125902015-03-26 16:44:14 +0000619void mp_emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000620 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100621 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100622}
623
Damien George41125902015-03-26 16:44:14 +0000624void mp_emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000625 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100626 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100627}
628
Damien George41125902015-03-26 16:44:14 +0000629void mp_emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000630 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100631 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100632}
633
Damien George41125902015-03-26 16:44:14 +0000634void mp_emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000635 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100636 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100637}
638
Damien George41125902015-03-26 16:44:14 +0000639void mp_emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000640 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100641 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100642}
643
Damien George41125902015-03-26 16:44:14 +0000644void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000645 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100646 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100647}
648
Damien George41125902015-03-26 16:44:14 +0000649void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000650 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000651 if (cond) {
652 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
653 } else {
654 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
655 }
Damien429d7192013-10-04 19:53:11 +0100656}
657
Damien George41125902015-03-26 16:44:14 +0000658void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000659 emit_bc_pre(emit, -1);
Damien George63f38322015-02-28 15:04:06 +0000660 if (cond) {
661 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
662 } else {
663 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
664 }
Damien429d7192013-10-04 19:53:11 +0100665}
666
Damien George41125902015-03-26 16:44:14 +0000667void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000668 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000669 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100670 if (label & MP_EMIT_BREAK_FROM_FOR) {
671 // need to pop the iterator if we are breaking out of a for loop
672 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
673 }
674 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
675 } else {
676 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
677 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000678 }
Damien429d7192013-10-04 19:53:11 +0100679}
680
Damien George41125902015-03-26 16:44:14 +0000681void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien George8c1d23a2015-04-24 01:52:28 +0100682 // TODO We can probably optimise the amount of needed stack space, since
683 // we don't actually need 4 slots during the entire with block, only in
684 // the cleanup handler in certain cases. It needs some thinking.
685 emit_bc_pre(emit, 4);
Damien George3417bc22014-05-10 10:36:38 +0100686 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100687}
688
Damien George41125902015-03-26 16:44:14 +0000689void mp_emit_bc_with_cleanup(emit_t *emit) {
Damien George8c1d23a2015-04-24 01:52:28 +0100690 emit_bc_pre(emit, -4);
Damien George3417bc22014-05-10 10:36:38 +0100691 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100692}
693
Damien George41125902015-03-26 16:44:14 +0000694void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000695 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100696 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100697}
698
Damien George41125902015-03-26 16:44:14 +0000699void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000700 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100701 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100702}
703
Damien George41125902015-03-26 16:44:14 +0000704void mp_emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000705 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100706 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100707}
708
Damien George41125902015-03-26 16:44:14 +0000709void mp_emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000710 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100711 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Damien George41125902015-03-26 16:44:14 +0000714void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000715 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100716 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100717}
718
Damien George41125902015-03-26 16:44:14 +0000719void mp_emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000720 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100721}
722
Damien George41125902015-03-26 16:44:14 +0000723void mp_emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000724 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100725 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100726}
727
Damien George41125902015-03-26 16:44:14 +0000728void mp_emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000729 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100730 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100731}
732
Damien George41125902015-03-26 16:44:14 +0000733void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged17926d2014-03-30 13:35:08 +0100734 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000735 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100736 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000737 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100738 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000739 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000740 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100741 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000742 }
Damien429d7192013-10-04 19:53:11 +0100743}
744
Damien George41125902015-03-26 16:44:14 +0000745void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000746 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100747 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000748 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100749 op = MP_BINARY_OP_IN;
750 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000751 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100752 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000753 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000754 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100755 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000756 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000757 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100758 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000759 }
Damien429d7192013-10-04 19:53:11 +0100760}
761
Damien George41125902015-03-26 16:44:14 +0000762void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000763 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100764 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100765}
766
Damien George41125902015-03-26 16:44:14 +0000767void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000768 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100769 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100770}
771
Damien George41125902015-03-26 16:44:14 +0000772void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000773 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100774 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100775}
776
Damien George41125902015-03-26 16:44:14 +0000777void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000778 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100779 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100780}
781
Damien George41125902015-03-26 16:44:14 +0000782void mp_emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000783 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100784 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100785}
786
Damien George41125902015-03-26 16:44:14 +0000787void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000788 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100789 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100790}
791
Damien Georgee37dcaa2014-12-27 17:07:16 +0000792#if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000793void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000794 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100795 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100796}
797
Damien George41125902015-03-26 16:44:14 +0000798void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000799 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100800 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100801}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000802#endif
Damien429d7192013-10-04 19:53:11 +0100803
Damien George83204f32014-12-27 17:20:41 +0000804#if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000805void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000806 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100807 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100808}
Damien George83204f32014-12-27 17:20:41 +0000809#endif
Damien429d7192013-10-04 19:53:11 +0100810
Damien George41125902015-03-26 16:44:14 +0000811void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000812 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100813 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100814}
815
Damien George41125902015-03-26 16:44:14 +0000816void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000817 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100818 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100819}
820
Damien George41125902015-03-26 16:44:14 +0000821void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100822 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000823 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100824 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000825 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100826 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100827 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200828 }
Damien429d7192013-10-04 19:53:11 +0100829}
830
Damien George41125902015-03-26 16:44:14 +0000831void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100832 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100833 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100834 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
835 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200836 } else {
Damien George3558f622014-04-20 17:50:40 +0100837 assert(n_closed_over <= 255);
838 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100839 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
840 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200841 }
Damien429d7192013-10-04 19:53:11 +0100842}
843
Damien George7ff996c2014-09-08 23:05:16 +0100844STATIC 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 +0100845 if (star_flags) {
Damien George7ff996c2014-09-08 23:05:16 +0100846 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 +0100847 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 +0100848 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100849 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100850 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 +0100851 }
Damien George523b5752014-03-31 11:59:23 +0100852}
853
Damien George41125902015-03-26 16:44:14 +0000854void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100855 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100856}
857
Damien George41125902015-03-26 16:44:14 +0000858void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100859 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100860}
861
Damien George41125902015-03-26 16:44:14 +0000862void mp_emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000863 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100864 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100865 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100866}
867
Damien George41125902015-03-26 16:44:14 +0000868void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000869 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000870 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100871 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100872}
873
Damien George41125902015-03-26 16:44:14 +0000874void mp_emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000875 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100876 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100877 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100878}
879
Damien George41125902015-03-26 16:44:14 +0000880void mp_emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000881 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100882 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100883 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100884}
885
Damien George41125902015-03-26 16:44:14 +0000886void mp_emit_bc_start_except_handler(emit_t *emit) {
887 mp_emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
Damien Georgeb601d952014-06-30 05:17:25 +0100888}
889
Damien George41125902015-03-26 16:44:14 +0000890void mp_emit_bc_end_except_handler(emit_t *emit) {
891 mp_emit_bc_adjust_stack_size(emit, -5); // stack adjust
Damien Georgeb601d952014-06-30 05:17:25 +0100892}
893
Damien George41125902015-03-26 16:44:14 +0000894#if MICROPY_EMIT_NATIVE
Damien6cdd3af2013-10-05 18:08:26 +0100895const emit_method_table_t emit_bc_method_table = {
Damien George41125902015-03-26 16:44:14 +0000896 mp_emit_bc_set_native_type,
897 mp_emit_bc_start_pass,
898 mp_emit_bc_end_pass,
899 mp_emit_bc_last_emit_was_return_value,
900 mp_emit_bc_adjust_stack_size,
901 mp_emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100902
Damien George542bd6b2015-03-26 14:42:40 +0000903 {
Damien George41125902015-03-26 16:44:14 +0000904 mp_emit_bc_load_fast,
905 mp_emit_bc_load_deref,
906 mp_emit_bc_load_name,
907 mp_emit_bc_load_global,
Damien George542bd6b2015-03-26 14:42:40 +0000908 },
909 {
Damien George41125902015-03-26 16:44:14 +0000910 mp_emit_bc_store_fast,
911 mp_emit_bc_store_deref,
912 mp_emit_bc_store_name,
913 mp_emit_bc_store_global,
Damien George542bd6b2015-03-26 14:42:40 +0000914 },
915 {
Damien George41125902015-03-26 16:44:14 +0000916 mp_emit_bc_delete_fast,
917 mp_emit_bc_delete_deref,
918 mp_emit_bc_delete_name,
919 mp_emit_bc_delete_global,
Damien George542bd6b2015-03-26 14:42:40 +0000920 },
Damien4b03e772013-10-05 14:17:09 +0100921
Damien George41125902015-03-26 16:44:14 +0000922 mp_emit_bc_label_assign,
923 mp_emit_bc_import_name,
924 mp_emit_bc_import_from,
925 mp_emit_bc_import_star,
926 mp_emit_bc_load_const_tok,
927 mp_emit_bc_load_const_small_int,
928 mp_emit_bc_load_const_str,
929 mp_emit_bc_load_const_obj,
930 mp_emit_bc_load_null,
931 mp_emit_bc_load_attr,
932 mp_emit_bc_load_method,
933 mp_emit_bc_load_build_class,
934 mp_emit_bc_load_subscr,
935 mp_emit_bc_store_attr,
936 mp_emit_bc_store_subscr,
937 mp_emit_bc_delete_attr,
938 mp_emit_bc_delete_subscr,
939 mp_emit_bc_dup_top,
940 mp_emit_bc_dup_top_two,
941 mp_emit_bc_pop_top,
942 mp_emit_bc_rot_two,
943 mp_emit_bc_rot_three,
944 mp_emit_bc_jump,
945 mp_emit_bc_pop_jump_if,
946 mp_emit_bc_jump_if_or_pop,
947 mp_emit_bc_unwind_jump,
948 mp_emit_bc_unwind_jump,
949 mp_emit_bc_setup_with,
950 mp_emit_bc_with_cleanup,
951 mp_emit_bc_setup_except,
952 mp_emit_bc_setup_finally,
953 mp_emit_bc_end_finally,
954 mp_emit_bc_get_iter,
955 mp_emit_bc_for_iter,
956 mp_emit_bc_for_iter_end,
957 mp_emit_bc_pop_block,
958 mp_emit_bc_pop_except,
959 mp_emit_bc_unary_op,
960 mp_emit_bc_binary_op,
961 mp_emit_bc_build_tuple,
962 mp_emit_bc_build_list,
963 mp_emit_bc_list_append,
964 mp_emit_bc_build_map,
965 mp_emit_bc_store_map,
966 mp_emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000967 #if MICROPY_PY_BUILTINS_SET
Damien George41125902015-03-26 16:44:14 +0000968 mp_emit_bc_build_set,
969 mp_emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000970 #endif
Damien George83204f32014-12-27 17:20:41 +0000971 #if MICROPY_PY_BUILTINS_SLICE
Damien George41125902015-03-26 16:44:14 +0000972 mp_emit_bc_build_slice,
Damien George83204f32014-12-27 17:20:41 +0000973 #endif
Damien George41125902015-03-26 16:44:14 +0000974 mp_emit_bc_unpack_sequence,
975 mp_emit_bc_unpack_ex,
976 mp_emit_bc_make_function,
977 mp_emit_bc_make_closure,
978 mp_emit_bc_call_function,
979 mp_emit_bc_call_method,
980 mp_emit_bc_return_value,
981 mp_emit_bc_raise_varargs,
982 mp_emit_bc_yield_value,
983 mp_emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100984
Damien George41125902015-03-26 16:44:14 +0000985 mp_emit_bc_start_except_handler,
986 mp_emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100987};
Damien George41125902015-03-26 16:44:14 +0000988#else
989const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
990 mp_emit_bc_load_fast,
991 mp_emit_bc_load_deref,
992 mp_emit_bc_load_name,
993 mp_emit_bc_load_global,
994};
995
996const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
997 mp_emit_bc_store_fast,
998 mp_emit_bc_store_deref,
999 mp_emit_bc_store_name,
1000 mp_emit_bc_store_global,
1001};
1002
1003const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
1004 mp_emit_bc_delete_fast,
1005 mp_emit_bc_delete_deref,
1006 mp_emit_bc_delete_name,
1007 mp_emit_bc_delete_global,
1008};
1009#endif