blob: ce96d698283f90c78eb7351b3ded1b9c8ad4274c [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
33#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.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
Damien415eb6f2013-10-05 12:19:06 +010047struct _emit_t {
48 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010049 int stack_size;
50 bool last_emit_was_return_value;
51
52 scope_t *scope;
53
Damien George08335002014-01-18 23:24:36 +000054 uint last_source_line_offset;
55 uint last_source_line;
56
Damienb05d7072013-10-05 13:37:10 +010057 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010058 uint *label_offsets;
59
Damien George08335002014-01-18 23:24:36 +000060 uint code_info_offset;
61 uint code_info_size;
62 uint byte_code_offset;
63 uint byte_code_size;
64 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010065 byte dummy_data[8];
66};
67
Damien George1d24ea52014-04-08 21:11:49 +010068STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010069STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010070
Damien Georgecbd2f742014-01-19 11:48:48 +000071emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000072 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010073 emit->max_num_labels = max_num_labels;
74 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010075 return emit;
76}
Damien4b03e772013-10-05 14:17:09 +010077
Damien George41d02b62014-01-24 22:42:28 +000078void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020079 m_del(uint, emit->label_offsets, emit->max_num_labels);
80 m_del_obj(emit_t, emit);
81}
82
Damien George08335002014-01-18 23:24:36 +000083// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020084STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010085 //printf("emit %d\n", num_bytes_to_write);
86 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +000087 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010088 return emit->dummy_data;
89 } else {
Damien George08335002014-01-18 23:24:36 +000090 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
91 byte *c = emit->code_base + emit->code_info_offset;
92 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010093 return c;
94 }
95}
96
Damien Georgedf8127a2014-04-13 11:04:33 +010097STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
98 emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
99}
100
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200101STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +0000102 byte* c = emit_get_cur_to_write_code_info(emit, 4);
103 // TODO variable length encoding for qstr
104 c[0] = qstr & 0xff;
105 c[1] = (qstr >> 8) & 0xff;
106 c[2] = (qstr >> 16) & 0xff;
107 c[3] = (qstr >> 24) & 0xff;
108}
109
Damien George73496fb2014-04-13 14:51:56 +0100110#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200111STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, uint lines_to_skip) {
Damien George73496fb2014-04-13 14:51:56 +0100112 assert(bytes_to_skip > 0 || lines_to_skip > 0);
113 while (bytes_to_skip > 0 || lines_to_skip > 0) {
114 uint b = MIN(bytes_to_skip, 31);
115 uint l = MIN(lines_to_skip, 7);
116 bytes_to_skip -= b;
117 lines_to_skip -= l;
118 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
Damien George28eb5772014-01-25 11:43:20 +0000119 }
Damien George08335002014-01-18 23:24:36 +0000120}
Damien George73496fb2014-04-13 14:51:56 +0100121#endif
Damien George08335002014-01-18 23:24:36 +0000122
123// all functions must go through this one to emit byte code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200124STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000125 //printf("emit %d\n", num_bytes_to_write);
126 if (emit->pass < PASS_3) {
127 emit->byte_code_offset += num_bytes_to_write;
128 return emit->dummy_data;
129 } else {
130 assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
131 byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
132 emit->byte_code_offset += num_bytes_to_write;
133 return c;
134 }
135}
136
Damien Georgedf8127a2014-04-13 11:04:33 +0100137STATIC void emit_align_byte_code_to_machine_word(emit_t* emit) {
138 emit->byte_code_offset = (emit->byte_code_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
139}
140
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200141STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
Damien George08335002014-01-18 23:24:36 +0000142 byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100143 c[0] = b1;
144}
145
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200146STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100147 assert((b2 & (~0xff)) == 0);
Damien George08335002014-01-18 23:24:36 +0000148 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100149 c[0] = b1;
150 c[1] = b2;
151}
152
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200153STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200154 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200155 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200156 byte *p = buf + sizeof(buf);
157 // We encode in little-ending order, but store in big-endian, to help decoding
158 do {
159 *--p = num & 0x7f;
160 num >>= 7;
161 } while (num != 0);
162 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
163 while (p != buf + sizeof(buf) - 1) {
164 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000165 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200166 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000167}
168
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200169// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200170STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200171 emit_write_byte_code_byte(emit, b1);
172
173 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200174 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200175 byte *p = buf + sizeof(buf);
176 // We encode in little-ending order, but store in big-endian, to help decoding
177 do {
178 *--p = num & 0x7f;
179 num >>= 7;
180 } while (num != 0 && num != -1);
181 // Make sure that highest bit we stored (mask 0x40) matches sign
182 // of the number. If not, store extra byte just to encode sign
183 if (num == -1 && (*p & 0x40) == 0) {
184 *--p = 0x7f;
185 } else if (num == 0 && (*p & 0x40) != 0) {
186 *--p = 0;
187 }
188
189 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
190 while (p != buf + sizeof(buf) - 1) {
191 *c++ = *p++ | 0x80;
192 }
193 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100194}
195
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200196STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000197 emit_write_byte_code_byte(emit, b);
198 emit_write_byte_code_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100199}
200
Damien Georgedf8127a2014-04-13 11:04:33 +0100201// aligns the pointer so it is friendly to GC
202STATIC void emit_write_byte_code_byte_ptr(emit_t* emit, byte b, void *ptr) {
203 emit_write_byte_code_byte(emit, b);
204 emit_align_byte_code_to_machine_word(emit);
205 machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_byte_code(emit, sizeof(machine_uint_t));
206 *c = (machine_uint_t)ptr;
207}
208
Damien Georgefb083ea2014-02-01 18:29:40 +0000209/* currently unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200210STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000211 emit_write_byte_code_byte(emit, b);
212 emit_write_byte_code_byte_uint(emit, num1);
213 emit_write_byte_code_byte_uint(emit, num2);
214}
215*/
216
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200217STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000218 emit_write_byte_code_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100219}
220
Damien03c9cfb2013-11-05 22:06:08 +0000221// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George6f355fd2014-04-10 14:11:31 +0100222STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000223 uint byte_code_offset;
Damien429d7192013-10-04 19:53:11 +0100224 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000225 byte_code_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100226 } else {
Damien George08335002014-01-18 23:24:36 +0000227 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100228 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100229 byte *c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000230 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000231 c[1] = byte_code_offset;
232 c[2] = byte_code_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000233}
234
235// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George6f355fd2014-04-10 14:11:31 +0100236STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000237 int byte_code_offset;
Damien03c9cfb2013-11-05 22:06:08 +0000238 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000239 byte_code_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000240 } else {
Damien George08335002014-01-18 23:24:36 +0000241 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000242 }
Damien George08335002014-01-18 23:24:36 +0000243 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000244 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000245 c[1] = byte_code_offset;
246 c[2] = byte_code_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100247}
248
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200249STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000250}
251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200252STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000253 emit->pass = pass;
254 emit->stack_size = 0;
255 emit->last_emit_was_return_value = false;
256 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000257 emit->last_source_line_offset = 0;
258 emit->last_source_line = 1;
Damien George6baf76e2013-12-30 22:32:17 +0000259 if (pass == PASS_2) {
260 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
261 }
Damien George08335002014-01-18 23:24:36 +0000262 emit->byte_code_offset = 0;
263 emit->code_info_offset = 0;
264
265 // write code info size (don't know size at this stage in PASS_2 so need to use maximum space (4 bytes) to write it)
266 {
267 byte* c = emit_get_cur_to_write_code_info(emit, 4);
268 machine_uint_t s = emit->code_info_size;
269 c[0] = s & 0xff;
270 c[1] = (s >> 8) & 0xff;
271 c[2] = (s >> 16) & 0xff;
272 c[3] = (s >> 24) & 0xff;
273 }
274
275 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000276 emit_write_code_info_qstr(emit, scope->source_file);
277 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000278
Damien Georgebee17b02014-03-27 11:07:04 +0000279 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000280 {
Damien Georgebee17b02014-03-27 11:07:04 +0000281 byte* c = emit_get_cur_to_write_byte_code(emit, 4);
282 uint n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000283 if (n_state == 0) {
284 // Need at least 1 entry in the state, in the case an exception is
285 // propagated through this function, the exception is returned in
286 // the highest slot in the state (fastn[0], see vm.c).
287 n_state = 1;
288 }
Damien Georgebee17b02014-03-27 11:07:04 +0000289 c[0] = n_state & 0xff;
290 c[1] = (n_state >> 8) & 0xff;
291 c[2] = scope->exc_stack_size & 0xff;
292 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000293 }
294
295 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000296 int num_cell = 0;
297 for (int i = 0; i < scope->id_info_len; i++) {
298 id_info_t *id = &scope->id_info[i];
299 if (id->kind == ID_INFO_KIND_CELL) {
300 num_cell += 1;
301 }
302 }
303 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000304 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000305 for (int i = 0; i < scope->id_info_len; i++) {
306 id_info_t *id = &scope->id_info[i];
307 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000308 emit_write_byte_code_byte(emit, id->local_num); // write the local which should be converted to a cell
Damien George6baf76e2013-12-30 22:32:17 +0000309 }
310 }
311}
312
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200313STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000314 // check stack is back to zero size
315 if (emit->stack_size != 0) {
316 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
317 }
318
Damien George73496fb2014-04-13 14:51:56 +0100319 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien Georgedf8127a2014-04-13 11:04:33 +0100320 emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned
Damien George08335002014-01-18 23:24:36 +0000321
Damien George6baf76e2013-12-30 22:32:17 +0000322 if (emit->pass == PASS_2) {
323 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000324 emit->code_info_size = emit->code_info_offset;
325 emit->byte_code_size = emit->byte_code_offset;
Damien Georgedf8127a2014-04-13 11:04:33 +0100326 emit->code_base = m_new0(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000327
328 } else if (emit->pass == PASS_3) {
Damien George2827d622014-04-27 15:50:52 +0100329 qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
330 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200331 arg_names[i] = emit->scope->id_info[i].qstr;
332 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100333 mp_emit_glue_assign_byte_code(emit->scope->raw_code, emit->code_base,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200334 emit->code_info_size + emit->byte_code_size,
Damien George2827d622014-04-27 15:50:52 +0100335 emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
336 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000337 }
338}
339
Damien George069a35e2014-04-10 17:22:19 +0000340STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100341 return emit->last_emit_was_return_value;
342}
343
Damien Georged66ae182014-04-10 17:28:54 +0000344STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
345 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100346}
347
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200348STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000349 //printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
Damien George62ad1892014-01-29 21:51:51 +0000350#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000351 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000352 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
353 uint lines_to_skip = source_line - emit->last_source_line;
354 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000355 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000356 emit->last_source_line_offset = emit->byte_code_offset;
357 emit->last_source_line = source_line;
358 }
Damien George62ad1892014-01-29 21:51:51 +0000359#endif
Damien George08335002014-01-18 23:24:36 +0000360}
361
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200362STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100363 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
364}
365
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200366STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100367 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100371 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
372}
373
Damien Georgece8f07a2014-03-27 23:30:26 +0000374STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damien George78035b92014-04-09 12:27:39 +0100375 assert((int)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100376 emit->stack_size += stack_size_delta;
377 if (emit->stack_size > emit->scope->stack_size) {
378 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100379 }
380 emit->last_emit_was_return_value = false;
381}
382
Damien George6f355fd2014-04-10 14:11:31 +0100383STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000384 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100385 assert(l < emit->max_num_labels);
386 if (emit->pass == PASS_2) {
387 // assign label offset
388 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000389 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100390 } else if (emit->pass == PASS_3) {
391 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000392 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
393 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100394 }
395}
396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200397STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000398 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000399 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100400}
401
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200402STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000403 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000404 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100405}
406
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200407STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000408 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000409 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100410}
411
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200412STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000413 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100414 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000415 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
416 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
417 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
418 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100419 default: assert(0);
420 }
421}
422
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200423STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000424 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000425 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100426}
427
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200428STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000429 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000430 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100431}
432
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200433STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000434 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000435 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100436}
437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000439 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100440 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000441 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100442 } else {
Damien George08335002014-01-18 23:24:36 +0000443 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100444 }
445}
446
Damien George523b5752014-03-31 11:59:23 +0100447STATIC void emit_bc_load_null(emit_t *emit) {
448 emit_bc_pre(emit, 1);
449 emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
450};
451
Damien George2bf7c092014-04-09 15:26:46 +0100452STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100453 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000454 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100455 switch (local_num) {
456 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
457 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
458 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
459 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100460 }
461}
462
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200463STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000464 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000465 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000466}
467
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200468STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000469 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000470 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100471}
472
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200473STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000474 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000475 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100476}
477
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200478STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000479 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000480 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100481}
482
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200483STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000484 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000485 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100486}
487
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200488STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000489 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000490 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100491}
492
Damien George729f7b42014-04-17 22:10:53 +0100493STATIC void emit_bc_load_subscr(emit_t *emit) {
494 emit_bc_pre(emit, -1);
495 emit_write_byte_code_byte(emit, MP_BC_LOAD_SUBSCR);
496}
497
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200498STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100499 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000500 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100501 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000502 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
503 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
504 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
505 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100506 }
507}
508
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200509STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000510 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000511 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000512}
513
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200514STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000515 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000516 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100517}
518
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200519STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000520 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000521 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100522}
523
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200524STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000525 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000526 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100527}
528
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200529STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000530 emit_bc_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000531 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100532}
533
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200534STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100535 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100536}
537
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200538STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100539 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000540}
541
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200542STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000543 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000544 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100545}
546
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200547STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000548 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000549 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100550}
551
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200552STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien George1d24ea52014-04-08 21:11:49 +0100553 emit_bc_load_null(emit);
554 emit_bc_rot_two(emit);
555 emit_bc_store_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100556}
557
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200558STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100559 emit_bc_load_null(emit);
560 emit_bc_rot_three(emit);
561 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100562}
563
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200564STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000565 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000566 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100567}
568
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200569STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000570 emit_bc_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000571 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100572}
573
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200574STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000575 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000576 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100577}
578
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200579STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000580 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000581 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100582}
583
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200584STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000585 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000586 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100587}
588
Damien George6f355fd2014-04-10 14:11:31 +0100589STATIC void emit_bc_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000590 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000591 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100592}
593
Damien George6f355fd2014-04-10 14:11:31 +0100594STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000595 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000596 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100597}
598
Damien George6f355fd2014-04-10 14:11:31 +0100599STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000600 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000601 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100602}
603
Damien George6f355fd2014-04-10 14:11:31 +0100604STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000605 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000606 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100607}
608
Damien George6f355fd2014-04-10 14:11:31 +0100609STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000610 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000611 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100612}
613
Damien George6f355fd2014-04-10 14:11:31 +0100614STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000615 if (except_depth == 0) {
616 emit_bc_jump(emit, label);
617 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000618 emit_bc_pre(emit, 0);
Damien Georgecbddb272014-02-01 20:08:18 +0000619 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
620 emit_write_byte_code_byte(emit, except_depth);
621 }
Damien429d7192013-10-04 19:53:11 +0100622}
623
Damien George6f355fd2014-04-10 14:11:31 +0100624STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000625 emit_bc_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000626 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100627}
628
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200629STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000630 emit_bc_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000631 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100632}
633
Damien George6f355fd2014-04-10 14:11:31 +0100634STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000635 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000636 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100637}
638
Damien George6f355fd2014-04-10 14:11:31 +0100639STATIC void emit_bc_setup_finally(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000640 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000641 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100642}
643
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200644STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000645 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000646 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100647}
648
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200649STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000650 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000651 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100652}
653
Damien George6f355fd2014-04-10 14:11:31 +0100654STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000655 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000656 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100657}
658
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200659STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000660 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100661}
662
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200663STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000664 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000665 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100666}
667
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200668STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000669 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000670 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100671}
672
Damien Georged17926d2014-03-30 13:35:08 +0100673STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
674 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000675 emit_bc_pre(emit, 0);
Damien Georged17926d2014-03-30 13:35:08 +0100676 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000677 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000678 emit_write_byte_code_byte(emit, MP_BC_NOT);
679 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000680 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000681 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
682 }
Damien429d7192013-10-04 19:53:11 +0100683}
684
Damien Georged17926d2014-03-30 13:35:08 +0100685STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000686 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100687 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000688 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100689 op = MP_BINARY_OP_IN;
690 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000691 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100692 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000693 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000694 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000695 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000696 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000697 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000698 emit_write_byte_code_byte(emit, MP_BC_NOT);
699 }
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100703 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000704 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000705 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100706}
707
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200708STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100709 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000710 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000711 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200714STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100715 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000716 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000717 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100718}
719
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200720STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100721 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000722 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000723 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100724}
725
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200726STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000727 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000728 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100729}
730
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200731STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100732 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000733 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000734 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100735}
736
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200737STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100738 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000739 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000740 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100741}
742
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200743STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100744 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000745 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000746 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100747}
748
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200749STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100750 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000751 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000752 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100753}
754
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200755STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100756 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000757 emit_bc_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000758 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100759}
760
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200761STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100762 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000763 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000764 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100765}
766
Damien George30565092014-03-31 11:30:17 +0100767STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100768 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000769 emit_bc_pre(emit, 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100770 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000771 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100772 emit_bc_pre(emit, -1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100773 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200774 }
Damien429d7192013-10-04 19:53:11 +0100775}
776
Damien George3558f622014-04-20 17:50:40 +0100777STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100778 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100779 emit_bc_pre(emit, -n_closed_over + 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100780 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Damien George3558f622014-04-20 17:50:40 +0100781 emit_write_byte_code_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200782 } else {
Damien George3558f622014-04-20 17:50:40 +0100783 assert(n_closed_over <= 255);
784 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100785 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Damien George3558f622014-04-20 17:50:40 +0100786 emit_write_byte_code_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200787 }
Damien429d7192013-10-04 19:53:11 +0100788}
789
Damien George922ddd62014-04-09 12:43:17 +0100790STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uint bytecode_base, int n_positional, int n_keyword, uint star_flags) {
791 if (star_flags) {
792 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100793 // load dummy entry for non-existent pos_seq
794 emit_bc_load_null(emit);
795 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100796 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100797 // load dummy entry for non-existent kw_dict
798 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100799 }
Damien George523b5752014-03-31 11:59:23 +0100800 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
801 emit_write_byte_code_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100802 } else {
Damien George523b5752014-03-31 11:59:23 +0100803 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
804 emit_write_byte_code_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100805 }
Damien George523b5752014-03-31 11:59:23 +0100806}
807
Damien George922ddd62014-04-09 12:43:17 +0100808STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
809 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100810}
811
Damien George922ddd62014-04-09 12:43:17 +0100812STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
813 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100814}
815
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200816STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000817 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100818 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000819 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100820}
821
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200822STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000823 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000824 emit_bc_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000825 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100826}
827
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200828STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000829 emit_bc_pre(emit, 0);
Damien429d7192013-10-04 19:53:11 +0100830 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000831 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100832 }
Damien George08335002014-01-18 23:24:36 +0000833 emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100834}
835
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200836STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000837 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100838 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000839 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100840 }
Damien George08335002014-01-18 23:24:36 +0000841 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100842}
843
Damien6cdd3af2013-10-05 18:08:26 +0100844const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100845 emit_bc_set_native_types,
846 emit_bc_start_pass,
847 emit_bc_end_pass,
848 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000849 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000850 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100851
Damien4b03e772013-10-05 14:17:09 +0100852 emit_bc_load_id,
853 emit_bc_store_id,
854 emit_bc_delete_id,
855
Damien415eb6f2013-10-05 12:19:06 +0100856 emit_bc_label_assign,
857 emit_bc_import_name,
858 emit_bc_import_from,
859 emit_bc_import_star,
860 emit_bc_load_const_tok,
861 emit_bc_load_const_small_int,
862 emit_bc_load_const_int,
863 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100864 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100865 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100866 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100867 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000868 emit_bc_load_name,
869 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100870 emit_bc_load_attr,
871 emit_bc_load_method,
872 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100873 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100874 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000875 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100876 emit_bc_store_name,
877 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100878 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100879 emit_bc_store_subscr,
880 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000881 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100882 emit_bc_delete_name,
883 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100884 emit_bc_delete_attr,
885 emit_bc_delete_subscr,
886 emit_bc_dup_top,
887 emit_bc_dup_top_two,
888 emit_bc_pop_top,
889 emit_bc_rot_two,
890 emit_bc_rot_three,
891 emit_bc_jump,
892 emit_bc_pop_jump_if_true,
893 emit_bc_pop_jump_if_false,
894 emit_bc_jump_if_true_or_pop,
895 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000896 emit_bc_unwind_jump,
897 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100898 emit_bc_setup_with,
899 emit_bc_with_cleanup,
900 emit_bc_setup_except,
901 emit_bc_setup_finally,
902 emit_bc_end_finally,
903 emit_bc_get_iter,
904 emit_bc_for_iter,
905 emit_bc_for_iter_end,
906 emit_bc_pop_block,
907 emit_bc_pop_except,
908 emit_bc_unary_op,
909 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100910 emit_bc_build_tuple,
911 emit_bc_build_list,
912 emit_bc_list_append,
913 emit_bc_build_map,
914 emit_bc_store_map,
915 emit_bc_map_add,
916 emit_bc_build_set,
917 emit_bc_set_add,
918 emit_bc_build_slice,
919 emit_bc_unpack_sequence,
920 emit_bc_unpack_ex,
921 emit_bc_make_function,
922 emit_bc_make_closure,
923 emit_bc_call_function,
924 emit_bc_call_method,
925 emit_bc_return_value,
926 emit_bc_raise_varargs,
927 emit_bc_yield_value,
928 emit_bc_yield_from,
929};
Damien George5f6a25f2014-04-20 18:02:27 +0100930
931#endif // !MICROPY_EMIT_CPYTHON