blob: 9fab977909ce9604de06804d1dd4a202f01f5f6b [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <unistd.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <assert.h>
7
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
Damien429d7192013-10-04 19:53:11 +010013#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000016#include "bc0.h"
Damien429d7192013-10-04 19:53:11 +010017
Damien415eb6f2013-10-05 12:19:06 +010018struct _emit_t {
19 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010020 int stack_size;
21 bool last_emit_was_return_value;
22
23 scope_t *scope;
24
Damien George08335002014-01-18 23:24:36 +000025 uint last_source_line_offset;
26 uint last_source_line;
27
Damienb05d7072013-10-05 13:37:10 +010028 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010029 uint *label_offsets;
30
Damien George08335002014-01-18 23:24:36 +000031 uint code_info_offset;
32 uint code_info_size;
33 uint byte_code_offset;
34 uint byte_code_size;
35 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010036 byte dummy_data[8];
37};
38
Damien Georgecbd2f742014-01-19 11:48:48 +000039emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000040 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010041 emit->max_num_labels = max_num_labels;
42 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010043 return emit;
44}
Damien4b03e772013-10-05 14:17:09 +010045
Damien George41d02b62014-01-24 22:42:28 +000046void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020047 m_del(uint, emit->label_offsets, emit->max_num_labels);
48 m_del_obj(emit_t, emit);
49}
50
Damien George08335002014-01-18 23:24:36 +000051// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020052STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010053 //printf("emit %d\n", num_bytes_to_write);
54 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +000055 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010056 return emit->dummy_data;
57 } else {
Damien George08335002014-01-18 23:24:36 +000058 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
59 byte *c = emit->code_base + emit->code_info_offset;
60 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010061 return c;
62 }
63}
64
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020065STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +000066 byte* c = emit_get_cur_to_write_code_info(emit, 4);
67 // TODO variable length encoding for qstr
68 c[0] = qstr & 0xff;
69 c[1] = (qstr >> 8) & 0xff;
70 c[2] = (qstr >> 16) & 0xff;
71 c[3] = (qstr >> 24) & 0xff;
72}
73
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020074STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, uint lines_to_skip) {
Damien George28eb5772014-01-25 11:43:20 +000075 for (; bytes_to_skip > 31; bytes_to_skip -= 31) {
76 *emit_get_cur_to_write_code_info(emit, 1) = 31;
77 }
78 for (; lines_to_skip > 7; lines_to_skip -= 7) {
79 *emit_get_cur_to_write_code_info(emit, 1) = 7 << 5;
80 }
81 *emit_get_cur_to_write_code_info(emit, 1) = bytes_to_skip | (lines_to_skip << 5);
Damien George08335002014-01-18 23:24:36 +000082}
83
84// all functions must go through this one to emit byte code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020085STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +000086 //printf("emit %d\n", num_bytes_to_write);
87 if (emit->pass < PASS_3) {
88 emit->byte_code_offset += num_bytes_to_write;
89 return emit->dummy_data;
90 } else {
91 assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
92 byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
93 emit->byte_code_offset += num_bytes_to_write;
94 return c;
95 }
96}
97
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020098STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
Damien George08335002014-01-18 23:24:36 +000099 byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100100 c[0] = b1;
101}
102
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200103STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100104 assert((b2 & (~0xff)) == 0);
Damien George08335002014-01-18 23:24:36 +0000105 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100106 c[0] = b1;
107 c[1] = b2;
108}
109
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200110STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200111 // We store each 7 bits in a separate byte, and that's how many bytes needed
112 byte buf[(BYTES_PER_WORD * 8 + 7) / 7];
113 byte *p = buf + sizeof(buf);
114 // We encode in little-ending order, but store in big-endian, to help decoding
115 do {
116 *--p = num & 0x7f;
117 num >>= 7;
118 } while (num != 0);
119 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
120 while (p != buf + sizeof(buf) - 1) {
121 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000122 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200123 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000124}
125
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200126// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200127STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200128 emit_write_byte_code_byte(emit, b1);
129
130 // We store each 7 bits in a separate byte, and that's how many bytes needed
131 byte buf[(BYTES_PER_WORD * 8 + 7) / 7];
132 byte *p = buf + sizeof(buf);
133 // We encode in little-ending order, but store in big-endian, to help decoding
134 do {
135 *--p = num & 0x7f;
136 num >>= 7;
137 } while (num != 0 && num != -1);
138 // Make sure that highest bit we stored (mask 0x40) matches sign
139 // of the number. If not, store extra byte just to encode sign
140 if (num == -1 && (*p & 0x40) == 0) {
141 *--p = 0x7f;
142 } else if (num == 0 && (*p & 0x40) != 0) {
143 *--p = 0;
144 }
145
146 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
147 while (p != buf + sizeof(buf) - 1) {
148 *c++ = *p++ | 0x80;
149 }
150 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100151}
152
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200153STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000154 emit_write_byte_code_byte(emit, b);
155 emit_write_byte_code_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100156}
157
Damien Georgefb083ea2014-02-01 18:29:40 +0000158/* currently unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200159STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000160 emit_write_byte_code_byte(emit, b);
161 emit_write_byte_code_byte_uint(emit, num1);
162 emit_write_byte_code_byte_uint(emit, num2);
163}
164*/
165
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200166STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000167 emit_write_byte_code_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100168}
169
Damien03c9cfb2013-11-05 22:06:08 +0000170// unsigned labels are relative to ip following this instruction, stored as 16 bits
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200171STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, int label) {
Damien George08335002014-01-18 23:24:36 +0000172 uint byte_code_offset;
Damien429d7192013-10-04 19:53:11 +0100173 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000174 byte_code_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100175 } else {
Damien George08335002014-01-18 23:24:36 +0000176 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100177 }
Damien George08335002014-01-18 23:24:36 +0000178 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000179 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000180 c[1] = byte_code_offset;
181 c[2] = byte_code_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000182}
183
184// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200185STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, int label) {
Damien George08335002014-01-18 23:24:36 +0000186 int byte_code_offset;
Damien03c9cfb2013-11-05 22:06:08 +0000187 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000188 byte_code_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000189 } else {
Damien George08335002014-01-18 23:24:36 +0000190 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000191 }
Damien George08335002014-01-18 23:24:36 +0000192 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000193 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000194 c[1] = byte_code_offset;
195 c[2] = byte_code_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100196}
197
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200198STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000199}
200
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200201STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000202 emit->pass = pass;
203 emit->stack_size = 0;
204 emit->last_emit_was_return_value = false;
205 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000206 emit->last_source_line_offset = 0;
207 emit->last_source_line = 1;
Damien George6baf76e2013-12-30 22:32:17 +0000208 if (pass == PASS_2) {
209 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
210 }
Damien George08335002014-01-18 23:24:36 +0000211 emit->byte_code_offset = 0;
212 emit->code_info_offset = 0;
213
214 // 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)
215 {
216 byte* c = emit_get_cur_to_write_code_info(emit, 4);
217 machine_uint_t s = emit->code_info_size;
218 c[0] = s & 0xff;
219 c[1] = (s >> 8) & 0xff;
220 c[2] = (s >> 16) & 0xff;
221 c[3] = (s >> 24) & 0xff;
222 }
223
224 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000225 emit_write_code_info_qstr(emit, scope->source_file);
226 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000227
228 // prelude for initialising closed over variables
229 int num_cell = 0;
230 for (int i = 0; i < scope->id_info_len; i++) {
231 id_info_t *id = &scope->id_info[i];
232 if (id->kind == ID_INFO_KIND_CELL) {
233 num_cell += 1;
234 }
235 }
236 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000237 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000238 for (int i = 0; i < scope->id_info_len; i++) {
239 id_info_t *id = &scope->id_info[i];
240 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000241 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 +0000242 }
243 }
244}
245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000247 // check stack is back to zero size
248 if (emit->stack_size != 0) {
249 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
250 }
251
Damien George28eb5772014-01-25 11:43:20 +0000252 emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000253
Damien George6baf76e2013-12-30 22:32:17 +0000254 if (emit->pass == PASS_2) {
255 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000256 emit->code_info_size = emit->code_info_offset;
257 emit->byte_code_size = emit->byte_code_offset;
258 emit->code_base = m_new(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000259
260 } else if (emit->pass == PASS_3) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200261 qstr *arg_names = m_new(qstr, emit->scope->num_params);
262 for (int i = 0; i < emit->scope->num_params; i++) {
263 arg_names[i] = emit->scope->id_info[i].qstr;
264 }
265 rt_assign_byte_code(emit->scope->unique_code_id, emit->code_base,
266 emit->code_info_size + emit->byte_code_size,
267 emit->scope->num_params, emit->scope->num_locals, emit->scope->stack_size,
268 emit->scope->scope_flags, arg_names);
Damien George6baf76e2013-12-30 22:32:17 +0000269 }
270}
271
Damien415eb6f2013-10-05 12:19:06 +0100272bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100273 return emit->last_emit_was_return_value;
274}
275
Damien415eb6f2013-10-05 12:19:06 +0100276int emit_bc_get_stack_size(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100277 return emit->stack_size;
278}
279
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200280STATIC void emit_bc_set_stack_size(emit_t *emit, int size) {
Damienb05d7072013-10-05 13:37:10 +0100281 emit->stack_size = size;
Damien429d7192013-10-04 19:53:11 +0100282}
283
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200284STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000285 //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 +0000286#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000287 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000288 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
289 uint lines_to_skip = source_line - emit->last_source_line;
290 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000291 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000292 emit->last_source_line_offset = emit->byte_code_offset;
293 emit->last_source_line = source_line;
294 }
Damien George62ad1892014-01-29 21:51:51 +0000295#endif
Damien George08335002014-01-18 23:24:36 +0000296}
297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100299 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
300}
301
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200302STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100303 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
304}
305
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200306STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100307 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
308}
309
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200310// TODO: module-polymorphic function (read: name clash if made global)
Damien415eb6f2013-10-05 12:19:06 +0100311static void emit_pre(emit_t *emit, int stack_size_delta) {
Damienb05d7072013-10-05 13:37:10 +0100312 emit->stack_size += stack_size_delta;
313 if (emit->stack_size > emit->scope->stack_size) {
314 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100315 }
316 emit->last_emit_was_return_value = false;
317}
318
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200319STATIC void emit_bc_label_assign(emit_t *emit, int l) {
Damien429d7192013-10-04 19:53:11 +0100320 emit_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100321 assert(l < emit->max_num_labels);
322 if (emit->pass == PASS_2) {
323 // assign label offset
324 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000325 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100326 } else if (emit->pass == PASS_3) {
327 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000328 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
329 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100330 }
331}
332
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200333STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100334 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000335 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100336}
337
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200338STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100339 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000340 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100341}
342
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200343STATIC void emit_bc_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100344 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000345 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100346}
347
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200348STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100349 emit_pre(emit, 1);
350 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000351 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
352 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
353 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
354 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100355 default: assert(0);
356 }
357}
358
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200359STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien429d7192013-10-04 19:53:11 +0100360 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000361 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100365 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000366 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100367}
368
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200369STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100370 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000371 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100372}
373
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200374STATIC void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100375 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000376 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
Damien429d7192013-10-04 19:53:11 +0100377}
378
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200379STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100380 emit_pre(emit, 1);
381 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000382 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100383 } else {
Damien George08335002014-01-18 23:24:36 +0000384 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100385 }
386}
387
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200388STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000389 // not needed/supported for BC
Damien429d7192013-10-04 19:53:11 +0100390 assert(0);
391}
392
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200393STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100394 assert(local_num >= 0);
395 emit_pre(emit, 1);
396 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000397 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
398 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
399 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
400 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100401 }
402}
403
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000405 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000406 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000407}
408
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200409STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien George6baf76e2013-12-30 22:32:17 +0000410 // not needed/supported for BC
411 assert(0);
Damien9ecbcff2013-12-11 00:41:43 +0000412}
413
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200414STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100415 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000416 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100417}
418
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200419STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100420 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000421 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100422}
423
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200424STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100425 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000426 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100427}
428
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200429STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien George08d07552014-01-29 18:58:52 +0000430 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000431 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100432}
433
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200434STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100435 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000436 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100437}
438
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200439STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100440 assert(local_num >= 0);
441 emit_pre(emit, -1);
442 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000443 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
444 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
445 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
446 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100447 }
448}
449
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200450STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000451 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000452 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000453}
454
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100456 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000457 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100458}
459
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200460STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100461 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000462 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100463}
464
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200465STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100466 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000467 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100468}
469
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200470STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100471 emit_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000472 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100473}
474
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200475STATIC void emit_bc_store_locals(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100476 // not needed
Damiena3977762013-10-09 23:10:10 +0100477 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000478 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damiena3977762013-10-09 23:10:10 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100482 assert(local_num >= 0);
483 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000484 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100485}
486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000488 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000489 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000490}
491
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200492STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100493 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000494 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100495}
496
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200497STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100498 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000499 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100500}
501
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200502STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100503 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000504 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100505}
506
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200507STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100508 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000509 emit_write_byte_code_byte(emit, MP_BC_DELETE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100510}
511
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200512STATIC void emit_bc_dup_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100513 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000514 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100515}
516
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200517STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100518 emit_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000519 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100520}
521
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200522STATIC void emit_bc_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100523 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000524 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100525}
526
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200527STATIC void emit_bc_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100528 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000529 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100530}
531
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200532STATIC void emit_bc_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100533 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000534 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100535}
536
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200537STATIC void emit_bc_jump(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100538 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000539 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100540}
541
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200542STATIC void emit_bc_pop_jump_if_true(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100543 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000544 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100545}
546
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200547STATIC void emit_bc_pop_jump_if_false(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100548 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000549 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100550}
551
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200552STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100553 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000554 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100555}
556
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200557STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100558 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000559 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100560}
561
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200562STATIC void emit_bc_setup_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100563 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000564 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label);
Damien429d7192013-10-04 19:53:11 +0100565}
566
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200567STATIC void emit_bc_unwind_jump(emit_t *emit, int label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000568 if (except_depth == 0) {
569 emit_bc_jump(emit, label);
570 } else {
571 emit_pre(emit, 0);
572 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
573 emit_write_byte_code_byte(emit, except_depth);
574 }
Damien429d7192013-10-04 19:53:11 +0100575}
576
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200577STATIC void emit_bc_setup_with(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100578 emit_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000579 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100580}
581
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200582STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100583 emit_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000584 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100585}
586
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200587STATIC void emit_bc_setup_except(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100588 emit_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000589 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100590}
591
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200592STATIC void emit_bc_setup_finally(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100593 emit_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000594 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100595}
596
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200597STATIC void emit_bc_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100598 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000599 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100600}
601
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200602STATIC void emit_bc_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100603 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000604 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100605}
606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC void emit_bc_for_iter(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100608 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000609 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100610}
611
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200612STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100613 emit_pre(emit, -1);
614}
615
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC void emit_bc_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100617 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000618 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100619}
620
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200621STATIC void emit_bc_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100622 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000623 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100624}
625
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200626STATIC void emit_bc_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000627 if (op == RT_UNARY_OP_NOT) {
628 emit_pre(emit, 0);
629 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, RT_UNARY_OP_BOOL);
630 emit_pre(emit, 0);
631 emit_write_byte_code_byte(emit, MP_BC_NOT);
632 } else {
633 emit_pre(emit, 0);
634 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
635 }
Damien429d7192013-10-04 19:53:11 +0100636}
637
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200638STATIC void emit_bc_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000639 bool invert = false;
640 if (op == RT_BINARY_OP_NOT_IN) {
641 invert = true;
642 op = RT_BINARY_OP_IN;
643 } else if (op == RT_BINARY_OP_IS_NOT) {
644 invert = true;
645 op = RT_BINARY_OP_IS;
646 }
Damien429d7192013-10-04 19:53:11 +0100647 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000648 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000649 if (invert) {
650 emit_pre(emit, 0);
651 emit_write_byte_code_byte(emit, MP_BC_NOT);
652 }
Damien429d7192013-10-04 19:53:11 +0100653}
654
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200655STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100656 assert(n_args >= 0);
657 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000658 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100659}
660
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200661STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100662 assert(n_args >= 0);
663 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000664 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100665}
666
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200667STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100668 assert(list_stack_index >= 0);
669 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000670 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100671}
672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200673STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100674 assert(n_args >= 0);
675 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000676 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100677}
678
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200679STATIC void emit_bc_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100680 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000681 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100682}
683
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200684STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100685 assert(map_stack_index >= 0);
686 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000687 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200690STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100691 assert(n_args >= 0);
692 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000693 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100694}
695
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200696STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100697 assert(set_stack_index >= 0);
698 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000699 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100703 assert(n_args >= 0);
704 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000705 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100706}
707
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200708STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100709 assert(n_args >= 0);
710 emit_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000711 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200714STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100715 assert(n_left >=0 && n_right >= 0);
716 emit_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000717 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100718}
719
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200720STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
Paul Sokolovsky90750022014-02-01 15:05:04 +0200721 assert(n_dict_params == 0);
Damien Georgefb083ea2014-02-01 18:29:40 +0000722 if (n_default_params == 0) {
723 emit_pre(emit, 1);
724 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION, scope->unique_code_id);
725 } else {
Paul Sokolovsky90750022014-02-01 15:05:04 +0200726 emit_bc_build_tuple(emit, n_default_params);
727 emit_pre(emit, 0);
728 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->unique_code_id);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200729 }
Damien429d7192013-10-04 19:53:11 +0100730}
731
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200732STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
Damien9ecbcff2013-12-11 00:41:43 +0000733 assert(n_default_params == 0 && n_dict_params == 0);
734 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000735 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE, scope->unique_code_id);
Damien429d7192013-10-04 19:53:11 +0100736}
737
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200738STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
Damien429d7192013-10-04 19:53:11 +0100739 int s = 0;
740 if (have_star_arg) {
741 s += 1;
742 }
743 if (have_dbl_star_arg) {
744 s += 1;
745 }
746 emit_pre(emit, -n_positional - 2 * n_keyword - s);
747 int op;
748 if (have_star_arg) {
749 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000750 op = MP_BC_CALL_FUNCTION_VAR_KW;
Damien429d7192013-10-04 19:53:11 +0100751 } else {
Damiend99b0522013-12-21 18:17:45 +0000752 op = MP_BC_CALL_FUNCTION_VAR;
Damien429d7192013-10-04 19:53:11 +0100753 }
754 } else {
755 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000756 op = MP_BC_CALL_FUNCTION_KW;
Damien429d7192013-10-04 19:53:11 +0100757 } else {
Damiend99b0522013-12-21 18:17:45 +0000758 op = MP_BC_CALL_FUNCTION;
Damien429d7192013-10-04 19:53:11 +0100759 }
760 }
Damien George08335002014-01-18 23:24:36 +0000761 emit_write_byte_code_byte_uint(emit, op, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints
Damien429d7192013-10-04 19:53:11 +0100762}
763
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200764STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
Damien429d7192013-10-04 19:53:11 +0100765 int s = 0;
766 if (have_star_arg) {
767 s += 1;
768 }
769 if (have_dbl_star_arg) {
770 s += 1;
771 }
Damien George08d07552014-01-29 18:58:52 +0000772 emit_pre(emit, -1 - n_positional - 2 * n_keyword - s);
Damien429d7192013-10-04 19:53:11 +0100773 int op;
774 if (have_star_arg) {
775 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000776 op = MP_BC_CALL_METHOD_VAR_KW;
Damien429d7192013-10-04 19:53:11 +0100777 } else {
Damiend99b0522013-12-21 18:17:45 +0000778 op = MP_BC_CALL_METHOD_VAR;
Damien429d7192013-10-04 19:53:11 +0100779 }
780 } else {
781 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000782 op = MP_BC_CALL_METHOD_KW;
Damien429d7192013-10-04 19:53:11 +0100783 } else {
Damiend99b0522013-12-21 18:17:45 +0000784 op = MP_BC_CALL_METHOD;
Damien429d7192013-10-04 19:53:11 +0100785 }
786 }
Damien George08335002014-01-18 23:24:36 +0000787 emit_write_byte_code_byte_uint(emit, op, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints
Damien429d7192013-10-04 19:53:11 +0100788}
789
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200790STATIC void emit_bc_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100791 emit_pre(emit, -1);
792 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000793 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100794}
795
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200796STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000797 assert(0 <= n_args && n_args <= 2);
Damien429d7192013-10-04 19:53:11 +0100798 emit_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000799 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100800}
801
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200802STATIC void emit_bc_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100803 emit_pre(emit, 0);
804 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000805 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100806 }
Damien George08335002014-01-18 23:24:36 +0000807 emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100808}
809
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200810STATIC void emit_bc_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100811 emit_pre(emit, -1);
812 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000813 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100814 }
Damien George08335002014-01-18 23:24:36 +0000815 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100816}
817
Damien6cdd3af2013-10-05 18:08:26 +0100818const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100819 emit_bc_set_native_types,
820 emit_bc_start_pass,
821 emit_bc_end_pass,
822 emit_bc_last_emit_was_return_value,
823 emit_bc_get_stack_size,
824 emit_bc_set_stack_size,
Damien George08335002014-01-18 23:24:36 +0000825 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100826
Damien4b03e772013-10-05 14:17:09 +0100827 emit_bc_load_id,
828 emit_bc_store_id,
829 emit_bc_delete_id,
830
Damien415eb6f2013-10-05 12:19:06 +0100831 emit_bc_label_assign,
832 emit_bc_import_name,
833 emit_bc_import_from,
834 emit_bc_import_star,
835 emit_bc_load_const_tok,
836 emit_bc_load_const_small_int,
837 emit_bc_load_const_int,
838 emit_bc_load_const_dec,
839 emit_bc_load_const_id,
840 emit_bc_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100841 emit_bc_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100842 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100843 emit_bc_load_deref,
844 emit_bc_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000845 emit_bc_load_name,
846 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100847 emit_bc_load_attr,
848 emit_bc_load_method,
849 emit_bc_load_build_class,
850 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000851 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100852 emit_bc_store_name,
853 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100854 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100855 emit_bc_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100856 emit_bc_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100857 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000858 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100859 emit_bc_delete_name,
860 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100861 emit_bc_delete_attr,
862 emit_bc_delete_subscr,
863 emit_bc_dup_top,
864 emit_bc_dup_top_two,
865 emit_bc_pop_top,
866 emit_bc_rot_two,
867 emit_bc_rot_three,
868 emit_bc_jump,
869 emit_bc_pop_jump_if_true,
870 emit_bc_pop_jump_if_false,
871 emit_bc_jump_if_true_or_pop,
872 emit_bc_jump_if_false_or_pop,
873 emit_bc_setup_loop,
Damien Georgecbddb272014-02-01 20:08:18 +0000874 emit_bc_unwind_jump,
875 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100876 emit_bc_setup_with,
877 emit_bc_with_cleanup,
878 emit_bc_setup_except,
879 emit_bc_setup_finally,
880 emit_bc_end_finally,
881 emit_bc_get_iter,
882 emit_bc_for_iter,
883 emit_bc_for_iter_end,
884 emit_bc_pop_block,
885 emit_bc_pop_except,
886 emit_bc_unary_op,
887 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100888 emit_bc_build_tuple,
889 emit_bc_build_list,
890 emit_bc_list_append,
891 emit_bc_build_map,
892 emit_bc_store_map,
893 emit_bc_map_add,
894 emit_bc_build_set,
895 emit_bc_set_add,
896 emit_bc_build_slice,
897 emit_bc_unpack_sequence,
898 emit_bc_unpack_ex,
899 emit_bc_make_function,
900 emit_bc_make_closure,
901 emit_bc_call_function,
902 emit_bc_call_method,
903 emit_bc_return_value,
904 emit_bc_raise_varargs,
905 emit_bc_yield_value,
906 emit_bc_yield_from,
907};