blob: 4dad61bb2133202704ca1599e46251d77c5d0ca8 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
6
7#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010012#include "obj.h"
13#include "emitglue.h"
Damien429d7192013-10-04 19:53:11 +010014#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010016#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000017#include "bc0.h"
Damien429d7192013-10-04 19:53:11 +010018
Damien415eb6f2013-10-05 12:19:06 +010019struct _emit_t {
20 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010021 int stack_size;
22 bool last_emit_was_return_value;
23
24 scope_t *scope;
25
Damien George08335002014-01-18 23:24:36 +000026 uint last_source_line_offset;
27 uint last_source_line;
28
Damienb05d7072013-10-05 13:37:10 +010029 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010030 uint *label_offsets;
31
Damien George08335002014-01-18 23:24:36 +000032 uint code_info_offset;
33 uint code_info_size;
34 uint byte_code_offset;
35 uint byte_code_size;
36 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010037 byte dummy_data[8];
38};
39
Damien George1d24ea52014-04-08 21:11:49 +010040STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010041STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010042
Damien Georgecbd2f742014-01-19 11:48:48 +000043emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000044 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010045 emit->max_num_labels = max_num_labels;
46 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010047 return emit;
48}
Damien4b03e772013-10-05 14:17:09 +010049
Damien George41d02b62014-01-24 22:42:28 +000050void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020051 m_del(uint, emit->label_offsets, emit->max_num_labels);
52 m_del_obj(emit_t, emit);
53}
54
Damien George08335002014-01-18 23:24:36 +000055// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020056STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010057 //printf("emit %d\n", num_bytes_to_write);
58 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +000059 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010060 return emit->dummy_data;
61 } else {
Damien George08335002014-01-18 23:24:36 +000062 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
63 byte *c = emit->code_base + emit->code_info_offset;
64 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010065 return c;
66 }
67}
68
Damien Georgedf8127a2014-04-13 11:04:33 +010069STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
70 emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
71}
72
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020073STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +000074 byte* c = emit_get_cur_to_write_code_info(emit, 4);
75 // TODO variable length encoding for qstr
76 c[0] = qstr & 0xff;
77 c[1] = (qstr >> 8) & 0xff;
78 c[2] = (qstr >> 16) & 0xff;
79 c[3] = (qstr >> 24) & 0xff;
80}
81
Damien George73496fb2014-04-13 14:51:56 +010082#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020083STATIC 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 +010084 assert(bytes_to_skip > 0 || lines_to_skip > 0);
85 while (bytes_to_skip > 0 || lines_to_skip > 0) {
86 uint b = MIN(bytes_to_skip, 31);
87 uint l = MIN(lines_to_skip, 7);
88 bytes_to_skip -= b;
89 lines_to_skip -= l;
90 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
Damien George28eb5772014-01-25 11:43:20 +000091 }
Damien George08335002014-01-18 23:24:36 +000092}
Damien George73496fb2014-04-13 14:51:56 +010093#endif
Damien George08335002014-01-18 23:24:36 +000094
95// all functions must go through this one to emit byte code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020096STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +000097 //printf("emit %d\n", num_bytes_to_write);
98 if (emit->pass < PASS_3) {
99 emit->byte_code_offset += num_bytes_to_write;
100 return emit->dummy_data;
101 } else {
102 assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
103 byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
104 emit->byte_code_offset += num_bytes_to_write;
105 return c;
106 }
107}
108
Damien Georgedf8127a2014-04-13 11:04:33 +0100109STATIC void emit_align_byte_code_to_machine_word(emit_t* emit) {
110 emit->byte_code_offset = (emit->byte_code_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
111}
112
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200113STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
Damien George08335002014-01-18 23:24:36 +0000114 byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100115 c[0] = b1;
116}
117
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200118STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100119 assert((b2 & (~0xff)) == 0);
Damien George08335002014-01-18 23:24:36 +0000120 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100121 c[0] = b1;
122 c[1] = b2;
123}
124
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200125STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200126 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200127 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200128 byte *p = buf + sizeof(buf);
129 // We encode in little-ending order, but store in big-endian, to help decoding
130 do {
131 *--p = num & 0x7f;
132 num >>= 7;
133 } while (num != 0);
134 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
135 while (p != buf + sizeof(buf) - 1) {
136 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000137 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200138 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000139}
140
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200141// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200142STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200143 emit_write_byte_code_byte(emit, b1);
144
145 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200146 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200147 byte *p = buf + sizeof(buf);
148 // We encode in little-ending order, but store in big-endian, to help decoding
149 do {
150 *--p = num & 0x7f;
151 num >>= 7;
152 } while (num != 0 && num != -1);
153 // Make sure that highest bit we stored (mask 0x40) matches sign
154 // of the number. If not, store extra byte just to encode sign
155 if (num == -1 && (*p & 0x40) == 0) {
156 *--p = 0x7f;
157 } else if (num == 0 && (*p & 0x40) != 0) {
158 *--p = 0;
159 }
160
161 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
162 while (p != buf + sizeof(buf) - 1) {
163 *c++ = *p++ | 0x80;
164 }
165 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100166}
167
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200168STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000169 emit_write_byte_code_byte(emit, b);
170 emit_write_byte_code_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100171}
172
Damien Georgedf8127a2014-04-13 11:04:33 +0100173// aligns the pointer so it is friendly to GC
174STATIC void emit_write_byte_code_byte_ptr(emit_t* emit, byte b, void *ptr) {
175 emit_write_byte_code_byte(emit, b);
176 emit_align_byte_code_to_machine_word(emit);
177 machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_byte_code(emit, sizeof(machine_uint_t));
178 *c = (machine_uint_t)ptr;
179}
180
Damien Georgefb083ea2014-02-01 18:29:40 +0000181/* currently unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200182STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000183 emit_write_byte_code_byte(emit, b);
184 emit_write_byte_code_byte_uint(emit, num1);
185 emit_write_byte_code_byte_uint(emit, num2);
186}
187*/
188
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200189STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000190 emit_write_byte_code_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100191}
192
Damien03c9cfb2013-11-05 22:06:08 +0000193// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George6f355fd2014-04-10 14:11:31 +0100194STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000195 uint byte_code_offset;
Damien429d7192013-10-04 19:53:11 +0100196 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000197 byte_code_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100198 } else {
Damien George08335002014-01-18 23:24:36 +0000199 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100200 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100201 byte *c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000202 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000203 c[1] = byte_code_offset;
204 c[2] = byte_code_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000205}
206
207// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George6f355fd2014-04-10 14:11:31 +0100208STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000209 int byte_code_offset;
Damien03c9cfb2013-11-05 22:06:08 +0000210 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000211 byte_code_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000212 } else {
Damien George08335002014-01-18 23:24:36 +0000213 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000214 }
Damien George08335002014-01-18 23:24:36 +0000215 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000216 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000217 c[1] = byte_code_offset;
218 c[2] = byte_code_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100219}
220
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200221STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000222}
223
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200224STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000225 emit->pass = pass;
226 emit->stack_size = 0;
227 emit->last_emit_was_return_value = false;
228 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000229 emit->last_source_line_offset = 0;
230 emit->last_source_line = 1;
Damien George6baf76e2013-12-30 22:32:17 +0000231 if (pass == PASS_2) {
232 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
233 }
Damien George08335002014-01-18 23:24:36 +0000234 emit->byte_code_offset = 0;
235 emit->code_info_offset = 0;
236
237 // write code info size (don't know size at this stage in PASS_2 so need to use maximum space (4 bytes) to write it)
238 {
239 byte* c = emit_get_cur_to_write_code_info(emit, 4);
240 machine_uint_t s = emit->code_info_size;
241 c[0] = s & 0xff;
242 c[1] = (s >> 8) & 0xff;
243 c[2] = (s >> 16) & 0xff;
244 c[3] = (s >> 24) & 0xff;
245 }
246
247 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000248 emit_write_code_info_qstr(emit, scope->source_file);
249 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000250
Damien Georgebee17b02014-03-27 11:07:04 +0000251 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000252 {
Damien Georgebee17b02014-03-27 11:07:04 +0000253 byte* c = emit_get_cur_to_write_byte_code(emit, 4);
254 uint n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000255 if (n_state == 0) {
256 // Need at least 1 entry in the state, in the case an exception is
257 // propagated through this function, the exception is returned in
258 // the highest slot in the state (fastn[0], see vm.c).
259 n_state = 1;
260 }
Damien Georgebee17b02014-03-27 11:07:04 +0000261 c[0] = n_state & 0xff;
262 c[1] = (n_state >> 8) & 0xff;
263 c[2] = scope->exc_stack_size & 0xff;
264 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000265 }
266
267 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000268 int num_cell = 0;
269 for (int i = 0; i < scope->id_info_len; i++) {
270 id_info_t *id = &scope->id_info[i];
271 if (id->kind == ID_INFO_KIND_CELL) {
272 num_cell += 1;
273 }
274 }
275 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000276 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000277 for (int i = 0; i < scope->id_info_len; i++) {
278 id_info_t *id = &scope->id_info[i];
279 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000280 emit_write_byte_code_byte(emit, id->local_num); // write the local which should be converted to a cell
Damien George6baf76e2013-12-30 22:32:17 +0000281 }
282 }
283}
284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000286 // check stack is back to zero size
287 if (emit->stack_size != 0) {
288 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
289 }
290
Damien George73496fb2014-04-13 14:51:56 +0100291 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien Georgedf8127a2014-04-13 11:04:33 +0100292 emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned
Damien George08335002014-01-18 23:24:36 +0000293
Damien George6baf76e2013-12-30 22:32:17 +0000294 if (emit->pass == PASS_2) {
295 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000296 emit->code_info_size = emit->code_info_offset;
297 emit->byte_code_size = emit->byte_code_offset;
Damien Georgedf8127a2014-04-13 11:04:33 +0100298 emit->code_base = m_new0(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000299
300 } else if (emit->pass == PASS_3) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200301 qstr *arg_names = m_new(qstr, emit->scope->num_params);
302 for (int i = 0; i < emit->scope->num_params; i++) {
303 arg_names[i] = emit->scope->id_info[i].qstr;
304 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100305 mp_emit_glue_assign_byte_code(emit->scope->raw_code, emit->code_base,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200306 emit->code_info_size + emit->byte_code_size,
Damien George2326d522014-03-27 23:26:35 +0000307 emit->scope->num_params, emit->scope->num_locals,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200308 emit->scope->scope_flags, arg_names);
Damien George6baf76e2013-12-30 22:32:17 +0000309 }
310}
311
Damien George069a35e2014-04-10 17:22:19 +0000312STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100313 return emit->last_emit_was_return_value;
314}
315
Damien Georged66ae182014-04-10 17:28:54 +0000316STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
317 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100318}
319
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200320STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000321 //printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
Damien George62ad1892014-01-29 21:51:51 +0000322#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000323 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000324 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
325 uint lines_to_skip = source_line - emit->last_source_line;
326 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000327 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000328 emit->last_source_line_offset = emit->byte_code_offset;
329 emit->last_source_line = source_line;
330 }
Damien George62ad1892014-01-29 21:51:51 +0000331#endif
Damien George08335002014-01-18 23:24:36 +0000332}
333
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200334STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100335 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
336}
337
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200338STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100339 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
340}
341
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200342STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100343 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
344}
345
Damien Georgece8f07a2014-03-27 23:30:26 +0000346STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damien George78035b92014-04-09 12:27:39 +0100347 assert((int)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100348 emit->stack_size += stack_size_delta;
349 if (emit->stack_size > emit->scope->stack_size) {
350 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100351 }
352 emit->last_emit_was_return_value = false;
353}
354
Damien George6f355fd2014-04-10 14:11:31 +0100355STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000356 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100357 assert(l < emit->max_num_labels);
358 if (emit->pass == PASS_2) {
359 // assign label offset
360 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000361 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100362 } else if (emit->pass == PASS_3) {
363 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000364 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
365 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100366 }
367}
368
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200369STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000370 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000371 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100372}
373
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200374STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000375 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000376 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100377}
378
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200379STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000380 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000381 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100382}
383
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200384STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000385 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100386 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000387 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
388 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
389 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
390 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100391 default: assert(0);
392 }
393}
394
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200395STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000396 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000397 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100398}
399
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200400STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000401 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000402 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100403}
404
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200405STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000406 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000407 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100408}
409
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200410STATIC void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000411 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000412 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
Damien429d7192013-10-04 19:53:11 +0100413}
414
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200415STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000416 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100417 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000418 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100419 } else {
Damien George08335002014-01-18 23:24:36 +0000420 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100421 }
422}
423
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200424STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000425 // not needed/supported for BC
Damien429d7192013-10-04 19:53:11 +0100426 assert(0);
427}
428
Damien George523b5752014-03-31 11:59:23 +0100429STATIC void emit_bc_load_null(emit_t *emit) {
430 emit_bc_pre(emit, 1);
431 emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
432};
433
Damien George2bf7c092014-04-09 15:26:46 +0100434STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100435 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000436 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100437 switch (local_num) {
438 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
439 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
440 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
441 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100442 }
443}
444
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200445STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000446 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000447 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000448}
449
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200450STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien George6baf76e2013-12-30 22:32:17 +0000451 // not needed/supported for BC
452 assert(0);
Damien9ecbcff2013-12-11 00:41:43 +0000453}
454
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000456 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000457 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100458}
459
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200460STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000461 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000462 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100463}
464
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200465STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000466 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000467 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100468}
469
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200470STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000471 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000472 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200475STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000476 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000477 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200480STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100481 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000482 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100483 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000484 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
485 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
486 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
487 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100488 }
489}
490
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200491STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000492 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000493 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000494}
495
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200496STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000497 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000498 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100499}
500
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200501STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000502 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000503 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100504}
505
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200506STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000507 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000508 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100509}
510
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200511STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000512 emit_bc_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000513 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100514}
515
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200516STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100517 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100518}
519
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200520STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100521 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000522}
523
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200524STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000525 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000526 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100527}
528
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200529STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000530 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000531 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100532}
533
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200534STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien George1d24ea52014-04-08 21:11:49 +0100535 emit_bc_load_null(emit);
536 emit_bc_rot_two(emit);
537 emit_bc_store_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100538}
539
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200540STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100541 emit_bc_load_null(emit);
542 emit_bc_rot_three(emit);
543 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100544}
545
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200546STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000547 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000548 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100549}
550
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200551STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000552 emit_bc_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000553 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100554}
555
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200556STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000557 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000558 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100559}
560
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200561STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000562 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000563 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100564}
565
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200566STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000567 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000568 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100569}
570
Damien George6f355fd2014-04-10 14:11:31 +0100571STATIC void emit_bc_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000572 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000573 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100574}
575
Damien George6f355fd2014-04-10 14:11:31 +0100576STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000577 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000578 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100579}
580
Damien George6f355fd2014-04-10 14:11:31 +0100581STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000582 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000583 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100584}
585
Damien George6f355fd2014-04-10 14:11:31 +0100586STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000587 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000588 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100589}
590
Damien George6f355fd2014-04-10 14:11:31 +0100591STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000592 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000593 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100594}
595
Damien George6f355fd2014-04-10 14:11:31 +0100596STATIC void emit_bc_setup_loop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000597 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000598 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Damien George6f355fd2014-04-10 14:11:31 +0100601STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000602 if (except_depth == 0) {
603 emit_bc_jump(emit, label);
604 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000605 emit_bc_pre(emit, 0);
Damien Georgecbddb272014-02-01 20:08:18 +0000606 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
607 emit_write_byte_code_byte(emit, except_depth);
608 }
Damien429d7192013-10-04 19:53:11 +0100609}
610
Damien George6f355fd2014-04-10 14:11:31 +0100611STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000612 emit_bc_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000613 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100614}
615
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000617 emit_bc_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000618 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100619}
620
Damien George6f355fd2014-04-10 14:11:31 +0100621STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000622 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000623 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100624}
625
Damien George6f355fd2014-04-10 14:11:31 +0100626STATIC void emit_bc_setup_finally(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000627 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000628 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100629}
630
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200631STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000632 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000633 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100634}
635
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200636STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000637 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000638 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100639}
640
Damien George6f355fd2014-04-10 14:11:31 +0100641STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000642 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000643 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100644}
645
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200646STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000647 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100648}
649
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200650STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000651 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000652 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100653}
654
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200655STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000656 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000657 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100658}
659
Damien Georged17926d2014-03-30 13:35:08 +0100660STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
661 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000662 emit_bc_pre(emit, 0);
Damien Georged17926d2014-03-30 13:35:08 +0100663 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000664 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000665 emit_write_byte_code_byte(emit, MP_BC_NOT);
666 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000667 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000668 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
669 }
Damien429d7192013-10-04 19:53:11 +0100670}
671
Damien Georged17926d2014-03-30 13:35:08 +0100672STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000673 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100674 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000675 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100676 op = MP_BINARY_OP_IN;
677 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000678 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100679 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000680 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000681 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000682 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000683 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000684 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000685 emit_write_byte_code_byte(emit, MP_BC_NOT);
686 }
Damien429d7192013-10-04 19:53:11 +0100687}
688
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200689STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100690 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000691 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000692 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100693}
694
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200695STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100696 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000697 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000698 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100699}
700
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200701STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100702 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000703 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000704 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100705}
706
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200707STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100708 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000709 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000710 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100711}
712
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200713STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000714 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000715 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100716}
717
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200718STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100719 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000720 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000721 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100722}
723
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200724STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100725 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000726 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000727 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100728}
729
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200730STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100731 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000732 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000733 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100734}
735
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200736STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100737 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000738 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000739 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100740}
741
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200742STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100743 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000744 emit_bc_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000745 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100746}
747
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200748STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100749 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000750 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000751 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100752}
753
Damien George30565092014-03-31 11:30:17 +0100754STATIC 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 +0100755 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000756 emit_bc_pre(emit, 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100757 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000758 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100759 if (n_pos_defaults == 0) {
760 // load dummy entry for non-existent positional default tuple
761 emit_bc_load_null(emit);
Damien George69b89d22014-04-11 13:38:30 +0000762 emit_bc_rot_two(emit);
Damien Georgee337f1e2014-03-31 15:18:37 +0100763 } else if (n_kw_defaults == 0) {
764 // load dummy entry for non-existent keyword default dict
765 emit_bc_load_null(emit);
Damien Georgee337f1e2014-03-31 15:18:37 +0100766 }
767 emit_bc_pre(emit, -1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100768 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200769 }
Damien429d7192013-10-04 19:53:11 +0100770}
771
Damien George30565092014-03-31 11:30:17 +0100772STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100773 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000774 emit_bc_pre(emit, 0);
Damien Georgedf8127a2014-04-13 11:04:33 +0100775 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200776 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100777 if (n_pos_defaults == 0) {
778 // load dummy entry for non-existent positional default tuple
779 emit_bc_load_null(emit);
Damien George69b89d22014-04-11 13:38:30 +0000780 emit_bc_rot_three(emit);
Damien Georgee337f1e2014-03-31 15:18:37 +0100781 } else if (n_kw_defaults == 0) {
782 // load dummy entry for non-existent keyword default dict
783 emit_bc_load_null(emit);
Damien George69b89d22014-04-11 13:38:30 +0000784 emit_bc_rot_two(emit);
Damien Georgee337f1e2014-03-31 15:18:37 +0100785 }
786 emit_bc_pre(emit, -2);
Damien Georgedf8127a2014-04-13 11:04:33 +0100787 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200788 }
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George922ddd62014-04-09 12:43:17 +0100791STATIC 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) {
792 if (star_flags) {
793 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100794 // load dummy entry for non-existent pos_seq
795 emit_bc_load_null(emit);
796 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100797 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100798 // load dummy entry for non-existent kw_dict
799 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100800 }
Damien George523b5752014-03-31 11:59:23 +0100801 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
802 emit_write_byte_code_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100803 } else {
Damien George523b5752014-03-31 11:59:23 +0100804 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
805 emit_write_byte_code_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
Damien429d7192013-10-04 19:53:11 +0100806 }
Damien George523b5752014-03-31 11:59:23 +0100807}
808
Damien George922ddd62014-04-09 12:43:17 +0100809STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
810 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100811}
812
Damien George922ddd62014-04-09 12:43:17 +0100813STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
814 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100815}
816
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200817STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000818 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100819 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000820 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100821}
822
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200823STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000824 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000825 emit_bc_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000826 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100827}
828
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200829STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000830 emit_bc_pre(emit, 0);
Damien429d7192013-10-04 19:53:11 +0100831 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000832 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100833 }
Damien George08335002014-01-18 23:24:36 +0000834 emit_write_byte_code_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);
Damien429d7192013-10-04 19:53:11 +0100839 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000840 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100841 }
Damien George08335002014-01-18 23:24:36 +0000842 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100843}
844
Damien6cdd3af2013-10-05 18:08:26 +0100845const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100846 emit_bc_set_native_types,
847 emit_bc_start_pass,
848 emit_bc_end_pass,
849 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000850 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000851 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100852
Damien4b03e772013-10-05 14:17:09 +0100853 emit_bc_load_id,
854 emit_bc_store_id,
855 emit_bc_delete_id,
856
Damien415eb6f2013-10-05 12:19:06 +0100857 emit_bc_label_assign,
858 emit_bc_import_name,
859 emit_bc_import_from,
860 emit_bc_import_star,
861 emit_bc_load_const_tok,
862 emit_bc_load_const_small_int,
863 emit_bc_load_const_int,
864 emit_bc_load_const_dec,
865 emit_bc_load_const_id,
866 emit_bc_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100867 emit_bc_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100868 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100869 emit_bc_load_deref,
870 emit_bc_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000871 emit_bc_load_name,
872 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100873 emit_bc_load_attr,
874 emit_bc_load_method,
875 emit_bc_load_build_class,
876 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000877 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100878 emit_bc_store_name,
879 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100880 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100881 emit_bc_store_subscr,
882 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000883 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100884 emit_bc_delete_name,
885 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100886 emit_bc_delete_attr,
887 emit_bc_delete_subscr,
888 emit_bc_dup_top,
889 emit_bc_dup_top_two,
890 emit_bc_pop_top,
891 emit_bc_rot_two,
892 emit_bc_rot_three,
893 emit_bc_jump,
894 emit_bc_pop_jump_if_true,
895 emit_bc_pop_jump_if_false,
896 emit_bc_jump_if_true_or_pop,
897 emit_bc_jump_if_false_or_pop,
898 emit_bc_setup_loop,
Damien Georgecbddb272014-02-01 20:08:18 +0000899 emit_bc_unwind_jump,
900 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100901 emit_bc_setup_with,
902 emit_bc_with_cleanup,
903 emit_bc_setup_except,
904 emit_bc_setup_finally,
905 emit_bc_end_finally,
906 emit_bc_get_iter,
907 emit_bc_for_iter,
908 emit_bc_for_iter_end,
909 emit_bc_pop_block,
910 emit_bc_pop_except,
911 emit_bc_unary_op,
912 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100913 emit_bc_build_tuple,
914 emit_bc_build_list,
915 emit_bc_list_append,
916 emit_bc_build_map,
917 emit_bc_store_map,
918 emit_bc_map_add,
919 emit_bc_build_set,
920 emit_bc_set_add,
921 emit_bc_build_slice,
922 emit_bc_unpack_sequence,
923 emit_bc_unpack_ex,
924 emit_bc_make_function,
925 emit_bc_make_closure,
926 emit_bc_call_function,
927 emit_bc_call_method,
928 emit_bc_return_value,
929 emit_bc_raise_varargs,
930 emit_bc_yield_value,
931 emit_bc_yield_from,
932};