blob: fcaa9b7fae0cd2f03480cea69e2ad0f562532c92 [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"
Damiend99b0522013-12-21 18:17:45 +000015#include "bc0.h"
Damien429d7192013-10-04 19:53:11 +010016
Damien415eb6f2013-10-05 12:19:06 +010017struct _emit_t {
18 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010019 int stack_size;
20 bool last_emit_was_return_value;
21
22 scope_t *scope;
23
Damien George08335002014-01-18 23:24:36 +000024 uint last_source_line_offset;
25 uint last_source_line;
26
Damienb05d7072013-10-05 13:37:10 +010027 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010028 uint *label_offsets;
29
Damien George08335002014-01-18 23:24:36 +000030 uint code_info_offset;
31 uint code_info_size;
32 uint byte_code_offset;
33 uint byte_code_size;
34 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010035 byte dummy_data[8];
36};
37
Damien Georgecbd2f742014-01-19 11:48:48 +000038emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000039 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010040 emit->max_num_labels = max_num_labels;
41 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010042 return emit;
43}
Damien4b03e772013-10-05 14:17:09 +010044
Damien George41d02b62014-01-24 22:42:28 +000045void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020046 m_del(uint, emit->label_offsets, emit->max_num_labels);
47 m_del_obj(emit_t, emit);
48}
49
Damien George08335002014-01-18 23:24:36 +000050// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020051STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010052 //printf("emit %d\n", num_bytes_to_write);
53 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +000054 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010055 return emit->dummy_data;
56 } else {
Damien George08335002014-01-18 23:24:36 +000057 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
58 byte *c = emit->code_base + emit->code_info_offset;
59 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010060 return c;
61 }
62}
63
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020064STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +000065 byte* c = emit_get_cur_to_write_code_info(emit, 4);
66 // TODO variable length encoding for qstr
67 c[0] = qstr & 0xff;
68 c[1] = (qstr >> 8) & 0xff;
69 c[2] = (qstr >> 16) & 0xff;
70 c[3] = (qstr >> 24) & 0xff;
71}
72
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020073STATIC 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 +000074 for (; bytes_to_skip > 31; bytes_to_skip -= 31) {
75 *emit_get_cur_to_write_code_info(emit, 1) = 31;
76 }
77 for (; lines_to_skip > 7; lines_to_skip -= 7) {
78 *emit_get_cur_to_write_code_info(emit, 1) = 7 << 5;
79 }
80 *emit_get_cur_to_write_code_info(emit, 1) = bytes_to_skip | (lines_to_skip << 5);
Damien George08335002014-01-18 23:24:36 +000081}
82
83// all functions must go through this one to emit byte code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020084STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +000085 //printf("emit %d\n", num_bytes_to_write);
86 if (emit->pass < PASS_3) {
87 emit->byte_code_offset += num_bytes_to_write;
88 return emit->dummy_data;
89 } else {
90 assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
91 byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
92 emit->byte_code_offset += num_bytes_to_write;
93 return c;
94 }
95}
96
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020097STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
Damien George08335002014-01-18 23:24:36 +000098 byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Damien429d7192013-10-04 19:53:11 +010099 c[0] = b1;
100}
101
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200102STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100103 assert((b2 & (~0xff)) == 0);
Damien George08335002014-01-18 23:24:36 +0000104 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100105 c[0] = b1;
106 c[1] = b2;
107}
108
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200109STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200110 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200111 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200112 byte *p = buf + sizeof(buf);
113 // We encode in little-ending order, but store in big-endian, to help decoding
114 do {
115 *--p = num & 0x7f;
116 num >>= 7;
117 } while (num != 0);
118 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
119 while (p != buf + sizeof(buf) - 1) {
120 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000121 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200122 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000123}
124
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200125// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200126STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200127 emit_write_byte_code_byte(emit, b1);
128
129 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200130 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200131 byte *p = buf + sizeof(buf);
132 // We encode in little-ending order, but store in big-endian, to help decoding
133 do {
134 *--p = num & 0x7f;
135 num >>= 7;
136 } while (num != 0 && num != -1);
137 // Make sure that highest bit we stored (mask 0x40) matches sign
138 // of the number. If not, store extra byte just to encode sign
139 if (num == -1 && (*p & 0x40) == 0) {
140 *--p = 0x7f;
141 } else if (num == 0 && (*p & 0x40) != 0) {
142 *--p = 0;
143 }
144
145 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
146 while (p != buf + sizeof(buf) - 1) {
147 *c++ = *p++ | 0x80;
148 }
149 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100150}
151
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200152STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000153 emit_write_byte_code_byte(emit, b);
154 emit_write_byte_code_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100155}
156
Damien Georgefb083ea2014-02-01 18:29:40 +0000157/* currently unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200158STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000159 emit_write_byte_code_byte(emit, b);
160 emit_write_byte_code_byte_uint(emit, num1);
161 emit_write_byte_code_byte_uint(emit, num2);
162}
163*/
164
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200165STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000166 emit_write_byte_code_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100167}
168
Damien03c9cfb2013-11-05 22:06:08 +0000169// unsigned labels are relative to ip following this instruction, stored as 16 bits
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200170STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, int label) {
Damien George08335002014-01-18 23:24:36 +0000171 uint byte_code_offset;
Damien429d7192013-10-04 19:53:11 +0100172 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000173 byte_code_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100174 } else {
Damien George08335002014-01-18 23:24:36 +0000175 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100176 }
Damien George08335002014-01-18 23:24:36 +0000177 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000178 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000179 c[1] = byte_code_offset;
180 c[2] = byte_code_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000181}
182
183// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200184STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, int label) {
Damien George08335002014-01-18 23:24:36 +0000185 int byte_code_offset;
Damien03c9cfb2013-11-05 22:06:08 +0000186 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000187 byte_code_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000188 } else {
Damien George08335002014-01-18 23:24:36 +0000189 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000190 }
Damien George08335002014-01-18 23:24:36 +0000191 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000192 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000193 c[1] = byte_code_offset;
194 c[2] = byte_code_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000198}
199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000201 emit->pass = pass;
202 emit->stack_size = 0;
203 emit->last_emit_was_return_value = false;
204 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000205 emit->last_source_line_offset = 0;
206 emit->last_source_line = 1;
Damien George6baf76e2013-12-30 22:32:17 +0000207 if (pass == PASS_2) {
208 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
209 }
Damien George08335002014-01-18 23:24:36 +0000210 emit->byte_code_offset = 0;
211 emit->code_info_offset = 0;
212
213 // 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)
214 {
215 byte* c = emit_get_cur_to_write_code_info(emit, 4);
216 machine_uint_t s = emit->code_info_size;
217 c[0] = s & 0xff;
218 c[1] = (s >> 8) & 0xff;
219 c[2] = (s >> 16) & 0xff;
220 c[3] = (s >> 24) & 0xff;
221 }
222
223 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000224 emit_write_code_info_qstr(emit, scope->source_file);
225 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000226
Damien George8dcc0c72014-03-27 10:55:21 +0000227 // bytecode prelude: exception stack size; 16 bit uint for now
228 {
229 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
230 c[0] = scope->exc_stack_size & 0xff;
231 c[1] = (scope->exc_stack_size >> 8) & 0xff;
232 }
233
234 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000235 int num_cell = 0;
236 for (int i = 0; i < scope->id_info_len; i++) {
237 id_info_t *id = &scope->id_info[i];
238 if (id->kind == ID_INFO_KIND_CELL) {
239 num_cell += 1;
240 }
241 }
242 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000243 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000244 for (int i = 0; i < scope->id_info_len; i++) {
245 id_info_t *id = &scope->id_info[i];
246 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000247 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 +0000248 }
249 }
250}
251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200252STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000253 // check stack is back to zero size
254 if (emit->stack_size != 0) {
255 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
256 }
257
Damien George28eb5772014-01-25 11:43:20 +0000258 emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info
Damien George08335002014-01-18 23:24:36 +0000259
Damien George6baf76e2013-12-30 22:32:17 +0000260 if (emit->pass == PASS_2) {
261 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000262 emit->code_info_size = emit->code_info_offset;
263 emit->byte_code_size = emit->byte_code_offset;
264 emit->code_base = m_new(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000265
266 } else if (emit->pass == PASS_3) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200267 qstr *arg_names = m_new(qstr, emit->scope->num_params);
268 for (int i = 0; i < emit->scope->num_params; i++) {
269 arg_names[i] = emit->scope->id_info[i].qstr;
270 }
271 rt_assign_byte_code(emit->scope->unique_code_id, emit->code_base,
272 emit->code_info_size + emit->byte_code_size,
273 emit->scope->num_params, emit->scope->num_locals, emit->scope->stack_size,
274 emit->scope->scope_flags, arg_names);
Damien George6baf76e2013-12-30 22:32:17 +0000275 }
276}
277
Damien415eb6f2013-10-05 12:19:06 +0100278bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100279 return emit->last_emit_was_return_value;
280}
281
Damien415eb6f2013-10-05 12:19:06 +0100282int emit_bc_get_stack_size(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100283 return emit->stack_size;
284}
285
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200286STATIC void emit_bc_set_stack_size(emit_t *emit, int size) {
Damienb05d7072013-10-05 13:37:10 +0100287 emit->stack_size = size;
Damien429d7192013-10-04 19:53:11 +0100288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000291 //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 +0000292#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000293 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000294 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
295 uint lines_to_skip = source_line - emit->last_source_line;
296 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000297 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000298 emit->last_source_line_offset = emit->byte_code_offset;
299 emit->last_source_line = source_line;
300 }
Damien George62ad1892014-01-29 21:51:51 +0000301#endif
Damien George08335002014-01-18 23:24:36 +0000302}
303
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200304STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100305 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
306}
307
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200308STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100309 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
310}
311
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200312STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100313 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
314}
315
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200316// TODO: module-polymorphic function (read: name clash if made global)
Damien415eb6f2013-10-05 12:19:06 +0100317static void emit_pre(emit_t *emit, int stack_size_delta) {
Damienb05d7072013-10-05 13:37:10 +0100318 emit->stack_size += stack_size_delta;
319 if (emit->stack_size > emit->scope->stack_size) {
320 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100321 }
322 emit->last_emit_was_return_value = false;
323}
324
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200325STATIC void emit_bc_label_assign(emit_t *emit, int l) {
Damien429d7192013-10-04 19:53:11 +0100326 emit_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100327 assert(l < emit->max_num_labels);
328 if (emit->pass == PASS_2) {
329 // assign label offset
330 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000331 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100332 } else if (emit->pass == PASS_3) {
333 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000334 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
335 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100336 }
337}
338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100340 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000341 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100342}
343
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200344STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100345 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000346 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100347}
348
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200349STATIC void emit_bc_import_star(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100350 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000351 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100352}
353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200354STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien429d7192013-10-04 19:53:11 +0100355 emit_pre(emit, 1);
356 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000357 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
358 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
359 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
360 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100361 default: assert(0);
362 }
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien429d7192013-10-04 19:53:11 +0100366 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000367 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100368}
369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200370STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100371 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000372 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100373}
374
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200375STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100376 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000377 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100378}
379
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200380STATIC void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100381 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000382 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
Damien429d7192013-10-04 19:53:11 +0100383}
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien429d7192013-10-04 19:53:11 +0100386 emit_pre(emit, 1);
387 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000388 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100389 } else {
Damien George08335002014-01-18 23:24:36 +0000390 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100391 }
392}
393
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200394STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
Damiena1b26932013-12-12 15:34:40 +0000395 // not needed/supported for BC
Damien429d7192013-10-04 19:53:11 +0100396 assert(0);
397}
398
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200399STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100400 assert(local_num >= 0);
401 emit_pre(emit, 1);
402 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000403 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
404 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
405 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
406 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100407 }
408}
409
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200410STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000411 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000412 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000413}
414
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200415STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien George6baf76e2013-12-30 22:32:17 +0000416 // not needed/supported for BC
417 assert(0);
Damien9ecbcff2013-12-11 00:41:43 +0000418}
419
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200420STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100421 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000422 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100423}
424
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200425STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100426 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000427 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100428}
429
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200430STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100431 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000432 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100433}
434
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200435STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien George08d07552014-01-29 18:58:52 +0000436 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000437 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100438}
439
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200440STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100441 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000442 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100443}
444
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200445STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100446 assert(local_num >= 0);
447 emit_pre(emit, -1);
448 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000449 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
450 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
451 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
452 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100453 }
454}
455
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200456STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000457 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000458 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000459}
460
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200461STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100462 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000463 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100464}
465
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200466STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100467 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000468 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100469}
470
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200471STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100472 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000473 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100474}
475
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200476STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100477 emit_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000478 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100479}
480
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200481STATIC void emit_bc_store_locals(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100482 // not needed
Damiena3977762013-10-09 23:10:10 +0100483 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000484 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damiena3977762013-10-09 23:10:10 +0100485}
486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200487STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100488 assert(local_num >= 0);
489 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000490 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST_N, local_num);
Damien429d7192013-10-04 19:53:11 +0100491}
492
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200493STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000494 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000495 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000496}
497
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200498STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100499 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000500 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100501}
502
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200503STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100504 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000505 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100506}
507
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200508STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100509 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000510 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100514 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000515 emit_write_byte_code_byte(emit, MP_BC_DELETE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100516}
517
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200518STATIC void emit_bc_dup_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100519 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000520 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100521}
522
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200523STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100524 emit_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000525 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100526}
527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200528STATIC void emit_bc_pop_top(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100529 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000530 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100531}
532
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200533STATIC void emit_bc_rot_two(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100534 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000535 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100536}
537
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200538STATIC void emit_bc_rot_three(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100539 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000540 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100541}
542
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200543STATIC void emit_bc_jump(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100544 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000545 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100546}
547
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200548STATIC void emit_bc_pop_jump_if_true(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100549 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000550 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100551}
552
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200553STATIC void emit_bc_pop_jump_if_false(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100554 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000555 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100556}
557
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200558STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100559 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000560 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100561}
562
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200563STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100564 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000565 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200568STATIC void emit_bc_setup_loop(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100569 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000570 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200573STATIC void emit_bc_unwind_jump(emit_t *emit, int label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000574 if (except_depth == 0) {
575 emit_bc_jump(emit, label);
576 } else {
577 emit_pre(emit, 0);
578 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
579 emit_write_byte_code_byte(emit, except_depth);
580 }
Damien429d7192013-10-04 19:53:11 +0100581}
582
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200583STATIC void emit_bc_setup_with(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100584 emit_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000585 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100586}
587
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200588STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100589 emit_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000590 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200593STATIC void emit_bc_setup_except(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100594 emit_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000595 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC void emit_bc_setup_finally(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100599 emit_pre(emit, 6);
Damien George08335002014-01-18 23:24:36 +0000600 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100601}
602
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200603STATIC void emit_bc_end_finally(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100604 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000605 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100606}
607
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200608STATIC void emit_bc_get_iter(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100609 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000610 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200613STATIC void emit_bc_for_iter(emit_t *emit, int label) {
Damien429d7192013-10-04 19:53:11 +0100614 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000615 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200618STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100619 emit_pre(emit, -1);
620}
621
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200622STATIC void emit_bc_pop_block(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100623 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000624 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100625}
626
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200627STATIC void emit_bc_pop_except(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100628 emit_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000629 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100630}
631
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200632STATIC void emit_bc_unary_op(emit_t *emit, rt_unary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000633 if (op == RT_UNARY_OP_NOT) {
634 emit_pre(emit, 0);
635 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, RT_UNARY_OP_BOOL);
636 emit_pre(emit, 0);
637 emit_write_byte_code_byte(emit, MP_BC_NOT);
638 } else {
639 emit_pre(emit, 0);
640 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
641 }
Damien429d7192013-10-04 19:53:11 +0100642}
643
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200644STATIC void emit_bc_binary_op(emit_t *emit, rt_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000645 bool invert = false;
646 if (op == RT_BINARY_OP_NOT_IN) {
647 invert = true;
648 op = RT_BINARY_OP_IN;
649 } else if (op == RT_BINARY_OP_IS_NOT) {
650 invert = true;
651 op = RT_BINARY_OP_IS;
652 }
Damien429d7192013-10-04 19:53:11 +0100653 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000654 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000655 if (invert) {
656 emit_pre(emit, 0);
657 emit_write_byte_code_byte(emit, MP_BC_NOT);
658 }
Damien429d7192013-10-04 19:53:11 +0100659}
660
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200661STATIC void emit_bc_build_tuple(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_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100665}
666
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200667STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100668 assert(n_args >= 0);
669 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000670 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100671}
672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200673STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100674 assert(list_stack_index >= 0);
675 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000676 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100677}
678
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200679STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100680 assert(n_args >= 0);
681 emit_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000682 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100683}
684
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200685STATIC void emit_bc_store_map(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100686 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000687 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100688}
689
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200690STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100691 assert(map_stack_index >= 0);
692 emit_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000693 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100694}
695
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200696STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100697 assert(n_args >= 0);
698 emit_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000699 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100700}
701
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200702STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100703 assert(set_stack_index >= 0);
704 emit_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000705 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100706}
707
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200708STATIC void emit_bc_build_slice(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_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100712}
713
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200714STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100715 assert(n_args >= 0);
716 emit_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000717 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100718}
719
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200720STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100721 assert(n_left >=0 && n_right >= 0);
722 emit_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000723 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100724}
725
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200726STATIC 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 +0200727 assert(n_dict_params == 0);
Damien Georgefb083ea2014-02-01 18:29:40 +0000728 if (n_default_params == 0) {
729 emit_pre(emit, 1);
730 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION, scope->unique_code_id);
731 } else {
Paul Sokolovsky90750022014-02-01 15:05:04 +0200732 emit_pre(emit, 0);
733 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->unique_code_id);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200734 }
Damien429d7192013-10-04 19:53:11 +0100735}
736
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200737STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200738 assert(n_dict_params == 0);
739 if (n_default_params == 0) {
740 emit_pre(emit, 0);
741 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE, scope->unique_code_id);
742 } else {
743 emit_pre(emit, -1);
744 emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->unique_code_id);
745 }
Damien429d7192013-10-04 19:53:11 +0100746}
747
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200748STATIC 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 +0100749 int s = 0;
750 if (have_star_arg) {
751 s += 1;
752 }
753 if (have_dbl_star_arg) {
754 s += 1;
755 }
756 emit_pre(emit, -n_positional - 2 * n_keyword - s);
757 int op;
758 if (have_star_arg) {
759 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000760 op = MP_BC_CALL_FUNCTION_VAR_KW;
Damien429d7192013-10-04 19:53:11 +0100761 } else {
Damiend99b0522013-12-21 18:17:45 +0000762 op = MP_BC_CALL_FUNCTION_VAR;
Damien429d7192013-10-04 19:53:11 +0100763 }
764 } else {
765 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000766 op = MP_BC_CALL_FUNCTION_KW;
Damien429d7192013-10-04 19:53:11 +0100767 } else {
Damiend99b0522013-12-21 18:17:45 +0000768 op = MP_BC_CALL_FUNCTION;
Damien429d7192013-10-04 19:53:11 +0100769 }
770 }
Damien George08335002014-01-18 23:24:36 +0000771 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 +0100772}
773
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200774STATIC 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 +0100775 int s = 0;
776 if (have_star_arg) {
777 s += 1;
778 }
779 if (have_dbl_star_arg) {
780 s += 1;
781 }
Damien George08d07552014-01-29 18:58:52 +0000782 emit_pre(emit, -1 - n_positional - 2 * n_keyword - s);
Damien429d7192013-10-04 19:53:11 +0100783 int op;
784 if (have_star_arg) {
785 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000786 op = MP_BC_CALL_METHOD_VAR_KW;
Damien429d7192013-10-04 19:53:11 +0100787 } else {
Damiend99b0522013-12-21 18:17:45 +0000788 op = MP_BC_CALL_METHOD_VAR;
Damien429d7192013-10-04 19:53:11 +0100789 }
790 } else {
791 if (have_dbl_star_arg) {
Damiend99b0522013-12-21 18:17:45 +0000792 op = MP_BC_CALL_METHOD_KW;
Damien429d7192013-10-04 19:53:11 +0100793 } else {
Damiend99b0522013-12-21 18:17:45 +0000794 op = MP_BC_CALL_METHOD;
Damien429d7192013-10-04 19:53:11 +0100795 }
796 }
Damien George08335002014-01-18 23:24:36 +0000797 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 +0100798}
799
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200800STATIC void emit_bc_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100801 emit_pre(emit, -1);
802 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000803 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100804}
805
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200806STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000807 assert(0 <= n_args && n_args <= 2);
Damien429d7192013-10-04 19:53:11 +0100808 emit_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000809 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100810}
811
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200812STATIC void emit_bc_yield_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100813 emit_pre(emit, 0);
814 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000815 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100816 }
Damien George08335002014-01-18 23:24:36 +0000817 emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100818}
819
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200820STATIC void emit_bc_yield_from(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100821 emit_pre(emit, -1);
822 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000823 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100824 }
Damien George08335002014-01-18 23:24:36 +0000825 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100826}
827
Damien6cdd3af2013-10-05 18:08:26 +0100828const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100829 emit_bc_set_native_types,
830 emit_bc_start_pass,
831 emit_bc_end_pass,
832 emit_bc_last_emit_was_return_value,
833 emit_bc_get_stack_size,
834 emit_bc_set_stack_size,
Damien George08335002014-01-18 23:24:36 +0000835 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100836
Damien4b03e772013-10-05 14:17:09 +0100837 emit_bc_load_id,
838 emit_bc_store_id,
839 emit_bc_delete_id,
840
Damien415eb6f2013-10-05 12:19:06 +0100841 emit_bc_label_assign,
842 emit_bc_import_name,
843 emit_bc_import_from,
844 emit_bc_import_star,
845 emit_bc_load_const_tok,
846 emit_bc_load_const_small_int,
847 emit_bc_load_const_int,
848 emit_bc_load_const_dec,
849 emit_bc_load_const_id,
850 emit_bc_load_const_str,
Damien415eb6f2013-10-05 12:19:06 +0100851 emit_bc_load_const_verbatim_str,
Damien415eb6f2013-10-05 12:19:06 +0100852 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100853 emit_bc_load_deref,
854 emit_bc_load_closure,
Damien9ecbcff2013-12-11 00:41:43 +0000855 emit_bc_load_name,
856 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100857 emit_bc_load_attr,
858 emit_bc_load_method,
859 emit_bc_load_build_class,
860 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000861 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100862 emit_bc_store_name,
863 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100864 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100865 emit_bc_store_subscr,
Damiena3977762013-10-09 23:10:10 +0100866 emit_bc_store_locals,
Damien415eb6f2013-10-05 12:19:06 +0100867 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000868 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100869 emit_bc_delete_name,
870 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100871 emit_bc_delete_attr,
872 emit_bc_delete_subscr,
873 emit_bc_dup_top,
874 emit_bc_dup_top_two,
875 emit_bc_pop_top,
876 emit_bc_rot_two,
877 emit_bc_rot_three,
878 emit_bc_jump,
879 emit_bc_pop_jump_if_true,
880 emit_bc_pop_jump_if_false,
881 emit_bc_jump_if_true_or_pop,
882 emit_bc_jump_if_false_or_pop,
883 emit_bc_setup_loop,
Damien Georgecbddb272014-02-01 20:08:18 +0000884 emit_bc_unwind_jump,
885 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100886 emit_bc_setup_with,
887 emit_bc_with_cleanup,
888 emit_bc_setup_except,
889 emit_bc_setup_finally,
890 emit_bc_end_finally,
891 emit_bc_get_iter,
892 emit_bc_for_iter,
893 emit_bc_for_iter_end,
894 emit_bc_pop_block,
895 emit_bc_pop_except,
896 emit_bc_unary_op,
897 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100898 emit_bc_build_tuple,
899 emit_bc_build_list,
900 emit_bc_list_append,
901 emit_bc_build_map,
902 emit_bc_store_map,
903 emit_bc_map_add,
904 emit_bc_build_set,
905 emit_bc_set_add,
906 emit_bc_build_slice,
907 emit_bc_unpack_sequence,
908 emit_bc_unpack_ex,
909 emit_bc_make_function,
910 emit_bc_make_closure,
911 emit_bc_call_function,
912 emit_bc_call_method,
913 emit_bc_return_value,
914 emit_bc_raise_varargs,
915 emit_bc_yield_value,
916 emit_bc_yield_from,
917};