blob: cee6d3396c1cf0e1075873152df5befdc1ea691a [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 George1084b0f2014-10-25 15:07:02 +0100221STATIC void emit_write_bytecode_prealigned_ptr(emit_t* emit, void *ptr) {
222 mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
223 // Verify thar c is already uint-aligned
224 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
225 *c = (mp_uint_t)ptr;
226}
227
Damien Georgedf8127a2014-04-13 11:04:33 +0100228// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100229STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
230 emit_write_bytecode_byte(emit, b);
231 emit_align_bytecode_to_machine_word(emit);
Damien George40f3c022014-07-03 13:25:24 +0100232 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 +0300233 // Verify thar c is already uint-aligned
234 assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
Damien George40f3c022014-07-03 13:25:24 +0100235 *c = (mp_uint_t)ptr;
Damien Georgedf8127a2014-04-13 11:04:33 +0100236}
237
Damien Georgefb083ea2014-02-01 18:29:40 +0000238/* currently unused
Damien George7ff996c2014-09-08 23:05:16 +0100239STATIC 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 +0100240 emit_write_bytecode_byte(emit, b);
241 emit_write_bytecode_byte_uint(emit, num1);
242 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000243}
244*/
245
Damien George7ff996c2014-09-08 23:05:16 +0100246STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
247 emit_write_bytecode_byte_uint(emit, b, qst);
Damien429d7192013-10-04 19:53:11 +0100248}
249
Damien03c9cfb2013-11-05 22:06:08 +0000250// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George7ff996c2014-09-08 23:05:16 +0100251STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, mp_uint_t label) {
252 mp_uint_t bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100253 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100254 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100255 } else {
Damien George3417bc22014-05-10 10:36:38 +0100256 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100257 }
Damien George3417bc22014-05-10 10:36:38 +0100258 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000259 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100260 c[1] = bytecode_offset;
261 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000262}
263
264// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George7ff996c2014-09-08 23:05:16 +0100265STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint_t label) {
Damien George3417bc22014-05-10 10:36:38 +0100266 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100267 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100268 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000269 } else {
Damien George3417bc22014-05-10 10:36:38 +0100270 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000271 }
Damien George3417bc22014-05-10 10:36:38 +0100272 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000273 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100274 c[1] = bytecode_offset;
275 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100276}
277
Damien George2ac4af62014-08-15 16:45:41 +0100278STATIC 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 +0000279}
280
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200281STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000282 emit->pass = pass;
283 emit->stack_size = 0;
284 emit->last_emit_was_return_value = false;
285 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000286 emit->last_source_line_offset = 0;
287 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100288 if (pass < MP_PASS_EMIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100289 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
Damien George6baf76e2013-12-30 22:32:17 +0000290 }
Damien George3417bc22014-05-10 10:36:38 +0100291 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000292 emit->code_info_offset = 0;
293
Damien Georgeb534e1b2014-09-04 14:44:01 +0100294 // Write code info size as compressed uint. If we are not in the final pass
295 // then space for this uint is reserved in emit_bc_end_pass.
296 if (pass == MP_PASS_EMIT) {
297 emit_write_code_info_uint(emit, emit->code_info_size);
Damien George08335002014-01-18 23:24:36 +0000298 }
299
Damien Georgeb534e1b2014-09-04 14:44:01 +0100300 // write the name and source file of this function
Damien Georgecbd2f742014-01-19 11:48:48 +0000301 emit_write_code_info_qstr(emit, scope->simple_name);
Damien Georgeb534e1b2014-09-04 14:44:01 +0100302 emit_write_code_info_qstr(emit, scope->source_file);
Damien George6baf76e2013-12-30 22:32:17 +0000303
Damien George1084b0f2014-10-25 15:07:02 +0100304 // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
305 // we store them as full word-sized objects for efficient access in mp_setup_code_state
306 // this is the start of the prelude and is guaranteed to be aligned on a word boundary
307 {
308 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
309 emit_write_bytecode_prealigned_ptr(emit, MP_OBJ_NEW_QSTR(scope->id_info[i].qst));
310 }
311 }
312
313 // bytecode prelude: local state size and exception stack size
Damien George8dcc0c72014-03-27 10:55:21 +0000314 {
Damien George7ff996c2014-09-08 23:05:16 +0100315 mp_uint_t n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000316 if (n_state == 0) {
317 // Need at least 1 entry in the state, in the case an exception is
318 // propagated through this function, the exception is returned in
319 // the highest slot in the state (fastn[0], see vm.c).
320 n_state = 1;
321 }
Damien Georgeb534e1b2014-09-04 14:44:01 +0100322 emit_write_bytecode_uint(emit, n_state);
323 emit_write_bytecode_uint(emit, scope->exc_stack_size);
Damien George8dcc0c72014-03-27 10:55:21 +0000324 }
325
326 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000327 int num_cell = 0;
328 for (int i = 0; i < scope->id_info_len; i++) {
329 id_info_t *id = &scope->id_info[i];
330 if (id->kind == ID_INFO_KIND_CELL) {
331 num_cell += 1;
332 }
333 }
334 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100335 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000336 for (int i = 0; i < scope->id_info_len; i++) {
337 id_info_t *id = &scope->id_info[i];
338 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100339 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 +0000340 }
341 }
342}
343
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200344STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000345 // check stack is back to zero size
346 if (emit->stack_size != 0) {
347 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
348 }
349
Damien George73496fb2014-04-13 14:51:56 +0100350 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George08335002014-01-18 23:24:36 +0000351
Damien George36db6bc2014-05-07 17:24:22 +0100352 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100353 // Need to make sure we have enough room in the code-info block to write
354 // the size of the code-info block. Since the size is written as a
355 // compressed uint, we don't know its size until we write it! Thus, we
356 // take the biggest possible value it could be and write that here.
357 // Then there will be enough room to write the value, and any leftover
358 // space will be absorbed in the alignment at the end of the code-info
359 // block.
360 mp_uint_t max_code_info_size =
361 emit->code_info_offset // current code-info size
362 + BYTES_FOR_INT // maximum space for compressed uint
363 + BYTES_PER_WORD - 1; // maximum space for alignment padding
364 emit_write_code_info_uint(emit, max_code_info_size);
365
366 // Align code-info so that following bytecode is aligned on a machine word.
367 // We don't need to write anything here, it's just dead space between the
368 // code-info block and the bytecode block that follows it.
369 emit_align_code_info_to_machine_word(emit);
370
371 // calculate size of total code-info + bytecode, in bytes
Damien George08335002014-01-18 23:24:36 +0000372 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100373 emit->bytecode_size = emit->bytecode_offset;
374 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000375
Damien George36db6bc2014-05-07 17:24:22 +0100376 } else if (emit->pass == MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100377 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
378 emit->code_info_size + emit->bytecode_size,
Damien George1084b0f2014-10-25 15:07:02 +0100379 emit->scope->num_pos_args, emit->scope->num_kwonly_args,
Damien George2827d622014-04-27 15:50:52 +0100380 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000381 }
382}
383
Damien George069a35e2014-04-10 17:22:19 +0000384STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100385 return emit->last_emit_was_return_value;
386}
387
Damien George7ff996c2014-09-08 23:05:16 +0100388STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000389 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100390}
391
Damien George7ff996c2014-09-08 23:05:16 +0100392STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100393 //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 +0000394#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovskyb8f117d2014-06-02 19:39:15 +0300395 if (mp_optimise_value >= 3) {
396 // If we compile with -O3, don't store line numbers.
397 return;
398 }
Damien George08335002014-01-18 23:24:36 +0000399 if (source_line > emit->last_source_line) {
Damien George7ff996c2014-09-08 23:05:16 +0100400 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
401 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
Damien George28eb5772014-01-25 11:43:20 +0000402 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100403 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000404 emit->last_source_line = source_line;
405 }
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_load_id(emit_t *emit, qstr qst) {
410 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100411}
412
Damien George7ff996c2014-09-08 23:05:16 +0100413STATIC void emit_bc_store_id(emit_t *emit, qstr qst) {
414 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100415}
416
Damien George7ff996c2014-09-08 23:05:16 +0100417STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) {
418 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst);
Damien4b03e772013-10-05 14:17:09 +0100419}
420
Damien George7ff996c2014-09-08 23:05:16 +0100421STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
422 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100423 emit->stack_size += stack_size_delta;
424 if (emit->stack_size > emit->scope->stack_size) {
425 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100426 }
427 emit->last_emit_was_return_value = false;
428}
429
Damien George7ff996c2014-09-08 23:05:16 +0100430STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000431 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100432 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100433 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100434 // assign label offset
435 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100436 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100437 } else {
438 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100439 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
440 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100441 }
442}
443
Damien George7ff996c2014-09-08 23:05:16 +0100444STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000445 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100446 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Damien George7ff996c2014-09-08 23:05:16 +0100449STATIC void emit_bc_import_from(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000450 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100451 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
Damien429d7192013-10-04 19:53:11 +0100452}
453
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200454STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000455 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100456 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100457}
458
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200459STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000460 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100461 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100462 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
463 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
464 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
465 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100466 default: assert(0);
467 }
468}
469
Damien George40f3c022014-07-03 13:25:24 +0100470STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000471 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100472 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Damien George7ff996c2014-09-08 23:05:16 +0100475STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000476 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100477 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Damien George7ff996c2014-09-08 23:05:16 +0100480STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000481 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100482 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
Damien429d7192013-10-04 19:53:11 +0100483}
484
Damien George7ff996c2014-09-08 23:05:16 +0100485STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000486 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100487 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100488 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100489 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100490 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100491 }
492}
493
Damien George523b5752014-03-31 11:59:23 +0100494STATIC void emit_bc_load_null(emit_t *emit) {
495 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100496 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100497};
498
Damien George7ff996c2014-09-08 23:05:16 +0100499STATIC 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 +0100500 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000501 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100502 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100503 case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
504 case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
505 case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
506 default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100507 }
508}
509
Damien George7ff996c2014-09-08 23:05:16 +0100510STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000511 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100512 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000513}
514
Damien George7ff996c2014-09-08 23:05:16 +0100515STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000516 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100517 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100518}
519
Damien George7ff996c2014-09-08 23:05:16 +0100520STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000521 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100522 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100523}
524
Damien George7ff996c2014-09-08 23:05:16 +0100525STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000526 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100527 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100528}
529
Damien George7ff996c2014-09-08 23:05:16 +0100530STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000531 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100532 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100533}
534
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200535STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000536 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100537 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100538}
539
Damien George729f7b42014-04-17 22:10:53 +0100540STATIC void emit_bc_load_subscr(emit_t *emit) {
541 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100542 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100543}
544
Damien George7ff996c2014-09-08 23:05:16 +0100545STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100546 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000547 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100548 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100549 case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
550 case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
551 case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
552 default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100553 }
554}
555
Damien George7ff996c2014-09-08 23:05:16 +0100556STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000557 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100558 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000559}
560
Damien George7ff996c2014-09-08 23:05:16 +0100561STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000562 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100563 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100564}
565
Damien George7ff996c2014-09-08 23:05:16 +0100566STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000567 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100568 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100569}
570
Damien George7ff996c2014-09-08 23:05:16 +0100571STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000572 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100573 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100574}
575
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200576STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000577 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100578 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100579}
580
Damien George7ff996c2014-09-08 23:05:16 +0100581STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100582 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100583}
584
Damien George7ff996c2014-09-08 23:05:16 +0100585STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100586 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000587}
588
Damien George7ff996c2014-09-08 23:05:16 +0100589STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000590 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100591 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100592}
593
Damien George7ff996c2014-09-08 23:05:16 +0100594STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000595 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100596 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100597}
598
Damien George7ff996c2014-09-08 23:05:16 +0100599STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
Damien George1d24ea52014-04-08 21:11:49 +0100600 emit_bc_load_null(emit);
601 emit_bc_rot_two(emit);
Damien George7ff996c2014-09-08 23:05:16 +0100602 emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100603}
604
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200605STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100606 emit_bc_load_null(emit);
607 emit_bc_rot_three(emit);
608 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100609}
610
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200611STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000612 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100613 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100614}
615
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000617 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100618 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100619}
620
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200621STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000622 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100623 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100624}
625
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200626STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000627 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100628 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100629}
630
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200631STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000632 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100633 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100634}
635
Damien George7ff996c2014-09-08 23:05:16 +0100636STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000637 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100638 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100639}
640
Damien George7ff996c2014-09-08 23:05:16 +0100641STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000642 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100643 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Damien George7ff996c2014-09-08 23:05:16 +0100646STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000647 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100648 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100649}
650
Damien George7ff996c2014-09-08 23:05:16 +0100651STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000652 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100653 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100654}
655
Damien George7ff996c2014-09-08 23:05:16 +0100656STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000657 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100658 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100659}
660
Damien George7ff996c2014-09-08 23:05:16 +0100661STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000662 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000663 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100664 if (label & MP_EMIT_BREAK_FROM_FOR) {
665 // need to pop the iterator if we are breaking out of a for loop
666 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
667 }
668 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
669 } else {
670 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
671 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000672 }
Damien429d7192013-10-04 19:53:11 +0100673}
674
Damien George7ff996c2014-09-08 23:05:16 +0100675STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000676 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100677 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100678}
679
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200680STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000681 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100682 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien George7ff996c2014-09-08 23:05:16 +0100685STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000686 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100687 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Damien George7ff996c2014-09-08 23:05:16 +0100690STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000691 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100692 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100693}
694
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200695STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000696 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100697 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100698}
699
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200700STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000701 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100702 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100703}
704
Damien George7ff996c2014-09-08 23:05:16 +0100705STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000706 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100707 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100708}
709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200710STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000711 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200714STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000715 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100716 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100717}
718
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200719STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000720 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100721 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100722}
723
Damien Georged17926d2014-03-30 13:35:08 +0100724STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
725 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000726 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100727 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000728 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100729 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000730 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000731 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100732 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000733 }
Damien429d7192013-10-04 19:53:11 +0100734}
735
Damien Georged17926d2014-03-30 13:35:08 +0100736STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000737 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100738 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000739 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100740 op = MP_BINARY_OP_IN;
741 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000742 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100743 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000744 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000745 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100746 emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000747 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000748 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100749 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000750 }
Damien429d7192013-10-04 19:53:11 +0100751}
752
Damien George7ff996c2014-09-08 23:05:16 +0100753STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000754 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100755 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100756}
757
Damien George7ff996c2014-09-08 23:05:16 +0100758STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000759 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100760 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100761}
762
Damien George7ff996c2014-09-08 23:05:16 +0100763STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000764 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100765 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100766}
767
Damien George7ff996c2014-09-08 23:05:16 +0100768STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000769 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100770 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100771}
772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000774 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100775 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100776}
777
Damien George7ff996c2014-09-08 23:05:16 +0100778STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000779 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100780 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100781}
782
Damien George7ff996c2014-09-08 23:05:16 +0100783STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000784 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100785 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100786}
787
Damien George7ff996c2014-09-08 23:05:16 +0100788STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000789 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100790 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100791}
792
Damien George7ff996c2014-09-08 23:05:16 +0100793STATIC void emit_bc_build_slice(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_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100796}
797
Damien George7ff996c2014-09-08 23:05:16 +0100798STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000799 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100800 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100801}
802
Damien George7ff996c2014-09-08 23:05:16 +0100803STATIC 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 +0000804 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100805 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100806}
807
Damien George7ff996c2014-09-08 23:05:16 +0100808STATIC 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 +0100809 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000810 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100811 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000812 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100813 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100814 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200815 }
Damien429d7192013-10-04 19:53:11 +0100816}
817
Damien George7ff996c2014-09-08 23:05:16 +0100818STATIC 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 +0100819 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100820 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100821 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
822 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200823 } else {
Damien George3558f622014-04-20 17:50:40 +0100824 assert(n_closed_over <= 255);
825 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100826 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
827 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200828 }
Damien429d7192013-10-04 19:53:11 +0100829}
830
Damien George7ff996c2014-09-08 23:05:16 +0100831STATIC 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 +0100832 if (star_flags) {
833 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100834 // load dummy entry for non-existent pos_seq
835 emit_bc_load_null(emit);
836 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100837 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100838 // load dummy entry for non-existent kw_dict
839 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100840 }
Damien George7ff996c2014-09-08 23:05:16 +0100841 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
Damien George3417bc22014-05-10 10:36:38 +0100842 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 +0100843 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100844 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100845 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 +0100846 }
Damien George523b5752014-03-31 11:59:23 +0100847}
848
Damien George7ff996c2014-09-08 23:05:16 +0100849STATIC 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 +0100850 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100851}
852
Damien George7ff996c2014-09-08 23:05:16 +0100853STATIC 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 +0100854 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100855}
856
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200857STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000858 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100859 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100860 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100861}
862
Damien George7ff996c2014-09-08 23:05:16 +0100863STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000864 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000865 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100866 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100867}
868
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200869STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000870 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100871 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100872 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100873}
874
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200875STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000876 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100877 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100878 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100879}
880
Damien Georgeb601d952014-06-30 05:17:25 +0100881STATIC void emit_bc_start_except_handler(emit_t *emit) {
882 emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
883}
884
885STATIC void emit_bc_end_except_handler(emit_t *emit) {
886 emit_bc_adjust_stack_size(emit, -5); // stack adjust
887}
888
Damien6cdd3af2013-10-05 18:08:26 +0100889const emit_method_table_t emit_bc_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100890 emit_bc_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100891 emit_bc_start_pass,
892 emit_bc_end_pass,
893 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000894 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000895 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100896
Damien4b03e772013-10-05 14:17:09 +0100897 emit_bc_load_id,
898 emit_bc_store_id,
899 emit_bc_delete_id,
900
Damien415eb6f2013-10-05 12:19:06 +0100901 emit_bc_label_assign,
902 emit_bc_import_name,
903 emit_bc_import_from,
904 emit_bc_import_star,
905 emit_bc_load_const_tok,
906 emit_bc_load_const_small_int,
907 emit_bc_load_const_int,
908 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100909 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100910 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100911 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100912 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000913 emit_bc_load_name,
914 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100915 emit_bc_load_attr,
916 emit_bc_load_method,
917 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100918 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100919 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000920 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100921 emit_bc_store_name,
922 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100923 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100924 emit_bc_store_subscr,
925 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000926 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100927 emit_bc_delete_name,
928 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100929 emit_bc_delete_attr,
930 emit_bc_delete_subscr,
931 emit_bc_dup_top,
932 emit_bc_dup_top_two,
933 emit_bc_pop_top,
934 emit_bc_rot_two,
935 emit_bc_rot_three,
936 emit_bc_jump,
937 emit_bc_pop_jump_if_true,
938 emit_bc_pop_jump_if_false,
939 emit_bc_jump_if_true_or_pop,
940 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000941 emit_bc_unwind_jump,
942 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100943 emit_bc_setup_with,
944 emit_bc_with_cleanup,
945 emit_bc_setup_except,
946 emit_bc_setup_finally,
947 emit_bc_end_finally,
948 emit_bc_get_iter,
949 emit_bc_for_iter,
950 emit_bc_for_iter_end,
951 emit_bc_pop_block,
952 emit_bc_pop_except,
953 emit_bc_unary_op,
954 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100955 emit_bc_build_tuple,
956 emit_bc_build_list,
957 emit_bc_list_append,
958 emit_bc_build_map,
959 emit_bc_store_map,
960 emit_bc_map_add,
961 emit_bc_build_set,
962 emit_bc_set_add,
963 emit_bc_build_slice,
964 emit_bc_unpack_sequence,
965 emit_bc_unpack_ex,
966 emit_bc_make_function,
967 emit_bc_make_closure,
968 emit_bc_call_function,
969 emit_bc_call_method,
970 emit_bc_return_value,
971 emit_bc_raise_varargs,
972 emit_bc_yield_value,
973 emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100974
975 emit_bc_start_except_handler,
976 emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100977};
Damien George5f6a25f2014-04-20 18:02:27 +0100978
979#endif // !MICROPY_EMIT_CPYTHON