blob: 05dc79ef4fc84246862f9176b6b11666abced9f1 [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
Damiend99b0522013-12-21 18:17:45 +000033#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030034#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010036#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010037#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010038#include "obj.h"
39#include "emitglue.h"
Damien429d7192013-10-04 19:53:11 +010040#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000041#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010042#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000043#include "bc0.h"
Damien429d7192013-10-04 19:53:11 +010044
Damien George5f6a25f2014-04-20 18:02:27 +010045#if !MICROPY_EMIT_CPYTHON
46
Damien George95977712014-05-10 18:07:08 +010047#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
48#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
49
Damien415eb6f2013-10-05 12:19:06 +010050struct _emit_t {
Damien George0fb80c32014-05-10 18:16:21 +010051 pass_kind_t pass : 8;
Damien George7ff996c2014-09-08 23:05:16 +010052 mp_uint_t last_emit_was_return_value : 8;
Damien George0fb80c32014-05-10 18:16:21 +010053
Damien429d7192013-10-04 19:53:11 +010054 int stack_size;
Damien429d7192013-10-04 19:53:11 +010055
56 scope_t *scope;
57
Damien George7ff996c2014-09-08 23:05:16 +010058 mp_uint_t last_source_line_offset;
59 mp_uint_t last_source_line;
Damien George08335002014-01-18 23:24:36 +000060
Damien George7ff996c2014-09-08 23:05:16 +010061 mp_uint_t max_num_labels;
62 mp_uint_t *label_offsets;
Damien429d7192013-10-04 19:53:11 +010063
Damien George7ff996c2014-09-08 23:05:16 +010064 mp_uint_t code_info_offset;
65 mp_uint_t code_info_size;
66 mp_uint_t bytecode_offset;
67 mp_uint_t bytecode_size;
Damien George08335002014-01-18 23:24:36 +000068 byte *code_base; // stores both byte code and code info
Damien George7ff996c2014-09-08 23:05:16 +010069 // Accessed as mp_uint_t, so must be aligned as such
Paul Sokolovsky58c95862014-07-12 14:51:48 +030070 byte dummy_data[DUMMY_DATA_SIZE];
Damien429d7192013-10-04 19:53:11 +010071};
72
Damien George1d24ea52014-04-08 21:11:49 +010073STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010074STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010075
Damien George7ff996c2014-09-08 23:05:16 +010076emit_t *emit_bc_new(mp_uint_t max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000077 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010078 emit->max_num_labels = max_num_labels;
Damien George7ff996c2014-09-08 23:05:16 +010079 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010080 return emit;
81}
Damien4b03e772013-10-05 14:17:09 +010082
Damien George41d02b62014-01-24 22:42:28 +000083void emit_bc_free(emit_t *emit) {
Damien George7ff996c2014-09-08 23:05:16 +010084 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020085 m_del_obj(emit_t, emit);
86}
87
Damien Georgeb534e1b2014-09-04 14:44:01 +010088STATIC void emit_write_uint(emit_t* emit, byte*(*allocator)(emit_t*, int), mp_uint_t val) {
89 // We store each 7 bits in a separate byte, and that's how many bytes needed
90 byte buf[BYTES_FOR_INT];
91 byte *p = buf + sizeof(buf);
92 // We encode in little-ending order, but store in big-endian, to help decoding
93 do {
94 *--p = val & 0x7f;
95 val >>= 7;
96 } while (val != 0);
97 byte* c = allocator(emit, buf + sizeof(buf) - p);
98 while (p != buf + sizeof(buf) - 1) {
99 *c++ = *p++ | 0x80;
100 }
101 *c = *p;
102}
103
Damien George08335002014-01-18 23:24:36 +0000104// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200105STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100106 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100107 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +0000108 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100109 return emit->dummy_data;
110 } else {
Damien George08335002014-01-18 23:24:36 +0000111 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
112 byte *c = emit->code_base + emit->code_info_offset;
113 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +0100114 return c;
115 }
116}
117
Damien Georgedf8127a2014-04-13 11:04:33 +0100118STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100119 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 +0100120}
121
Damien Georgeb534e1b2014-09-04 14:44:01 +0100122STATIC void emit_write_code_info_uint(emit_t* emit, mp_uint_t val) {
123 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
124}
125
126STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qst) {
127 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
Damien George08335002014-01-18 23:24:36 +0000128}
129
Damien George73496fb2014-04-13 14:51:56 +0100130#if MICROPY_ENABLE_SOURCE_LINE
Damien George7ff996c2014-09-08 23:05:16 +0100131STATIC 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 +0100132 assert(bytes_to_skip > 0 || lines_to_skip > 0);
Damien George4747bec2014-07-31 16:12:01 +0000133 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George73496fb2014-04-13 14:51:56 +0100134 while (bytes_to_skip > 0 || lines_to_skip > 0) {
Damien George4747bec2014-07-31 16:12:01 +0000135 mp_uint_t b, l;
136 if (lines_to_skip <= 6) {
137 // use 0b0LLBBBBB encoding
138 b = MIN(bytes_to_skip, 0x1f);
139 l = MIN(lines_to_skip, 0x3);
140 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
141 } else {
142 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
143 b = MIN(bytes_to_skip, 0xf);
144 l = MIN(lines_to_skip, 0x7ff);
145 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
146 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
147 ci[1] = l;
148 }
Damien George73496fb2014-04-13 14:51:56 +0100149 bytes_to_skip -= b;
150 lines_to_skip -= l;
Damien George28eb5772014-01-25 11:43:20 +0000151 }
Damien George08335002014-01-18 23:24:36 +0000152}
Damien George73496fb2014-04-13 14:51:56 +0100153#endif
Damien George08335002014-01-18 23:24:36 +0000154
155// all functions must go through this one to emit byte code
Damien George3417bc22014-05-10 10:36:38 +0100156STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000157 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100158 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100159 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000160 return emit->dummy_data;
161 } else {
Damien George3417bc22014-05-10 10:36:38 +0100162 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
163 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
164 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000165 return c;
166 }
167}
168
Damien George3417bc22014-05-10 10:36:38 +0100169STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
Damien George40f3c022014-07-03 13:25:24 +0100170 emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100171}
172
Damien George3417bc22014-05-10 10:36:38 +0100173STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
174 byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100175 c[0] = b1;
176}
177
Damien Georgeb534e1b2014-09-04 14:44:01 +0100178STATIC void emit_write_bytecode_uint(emit_t* emit, mp_uint_t val) {
179 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
180}
181
Damien George7ff996c2014-09-08 23:05:16 +0100182STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100183 assert((b2 & (~0xff)) == 0);
Damien George3417bc22014-05-10 10:36:38 +0100184 byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100185 c[0] = b1;
186 c[1] = b2;
187}
188
Damien George3417bc22014-05-10 10:36:38 +0100189// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
Damien George40f3c022014-07-03 13:25:24 +0100190STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
Damien George3417bc22014-05-10 10:36:38 +0100191 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200192
193 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100194 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200195 byte *p = buf + sizeof(buf);
196 // We encode in little-ending order, but store in big-endian, to help decoding
197 do {
198 *--p = num & 0x7f;
199 num >>= 7;
200 } while (num != 0 && num != -1);
201 // Make sure that highest bit we stored (mask 0x40) matches sign
202 // of the number. If not, store extra byte just to encode sign
203 if (num == -1 && (*p & 0x40) == 0) {
204 *--p = 0x7f;
205 } else if (num == 0 && (*p & 0x40) != 0) {
206 *--p = 0;
207 }
208
Damien George3417bc22014-05-10 10:36:38 +0100209 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200210 while (p != buf + sizeof(buf) - 1) {
211 *c++ = *p++ | 0x80;
212 }
213 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100214}
215
Damien Georgeb534e1b2014-09-04 14:44:01 +0100216STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, mp_uint_t val) {
Damien George3417bc22014-05-10 10:36:38 +0100217 emit_write_bytecode_byte(emit, b);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100218 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
Damien429d7192013-10-04 19:53:11 +0100219}
220
Damien Georgedf8127a2014-04-13 11:04:33 +0100221// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100222STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
223 emit_write_bytecode_byte(emit, b);
224 emit_align_bytecode_to_machine_word(emit);
Damien George40f3c022014-07-03 13:25:24 +0100225 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 +0300226 // Verify thar c is already uint-aligned
227 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
Damien George40f3c022014-07-03 13:25:24 +0100228 *c = (mp_uint_t)ptr;
Damien Georgedf8127a2014-04-13 11:04:33 +0100229}
230
Damien Georgefb083ea2014-02-01 18:29:40 +0000231/* currently unused
Damien George7ff996c2014-09-08 23:05:16 +0100232STATIC 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 +0100233 emit_write_bytecode_byte(emit, b);
234 emit_write_bytecode_byte_uint(emit, num1);
235 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000236}
237*/
238
Damien George7ff996c2014-09-08 23:05:16 +0100239STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
240 emit_write_bytecode_byte_uint(emit, b, qst);
Damien429d7192013-10-04 19:53:11 +0100241}
242
Damien03c9cfb2013-11-05 22:06:08 +0000243// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George7ff996c2014-09-08 23:05:16 +0100244STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) {
245 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100246 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100247 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100248 } else {
Damien George3417bc22014-05-10 10:36:38 +0100249 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100250 }
Damien George3417bc22014-05-10 10:36:38 +0100251 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000252 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100253 c[1] = bytecode_offset;
254 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000255}
256
257// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George7ff996c2014-09-08 23:05:16 +0100258STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100259 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100260 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100261 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000262 } else {
Damien George3417bc22014-05-10 10:36:38 +0100263 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000264 }
Damien George3417bc22014-05-10 10:36:38 +0100265 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000266 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100267 c[1] = bytecode_offset;
268 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100269}
270
Damien George2ac4af62014-08-15 16:45:41 +0100271STATIC void emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
Damien George6baf76e2013-12-30 22:32:17 +0000272}
273
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200274STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000275 emit->pass = pass;
276 emit->stack_size = 0;
277 emit->last_emit_was_return_value = false;
278 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000279 emit->last_source_line_offset = 0;
280 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100281 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100282 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000283 }
Damien George3417bc22014-05-10 10:36:38 +0100284 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000285 emit->code_info_offset = 0;
286
Damien Georgeb534e1b2014-09-04 14:44:01 +0100287 // Write code info size as compressed uint. If we are not in the final pass
288 // then space for this uint is reserved in emit_bc_end_pass.
289 if (pass == MP_PASS_EMIT) {
290 emit_write_code_info_uint(emit, emit->code_info_size);
Damien George08335002014-01-18 23:24:36 +0000291 }
292
Damien Georgeb534e1b2014-09-04 14:44:01 +0100293 // write the name and source file of this function
Damien Georgecbd2f742014-01-19 11:48:48 +0000294 emit_write_code_info_qstr(emit, scope->simple_name);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100295 emit_write_code_info_qstr(emit, scope->source_file);
Damien George6baf76e2013-12-30 22:32:17 +0000296
Damien Georgebee17b02014-03-27 11:07:04 +0000297 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000298 {
Damien George7ff996c2014-09-08 23:05:16 +0100299 mp_uint_t n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000300 if (n_state == 0) {
301 // Need at least 1 entry in the state, in the case an exception is
302 // propagated through this function, the exception is returned in
303 // the highest slot in the state (fastn[0], see vm.c).
304 n_state = 1;
305 }
Damien Georgeb534e1b2014-09-04 14:44:01 +0100306 emit_write_bytecode_uint(emit, n_state);
307 emit_write_bytecode_uint(emit, scope->exc_stack_size);
Damien George8dcc0c72014-03-27 10:55:21 +0000308 }
309
310 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000311 int num_cell = 0;
312 for (int i = 0; i < scope->id_info_len; i++) {
313 id_info_t *id = &scope->id_info[i];
314 if (id->kind == ID_INFO_KIND_CELL) {
315 num_cell += 1;
316 }
317 }
318 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100319 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000320 for (int i = 0; i < scope->id_info_len; i++) {
321 id_info_t *id = &scope->id_info[i];
322 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100323 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 +0000324 }
325 }
326}
327
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200328STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000329 // check stack is back to zero size
330 if (emit->stack_size != 0) {
331 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
332 }
333
Damien George73496fb2014-04-13 14:51:56 +0100334 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George08335002014-01-18 23:24:36 +0000335
Damien George36db6bc2014-05-07 17:24:22 +0100336 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100337 // Need to make sure we have enough room in the code-info block to write
338 // the size of the code-info block. Since the size is written as a
339 // compressed uint, we don't know its size until we write it! Thus, we
340 // take the biggest possible value it could be and write that here.
341 // Then there will be enough room to write the value, and any leftover
342 // space will be absorbed in the alignment at the end of the code-info
343 // block.
344 mp_uint_t max_code_info_size =
345 emit->code_info_offset // current code-info size
346 + BYTES_FOR_INT // maximum space for compressed uint
347 + BYTES_PER_WORD - 1; // maximum space for alignment padding
348 emit_write_code_info_uint(emit, max_code_info_size);
349
350 // Align code-info so that following bytecode is aligned on a machine word.
351 // We don't need to write anything here, it's just dead space between the
352 // code-info block and the bytecode block that follows it.
353 emit_align_code_info_to_machine_word(emit);
354
355 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000356 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100357 emit->bytecode_size = emit->bytecode_offset;
358 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000359
Damien George36db6bc2014-05-07 17:24:22 +0100360 } else if (emit->pass == MP_PASS_EMIT) {
Damien George2827d622014-04-27 15:50:52 +0100361 qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
362 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George7ff996c2014-09-08 23:05:16 +0100363 arg_names[i] = emit->scope->id_info[i].qst;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200364 }
Damien George3417bc22014-05-10 10:36:38 +0100365 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
366 emit->code_info_size + emit->bytecode_size,
Damien George2827d622014-04-27 15:50:52 +0100367 emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
368 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000369 }
370}
371
Damien George069a35e2014-04-10 17:22:19 +0000372STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100373 return emit->last_emit_was_return_value;
374}
375
Damien George7ff996c2014-09-08 23:05:16 +0100376STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000377 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100378}
379
Damien George7ff996c2014-09-08 23:05:16 +0100380STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100381 //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 +0000382#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300383 if (mp_optimise_value >= 3) {
384 // If we compile with -O3, don't store line numbers.
385 return;
386 }
Damien George08335002014-01-18 23:24:36 +0000387 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100388 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
389 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000390 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100391 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000392 emit->last_source_line = source_line;
393 }
Damien George62ad1892014-01-29 21:51:51 +0000394#endif
Damien George08335002014-01-18 23:24:36 +0000395}
396
Damien George7ff996c2014-09-08 23:05:16 +0100397STATIC void emit_bc_load_id(emit_t *emit, qstr qst) {
398 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100399}
400
Damien George7ff996c2014-09-08 23:05:16 +0100401STATIC void emit_bc_store_id(emit_t *emit, qstr qst) {
402 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100403}
404
Damien George7ff996c2014-09-08 23:05:16 +0100405STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) {
406 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100407}
408
Damien George7ff996c2014-09-08 23:05:16 +0100409STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
410 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100411 emit->stack_size += stack_size_delta;
412 if (emit->stack_size > emit->scope->stack_size) {
413 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100414 }
415 emit->last_emit_was_return_value = false;
416}
417
Damien George7ff996c2014-09-08 23:05:16 +0100418STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000419 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100420 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100421 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100422 // assign label offset
423 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100424 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100425 } else {
426 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100427 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
428 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100429 }
430}
431
Damien George7ff996c2014-09-08 23:05:16 +0100432STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000433 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100434 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100435}
436
Damien George7ff996c2014-09-08 23:05:16 +0100437STATIC void emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000438 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100439 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100440}
441
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200442STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000443 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100444 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100445}
446
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200447STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000448 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100449 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100450 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
451 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
452 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
453 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100454 default: assert(0);
455 }
456}
457
Damien George40f3c022014-07-03 13:25:24 +0100458STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000459 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100460 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100461}
462
Damien George7ff996c2014-09-08 23:05:16 +0100463STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000464 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100465 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
Damien429d7192013-10-04 19:53:11 +0100466}
467
Damien George7ff996c2014-09-08 23:05:16 +0100468STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000469 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100470 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
Damien429d7192013-10-04 19:53:11 +0100471}
472
Damien George7ff996c2014-09-08 23:05:16 +0100473STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000474 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100475 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100476 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100477 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100478 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100479 }
480}
481
Damien George523b5752014-03-31 11:59:23 +0100482STATIC void emit_bc_load_null(emit_t *emit) {
483 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100484 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100485};
486
Damien George7ff996c2014-09-08 23:05:16 +0100487STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100488 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000489 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100490 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100491 case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
492 case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
493 case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
494 default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100495 }
496}
497
Damien George7ff996c2014-09-08 23:05:16 +0100498STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000499 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100500 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000501}
502
Damien George7ff996c2014-09-08 23:05:16 +0100503STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000504 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100505 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100506}
507
Damien George7ff996c2014-09-08 23:05:16 +0100508STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000509 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100510 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100511}
512
Damien George7ff996c2014-09-08 23:05:16 +0100513STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000514 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100515 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100516}
517
Damien George7ff996c2014-09-08 23:05:16 +0100518STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000519 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100520 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100521}
522
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200523STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000524 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100525 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100526}
527
Damien George729f7b42014-04-17 22:10:53 +0100528STATIC void emit_bc_load_subscr(emit_t *emit) {
529 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100530 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100531}
532
Damien George7ff996c2014-09-08 23:05:16 +0100533STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100534 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000535 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100536 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100537 case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
538 case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
539 case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
540 default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100541 }
542}
543
Damien George7ff996c2014-09-08 23:05:16 +0100544STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000545 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100546 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000547}
548
Damien George7ff996c2014-09-08 23:05:16 +0100549STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000550 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100551 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100552}
553
Damien George7ff996c2014-09-08 23:05:16 +0100554STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000555 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100556 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100557}
558
Damien George7ff996c2014-09-08 23:05:16 +0100559STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000560 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100561 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100562}
563
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200564STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000565 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100566 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100567}
568
Damien George7ff996c2014-09-08 23:05:16 +0100569STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100570 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Damien George7ff996c2014-09-08 23:05:16 +0100573STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100574 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000575}
576
Damien George7ff996c2014-09-08 23:05:16 +0100577STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000578 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100579 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100580}
581
Damien George7ff996c2014-09-08 23:05:16 +0100582STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000583 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100584 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damien George7ff996c2014-09-08 23:05:16 +0100587STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
Damien George1d24ea52014-04-08 21:11:49 +0100588 emit_bc_load_null(emit);
589 emit_bc_rot_two(emit);
Damien George7ff996c2014-09-08 23:05:16 +0100590 emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200593STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100594 emit_bc_load_null(emit);
595 emit_bc_rot_three(emit);
596 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100597}
598
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200599STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000600 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100601 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100602}
603
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200604STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000605 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100606 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100607}
608
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200609STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000610 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100611 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200614STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000615 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100616 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100617}
618
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200619STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000620 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100621 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100622}
623
Damien George7ff996c2014-09-08 23:05:16 +0100624STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000625 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100626 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100627}
628
Damien George7ff996c2014-09-08 23:05:16 +0100629STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
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_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100632}
633
Damien George7ff996c2014-09-08 23:05:16 +0100634STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000635 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100636 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100637}
638
Damien George7ff996c2014-09-08 23:05:16 +0100639STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000640 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100641 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100642}
643
Damien George7ff996c2014-09-08 23:05:16 +0100644STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000645 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100646 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100647}
648
Damien George7ff996c2014-09-08 23:05:16 +0100649STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000650 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000651 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100652 if (label & MP_EMIT_BREAK_FROM_FOR) {
653 // need to pop the iterator if we are breaking out of a for loop
654 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
655 }
656 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
657 } else {
658 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
659 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000660 }
Damien429d7192013-10-04 19:53:11 +0100661}
662
Damien George7ff996c2014-09-08 23:05:16 +0100663STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000664 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100665 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100666}
667
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200668STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000669 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100670 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100671}
672
Damien George7ff996c2014-09-08 23:05:16 +0100673STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000674 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100675 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100676}
677
Damien George7ff996c2014-09-08 23:05:16 +0100678STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000679 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100680 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100681}
682
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200683STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000684 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100685 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100686}
687
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200688STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000689 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100690 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100691}
692
Damien George7ff996c2014-09-08 23:05:16 +0100693STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000694 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100695 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100696}
697
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200698STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000699 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000703 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100704 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100705}
706
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200707STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000708 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100709 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100710}
711
Damien Georged17926d2014-03-30 13:35:08 +0100712STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
713 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000714 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100715 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000716 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100717 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000718 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000719 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100720 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000721 }
Damien429d7192013-10-04 19:53:11 +0100722}
723
Damien Georged17926d2014-03-30 13:35:08 +0100724STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000725 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100726 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000727 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100728 op = MP_BINARY_OP_IN;
729 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000730 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100731 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000732 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000733 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100734 emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000735 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000736 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100737 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000738 }
Damien429d7192013-10-04 19:53:11 +0100739}
740
Damien George7ff996c2014-09-08 23:05:16 +0100741STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000742 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100743 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100744}
745
Damien George7ff996c2014-09-08 23:05:16 +0100746STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000747 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100748 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100749}
750
Damien George7ff996c2014-09-08 23:05:16 +0100751STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000752 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100753 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100754}
755
Damien George7ff996c2014-09-08 23:05:16 +0100756STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000757 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100758 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100759}
760
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200761STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000762 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100763 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100764}
765
Damien George7ff996c2014-09-08 23:05:16 +0100766STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000767 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100768 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100769}
770
Damien George7ff996c2014-09-08 23:05:16 +0100771STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000772 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100773 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100774}
775
Damien George7ff996c2014-09-08 23:05:16 +0100776STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000777 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100778 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100779}
780
Damien George7ff996c2014-09-08 23:05:16 +0100781STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000782 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100783 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100784}
785
Damien George7ff996c2014-09-08 23:05:16 +0100786STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000787 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100788 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George7ff996c2014-09-08 23:05:16 +0100791STATIC 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 +0000792 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100793 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100794}
795
Damien George7ff996c2014-09-08 23:05:16 +0100796STATIC 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 +0100797 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000798 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100799 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000800 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100801 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100802 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200803 }
Damien429d7192013-10-04 19:53:11 +0100804}
805
Damien George7ff996c2014-09-08 23:05:16 +0100806STATIC 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 +0100807 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100808 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100809 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
810 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200811 } else {
Damien George3558f622014-04-20 17:50:40 +0100812 assert(n_closed_over <= 255);
813 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100814 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
815 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200816 }
Damien429d7192013-10-04 19:53:11 +0100817}
818
Damien George7ff996c2014-09-08 23:05:16 +0100819STATIC 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 +0100820 if (star_flags) {
821 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100822 // load dummy entry for non-existent pos_seq
823 emit_bc_load_null(emit);
824 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100825 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100826 // load dummy entry for non-existent kw_dict
827 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100828 }
Damien George7ff996c2014-09-08 23:05:16 +0100829 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 +0100830 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 +0100831 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100832 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100833 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 +0100834 }
Damien George523b5752014-03-31 11:59:23 +0100835}
836
Damien George7ff996c2014-09-08 23:05:16 +0100837STATIC 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 +0100838 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100839}
840
Damien George7ff996c2014-09-08 23:05:16 +0100841STATIC 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 +0100842 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100843}
844
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200845STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000846 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100847 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100848 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100849}
850
Damien George7ff996c2014-09-08 23:05:16 +0100851STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000852 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000853 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100854 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100855}
856
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200857STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000858 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100859 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100860 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100861}
862
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200863STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000864 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100865 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100866 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100867}
868
Damien Georgeb601d952014-06-30 05:17:25 +0100869STATIC void emit_bc_start_except_handler(emit_t *emit) {
870 emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
871}
872
873STATIC void emit_bc_end_except_handler(emit_t *emit) {
874 emit_bc_adjust_stack_size(emit, -5); // stack adjust
875}
876
Damien6cdd3af2013-10-05 18:08:26 +0100877const emit_method_table_t emit_bc_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100878 emit_bc_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100879 emit_bc_start_pass,
880 emit_bc_end_pass,
881 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000882 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000883 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100884
Damien4b03e772013-10-05 14:17:09 +0100885 emit_bc_load_id,
886 emit_bc_store_id,
887 emit_bc_delete_id,
888
Damien415eb6f2013-10-05 12:19:06 +0100889 emit_bc_label_assign,
890 emit_bc_import_name,
891 emit_bc_import_from,
892 emit_bc_import_star,
893 emit_bc_load_const_tok,
894 emit_bc_load_const_small_int,
895 emit_bc_load_const_int,
896 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100897 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100898 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100899 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100900 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000901 emit_bc_load_name,
902 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100903 emit_bc_load_attr,
904 emit_bc_load_method,
905 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100906 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100907 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000908 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100909 emit_bc_store_name,
910 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100911 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100912 emit_bc_store_subscr,
913 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000914 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100915 emit_bc_delete_name,
916 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100917 emit_bc_delete_attr,
918 emit_bc_delete_subscr,
919 emit_bc_dup_top,
920 emit_bc_dup_top_two,
921 emit_bc_pop_top,
922 emit_bc_rot_two,
923 emit_bc_rot_three,
924 emit_bc_jump,
925 emit_bc_pop_jump_if_true,
926 emit_bc_pop_jump_if_false,
927 emit_bc_jump_if_true_or_pop,
928 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000929 emit_bc_unwind_jump,
930 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100931 emit_bc_setup_with,
932 emit_bc_with_cleanup,
933 emit_bc_setup_except,
934 emit_bc_setup_finally,
935 emit_bc_end_finally,
936 emit_bc_get_iter,
937 emit_bc_for_iter,
938 emit_bc_for_iter_end,
939 emit_bc_pop_block,
940 emit_bc_pop_except,
941 emit_bc_unary_op,
942 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100943 emit_bc_build_tuple,
944 emit_bc_build_list,
945 emit_bc_list_append,
946 emit_bc_build_map,
947 emit_bc_store_map,
948 emit_bc_map_add,
949 emit_bc_build_set,
950 emit_bc_set_add,
951 emit_bc_build_slice,
952 emit_bc_unpack_sequence,
953 emit_bc_unpack_ex,
954 emit_bc_make_function,
955 emit_bc_make_closure,
956 emit_bc_call_function,
957 emit_bc_call_method,
958 emit_bc_return_value,
959 emit_bc_raise_varargs,
960 emit_bc_yield_value,
961 emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100962
963 emit_bc_start_except_handler,
964 emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100965};
Damien George5f6a25f2014-04-20 18:02:27 +0100966
967#endif // !MICROPY_EMIT_CPYTHON