blob: 723d5eda25215d997e804c3a444a68907ae25122 [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 George8456cc02014-10-25 16:43:46 +0100472 if (-16 <= arg && arg <= 47) {
473 emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
474 } else {
475 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
476 }
Damien429d7192013-10-04 19:53:11 +0100477}
478
Damien George7ff996c2014-09-08 23:05:16 +0100479STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000480 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100481 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
Damien429d7192013-10-04 19:53:11 +0100482}
483
Damien George7ff996c2014-09-08 23:05:16 +0100484STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000485 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100486 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
Damien429d7192013-10-04 19:53:11 +0100487}
488
Damien George7ff996c2014-09-08 23:05:16 +0100489STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000490 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100491 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +0100492 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
Damien429d7192013-10-04 19:53:11 +0100493 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100494 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
Damien429d7192013-10-04 19:53:11 +0100495 }
496}
497
Damien George523b5752014-03-31 11:59:23 +0100498STATIC void emit_bc_load_null(emit_t *emit) {
499 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100500 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100501};
502
Damien George7ff996c2014-09-08 23:05:16 +0100503STATIC 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 +0100504 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000505 emit_bc_pre(emit, 1);
Damien George8456cc02014-10-25 16:43:46 +0100506 if (local_num <= 15) {
507 emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
508 } else {
509 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100510 }
511}
512
Damien George7ff996c2014-09-08 23:05:16 +0100513STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000514 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100515 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000516}
517
Damien George7ff996c2014-09-08 23:05:16 +0100518STATIC void emit_bc_load_name(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_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100521}
522
Damien George7ff996c2014-09-08 23:05:16 +0100523STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000524 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100525 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100526}
527
Damien George7ff996c2014-09-08 23:05:16 +0100528STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000529 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100530 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100531}
532
Damien George7ff996c2014-09-08 23:05:16 +0100533STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000534 emit_bc_pre(emit, 1);
Damien George7ff996c2014-09-08 23:05:16 +0100535 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
Damien429d7192013-10-04 19:53:11 +0100536}
537
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200538STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000539 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100540 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100541}
542
Damien George729f7b42014-04-17 22:10:53 +0100543STATIC void emit_bc_load_subscr(emit_t *emit) {
544 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100545 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100546}
547
Damien George7ff996c2014-09-08 23:05:16 +0100548STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien429d7192013-10-04 19:53:11 +0100549 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000550 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100551 if (local_num <= 15) {
552 emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
553 } else {
554 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100555 }
556}
557
Damien George7ff996c2014-09-08 23:05:16 +0100558STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000559 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100560 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000561}
562
Damien George7ff996c2014-09-08 23:05:16 +0100563STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000564 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100565 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Damien George7ff996c2014-09-08 23:05:16 +0100568STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000569 emit_bc_pre(emit, -1);
Damien George7ff996c2014-09-08 23:05:16 +0100570 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Damien George7ff996c2014-09-08 23:05:16 +0100573STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000574 emit_bc_pre(emit, -2);
Damien George7ff996c2014-09-08 23:05:16 +0100575 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
Damien429d7192013-10-04 19:53:11 +0100576}
577
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200578STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000579 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100580 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100581}
582
Damien George7ff996c2014-09-08 23:05:16 +0100583STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100584 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damien George7ff996c2014-09-08 23:05:16 +0100587STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100588 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000589}
590
Damien George7ff996c2014-09-08 23:05:16 +0100591STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000592 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100593 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
Damien429d7192013-10-04 19:53:11 +0100594}
595
Damien George7ff996c2014-09-08 23:05:16 +0100596STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000597 emit_bc_pre(emit, 0);
Damien George7ff996c2014-09-08 23:05:16 +0100598 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Damien George7ff996c2014-09-08 23:05:16 +0100601STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
Damien George1d24ea52014-04-08 21:11:49 +0100602 emit_bc_load_null(emit);
603 emit_bc_rot_two(emit);
Damien George7ff996c2014-09-08 23:05:16 +0100604 emit_bc_store_attr(emit, qst);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100608 emit_bc_load_null(emit);
609 emit_bc_rot_three(emit);
610 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200613STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000614 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100615 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200618STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000619 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100620 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100621}
622
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200623STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000624 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100625 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100626}
627
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200628STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100630 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100631}
632
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200633STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000634 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100635 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Damien George7ff996c2014-09-08 23:05:16 +0100638STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000639 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100640 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100641}
642
Damien George7ff996c2014-09-08 23:05:16 +0100643STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000644 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100645 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100646}
647
Damien George7ff996c2014-09-08 23:05:16 +0100648STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000649 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100650 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100651}
652
Damien George7ff996c2014-09-08 23:05:16 +0100653STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000654 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100655 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100656}
657
Damien George7ff996c2014-09-08 23:05:16 +0100658STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000659 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100660 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100661}
662
Damien George7ff996c2014-09-08 23:05:16 +0100663STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000664 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000665 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100666 if (label & MP_EMIT_BREAK_FROM_FOR) {
667 // need to pop the iterator if we are breaking out of a for loop
668 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
669 }
670 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
671 } else {
672 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
673 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000674 }
Damien429d7192013-10-04 19:53:11 +0100675}
676
Damien George7ff996c2014-09-08 23:05:16 +0100677STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000678 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100679 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100680}
681
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200682STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000683 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100684 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100685}
686
Damien George7ff996c2014-09-08 23:05:16 +0100687STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000688 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100689 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100690}
691
Damien George7ff996c2014-09-08 23:05:16 +0100692STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
Damien George069a35e2014-04-10 17:22:19 +0000693 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100694 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100695}
696
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200697STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000698 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100699 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_bc_get_iter(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_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100705}
706
Damien George7ff996c2014-09-08 23:05:16 +0100707STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000708 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100709 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100710}
711
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200712STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000713 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200716STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000717 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100718 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100719}
720
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200721STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000722 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100723 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100724}
725
Damien Georged17926d2014-03-30 13:35:08 +0100726STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
727 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000728 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100729 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000730 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100731 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000732 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000733 emit_bc_pre(emit, 0);
Damien George8456cc02014-10-25 16:43:46 +0100734 emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000735 }
Damien429d7192013-10-04 19:53:11 +0100736}
737
Damien Georged17926d2014-03-30 13:35:08 +0100738STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000739 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100740 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000741 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100742 op = MP_BINARY_OP_IN;
743 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000744 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100745 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000746 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000747 emit_bc_pre(emit, -1);
Damien George8456cc02014-10-25 16:43:46 +0100748 emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
Damien George9aa2a522014-02-01 23:04:09 +0000749 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000750 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100751 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000752 }
Damien429d7192013-10-04 19:53:11 +0100753}
754
Damien George7ff996c2014-09-08 23:05:16 +0100755STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000756 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100757 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100758}
759
Damien George7ff996c2014-09-08 23:05:16 +0100760STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000761 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100762 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100763}
764
Damien George7ff996c2014-09-08 23:05:16 +0100765STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000766 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100767 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100768}
769
Damien George7ff996c2014-09-08 23:05:16 +0100770STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000771 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100772 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100773}
774
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200775STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000776 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100777 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100778}
779
Damien George7ff996c2014-09-08 23:05:16 +0100780STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000781 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100782 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100783}
784
Damien Georgee37dcaa2014-12-27 17:07:16 +0000785#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +0100786STATIC void emit_bc_build_set(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_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George7ff996c2014-09-08 23:05:16 +0100791STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000792 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100793 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100794}
Damien Georgee37dcaa2014-12-27 17:07:16 +0000795#endif
Damien429d7192013-10-04 19:53:11 +0100796
Damien George7ff996c2014-09-08 23:05:16 +0100797STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000798 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100799 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100800}
801
Damien George7ff996c2014-09-08 23:05:16 +0100802STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000803 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100804 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100805}
806
Damien George7ff996c2014-09-08 23:05:16 +0100807STATIC void emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000808 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100809 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100810}
811
Damien George7ff996c2014-09-08 23:05:16 +0100812STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100813 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000814 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100815 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000816 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100817 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100818 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200819 }
Damien429d7192013-10-04 19:53:11 +0100820}
821
Damien George7ff996c2014-09-08 23:05:16 +0100822STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100823 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100824 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100825 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
826 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200827 } else {
Damien George3558f622014-04-20 17:50:40 +0100828 assert(n_closed_over <= 255);
829 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100830 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
831 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200832 }
Damien429d7192013-10-04 19:53:11 +0100833}
834
Damien George7ff996c2014-09-08 23:05:16 +0100835STATIC void emit_bc_call_function_method_helper(emit_t *emit, mp_int_t stack_adj, mp_uint_t bytecode_base, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100836 if (star_flags) {
837 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100838 // load dummy entry for non-existent pos_seq
839 emit_bc_load_null(emit);
840 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100841 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100842 // load dummy entry for non-existent kw_dict
843 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100844 }
Damien George7ff996c2014-09-08 23:05:16 +0100845 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
Damien George3417bc22014-05-10 10:36:38 +0100846 emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100847 } else {
Damien George7ff996c2014-09-08 23:05:16 +0100848 emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100849 emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100850 }
Damien George523b5752014-03-31 11:59:23 +0100851}
852
Damien George7ff996c2014-09-08 23:05:16 +0100853STATIC void emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100854 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100855}
856
Damien George7ff996c2014-09-08 23:05:16 +0100857STATIC void emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +0100858 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100859}
860
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200861STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000862 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100863 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100864 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100865}
866
Damien George7ff996c2014-09-08 23:05:16 +0100867STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien George25042b12014-01-11 09:33:39 +0000868 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000869 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100870 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100871}
872
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200873STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000874 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100875 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100876 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100877}
878
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200879STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000880 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100881 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100882 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100883}
884
Damien Georgeb601d952014-06-30 05:17:25 +0100885STATIC void emit_bc_start_except_handler(emit_t *emit) {
886 emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
887}
888
889STATIC void emit_bc_end_except_handler(emit_t *emit) {
890 emit_bc_adjust_stack_size(emit, -5); // stack adjust
891}
892
Damien6cdd3af2013-10-05 18:08:26 +0100893const emit_method_table_t emit_bc_method_table = {
Damien George2ac4af62014-08-15 16:45:41 +0100894 emit_bc_set_native_type,
Damien415eb6f2013-10-05 12:19:06 +0100895 emit_bc_start_pass,
896 emit_bc_end_pass,
897 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000898 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000899 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100900
Damien4b03e772013-10-05 14:17:09 +0100901 emit_bc_load_id,
902 emit_bc_store_id,
903 emit_bc_delete_id,
904
Damien415eb6f2013-10-05 12:19:06 +0100905 emit_bc_label_assign,
906 emit_bc_import_name,
907 emit_bc_import_from,
908 emit_bc_import_star,
909 emit_bc_load_const_tok,
910 emit_bc_load_const_small_int,
911 emit_bc_load_const_int,
912 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100913 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100914 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100915 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100916 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000917 emit_bc_load_name,
918 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100919 emit_bc_load_attr,
920 emit_bc_load_method,
921 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100922 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100923 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000924 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100925 emit_bc_store_name,
926 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100927 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100928 emit_bc_store_subscr,
929 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000930 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100931 emit_bc_delete_name,
932 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100933 emit_bc_delete_attr,
934 emit_bc_delete_subscr,
935 emit_bc_dup_top,
936 emit_bc_dup_top_two,
937 emit_bc_pop_top,
938 emit_bc_rot_two,
939 emit_bc_rot_three,
940 emit_bc_jump,
941 emit_bc_pop_jump_if_true,
942 emit_bc_pop_jump_if_false,
943 emit_bc_jump_if_true_or_pop,
944 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000945 emit_bc_unwind_jump,
946 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100947 emit_bc_setup_with,
948 emit_bc_with_cleanup,
949 emit_bc_setup_except,
950 emit_bc_setup_finally,
951 emit_bc_end_finally,
952 emit_bc_get_iter,
953 emit_bc_for_iter,
954 emit_bc_for_iter_end,
955 emit_bc_pop_block,
956 emit_bc_pop_except,
957 emit_bc_unary_op,
958 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100959 emit_bc_build_tuple,
960 emit_bc_build_list,
961 emit_bc_list_append,
962 emit_bc_build_map,
963 emit_bc_store_map,
964 emit_bc_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000965 #if MICROPY_PY_BUILTINS_SET
Damien415eb6f2013-10-05 12:19:06 +0100966 emit_bc_build_set,
967 emit_bc_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000968 #endif
Damien415eb6f2013-10-05 12:19:06 +0100969 emit_bc_build_slice,
970 emit_bc_unpack_sequence,
971 emit_bc_unpack_ex,
972 emit_bc_make_function,
973 emit_bc_make_closure,
974 emit_bc_call_function,
975 emit_bc_call_method,
976 emit_bc_return_value,
977 emit_bc_raise_varargs,
978 emit_bc_yield_value,
979 emit_bc_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +0100980
981 emit_bc_start_except_handler,
982 emit_bc_end_except_handler,
Damien415eb6f2013-10-05 12:19:06 +0100983};
Damien George5f6a25f2014-04-20 18:02:27 +0100984
985#endif // !MICROPY_EMIT_CPYTHON