blob: bfd378b5e5acbed90bea2b4a805d2500a265da9a [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 {
51 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010052 int stack_size;
53 bool last_emit_was_return_value;
54
55 scope_t *scope;
56
Damien George08335002014-01-18 23:24:36 +000057 uint last_source_line_offset;
58 uint last_source_line;
59
Damienb05d7072013-10-05 13:37:10 +010060 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010061 uint *label_offsets;
62
Damien George08335002014-01-18 23:24:36 +000063 uint code_info_offset;
64 uint code_info_size;
Damien George3417bc22014-05-10 10:36:38 +010065 uint bytecode_offset;
66 uint bytecode_size;
Damien George08335002014-01-18 23:24:36 +000067 byte *code_base; // stores both byte code and code info
Damien George95977712014-05-10 18:07:08 +010068 byte dummy_data[DUMMY_DATA_SIZE];
Damien429d7192013-10-04 19:53:11 +010069};
70
Damien George1d24ea52014-04-08 21:11:49 +010071STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010072STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010073
Damien Georgecbd2f742014-01-19 11:48:48 +000074emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000075 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010076 emit->max_num_labels = max_num_labels;
77 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010078 return emit;
79}
Damien4b03e772013-10-05 14:17:09 +010080
Damien George41d02b62014-01-24 22:42:28 +000081void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020082 m_del(uint, emit->label_offsets, emit->max_num_labels);
83 m_del_obj(emit_t, emit);
84}
85
Damien George08335002014-01-18 23:24:36 +000086// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020087STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010088 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010089 if (emit->pass < MP_PASS_EMIT) {
Damien George08335002014-01-18 23:24:36 +000090 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010091 return emit->dummy_data;
92 } else {
Damien George08335002014-01-18 23:24:36 +000093 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
94 byte *c = emit->code_base + emit->code_info_offset;
95 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010096 return c;
97 }
98}
99
Damien Georgedf8127a2014-04-13 11:04:33 +0100100STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
101 emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
102}
103
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200104STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +0000105 byte* c = emit_get_cur_to_write_code_info(emit, 4);
106 // TODO variable length encoding for qstr
107 c[0] = qstr & 0xff;
108 c[1] = (qstr >> 8) & 0xff;
109 c[2] = (qstr >> 16) & 0xff;
110 c[3] = (qstr >> 24) & 0xff;
111}
112
Damien George73496fb2014-04-13 14:51:56 +0100113#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200114STATIC 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 +0100115 assert(bytes_to_skip > 0 || lines_to_skip > 0);
116 while (bytes_to_skip > 0 || lines_to_skip > 0) {
117 uint b = MIN(bytes_to_skip, 31);
118 uint l = MIN(lines_to_skip, 7);
119 bytes_to_skip -= b;
120 lines_to_skip -= l;
121 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
Damien George28eb5772014-01-25 11:43:20 +0000122 }
Damien George08335002014-01-18 23:24:36 +0000123}
Damien George73496fb2014-04-13 14:51:56 +0100124#endif
Damien George08335002014-01-18 23:24:36 +0000125
126// all functions must go through this one to emit byte code
Damien George3417bc22014-05-10 10:36:38 +0100127STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +0000128 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100129 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100130 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000131 return emit->dummy_data;
132 } else {
Damien George3417bc22014-05-10 10:36:38 +0100133 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
134 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
135 emit->bytecode_offset += num_bytes_to_write;
Damien George08335002014-01-18 23:24:36 +0000136 return c;
137 }
138}
139
Damien George3417bc22014-05-10 10:36:38 +0100140STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
141 emit->bytecode_offset = (emit->bytecode_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
Damien Georgedf8127a2014-04-13 11:04:33 +0100142}
143
Damien George3417bc22014-05-10 10:36:38 +0100144STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
145 byte* c = emit_get_cur_to_write_bytecode(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100146 c[0] = b1;
147}
148
Damien George3417bc22014-05-10 10:36:38 +0100149STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100150 assert((b2 & (~0xff)) == 0);
Damien George3417bc22014-05-10 10:36:38 +0100151 byte* c = emit_get_cur_to_write_bytecode(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100152 c[0] = b1;
153 c[1] = b2;
154}
155
Damien George3417bc22014-05-10 10:36:38 +0100156STATIC void emit_write_bytecode_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200157 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100158 byte buf[BYTES_FOR_INT];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200159 byte *p = buf + sizeof(buf);
160 // We encode in little-ending order, but store in big-endian, to help decoding
161 do {
162 *--p = num & 0x7f;
163 num >>= 7;
164 } while (num != 0);
Damien George3417bc22014-05-10 10:36:38 +0100165 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200166 while (p != buf + sizeof(buf) - 1) {
167 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000168 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200169 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000170}
171
Damien George3417bc22014-05-10 10:36:38 +0100172// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
173STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, machine_int_t num) {
174 emit_write_bytecode_byte(emit, b1);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200175
176 // We store each 7 bits in a separate byte, and that's how many bytes needed
Damien George95977712014-05-10 18:07:08 +0100177 byte buf[BYTES_FOR_INT];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200178 byte *p = buf + sizeof(buf);
179 // We encode in little-ending order, but store in big-endian, to help decoding
180 do {
181 *--p = num & 0x7f;
182 num >>= 7;
183 } while (num != 0 && num != -1);
184 // Make sure that highest bit we stored (mask 0x40) matches sign
185 // of the number. If not, store extra byte just to encode sign
186 if (num == -1 && (*p & 0x40) == 0) {
187 *--p = 0x7f;
188 } else if (num == 0 && (*p & 0x40) != 0) {
189 *--p = 0;
190 }
191
Damien George3417bc22014-05-10 10:36:38 +0100192 byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200193 while (p != buf + sizeof(buf) - 1) {
194 *c++ = *p++ | 0x80;
195 }
196 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100197}
198
Damien George3417bc22014-05-10 10:36:38 +0100199STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, uint num) {
200 emit_write_bytecode_byte(emit, b);
201 emit_write_bytecode_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100202}
203
Damien Georgedf8127a2014-04-13 11:04:33 +0100204// aligns the pointer so it is friendly to GC
Damien George3417bc22014-05-10 10:36:38 +0100205STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
206 emit_write_bytecode_byte(emit, b);
207 emit_align_bytecode_to_machine_word(emit);
208 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 +0100209 *c = (machine_uint_t)ptr;
210}
211
Damien Georgefb083ea2014-02-01 18:29:40 +0000212/* currently unused
Damien George3417bc22014-05-10 10:36:38 +0100213STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
214 emit_write_bytecode_byte(emit, b);
215 emit_write_bytecode_byte_uint(emit, num1);
216 emit_write_bytecode_byte_uint(emit, num2);
Damien Georgefb083ea2014-02-01 18:29:40 +0000217}
218*/
219
Damien George3417bc22014-05-10 10:36:38 +0100220STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qstr) {
221 emit_write_bytecode_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100222}
223
Damien03c9cfb2013-11-05 22:06:08 +0000224// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George3417bc22014-05-10 10:36:38 +0100225STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
226 uint bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100227 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100228 bytecode_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100229 } else {
Damien George3417bc22014-05-10 10:36:38 +0100230 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100231 }
Damien George3417bc22014-05-10 10:36:38 +0100232 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000233 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100234 c[1] = bytecode_offset;
235 c[2] = bytecode_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000236}
237
238// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George3417bc22014-05-10 10:36:38 +0100239STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, uint label) {
240 int bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100241 if (emit->pass < MP_PASS_EMIT) {
Damien George3417bc22014-05-10 10:36:38 +0100242 bytecode_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000243 } else {
Damien George3417bc22014-05-10 10:36:38 +0100244 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000245 }
Damien George3417bc22014-05-10 10:36:38 +0100246 byte* c = emit_get_cur_to_write_bytecode(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000247 c[0] = b1;
Damien George3417bc22014-05-10 10:36:38 +0100248 c[1] = bytecode_offset;
249 c[2] = bytecode_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100250}
251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200252STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000253}
254
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200255STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000256 emit->pass = pass;
257 emit->stack_size = 0;
258 emit->last_emit_was_return_value = false;
259 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000260 emit->last_source_line_offset = 0;
261 emit->last_source_line = 1;
Damien George36db6bc2014-05-07 17:24:22 +0100262 if (pass < MP_PASS_EMIT) {
Damien George6baf76e2013-12-30 22:32:17 +0000263 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
264 }
Damien George3417bc22014-05-10 10:36:38 +0100265 emit->bytecode_offset = 0;
Damien George08335002014-01-18 23:24:36 +0000266 emit->code_info_offset = 0;
267
Damien George36db6bc2014-05-07 17:24:22 +0100268 // write code info size; use maximum space (4 bytes) to write it; TODO possible optimise this
Damien George08335002014-01-18 23:24:36 +0000269 {
270 byte* c = emit_get_cur_to_write_code_info(emit, 4);
271 machine_uint_t s = emit->code_info_size;
272 c[0] = s & 0xff;
273 c[1] = (s >> 8) & 0xff;
274 c[2] = (s >> 16) & 0xff;
275 c[3] = (s >> 24) & 0xff;
276 }
277
278 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000279 emit_write_code_info_qstr(emit, scope->source_file);
280 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000281
Damien Georgebee17b02014-03-27 11:07:04 +0000282 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000283 {
Damien George3417bc22014-05-10 10:36:38 +0100284 byte* c = emit_get_cur_to_write_bytecode(emit, 4);
Damien Georgebee17b02014-03-27 11:07:04 +0000285 uint n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000286 if (n_state == 0) {
287 // Need at least 1 entry in the state, in the case an exception is
288 // propagated through this function, the exception is returned in
289 // the highest slot in the state (fastn[0], see vm.c).
290 n_state = 1;
291 }
Damien Georgebee17b02014-03-27 11:07:04 +0000292 c[0] = n_state & 0xff;
293 c[1] = (n_state >> 8) & 0xff;
294 c[2] = scope->exc_stack_size & 0xff;
295 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000296 }
297
298 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000299 int num_cell = 0;
300 for (int i = 0; i < scope->id_info_len; i++) {
301 id_info_t *id = &scope->id_info[i];
302 if (id->kind == ID_INFO_KIND_CELL) {
303 num_cell += 1;
304 }
305 }
306 assert(num_cell <= 255);
Damien George3417bc22014-05-10 10:36:38 +0100307 emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000308 for (int i = 0; i < scope->id_info_len; i++) {
309 id_info_t *id = &scope->id_info[i];
310 if (id->kind == ID_INFO_KIND_CELL) {
Damien George3417bc22014-05-10 10:36:38 +0100311 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 +0000312 }
313 }
314}
315
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200316STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000317 // check stack is back to zero size
318 if (emit->stack_size != 0) {
319 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
320 }
321
Damien George73496fb2014-04-13 14:51:56 +0100322 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien George3417bc22014-05-10 10:36:38 +0100323 emit_align_code_info_to_machine_word(emit); // align so that following bytecode is aligned
Damien George08335002014-01-18 23:24:36 +0000324
Damien George36db6bc2014-05-07 17:24:22 +0100325 if (emit->pass == MP_PASS_CODE_SIZE) {
Damien George6baf76e2013-12-30 22:32:17 +0000326 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000327 emit->code_info_size = emit->code_info_offset;
Damien George3417bc22014-05-10 10:36:38 +0100328 emit->bytecode_size = emit->bytecode_offset;
329 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
Damien George6baf76e2013-12-30 22:32:17 +0000330
Damien George36db6bc2014-05-07 17:24:22 +0100331 } else if (emit->pass == MP_PASS_EMIT) {
Damien George2827d622014-04-27 15:50:52 +0100332 qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
333 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200334 arg_names[i] = emit->scope->id_info[i].qstr;
335 }
Damien George3417bc22014-05-10 10:36:38 +0100336 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
337 emit->code_info_size + emit->bytecode_size,
Damien George2827d622014-04-27 15:50:52 +0100338 emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
339 emit->scope->scope_flags);
Damien George6baf76e2013-12-30 22:32:17 +0000340 }
341}
342
Damien George069a35e2014-04-10 17:22:19 +0000343STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100344 return emit->last_emit_was_return_value;
345}
346
Damien Georged66ae182014-04-10 17:28:54 +0000347STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
348 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100349}
350
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200351STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100352 //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 +0000353#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000354 if (source_line > emit->last_source_line) {
Damien George3417bc22014-05-10 10:36:38 +0100355 uint bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
Damien George28eb5772014-01-25 11:43:20 +0000356 uint lines_to_skip = source_line - emit->last_source_line;
357 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000358 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George3417bc22014-05-10 10:36:38 +0100359 emit->last_source_line_offset = emit->bytecode_offset;
Damien George08335002014-01-18 23:24:36 +0000360 emit->last_source_line = source_line;
361 }
Damien George62ad1892014-01-29 21:51:51 +0000362#endif
Damien George08335002014-01-18 23:24:36 +0000363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100366 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
367}
368
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200369STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100370 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
371}
372
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200373STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100374 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
375}
376
Damien Georgece8f07a2014-03-27 23:30:26 +0000377STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damien George78035b92014-04-09 12:27:39 +0100378 assert((int)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100379 emit->stack_size += stack_size_delta;
380 if (emit->stack_size > emit->scope->stack_size) {
381 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100382 }
383 emit->last_emit_was_return_value = false;
384}
385
Damien George6f355fd2014-04-10 14:11:31 +0100386STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000387 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100388 assert(l < emit->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100389 if (emit->pass < MP_PASS_EMIT) {
Damienb05d7072013-10-05 13:37:10 +0100390 // assign label offset
391 assert(emit->label_offsets[l] == -1);
Damien George3417bc22014-05-10 10:36:38 +0100392 emit->label_offsets[l] = emit->bytecode_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100393 } else {
394 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
Damien George3417bc22014-05-10 10:36:38 +0100395 //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
396 assert(emit->label_offsets[l] == emit->bytecode_offset);
Damien429d7192013-10-04 19:53:11 +0100397 }
398}
399
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200400STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000401 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100402 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100403}
404
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200405STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000406 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100407 emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100408}
409
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200410STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000411 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100412 emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100413}
414
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200415STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000416 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100417 switch (tok) {
Damien George3417bc22014-05-10 10:36:38 +0100418 case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
419 case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
420 case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
421 case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100422 default: assert(0);
423 }
424}
425
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200426STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000427 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100428 emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100429}
430
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200431STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000432 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100433 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100434}
435
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200436STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000437 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100438 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100439}
440
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200441STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000442 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100443 if (bytes) {
Damien George3417bc22014-05-10 10:36:38 +0100444 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100445 } else {
Damien George3417bc22014-05-10 10:36:38 +0100446 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100447 }
448}
449
Damien George523b5752014-03-31 11:59:23 +0100450STATIC void emit_bc_load_null(emit_t *emit) {
451 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100452 emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
Damien George523b5752014-03-31 11:59:23 +0100453};
454
Damien George2bf7c092014-04-09 15:26:46 +0100455STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100456 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000457 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100458 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100459 case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
460 case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
461 case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
462 default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100463 }
464}
465
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200466STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000467 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100468 emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000469}
470
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200471STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000472 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100473 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100474}
475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200476STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000477 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100478 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000482 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100483 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100484}
485
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200486STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000487 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100488 emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100489}
490
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200491STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000492 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100493 emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100494}
495
Damien George729f7b42014-04-17 22:10:53 +0100496STATIC void emit_bc_load_subscr(emit_t *emit) {
497 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100498 emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
Damien George729f7b42014-04-17 22:10:53 +0100499}
500
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200501STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100502 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000503 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100504 switch (local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100505 case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
506 case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
507 case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
508 default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100509 }
510}
511
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200512STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000513 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100514 emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000515}
516
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200517STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000518 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100519 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100520}
521
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200522STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000523 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100524 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100525}
526
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200527STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000528 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100529 emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100530}
531
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200532STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000533 emit_bc_pre(emit, -3);
Damien George3417bc22014-05-10 10:36:38 +0100534 emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100535}
536
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200537STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100538 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100539}
540
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200541STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien George3417bc22014-05-10 10:36:38 +0100542 emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000543}
544
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200545STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000546 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100547 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100548}
549
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200550STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000551 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100552 emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100553}
554
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200555STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien George1d24ea52014-04-08 21:11:49 +0100556 emit_bc_load_null(emit);
557 emit_bc_rot_two(emit);
558 emit_bc_store_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100559}
560
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200561STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100562 emit_bc_load_null(emit);
563 emit_bc_rot_three(emit);
564 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100565}
566
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200567STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000568 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100569 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100570}
571
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200572STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000573 emit_bc_pre(emit, 2);
Damien George3417bc22014-05-10 10:36:38 +0100574 emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100575}
576
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200577STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000578 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100579 emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100580}
581
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200582STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000583 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100584 emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200587STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000588 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100589 emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100590}
591
Damien George6f355fd2014-04-10 14:11:31 +0100592STATIC void emit_bc_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000593 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100594 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100595}
596
Damien George6f355fd2014-04-10 14:11:31 +0100597STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000598 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100599 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100600}
601
Damien George6f355fd2014-04-10 14:11:31 +0100602STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000603 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100604 emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Damien George6f355fd2014-04-10 14:11:31 +0100607STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000608 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100609 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Damien George6f355fd2014-04-10 14:11:31 +0100612STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000613 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100614 emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damien George6f355fd2014-04-10 14:11:31 +0100617STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000618 if (except_depth == 0) {
619 emit_bc_jump(emit, label);
620 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000621 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100622 emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
623 emit_write_bytecode_byte(emit, except_depth);
Damien Georgecbddb272014-02-01 20:08:18 +0000624 }
Damien429d7192013-10-04 19:53:11 +0100625}
626
Damien George6f355fd2014-04-10 14:11:31 +0100627STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000628 emit_bc_pre(emit, 7);
Damien George3417bc22014-05-10 10:36:38 +0100629 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100630}
631
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200632STATIC void emit_bc_with_cleanup(emit_t *emit) {
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(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100635}
636
Damien George6f355fd2014-04-10 14:11:31 +0100637STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000638 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100639 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100640}
641
Damien George6f355fd2014-04-10 14:11:31 +0100642STATIC void emit_bc_setup_finally(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_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100645}
646
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200647STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000648 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100649 emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100650}
651
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200652STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000653 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100654 emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100655}
656
Damien George6f355fd2014-04-10 14:11:31 +0100657STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000658 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100659 emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100660}
661
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200662STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000663 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100664}
665
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200666STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000667 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100668 emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100669}
670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200671STATIC void emit_bc_pop_except(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_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100674}
675
Damien Georged17926d2014-03-30 13:35:08 +0100676STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
677 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000678 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100679 emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000680 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100681 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000682 } else {
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, op);
Damien George9aa2a522014-02-01 23:04:09 +0000685 }
Damien429d7192013-10-04 19:53:11 +0100686}
687
Damien Georged17926d2014-03-30 13:35:08 +0100688STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000689 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100690 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000691 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100692 op = MP_BINARY_OP_IN;
693 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000694 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100695 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000696 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000697 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100698 emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000699 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000700 emit_bc_pre(emit, 0);
Damien George3417bc22014-05-10 10:36:38 +0100701 emit_write_bytecode_byte(emit, MP_BC_NOT);
Damien George9aa2a522014-02-01 23:04:09 +0000702 }
Damien429d7192013-10-04 19:53:11 +0100703}
704
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200705STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100706 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000707 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100708 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100709}
710
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200711STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100712 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000713 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100714 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100715}
716
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200717STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100718 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000719 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100720 emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100721}
722
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200723STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100724 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000725 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100726 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100727}
728
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200729STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000730 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100731 emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100732}
733
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200734STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100735 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000736 emit_bc_pre(emit, -2);
Damien George3417bc22014-05-10 10:36:38 +0100737 emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100738}
739
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200740STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100741 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000742 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100743 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100744}
745
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200746STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100747 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000748 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100749 emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100750}
751
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200752STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100753 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000754 emit_bc_pre(emit, 1 - n_args);
Damien George3417bc22014-05-10 10:36:38 +0100755 emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100756}
757
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200758STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100759 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000760 emit_bc_pre(emit, -1 + n_args);
Damien George3417bc22014-05-10 10:36:38 +0100761 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100762}
763
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200764STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100765 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000766 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George3417bc22014-05-10 10:36:38 +0100767 emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100768}
769
Damien George30565092014-03-31 11:30:17 +0100770STATIC 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 +0100771 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000772 emit_bc_pre(emit, 1);
Damien George3417bc22014-05-10 10:36:38 +0100773 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000774 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100775 emit_bc_pre(emit, -1);
Damien George3417bc22014-05-10 10:36:38 +0100776 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200777 }
Damien429d7192013-10-04 19:53:11 +0100778}
779
Damien George3558f622014-04-20 17:50:40 +0100780STATIC 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 +0100781 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100782 emit_bc_pre(emit, -n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100783 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
784 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200785 } else {
Damien George3558f622014-04-20 17:50:40 +0100786 assert(n_closed_over <= 255);
787 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien George3417bc22014-05-10 10:36:38 +0100788 emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
789 emit_write_bytecode_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200790 }
Damien429d7192013-10-04 19:53:11 +0100791}
792
Damien George922ddd62014-04-09 12:43:17 +0100793STATIC 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) {
794 if (star_flags) {
795 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100796 // load dummy entry for non-existent pos_seq
797 emit_bc_load_null(emit);
798 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100799 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100800 // load dummy entry for non-existent kw_dict
801 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100802 }
Damien George523b5752014-03-31 11:59:23 +0100803 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
Damien George3417bc22014-05-10 10:36:38 +0100804 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 +0100805 } else {
Damien George523b5752014-03-31 11:59:23 +0100806 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
Damien George3417bc22014-05-10 10:36:38 +0100807 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 +0100808 }
Damien George523b5752014-03-31 11:59:23 +0100809}
810
Damien George922ddd62014-04-09 12:43:17 +0100811STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
812 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100813}
814
Damien George922ddd62014-04-09 12:43:17 +0100815STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
816 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100817}
818
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200819STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000820 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100821 emit->last_emit_was_return_value = true;
Damien George3417bc22014-05-10 10:36:38 +0100822 emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100823}
824
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200825STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000826 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000827 emit_bc_pre(emit, -n_args);
Damien George3417bc22014-05-10 10:36:38 +0100828 emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100829}
830
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200831STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000832 emit_bc_pre(emit, 0);
Damien George36db6bc2014-05-07 17:24:22 +0100833 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100834 emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100835}
836
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200837STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000838 emit_bc_pre(emit, -1);
Damien George36db6bc2014-05-07 17:24:22 +0100839 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien George3417bc22014-05-10 10:36:38 +0100840 emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100841}
842
Damien6cdd3af2013-10-05 18:08:26 +0100843const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100844 emit_bc_set_native_types,
845 emit_bc_start_pass,
846 emit_bc_end_pass,
847 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000848 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000849 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100850
Damien4b03e772013-10-05 14:17:09 +0100851 emit_bc_load_id,
852 emit_bc_store_id,
853 emit_bc_delete_id,
854
Damien415eb6f2013-10-05 12:19:06 +0100855 emit_bc_label_assign,
856 emit_bc_import_name,
857 emit_bc_import_from,
858 emit_bc_import_star,
859 emit_bc_load_const_tok,
860 emit_bc_load_const_small_int,
861 emit_bc_load_const_int,
862 emit_bc_load_const_dec,
Damien415eb6f2013-10-05 12:19:06 +0100863 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100864 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100865 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100866 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000867 emit_bc_load_name,
868 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100869 emit_bc_load_attr,
870 emit_bc_load_method,
871 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100872 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100873 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000874 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100875 emit_bc_store_name,
876 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100877 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100878 emit_bc_store_subscr,
879 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000880 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100881 emit_bc_delete_name,
882 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100883 emit_bc_delete_attr,
884 emit_bc_delete_subscr,
885 emit_bc_dup_top,
886 emit_bc_dup_top_two,
887 emit_bc_pop_top,
888 emit_bc_rot_two,
889 emit_bc_rot_three,
890 emit_bc_jump,
891 emit_bc_pop_jump_if_true,
892 emit_bc_pop_jump_if_false,
893 emit_bc_jump_if_true_or_pop,
894 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000895 emit_bc_unwind_jump,
896 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100897 emit_bc_setup_with,
898 emit_bc_with_cleanup,
899 emit_bc_setup_except,
900 emit_bc_setup_finally,
901 emit_bc_end_finally,
902 emit_bc_get_iter,
903 emit_bc_for_iter,
904 emit_bc_for_iter_end,
905 emit_bc_pop_block,
906 emit_bc_pop_except,
907 emit_bc_unary_op,
908 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100909 emit_bc_build_tuple,
910 emit_bc_build_list,
911 emit_bc_list_append,
912 emit_bc_build_map,
913 emit_bc_store_map,
914 emit_bc_map_add,
915 emit_bc_build_set,
916 emit_bc_set_add,
917 emit_bc_build_slice,
918 emit_bc_unpack_sequence,
919 emit_bc_unpack_ex,
920 emit_bc_make_function,
921 emit_bc_make_closure,
922 emit_bc_call_function,
923 emit_bc_call_method,
924 emit_bc_return_value,
925 emit_bc_raise_varargs,
926 emit_bc_yield_value,
927 emit_bc_yield_from,
928};
Damien George5f6a25f2014-04-20 18:02:27 +0100929
930#endif // !MICROPY_EMIT_CPYTHON