blob: cfaea7c88a848701dd48a6445ed68f9b0d239a2b [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
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;
52 uint last_emit_was_return_value : 8;
53 byte dummy_data[DUMMY_DATA_SIZE];
54
Damien429d7192013-10-04 19:53:11 +010055 int stack_size;
Damien429d7192013-10-04 19:53:11 +010056
57 scope_t *scope;
58
Damien George08335002014-01-18 23:24:36 +000059 uint last_source_line_offset;
60 uint last_source_line;
61
Damienb05d7072013-10-05 13:37:10 +010062 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010063 uint *label_offsets;
64
Damien George08335002014-01-18 23:24:36 +000065 uint code_info_offset;
66 uint code_info_size;
Damien George3417bc22014-05-10 10:36:38 +010067 uint bytecode_offset;
68 uint bytecode_size;
Damien George08335002014-01-18 23:24:36 +000069 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010070};
71
Damien George1d24ea52014-04-08 21:11:49 +010072STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010073STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010074
Damien Georgecbd2f742014-01-19 11:48:48 +000075emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000076 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010077 emit->max_num_labels = max_num_labels;
78 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010079 return emit;
80}
Damien4b03e772013-10-05 14:17:09 +010081
Damien George41d02b62014-01-24 22:42:28 +000082void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020083 m_del(uint, emit->label_offsets, emit->max_num_labels);
84 m_del_obj(emit_t, emit);
85}
86
Damien George08335002014-01-18 23:24:36 +000087// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020088STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010089 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010090 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +000091 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010092 return emit->dummy_data;
93 } else {
Damien George08335002014-01-18 23:24:36 +000094 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
95 byte *c = emit->code_base + emit->code_info_offset;
96 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010097 return c;
98 }
99}
100
Damien Georgedf8127a2014-04-13 11:04:33 +0100101STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
102 emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
103}
104
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200105STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +0000106 byte* c = emit_get_cur_to_write_code_info(emit, 4);
107 // TODO variable length encoding for qstr
108 c[0] = qstr & 0xff;
109 c[1] = (qstr >> 8) & 0xff;
110 c[2] = (qstr >> 16) & 0xff;
111 c[3] = (qstr >> 24) & 0xff;
112}
113
Damien George73496fb2014-04-13 14:51:56 +0100114#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200115STATIC 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 +0100116 assert(bytes_to_skip > 0 || lines_to_skip > 0);
117 while (bytes_to_skip > 0 || lines_to_skip > 0) {
118 uint b = MIN(bytes_to_skip, 31);
119 uint l = MIN(lines_to_skip, 7);
120 bytes_to_skip -= b;
121 lines_to_skip -= l;
122 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
Damien George28eb5772014-01-25 11:43:20 +0000123 }
Damien George08335002014-01-18 23:24:36 +0000124}
Damien George73496fb2014-04-13 14:51:56 +0100125#endif
Damien George08335002014-01-18 23:24:36 +0000126
127// all functions must go through this one to emit byte code
Damien George3417bc22014-05-10 10:36:38 +0100128STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000129 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100130 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100131 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000132 return emit->dummy_data;
133 } else {
Damien George3417bc22014-05-10 10:36:38 +0100134 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
135 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
136 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000137 return c;
138 }
139}
140
Damien George3417bc22014-05-10 10:36:38 +0100141STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
142 emit->bytecode_offset = (emit->bytecode_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100143}
144
Damien George3417bc22014-05-10 10:36:38 +0100145STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
146 byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100147 c[0] = b1;
148}
149
Damien George3417bc22014-05-10 10:36:38 +0100150STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100151 assert((b2 & (~0xff)) == 0);
Damien George3417bc22014-05-10 10:36:38 +0100152 byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100153 c[0] = b1;
154 c[1] = b2;
155}
156
Damien George3417bc22014-05-10 10:36:38 +0100157STATIC void emit_write_bytecode_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200158 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100159 byte buf[BYTES_FOR_INT];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200160 byte *p = buf + sizeof(buf);
161 // We encode in little-ending order, but store in big-endian, to help decoding
162 do {
163 *--p = num & 0x7f;
164 num >>= 7;
165 } while (num != 0);
Damien George3417bc22014-05-10 10:36:38 +0100166 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200167 while (p != buf + sizeof(buf) - 1) {
168 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000169 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200170 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000171}
172
Damien George3417bc22014-05-10 10:36:38 +0100173// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
174STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, machine_int_t num) {
175 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200176
177 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100178 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200179 byte *p = buf + sizeof(buf);
180 // We encode in little-ending order, but store in big-endian, to help decoding
181 do {
182 *--p = num & 0x7f;
183 num >>= 7;
184 } while (num != 0 && num != -1);
185 // Make sure that highest bit we stored (mask 0x40) matches sign
186 // of the number. If not, store extra byte just to encode sign
187 if (num == -1 && (*p & 0x40) == 0) {
188 *--p = 0x7f;
189 } else if (num == 0 && (*p & 0x40) != 0) {
190 *--p = 0;
191 }
192
Damien George3417bc22014-05-10 10:36:38 +0100193 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200194 while (p != buf + sizeof(buf) - 1) {
195 *c++ = *p++ | 0x80;
196 }
197 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100198}
199
Damien George3417bc22014-05-10 10:36:38 +0100200STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, uint num) {
201 emit_write_bytecode_byte(emit, b);
202 emit_write_bytecode_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100203}
204
Damien Georgedf8127a2014-04-13 11:04:33 +0100205// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100206STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
207 emit_write_bytecode_byte(emit, b);
208 emit_align_bytecode_to_machine_word(emit);
209 machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(machine_uint_t));
Damien Georgedf8127a2014-04-13 11:04:33 +0100210 *c = (machine_uint_t)ptr;
211}
212
Damien Georgefb083ea2014-02-01 18:29:40 +0000213/* currently unused
Damien George3417bc22014-05-10 10:36:38 +0100214STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
215 emit_write_bytecode_byte(emit, b);
216 emit_write_bytecode_byte_uint(emit, num1);
217 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000218}
219*/
220
Damien George3417bc22014-05-10 10:36:38 +0100221STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qstr) {
222 emit_write_bytecode_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100223}
224
Damien03c9cfb2013-11-05 22:06:08 +0000225// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George3417bc22014-05-10 10:36:38 +0100226STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
227 uint bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100228 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100229 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100230 } else {
Damien George3417bc22014-05-10 10:36:38 +0100231 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100232 }
Damien George3417bc22014-05-10 10:36:38 +0100233 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000234 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100235 c[1] = bytecode_offset;
236 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000237}
238
239// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George3417bc22014-05-10 10:36:38 +0100240STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, uint label) {
241 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100242 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100243 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000244 } else {
Damien George3417bc22014-05-10 10:36:38 +0100245 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000246 }
Damien George3417bc22014-05-10 10:36:38 +0100247 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000248 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100249 c[1] = bytecode_offset;
250 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100251}
252
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200253STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000254}
255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200256STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000257 emit->pass = pass;
258 emit->stack_size = 0;
259 emit->last_emit_was_return_value = false;
260 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000261 emit->last_source_line_offset = 0;
262 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100263 if (pass < MP_PASS_EMIT) {
Damien George6baf76e2013-12-30 22:32:17 +0000264 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
265 }
Damien George3417bc22014-05-10 10:36:38 +0100266 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000267 emit->code_info_offset = 0;
268
Damien George36db6bc2014-05-07 17:24:22 +0100269 // write code info size; use maximum space (4 bytes) to write it; TODO possible optimise this
Damien George08335002014-01-18 23:24:36 +0000270 {
271 byte* c = emit_get_cur_to_write_code_info(emit, 4);
272 machine_uint_t s = emit->code_info_size;
273 c[0] = s & 0xff;
274 c[1] = (s >> 8) & 0xff;
275 c[2] = (s >> 16) & 0xff;
276 c[3] = (s >> 24) & 0xff;
277 }
278
279 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000280 emit_write_code_info_qstr(emit, scope->source_file);
281 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000282
Damien Georgebee17b02014-03-27 11:07:04 +0000283 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000284 {
Damien George3417bc22014-05-10 10:36:38 +0100285 byte* c = emit_get_cur_to_write_bytecode(emit, 4);
Damien Georgebee17b02014-03-27 11:07:04 +0000286 uint n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000287 if (n_state == 0) {
288 // Need at least 1 entry in the state, in the case an exception is
289 // propagated through this function, the exception is returned in
290 // the highest slot in the state (fastn[0], see vm.c).
291 n_state = 1;
292 }
Damien Georgebee17b02014-03-27 11:07:04 +0000293 c[0] = n_state & 0xff;
294 c[1] = (n_state >> 8) & 0xff;
295 c[2] = scope->exc_stack_size & 0xff;
296 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000297 }
298
299 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000300 int num_cell = 0;
301 for (int i = 0; i < scope->id_info_len; i++) {
302 id_info_t *id = &scope->id_info[i];
303 if (id->kind == ID_INFO_KIND_CELL) {
304 num_cell += 1;
305 }
306 }
307 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100308 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000309 for (int i = 0; i < scope->id_info_len; i++) {
310 id_info_t *id = &scope->id_info[i];
311 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100312 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 +0000313 }
314 }
315}
316
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200317STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000318 // check stack is back to zero size
319 if (emit->stack_size != 0) {
320 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
321 }
322
Damien George73496fb2014-04-13 14:51:56 +0100323 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George3417bc22014-05-10 10:36:38 +0100324 emit_align_code_info_to_machine_word(emit); // align so that following bytecode is aligned
Damien George08335002014-01-18 23:24:36 +0000325
Damien George36db6bc2014-05-07 17:24:22 +0100326 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien George6baf76e2013-12-30 22:32:17 +0000327 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000328 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100329 emit->bytecode_size = emit->bytecode_offset;
330 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000331
Damien George36db6bc2014-05-07 17:24:22 +0100332 } else if (emit->pass == MP_PASS_EMIT) {
Damien George2827d622014-04-27 15:50:52 +0100333 qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
334 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200335 arg_names[i] = emit->scope->id_info[i].qstr;
336 }
Damien George3417bc22014-05-10 10:36:38 +0100337 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
338 emit->code_info_size + emit->bytecode_size,
Damien George2827d622014-04-27 15:50:52 +0100339 emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
340 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000341 }
342}
343
Damien George069a35e2014-04-10 17:22:19 +0000344STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100345 return emit->last_emit_was_return_value;
346}
347
Damien Georged66ae182014-04-10 17:28:54 +0000348STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
349 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100350}
351
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200352STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100353 //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 +0000354#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000355 if (source_line > emit->last_source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100356 uint bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
Damien George28eb5772014-01-25 11:43:20 +0000357 uint lines_to_skip = source_line - emit->last_source_line;
358 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000359 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100360 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000361 emit->last_source_line = source_line;
362 }
Damien George62ad1892014-01-29 21:51:51 +0000363#endif
Damien George08335002014-01-18 23:24:36 +0000364}
365
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200366STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100367 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100371 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
372}
373
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200374STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100375 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
376}
377
Damien Georgece8f07a2014-03-27 23:30:26 +0000378STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damien George78035b92014-04-09 12:27:39 +0100379 assert((int)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100380 emit->stack_size += stack_size_delta;
381 if (emit->stack_size > emit->scope->stack_size) {
382 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100383 }
384 emit->last_emit_was_return_value = false;
385}
386
Damien George6f355fd2014-04-10 14:11:31 +0100387STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000388 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100389 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100390 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100391 // assign label offset
392 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100393 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100394 } else {
395 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100396 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
397 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100398 }
399}
400
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000402 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100403 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100404}
405
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200406STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000407 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100408 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100409}
410
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200411STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000412 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100413 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100414}
415
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200416STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000417 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100418 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100419 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
420 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
421 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
422 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100423 default: assert(0);
424 }
425}
426
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200427STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000428 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100429 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100430}
431
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200432STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000433 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100434 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100435}
436
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200437STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000438 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100439 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100440}
441
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200442STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000443 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100444 if (bytes) {
Damien George3417bc22014-05-10 10:36:38 +0100445 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100446 } else {
Damien George3417bc22014-05-10 10:36:38 +0100447 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100448 }
449}
450
Damien George523b5752014-03-31 11:59:23 +0100451STATIC void emit_bc_load_null(emit_t *emit) {
452 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100453 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100454};
455
Damien George2bf7c092014-04-09 15:26:46 +0100456STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100457 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000458 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100459 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100460 case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
461 case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
462 case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
463 default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100464 }
465}
466
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200467STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000468 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100469 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000470}
471
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200472STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000473 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100474 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100475}
476
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200477STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000478 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100479 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100480}
481
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200482STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000483 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100484 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100485}
486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000488 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100489 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100490}
491
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200492STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000493 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100494 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100495}
496
Damien George729f7b42014-04-17 22:10:53 +0100497STATIC void emit_bc_load_subscr(emit_t *emit) {
498 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100499 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100500}
501
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200502STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100503 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000504 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100505 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100506 case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
507 case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
508 case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
509 default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100510 }
511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int 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_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000516}
517
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200518STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000519 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100520 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100521}
522
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200523STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000524 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100525 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000529 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100530 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100531}
532
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200533STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000534 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100535 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100536}
537
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200538STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100539 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100540}
541
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200542STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100543 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000544}
545
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200546STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000547 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100548 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100549}
550
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200551STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000552 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100553 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100554}
555
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200556STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien George1d24ea52014-04-08 21:11:49 +0100557 emit_bc_load_null(emit);
558 emit_bc_rot_two(emit);
559 emit_bc_store_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100560}
561
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200562STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100563 emit_bc_load_null(emit);
564 emit_bc_rot_three(emit);
565 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200568STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000569 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100570 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200573STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000574 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100575 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100576}
577
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200578STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000579 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100580 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100581}
582
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200583STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000584 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100585 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100586}
587
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200588STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000589 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100590 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Damien George6f355fd2014-04-10 14:11:31 +0100593STATIC void emit_bc_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000594 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100595 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Damien George6f355fd2014-04-10 14:11:31 +0100598STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000599 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100600 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100601}
602
Damien George6f355fd2014-04-10 14:11:31 +0100603STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000604 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100605 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100606}
607
Damien George6f355fd2014-04-10 14:11:31 +0100608STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000609 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100610 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Damien George6f355fd2014-04-10 14:11:31 +0100613STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
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_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Damien George6f355fd2014-04-10 14:11:31 +0100618STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000619 if (except_depth == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000620 emit_bc_pre(emit, 0);
Damien George25c84642014-05-30 15:20:41 +0100621 if (label & MP_EMIT_BREAK_FROM_FOR) {
622 // need to pop the iterator if we are breaking out of a for loop
623 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
624 }
625 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
626 } else {
627 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
628 emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000629 }
Damien429d7192013-10-04 19:53:11 +0100630}
631
Damien George6f355fd2014-04-10 14:11:31 +0100632STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000633 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100634 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100635}
636
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200637STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000638 emit_bc_pre(emit, -7);
Damien George3417bc22014-05-10 10:36:38 +0100639 emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100640}
641
Damien George6f355fd2014-04-10 14:11:31 +0100642STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000643 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100644 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100645}
646
Damien George6f355fd2014-04-10 14:11:31 +0100647STATIC void emit_bc_setup_finally(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000648 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100649 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100650}
651
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200652STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000653 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100654 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100655}
656
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200657STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000658 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100659 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100660}
661
Damien George6f355fd2014-04-10 14:11:31 +0100662STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000663 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100664 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100665}
666
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200667STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000668 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100669}
670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200671STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000672 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100673 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100674}
675
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200676STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000677 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100678 emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100679}
680
Damien Georged17926d2014-03-30 13:35:08 +0100681STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
682 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000683 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100684 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000685 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100686 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000687 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000688 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100689 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000690 }
Damien429d7192013-10-04 19:53:11 +0100691}
692
Damien Georged17926d2014-03-30 13:35:08 +0100693STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000694 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100695 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000696 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100697 op = MP_BINARY_OP_IN;
698 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000699 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100700 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000701 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000702 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100703 emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000704 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000705 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100706 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000707 }
Damien429d7192013-10-04 19:53:11 +0100708}
709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200710STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100711 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000712 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100713 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200716STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100717 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000718 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100719 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100720}
721
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200722STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100723 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000724 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100725 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100726}
727
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200728STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100729 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000730 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100731 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100732}
733
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200734STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000735 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100736 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100737}
738
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200739STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100740 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000741 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100742 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100743}
744
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200745STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100746 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000747 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100748 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100749}
750
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200751STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100752 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000753 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100754 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100755}
756
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200757STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100758 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000759 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100760 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100761}
762
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200763STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100764 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000765 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100766 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100767}
768
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200769STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100770 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000771 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100772 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100773}
774
Damien George30565092014-03-31 11:30:17 +0100775STATIC 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 +0100776 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000777 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100778 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000779 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100780 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100781 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200782 }
Damien429d7192013-10-04 19:53:11 +0100783}
784
Damien George3558f622014-04-20 17:50:40 +0100785STATIC 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 +0100786 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100787 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100788 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
789 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200790 } else {
Damien George3558f622014-04-20 17:50:40 +0100791 assert(n_closed_over <= 255);
792 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100793 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
794 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200795 }
Damien429d7192013-10-04 19:53:11 +0100796}
797
Damien George922ddd62014-04-09 12:43:17 +0100798STATIC 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) {
799 if (star_flags) {
800 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100801 // load dummy entry for non-existent pos_seq
802 emit_bc_load_null(emit);
803 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100804 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100805 // load dummy entry for non-existent kw_dict
806 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100807 }
Damien George523b5752014-03-31 11:59:23 +0100808 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
Damien George3417bc22014-05-10 10:36:38 +0100809 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 +0100810 } else {
Damien George523b5752014-03-31 11:59:23 +0100811 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100812 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 +0100813 }
Damien George523b5752014-03-31 11:59:23 +0100814}
815
Damien George922ddd62014-04-09 12:43:17 +0100816STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
817 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100818}
819
Damien George922ddd62014-04-09 12:43:17 +0100820STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
821 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100822}
823
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200824STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000825 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100826 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100827 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100828}
829
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200830STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000831 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000832 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100833 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100834}
835
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200836STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000837 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100838 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100839 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100840}
841
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200842STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000843 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100844 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100845 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100846}
847
Damien6cdd3af2013-10-05 18:08:26 +0100848const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100849 emit_bc_set_native_types,
850 emit_bc_start_pass,
851 emit_bc_end_pass,
852 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000853 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000854 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100855
Damien4b03e772013-10-05 14:17:09 +0100856 emit_bc_load_id,
857 emit_bc_store_id,
858 emit_bc_delete_id,
859
Damien415eb6f2013-10-05 12:19:06 +0100860 emit_bc_label_assign,
861 emit_bc_import_name,
862 emit_bc_import_from,
863 emit_bc_import_star,
864 emit_bc_load_const_tok,
865 emit_bc_load_const_small_int,
866 emit_bc_load_const_int,
867 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100868 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100869 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100870 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100871 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000872 emit_bc_load_name,
873 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100874 emit_bc_load_attr,
875 emit_bc_load_method,
876 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100877 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100878 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000879 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100880 emit_bc_store_name,
881 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100882 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100883 emit_bc_store_subscr,
884 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000885 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100886 emit_bc_delete_name,
887 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100888 emit_bc_delete_attr,
889 emit_bc_delete_subscr,
890 emit_bc_dup_top,
891 emit_bc_dup_top_two,
892 emit_bc_pop_top,
893 emit_bc_rot_two,
894 emit_bc_rot_three,
895 emit_bc_jump,
896 emit_bc_pop_jump_if_true,
897 emit_bc_pop_jump_if_false,
898 emit_bc_jump_if_true_or_pop,
899 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000900 emit_bc_unwind_jump,
901 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100902 emit_bc_setup_with,
903 emit_bc_with_cleanup,
904 emit_bc_setup_except,
905 emit_bc_setup_finally,
906 emit_bc_end_finally,
907 emit_bc_get_iter,
908 emit_bc_for_iter,
909 emit_bc_for_iter_end,
910 emit_bc_pop_block,
911 emit_bc_pop_except,
912 emit_bc_unary_op,
913 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100914 emit_bc_build_tuple,
915 emit_bc_build_list,
916 emit_bc_list_append,
917 emit_bc_build_map,
918 emit_bc_store_map,
919 emit_bc_map_add,
920 emit_bc_build_set,
921 emit_bc_set_add,
922 emit_bc_build_slice,
923 emit_bc_unpack_sequence,
924 emit_bc_unpack_ex,
925 emit_bc_make_function,
926 emit_bc_make_closure,
927 emit_bc_call_function,
928 emit_bc_call_method,
929 emit_bc_return_value,
930 emit_bc_raise_varargs,
931 emit_bc_yield_value,
932 emit_bc_yield_from,
933};
Damien George5f6a25f2014-04-20 18:02:27 +0100934
935#endif // !MICROPY_EMIT_CPYTHON