blob: 0a8344855945214117e054f22b19a607fba576ca [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"
Damien429d7192013-10-04 19:53:11 +010012#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010014#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000015#include "emitglue.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
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200112 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200113 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
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200131 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200132 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
Damien Georgebee17b02014-03-27 11:07:04 +0000228 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000229 {
Damien Georgebee17b02014-03-27 11:07:04 +0000230 byte* c = emit_get_cur_to_write_byte_code(emit, 4);
231 uint n_state = scope->num_locals + scope->stack_size;
232 c[0] = n_state & 0xff;
233 c[1] = (n_state >> 8) & 0xff;
234 c[2] = scope->exc_stack_size & 0xff;
235 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000236 }
237
238 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000239 int num_cell = 0;
240 for (int i = 0; i < scope->id_info_len; i++) {
241 id_info_t *id = &scope->id_info[i];
242 if (id->kind == ID_INFO_KIND_CELL) {
243 num_cell += 1;
244 }
245 }
246 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000247 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000248 for (int i = 0; i < scope->id_info_len; i++) {
249 id_info_t *id = &scope->id_info[i];
250 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000251 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 +0000252 }
253 }
254}
255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200256STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000257 // check stack is back to zero size
258 if (emit->stack_size != 0) {
259 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
260 }
261
Damien George28eb5772014-01-25 11:43:20 +0000262 emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000263
Damien George6baf76e2013-12-30 22:32:17 +0000264 if (emit->pass == PASS_2) {
265 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000266 emit->code_info_size = emit->code_info_offset;
267 emit->byte_code_size = emit->byte_code_offset;
268 emit->code_base = m_new(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000269
270 } else if (emit->pass == PASS_3) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200271 qstr *arg_names = m_new(qstr, emit->scope->num_params);
272 for (int i = 0; i < emit->scope->num_params; i++) {
273 arg_names[i] = emit->scope->id_info[i].qstr;
274 }
Damien George2326d522014-03-27 23:26:35 +0000275 mp_emit_glue_assign_byte_code(emit->scope->unique_code_id, emit->code_base,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200276 emit->code_info_size + emit->byte_code_size,
Damien George2326d522014-03-27 23:26:35 +0000277 emit->scope->num_params, emit->scope->num_locals,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200278 emit->scope->scope_flags, arg_names);
Damien George6baf76e2013-12-30 22:32:17 +0000279 }
280}
281
Damien415eb6f2013-10-05 12:19:06 +0100282bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100283 return emit->last_emit_was_return_value;
284}
285
Damien415eb6f2013-10-05 12:19:06 +0100286int emit_bc_get_stack_size(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100287 return emit->stack_size;
288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC void emit_bc_set_stack_size(emit_t *emit, int size) {
Damienb05d7072013-10-05 13:37:10 +0100291 emit->stack_size = size;
Damien429d7192013-10-04 19:53:11 +0100292}
293
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200294STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000295 //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 +0000296#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000297 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000298 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
299 uint lines_to_skip = source_line - emit->last_source_line;
300 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000301 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000302 emit->last_source_line_offset = emit->byte_code_offset;
303 emit->last_source_line = source_line;
304 }
Damien George62ad1892014-01-29 21:51:51 +0000305#endif
Damien George08335002014-01-18 23:24:36 +0000306}
307
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200308STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100309 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
310}
311
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200312STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100313 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
314}
315
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200316STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100317 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
318}
319
Damien Georgece8f07a2014-03-27 23:30:26 +0000320STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damienb05d7072013-10-05 13:37:10 +0100321 emit->stack_size += stack_size_delta;
322 if (emit->stack_size > emit->scope->stack_size) {
323 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100324 }
325 emit->last_emit_was_return_value = false;
326}
327
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200328STATIC void emit_bc_label_assign(emit_t *emit, int l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000329 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100330 assert(l < emit->max_num_labels);
331 if (emit->pass == PASS_2) {
332 // assign label offset
333 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000334 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100335 } else if (emit->pass == PASS_3) {
336 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000337 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
338 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100339 }
340}
341
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200342STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000343 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000344 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100345}
346
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200347STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000348 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000349 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100350}
351
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200352STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000353 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000354 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100355}
356
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200357STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000358 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100359 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000360 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
361 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
362 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
363 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100364 default: assert(0);
365 }
366}
367
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200368STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000369 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000370 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100371}
372
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200373STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000374 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000375 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100376}
377
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200378STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000379 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000380 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100381}
382
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200383STATIC void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000384 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000385 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
Damien429d7192013-10-04 19:53:11 +0100386}
387
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200388STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000389 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100390 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000391 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100392 } else {
Damien George08335002014-01-18 23:24:36 +0000393 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100394 }
395}
396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200397STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000398 // not needed/supported for BC
Damien429d7192013-10-04 19:53:11 +0100399 assert(0);
400}
401
Damien George523b5752014-03-31 11:59:23 +0100402STATIC void emit_bc_load_null(emit_t *emit) {
403 emit_bc_pre(emit, 1);
404 emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
405};
406
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200407STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100408 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000409 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100410 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000411 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
412 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
413 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
414 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100415 }
416}
417
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200418STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000419 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000420 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000421}
422
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200423STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien George6baf76e2013-12-30 22:32:17 +0000424 // not needed/supported for BC
425 assert(0);
Damien9ecbcff2013-12-11 00:41:43 +0000426}
427
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200428STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000429 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000430 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100431}
432
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200433STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000434 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000435 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100436}
437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200438STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000439 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000440 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200443STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000444 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000445 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100446}
447
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200448STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000449 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000450 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200453STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100454 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000455 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100456 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000457 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
458 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
459 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
460 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100461 }
462}
463
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200464STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000465 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000466 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000467}
468
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200469STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000470 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000471 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100472}
473
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200474STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000475 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000476 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100477}
478
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200479STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000480 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000481 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100482}
483
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200484STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000485 emit_bc_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000486 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100487}
488
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200489STATIC void emit_bc_store_locals(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100490 // not needed
Damien Georgece8f07a2014-03-27 23:30:26 +0000491 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000492 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damiena3977762013-10-09 23:10:10 +0100493}
494
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200495STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100496 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000497 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000498 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100499}
500
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200501STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000502 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000503 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000504}
505
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200506STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000507 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000508 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100509}
510
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200511STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000512 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000513 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100514}
515
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200516STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000517 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000518 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100519}
520
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200521STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000522 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000523 emit_write_byte_code_byte(emit, MP_BC_DELETE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100524}
525
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200526STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000527 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000528 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100529}
530
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200531STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000532 emit_bc_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000533 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100534}
535
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200536STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000537 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000538 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100539}
540
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200541STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000542 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000543 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100544}
545
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200546STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000547 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000548 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100549}
550
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200551STATIC void emit_bc_jump(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000552 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000553 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100554}
555
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200556STATIC void emit_bc_pop_jump_if_true(emit_t *emit, int label) {
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_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100559}
560
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200561STATIC void emit_bc_pop_jump_if_false(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000562 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000563 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100564}
565
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200566STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000567 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000568 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100569}
570
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200571STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000572 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000573 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100574}
575
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200576STATIC void emit_bc_setup_loop(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000577 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000578 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label);
Damien429d7192013-10-04 19:53:11 +0100579}
580
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200581STATIC void emit_bc_unwind_jump(emit_t *emit, int label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000582 if (except_depth == 0) {
583 emit_bc_jump(emit, label);
584 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000585 emit_bc_pre(emit, 0);
Damien Georgecbddb272014-02-01 20:08:18 +0000586 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
587 emit_write_byte_code_byte(emit, except_depth);
588 }
Damien429d7192013-10-04 19:53:11 +0100589}
590
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200591STATIC void emit_bc_setup_with(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000592 emit_bc_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000593 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100594}
595
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200596STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000597 emit_bc_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000598 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100599}
600
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200601STATIC void emit_bc_setup_except(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000602 emit_bc_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000603 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100604}
605
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200606STATIC void emit_bc_setup_finally(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000607 emit_bc_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000608 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100609}
610
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200611STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000612 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000613 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100614}
615
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200616STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000617 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000618 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100619}
620
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200621STATIC void emit_bc_for_iter(emit_t *emit, int label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000622 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000623 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100624}
625
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200626STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000627 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100628}
629
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200630STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000631 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000632 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100633}
634
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200635STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000636 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000637 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100638}
639
Damien Georged17926d2014-03-30 13:35:08 +0100640STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
641 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000642 emit_bc_pre(emit, 0);
Damien Georged17926d2014-03-30 13:35:08 +0100643 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000644 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000645 emit_write_byte_code_byte(emit, MP_BC_NOT);
646 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000647 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000648 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
649 }
Damien429d7192013-10-04 19:53:11 +0100650}
651
Damien Georged17926d2014-03-30 13:35:08 +0100652STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000653 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100654 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000655 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100656 op = MP_BINARY_OP_IN;
657 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000658 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100659 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000660 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000661 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000662 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000663 if (invert) {
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 }
Damien429d7192013-10-04 19:53:11 +0100667}
668
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200669STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100670 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000671 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000672 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100673}
674
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200675STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100676 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000677 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000678 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100679}
680
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200681STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100682 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000683 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000684 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100685}
686
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200687STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100688 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000689 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000690 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100691}
692
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200693STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000694 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000695 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100696}
697
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200698STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100699 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000700 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000701 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100702}
703
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200704STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100705 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000706 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000707 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100708}
709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200710STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100711 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000712 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000713 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200716STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100717 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000718 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000719 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100720}
721
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200722STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100723 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000724 emit_bc_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000725 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100726}
727
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200728STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100729 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000730 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000731 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100732}
733
Damien George30565092014-03-31 11:30:17 +0100734STATIC 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 +0100735 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000736 emit_bc_pre(emit, 1);
Damien Georgefb083ea2014-02-01 18:29:40 +0000737 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION, scope->unique_code_id);
738 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100739 if (n_pos_defaults == 0) {
740 // load dummy entry for non-existent positional default tuple
741 emit_bc_load_null(emit);
742 } else if (n_kw_defaults == 0) {
743 // load dummy entry for non-existent keyword default dict
744 emit_bc_load_null(emit);
745 emit_bc_rot_two(emit);
746 }
747 emit_bc_pre(emit, -1);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200748 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->unique_code_id);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200749 }
Damien429d7192013-10-04 19:53:11 +0100750}
751
Damien George30565092014-03-31 11:30:17 +0100752STATIC 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 +0100753 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000754 emit_bc_pre(emit, 0);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200755 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE, scope->unique_code_id);
756 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100757 if (n_pos_defaults == 0) {
758 // load dummy entry for non-existent positional default tuple
759 emit_bc_load_null(emit);
760 emit_bc_rot_two(emit);
761 } else if (n_kw_defaults == 0) {
762 // load dummy entry for non-existent keyword default dict
763 emit_bc_load_null(emit);
764 emit_bc_rot_three(emit);
765 }
766 emit_bc_pre(emit, -2);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200767 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->unique_code_id);
768 }
Damien429d7192013-10-04 19:53:11 +0100769}
770
Damien George523b5752014-03-31 11:59:23 +0100771STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uint bytecode_base, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
772 if (have_star_arg || have_dbl_star_arg) {
773 if (!have_star_arg) {
774 // load dummy entry for non-existent pos_seq
775 emit_bc_load_null(emit);
776 emit_bc_rot_two(emit);
777 } else if (!have_dbl_star_arg) {
778 // load dummy entry for non-existent kw_dict
779 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100780 }
Damien George523b5752014-03-31 11:59:23 +0100781 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
782 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 +0100783 } else {
Damien George523b5752014-03-31 11:59:23 +0100784 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
785 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 +0100786 }
Damien George523b5752014-03-31 11:59:23 +0100787}
788
789STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
790 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +0100791}
792
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200793STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
Damien George523b5752014-03-31 11:59:23 +0100794 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +0100795}
796
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200797STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000798 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100799 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000800 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100801}
802
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200803STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000804 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000805 emit_bc_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000806 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100807}
808
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200809STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000810 emit_bc_pre(emit, 0);
Damien429d7192013-10-04 19:53:11 +0100811 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000812 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100813 }
Damien George08335002014-01-18 23:24:36 +0000814 emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100815}
816
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200817STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000818 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100819 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000820 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100821 }
Damien George08335002014-01-18 23:24:36 +0000822 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100823}
824
Damien6cdd3af2013-10-05 18:08:26 +0100825const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100826 emit_bc_set_native_types,
827 emit_bc_start_pass,
828 emit_bc_end_pass,
829 emit_bc_last_emit_was_return_value,
830 emit_bc_get_stack_size,
831 emit_bc_set_stack_size,
Damien George08335002014-01-18 23:24:36 +0000832 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100833
Damien4b03e772013-10-05 14:17:09 +0100834 emit_bc_load_id,
835 emit_bc_store_id,
836 emit_bc_delete_id,
837
Damien415eb6f2013-10-05 12:19:06 +0100838 emit_bc_label_assign,
839 emit_bc_import_name,
840 emit_bc_import_from,
841 emit_bc_import_star,
842 emit_bc_load_const_tok,
843 emit_bc_load_const_small_int,
844 emit_bc_load_const_int,
845 emit_bc_load_const_dec,
846 emit_bc_load_const_id,
847 emit_bc_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100848 emit_bc_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100849 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100850 emit_bc_load_deref,
851 emit_bc_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000852 emit_bc_load_name,
853 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100854 emit_bc_load_attr,
855 emit_bc_load_method,
856 emit_bc_load_build_class,
857 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000858 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100859 emit_bc_store_name,
860 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100861 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100862 emit_bc_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100863 emit_bc_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100864 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000865 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100866 emit_bc_delete_name,
867 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100868 emit_bc_delete_attr,
869 emit_bc_delete_subscr,
870 emit_bc_dup_top,
871 emit_bc_dup_top_two,
872 emit_bc_pop_top,
873 emit_bc_rot_two,
874 emit_bc_rot_three,
875 emit_bc_jump,
876 emit_bc_pop_jump_if_true,
877 emit_bc_pop_jump_if_false,
878 emit_bc_jump_if_true_or_pop,
879 emit_bc_jump_if_false_or_pop,
880 emit_bc_setup_loop,
Damien Georgecbddb272014-02-01 20:08:18 +0000881 emit_bc_unwind_jump,
882 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100883 emit_bc_setup_with,
884 emit_bc_with_cleanup,
885 emit_bc_setup_except,
886 emit_bc_setup_finally,
887 emit_bc_end_finally,
888 emit_bc_get_iter,
889 emit_bc_for_iter,
890 emit_bc_for_iter_end,
891 emit_bc_pop_block,
892 emit_bc_pop_except,
893 emit_bc_unary_op,
894 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100895 emit_bc_build_tuple,
896 emit_bc_build_list,
897 emit_bc_list_append,
898 emit_bc_build_map,
899 emit_bc_store_map,
900 emit_bc_map_add,
901 emit_bc_build_set,
902 emit_bc_set_add,
903 emit_bc_build_slice,
904 emit_bc_unpack_sequence,
905 emit_bc_unpack_ex,
906 emit_bc_make_function,
907 emit_bc_make_closure,
908 emit_bc_call_function,
909 emit_bc_call_method,
910 emit_bc_return_value,
911 emit_bc_raise_varargs,
912 emit_bc_yield_value,
913 emit_bc_yield_from,
914};