blob: f58cec1f1fb9052a6f0e258947cb5cf617186d16 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
6
7#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010012#include "obj.h"
13#include "emitglue.h"
Damien429d7192013-10-04 19:53:11 +010014#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010016#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000017#include "bc0.h"
Damien429d7192013-10-04 19:53:11 +010018
Damien George5f6a25f2014-04-20 18:02:27 +010019#if !MICROPY_EMIT_CPYTHON
20
Damien415eb6f2013-10-05 12:19:06 +010021struct _emit_t {
22 pass_kind_t pass;
Damien429d7192013-10-04 19:53:11 +010023 int stack_size;
24 bool last_emit_was_return_value;
25
26 scope_t *scope;
27
Damien George08335002014-01-18 23:24:36 +000028 uint last_source_line_offset;
29 uint last_source_line;
30
Damienb05d7072013-10-05 13:37:10 +010031 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010032 uint *label_offsets;
33
Damien George08335002014-01-18 23:24:36 +000034 uint code_info_offset;
35 uint code_info_size;
36 uint byte_code_offset;
37 uint byte_code_size;
38 byte *code_base; // stores both byte code and code info
Damien429d7192013-10-04 19:53:11 +010039 byte dummy_data[8];
40};
41
Damien George1d24ea52014-04-08 21:11:49 +010042STATIC void emit_bc_rot_two(emit_t *emit);
Damien Georgef4c9b332014-04-08 21:32:29 +010043STATIC void emit_bc_rot_three(emit_t *emit);
Damien George1d24ea52014-04-08 21:11:49 +010044
Damien Georgecbd2f742014-01-19 11:48:48 +000045emit_t *emit_bc_new(uint max_num_labels) {
Damien George08335002014-01-18 23:24:36 +000046 emit_t *emit = m_new0(emit_t, 1);
Damien6cdd3af2013-10-05 18:08:26 +010047 emit->max_num_labels = max_num_labels;
48 emit->label_offsets = m_new(uint, emit->max_num_labels);
Damien6cdd3af2013-10-05 18:08:26 +010049 return emit;
50}
Damien4b03e772013-10-05 14:17:09 +010051
Damien George41d02b62014-01-24 22:42:28 +000052void emit_bc_free(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +020053 m_del(uint, emit->label_offsets, emit->max_num_labels);
54 m_del_obj(emit_t, emit);
55}
56
Damien George08335002014-01-18 23:24:36 +000057// all functions must go through this one to emit code info
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020058STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010059 //printf("emit %d\n", num_bytes_to_write);
60 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +000061 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010062 return emit->dummy_data;
63 } else {
Damien George08335002014-01-18 23:24:36 +000064 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
65 byte *c = emit->code_base + emit->code_info_offset;
66 emit->code_info_offset += num_bytes_to_write;
Damien429d7192013-10-04 19:53:11 +010067 return c;
68 }
69}
70
Damien Georgedf8127a2014-04-13 11:04:33 +010071STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
72 emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
73}
74
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020075STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
Damien George08335002014-01-18 23:24:36 +000076 byte* c = emit_get_cur_to_write_code_info(emit, 4);
77 // TODO variable length encoding for qstr
78 c[0] = qstr & 0xff;
79 c[1] = (qstr >> 8) & 0xff;
80 c[2] = (qstr >> 16) & 0xff;
81 c[3] = (qstr >> 24) & 0xff;
82}
83
Damien George73496fb2014-04-13 14:51:56 +010084#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020085STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, uint lines_to_skip) {
Damien George73496fb2014-04-13 14:51:56 +010086 assert(bytes_to_skip > 0 || lines_to_skip > 0);
87 while (bytes_to_skip > 0 || lines_to_skip > 0) {
88 uint b = MIN(bytes_to_skip, 31);
89 uint l = MIN(lines_to_skip, 7);
90 bytes_to_skip -= b;
91 lines_to_skip -= l;
92 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
Damien George28eb5772014-01-25 11:43:20 +000093 }
Damien George08335002014-01-18 23:24:36 +000094}
Damien George73496fb2014-04-13 14:51:56 +010095#endif
Damien George08335002014-01-18 23:24:36 +000096
97// all functions must go through this one to emit byte code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020098STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
Damien George08335002014-01-18 23:24:36 +000099 //printf("emit %d\n", num_bytes_to_write);
100 if (emit->pass < PASS_3) {
101 emit->byte_code_offset += num_bytes_to_write;
102 return emit->dummy_data;
103 } else {
104 assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
105 byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
106 emit->byte_code_offset += num_bytes_to_write;
107 return c;
108 }
109}
110
Damien Georgedf8127a2014-04-13 11:04:33 +0100111STATIC void emit_align_byte_code_to_machine_word(emit_t* emit) {
112 emit->byte_code_offset = (emit->byte_code_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
113}
114
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200115STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
Damien George08335002014-01-18 23:24:36 +0000116 byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100117 c[0] = b1;
118}
119
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200120STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
Damien429d7192013-10-04 19:53:11 +0100121 assert((b2 & (~0xff)) == 0);
Damien George08335002014-01-18 23:24:36 +0000122 byte* c = emit_get_cur_to_write_byte_code(emit, 2);
Damien429d7192013-10-04 19:53:11 +0100123 c[0] = b1;
124 c[1] = b2;
125}
126
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200127STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200128 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200129 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200130 byte *p = buf + sizeof(buf);
131 // We encode in little-ending order, but store in big-endian, to help decoding
132 do {
133 *--p = num & 0x7f;
134 num >>= 7;
135 } while (num != 0);
136 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
137 while (p != buf + sizeof(buf) - 1) {
138 *c++ = *p++ | 0x80;
Damien Georgefb083ea2014-02-01 18:29:40 +0000139 }
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +0200140 *c = *p;
Damien Georgefb083ea2014-02-01 18:29:40 +0000141}
142
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200143// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200144STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200145 emit_write_byte_code_byte(emit, b1);
146
147 // We store each 7 bits in a separate byte, and that's how many bytes needed
Paul Sokolovskya8d31b22014-02-20 13:25:05 +0200148 byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200149 byte *p = buf + sizeof(buf);
150 // We encode in little-ending order, but store in big-endian, to help decoding
151 do {
152 *--p = num & 0x7f;
153 num >>= 7;
154 } while (num != 0 && num != -1);
155 // Make sure that highest bit we stored (mask 0x40) matches sign
156 // of the number. If not, store extra byte just to encode sign
157 if (num == -1 && (*p & 0x40) == 0) {
158 *--p = 0x7f;
159 } else if (num == 0 && (*p & 0x40) != 0) {
160 *--p = 0;
161 }
162
163 byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
164 while (p != buf + sizeof(buf) - 1) {
165 *c++ = *p++ | 0x80;
166 }
167 *c = *p;
Damien429d7192013-10-04 19:53:11 +0100168}
169
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200170STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000171 emit_write_byte_code_byte(emit, b);
172 emit_write_byte_code_uint(emit, num);
Damien429d7192013-10-04 19:53:11 +0100173}
174
Damien Georgedf8127a2014-04-13 11:04:33 +0100175// aligns the pointer so it is friendly to GC
176STATIC void emit_write_byte_code_byte_ptr(emit_t* emit, byte b, void *ptr) {
177 emit_write_byte_code_byte(emit, b);
178 emit_align_byte_code_to_machine_word(emit);
179 machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_byte_code(emit, sizeof(machine_uint_t));
180 *c = (machine_uint_t)ptr;
181}
182
Damien Georgefb083ea2014-02-01 18:29:40 +0000183/* currently unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200184STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000185 emit_write_byte_code_byte(emit, b);
186 emit_write_byte_code_byte_uint(emit, num1);
187 emit_write_byte_code_byte_uint(emit, num2);
188}
189*/
190
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200191STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
Damien Georgefb083ea2014-02-01 18:29:40 +0000192 emit_write_byte_code_byte_uint(emit, b, qstr);
Damien429d7192013-10-04 19:53:11 +0100193}
194
Damien03c9cfb2013-11-05 22:06:08 +0000195// unsigned labels are relative to ip following this instruction, stored as 16 bits
Damien George6f355fd2014-04-10 14:11:31 +0100196STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000197 uint byte_code_offset;
Damien429d7192013-10-04 19:53:11 +0100198 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000199 byte_code_offset = 0;
Damien429d7192013-10-04 19:53:11 +0100200 } else {
Damien George08335002014-01-18 23:24:36 +0000201 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
Damien429d7192013-10-04 19:53:11 +0100202 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100203 byte *c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000204 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000205 c[1] = byte_code_offset;
206 c[2] = byte_code_offset >> 8;
Damien03c9cfb2013-11-05 22:06:08 +0000207}
208
209// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
Damien George6f355fd2014-04-10 14:11:31 +0100210STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, uint label) {
Damien George08335002014-01-18 23:24:36 +0000211 int byte_code_offset;
Damien03c9cfb2013-11-05 22:06:08 +0000212 if (emit->pass < PASS_3) {
Damien George08335002014-01-18 23:24:36 +0000213 byte_code_offset = 0;
Damien03c9cfb2013-11-05 22:06:08 +0000214 } else {
Damien George08335002014-01-18 23:24:36 +0000215 byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
Damien03c9cfb2013-11-05 22:06:08 +0000216 }
Damien George08335002014-01-18 23:24:36 +0000217 byte* c = emit_get_cur_to_write_byte_code(emit, 3);
Damien03c9cfb2013-11-05 22:06:08 +0000218 c[0] = b1;
Damien George08335002014-01-18 23:24:36 +0000219 c[1] = byte_code_offset;
220 c[2] = byte_code_offset >> 8;
Damien429d7192013-10-04 19:53:11 +0100221}
222
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200223STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
Damien George6baf76e2013-12-30 22:32:17 +0000224}
225
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200226STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George6baf76e2013-12-30 22:32:17 +0000227 emit->pass = pass;
228 emit->stack_size = 0;
229 emit->last_emit_was_return_value = false;
230 emit->scope = scope;
Damien George08335002014-01-18 23:24:36 +0000231 emit->last_source_line_offset = 0;
232 emit->last_source_line = 1;
Damien George6baf76e2013-12-30 22:32:17 +0000233 if (pass == PASS_2) {
234 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
235 }
Damien George08335002014-01-18 23:24:36 +0000236 emit->byte_code_offset = 0;
237 emit->code_info_offset = 0;
238
239 // 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)
240 {
241 byte* c = emit_get_cur_to_write_code_info(emit, 4);
242 machine_uint_t s = emit->code_info_size;
243 c[0] = s & 0xff;
244 c[1] = (s >> 8) & 0xff;
245 c[2] = (s >> 16) & 0xff;
246 c[3] = (s >> 24) & 0xff;
247 }
248
249 // code info
Damien Georgecbd2f742014-01-19 11:48:48 +0000250 emit_write_code_info_qstr(emit, scope->source_file);
251 emit_write_code_info_qstr(emit, scope->simple_name);
Damien George6baf76e2013-12-30 22:32:17 +0000252
Damien Georgebee17b02014-03-27 11:07:04 +0000253 // bytecode prelude: local state size and exception stack size; 16 bit uints for now
Damien George8dcc0c72014-03-27 10:55:21 +0000254 {
Damien Georgebee17b02014-03-27 11:07:04 +0000255 byte* c = emit_get_cur_to_write_byte_code(emit, 4);
256 uint n_state = scope->num_locals + scope->stack_size;
Damien George190d1ba2014-04-10 16:59:56 +0000257 if (n_state == 0) {
258 // Need at least 1 entry in the state, in the case an exception is
259 // propagated through this function, the exception is returned in
260 // the highest slot in the state (fastn[0], see vm.c).
261 n_state = 1;
262 }
Damien Georgebee17b02014-03-27 11:07:04 +0000263 c[0] = n_state & 0xff;
264 c[1] = (n_state >> 8) & 0xff;
265 c[2] = scope->exc_stack_size & 0xff;
266 c[3] = (scope->exc_stack_size >> 8) & 0xff;
Damien George8dcc0c72014-03-27 10:55:21 +0000267 }
268
269 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000270 int num_cell = 0;
271 for (int i = 0; i < scope->id_info_len; i++) {
272 id_info_t *id = &scope->id_info[i];
273 if (id->kind == ID_INFO_KIND_CELL) {
274 num_cell += 1;
275 }
276 }
277 assert(num_cell <= 255);
Damien George08335002014-01-18 23:24:36 +0000278 emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
Damien George6baf76e2013-12-30 22:32:17 +0000279 for (int i = 0; i < scope->id_info_len; i++) {
280 id_info_t *id = &scope->id_info[i];
281 if (id->kind == ID_INFO_KIND_CELL) {
Damien George08335002014-01-18 23:24:36 +0000282 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 +0000283 }
284 }
285}
286
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200287STATIC void emit_bc_end_pass(emit_t *emit) {
Damien George6baf76e2013-12-30 22:32:17 +0000288 // check stack is back to zero size
289 if (emit->stack_size != 0) {
290 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
291 }
292
Damien George73496fb2014-04-13 14:51:56 +0100293 *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
Damien Georgedf8127a2014-04-13 11:04:33 +0100294 emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned
Damien George08335002014-01-18 23:24:36 +0000295
Damien George6baf76e2013-12-30 22:32:17 +0000296 if (emit->pass == PASS_2) {
297 // calculate size of code in bytes
Damien George08335002014-01-18 23:24:36 +0000298 emit->code_info_size = emit->code_info_offset;
299 emit->byte_code_size = emit->byte_code_offset;
Damien Georgedf8127a2014-04-13 11:04:33 +0100300 emit->code_base = m_new0(byte, emit->code_info_size + emit->byte_code_size);
Damien George6baf76e2013-12-30 22:32:17 +0000301
302 } else if (emit->pass == PASS_3) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200303 qstr *arg_names = m_new(qstr, emit->scope->num_params);
304 for (int i = 0; i < emit->scope->num_params; i++) {
305 arg_names[i] = emit->scope->id_info[i].qstr;
306 }
Damien Georgedf8127a2014-04-13 11:04:33 +0100307 mp_emit_glue_assign_byte_code(emit->scope->raw_code, emit->code_base,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200308 emit->code_info_size + emit->byte_code_size,
Damien George2326d522014-03-27 23:26:35 +0000309 emit->scope->num_params, emit->scope->num_locals,
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200310 emit->scope->scope_flags, arg_names);
Damien George6baf76e2013-12-30 22:32:17 +0000311 }
312}
313
Damien George069a35e2014-04-10 17:22:19 +0000314STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
Damien429d7192013-10-04 19:53:11 +0100315 return emit->last_emit_was_return_value;
316}
317
Damien Georged66ae182014-04-10 17:28:54 +0000318STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
319 emit->stack_size += delta;
Damien429d7192013-10-04 19:53:11 +0100320}
321
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200322STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000323 //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 +0000324#if MICROPY_ENABLE_SOURCE_LINE
Damien George08335002014-01-18 23:24:36 +0000325 if (source_line > emit->last_source_line) {
Damien George28eb5772014-01-25 11:43:20 +0000326 uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
327 uint lines_to_skip = source_line - emit->last_source_line;
328 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
Damien Georgecbd2f742014-01-19 11:48:48 +0000329 //printf(" %d %d\n", bytes_to_skip, lines_to_skip);
Damien George08335002014-01-18 23:24:36 +0000330 emit->last_source_line_offset = emit->byte_code_offset;
331 emit->last_source_line = source_line;
332 }
Damien George62ad1892014-01-29 21:51:51 +0000333#endif
Damien George08335002014-01-18 23:24:36 +0000334}
335
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200336STATIC void emit_bc_load_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100337 emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
338}
339
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200340STATIC void emit_bc_store_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100341 emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
342}
343
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200344STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
Damien4b03e772013-10-05 14:17:09 +0100345 emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
346}
347
Damien Georgece8f07a2014-03-27 23:30:26 +0000348STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
Damien George78035b92014-04-09 12:27:39 +0100349 assert((int)emit->stack_size + stack_size_delta >= 0);
Damienb05d7072013-10-05 13:37:10 +0100350 emit->stack_size += stack_size_delta;
351 if (emit->stack_size > emit->scope->stack_size) {
352 emit->scope->stack_size = emit->stack_size;
Damien429d7192013-10-04 19:53:11 +0100353 }
354 emit->last_emit_was_return_value = false;
355}
356
Damien George6f355fd2014-04-10 14:11:31 +0100357STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000358 emit_bc_pre(emit, 0);
Damienb05d7072013-10-05 13:37:10 +0100359 assert(l < emit->max_num_labels);
360 if (emit->pass == PASS_2) {
361 // assign label offset
362 assert(emit->label_offsets[l] == -1);
Damien George08335002014-01-18 23:24:36 +0000363 emit->label_offsets[l] = emit->byte_code_offset;
Damienb05d7072013-10-05 13:37:10 +0100364 } else if (emit->pass == PASS_3) {
365 // ensure label offset has not changed from PASS_2 to PASS_3
Damien George08335002014-01-18 23:24:36 +0000366 //printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
367 assert(emit->label_offsets[l] == emit->byte_code_offset);
Damien429d7192013-10-04 19:53:11 +0100368 }
369}
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000372 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000373 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100374}
375
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200376STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000377 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000378 emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
Damien429d7192013-10-04 19:53:11 +0100379}
380
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200381STATIC void emit_bc_import_star(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000382 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000383 emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
Damien429d7192013-10-04 19:53:11 +0100384}
385
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200386STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000387 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100388 switch (tok) {
Damien George08335002014-01-18 23:24:36 +0000389 case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
390 case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
391 case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
392 case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
Damien429d7192013-10-04 19:53:11 +0100393 default: assert(0);
394 }
395}
396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200397STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000398 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000399 emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
Damien429d7192013-10-04 19:53:11 +0100400}
401
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200402STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000403 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000404 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
Damien429d7192013-10-04 19:53:11 +0100405}
406
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200407STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000408 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000409 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
Damien429d7192013-10-04 19:53:11 +0100410}
411
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200412STATIC void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000413 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000414 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_ID, qstr);
Damien429d7192013-10-04 19:53:11 +0100415}
416
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200417STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000418 emit_bc_pre(emit, 1);
Damien429d7192013-10-04 19:53:11 +0100419 if (bytes) {
Damien George08335002014-01-18 23:24:36 +0000420 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
Damien429d7192013-10-04 19:53:11 +0100421 } else {
Damien George08335002014-01-18 23:24:36 +0000422 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
Damien429d7192013-10-04 19:53:11 +0100423 }
424}
425
Damien George523b5752014-03-31 11:59:23 +0100426STATIC void emit_bc_load_null(emit_t *emit) {
427 emit_bc_pre(emit, 1);
428 emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
429};
430
Damien George2bf7c092014-04-09 15:26:46 +0100431STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100432 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000433 emit_bc_pre(emit, 1);
Damien George6ce42772014-04-12 18:20:40 +0100434 switch (local_num) {
435 case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
436 case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
437 case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
438 default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100439 }
440}
441
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200442STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000443 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000444 emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000445}
446
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200447STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000448 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000449 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100450}
451
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200452STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000453 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000454 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100455}
456
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200457STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000458 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000459 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100460}
461
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200462STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000463 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000464 emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
Damien429d7192013-10-04 19:53:11 +0100465}
466
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200467STATIC void emit_bc_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000468 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000469 emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
Damien429d7192013-10-04 19:53:11 +0100470}
471
Damien George729f7b42014-04-17 22:10:53 +0100472STATIC void emit_bc_load_subscr(emit_t *emit) {
473 emit_bc_pre(emit, -1);
474 emit_write_byte_code_byte(emit, MP_BC_LOAD_SUBSCR);
475}
476
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200477STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien429d7192013-10-04 19:53:11 +0100478 assert(local_num >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000479 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100480 switch (local_num) {
Damien George08335002014-01-18 23:24:36 +0000481 case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
482 case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
483 case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
484 default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
Damien429d7192013-10-04 19:53:11 +0100485 }
486}
487
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200488STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000489 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000490 emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000491}
492
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200493STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000494 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000495 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100496}
497
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200498STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000499 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000500 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100501}
502
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200503STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000504 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000505 emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
Damien429d7192013-10-04 19:53:11 +0100506}
507
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200508STATIC void emit_bc_store_subscr(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000509 emit_bc_pre(emit, -3);
Damien George08335002014-01-18 23:24:36 +0000510 emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100511}
512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100514 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
Damien429d7192013-10-04 19:53:11 +0100515}
516
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200517STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien George2bf7c092014-04-09 15:26:46 +0100518 emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
Damien9ecbcff2013-12-11 00:41:43 +0000519}
520
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200521STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000522 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000523 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
Damien429d7192013-10-04 19:53:11 +0100524}
525
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200526STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000527 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000528 emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
Damien429d7192013-10-04 19:53:11 +0100529}
530
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200531STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
Damien George1d24ea52014-04-08 21:11:49 +0100532 emit_bc_load_null(emit);
533 emit_bc_rot_two(emit);
534 emit_bc_store_attr(emit, qstr);
Damien429d7192013-10-04 19:53:11 +0100535}
536
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200537STATIC void emit_bc_delete_subscr(emit_t *emit) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100538 emit_bc_load_null(emit);
539 emit_bc_rot_three(emit);
540 emit_bc_store_subscr(emit);
Damien429d7192013-10-04 19:53:11 +0100541}
542
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200543STATIC void emit_bc_dup_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000544 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000545 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
Damien429d7192013-10-04 19:53:11 +0100546}
547
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200548STATIC void emit_bc_dup_top_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000549 emit_bc_pre(emit, 2);
Damien George08335002014-01-18 23:24:36 +0000550 emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
Damien429d7192013-10-04 19:53:11 +0100551}
552
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200553STATIC void emit_bc_pop_top(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000554 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000555 emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
Damien429d7192013-10-04 19:53:11 +0100556}
557
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200558STATIC void emit_bc_rot_two(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000559 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000560 emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
Damien429d7192013-10-04 19:53:11 +0100561}
562
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200563STATIC void emit_bc_rot_three(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000564 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000565 emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Damien George6f355fd2014-04-10 14:11:31 +0100568STATIC void emit_bc_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000569 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000570 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Damien George6f355fd2014-04-10 14:11:31 +0100573STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000574 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000575 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
Damien429d7192013-10-04 19:53:11 +0100576}
577
Damien George6f355fd2014-04-10 14:11:31 +0100578STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000579 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000580 emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
Damien429d7192013-10-04 19:53:11 +0100581}
582
Damien George6f355fd2014-04-10 14:11:31 +0100583STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000584 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000585 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100586}
587
Damien George6f355fd2014-04-10 14:11:31 +0100588STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000589 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000590 emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
Damien429d7192013-10-04 19:53:11 +0100591}
592
Damien George6f355fd2014-04-10 14:11:31 +0100593STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
Damien Georgecbddb272014-02-01 20:08:18 +0000594 if (except_depth == 0) {
595 emit_bc_jump(emit, label);
596 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000597 emit_bc_pre(emit, 0);
Damien Georgecbddb272014-02-01 20:08:18 +0000598 emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
599 emit_write_byte_code_byte(emit, except_depth);
600 }
Damien429d7192013-10-04 19:53:11 +0100601}
602
Damien George6f355fd2014-04-10 14:11:31 +0100603STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000604 emit_bc_pre(emit, 7);
Damien George08335002014-01-18 23:24:36 +0000605 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
Damien429d7192013-10-04 19:53:11 +0100606}
607
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200608STATIC void emit_bc_with_cleanup(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000609 emit_bc_pre(emit, -7);
Damien George08335002014-01-18 23:24:36 +0000610 emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
Damien429d7192013-10-04 19:53:11 +0100611}
612
Damien George6f355fd2014-04-10 14:11:31 +0100613STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000614 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000615 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
Damien429d7192013-10-04 19:53:11 +0100616}
617
Damien George6f355fd2014-04-10 14:11:31 +0100618STATIC void emit_bc_setup_finally(emit_t *emit, uint label) {
Damien George069a35e2014-04-10 17:22:19 +0000619 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000620 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
Damien429d7192013-10-04 19:53:11 +0100621}
622
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200623STATIC void emit_bc_end_finally(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000624 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000625 emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
Damien429d7192013-10-04 19:53:11 +0100626}
627
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200628STATIC void emit_bc_get_iter(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000630 emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
Damien429d7192013-10-04 19:53:11 +0100631}
632
Damien George6f355fd2014-04-10 14:11:31 +0100633STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000634 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000635 emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
Damien429d7192013-10-04 19:53:11 +0100636}
637
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200638STATIC void emit_bc_for_iter_end(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000639 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100640}
641
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200642STATIC void emit_bc_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000643 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000644 emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
Damien429d7192013-10-04 19:53:11 +0100645}
646
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200647STATIC void emit_bc_pop_except(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000648 emit_bc_pre(emit, 0);
Damien George08335002014-01-18 23:24:36 +0000649 emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
Damien429d7192013-10-04 19:53:11 +0100650}
651
Damien Georged17926d2014-03-30 13:35:08 +0100652STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
653 if (op == MP_UNARY_OP_NOT) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000654 emit_bc_pre(emit, 0);
Damien Georged17926d2014-03-30 13:35:08 +0100655 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
Damien Georgece8f07a2014-03-27 23:30:26 +0000656 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000657 emit_write_byte_code_byte(emit, MP_BC_NOT);
658 } else {
Damien Georgece8f07a2014-03-27 23:30:26 +0000659 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000660 emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
661 }
Damien429d7192013-10-04 19:53:11 +0100662}
663
Damien Georged17926d2014-03-30 13:35:08 +0100664STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien George9aa2a522014-02-01 23:04:09 +0000665 bool invert = false;
Damien Georged17926d2014-03-30 13:35:08 +0100666 if (op == MP_BINARY_OP_NOT_IN) {
Damien George9aa2a522014-02-01 23:04:09 +0000667 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100668 op = MP_BINARY_OP_IN;
669 } else if (op == MP_BINARY_OP_IS_NOT) {
Damien George9aa2a522014-02-01 23:04:09 +0000670 invert = true;
Damien Georged17926d2014-03-30 13:35:08 +0100671 op = MP_BINARY_OP_IS;
Damien George9aa2a522014-02-01 23:04:09 +0000672 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000673 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000674 emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
Damien George9aa2a522014-02-01 23:04:09 +0000675 if (invert) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000676 emit_bc_pre(emit, 0);
Damien George9aa2a522014-02-01 23:04:09 +0000677 emit_write_byte_code_byte(emit, MP_BC_NOT);
678 }
Damien429d7192013-10-04 19:53:11 +0100679}
680
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200681STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100682 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000683 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000684 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
Damien429d7192013-10-04 19:53:11 +0100685}
686
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200687STATIC void emit_bc_build_list(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 - n_args);
Damien George08335002014-01-18 23:24:36 +0000690 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
Damien429d7192013-10-04 19:53:11 +0100691}
692
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200693STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100694 assert(list_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000695 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000696 emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
Damien429d7192013-10-04 19:53:11 +0100697}
698
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200699STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100700 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000701 emit_bc_pre(emit, 1);
Damien George08335002014-01-18 23:24:36 +0000702 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
Damien429d7192013-10-04 19:53:11 +0100703}
704
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200705STATIC void emit_bc_store_map(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000706 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000707 emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
Damien429d7192013-10-04 19:53:11 +0100708}
709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200710STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100711 assert(map_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000712 emit_bc_pre(emit, -2);
Damien George08335002014-01-18 23:24:36 +0000713 emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200716STATIC void emit_bc_build_set(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_SET, n_args);
Damien429d7192013-10-04 19:53:11 +0100720}
721
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200722STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
Damien429d7192013-10-04 19:53:11 +0100723 assert(set_stack_index >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000724 emit_bc_pre(emit, -1);
Damien George08335002014-01-18 23:24:36 +0000725 emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
Damien429d7192013-10-04 19:53:11 +0100726}
727
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200728STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100729 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000730 emit_bc_pre(emit, 1 - n_args);
Damien George08335002014-01-18 23:24:36 +0000731 emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
Damien429d7192013-10-04 19:53:11 +0100732}
733
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200734STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
Damien429d7192013-10-04 19:53:11 +0100735 assert(n_args >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000736 emit_bc_pre(emit, -1 + n_args);
Damien George08335002014-01-18 23:24:36 +0000737 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
Damien429d7192013-10-04 19:53:11 +0100738}
739
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200740STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien429d7192013-10-04 19:53:11 +0100741 assert(n_left >=0 && n_right >= 0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000742 emit_bc_pre(emit, -1 + n_left + n_right + 1);
Damien George08335002014-01-18 23:24:36 +0000743 emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
Damien429d7192013-10-04 19:53:11 +0100744}
745
Damien George30565092014-03-31 11:30:17 +0100746STATIC 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 +0100747 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000748 emit_bc_pre(emit, 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100749 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
Damien Georgefb083ea2014-02-01 18:29:40 +0000750 } else {
Damien Georgee337f1e2014-03-31 15:18:37 +0100751 emit_bc_pre(emit, -1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100752 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
Paul Sokolovsky90750022014-02-01 15:05:04 +0200753 }
Damien429d7192013-10-04 19:53:11 +0100754}
755
Damien George3558f622014-04-20 17:50:40 +0100756STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100757 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George3558f622014-04-20 17:50:40 +0100758 emit_bc_pre(emit, -n_closed_over + 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100759 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
Damien George3558f622014-04-20 17:50:40 +0100760 emit_write_byte_code_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200761 } else {
Damien George3558f622014-04-20 17:50:40 +0100762 assert(n_closed_over <= 255);
763 emit_bc_pre(emit, -2 - n_closed_over + 1);
Damien Georgedf8127a2014-04-13 11:04:33 +0100764 emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
Damien George3558f622014-04-20 17:50:40 +0100765 emit_write_byte_code_byte(emit, n_closed_over);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200766 }
Damien429d7192013-10-04 19:53:11 +0100767}
768
Damien George922ddd62014-04-09 12:43:17 +0100769STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uint bytecode_base, int n_positional, int n_keyword, uint star_flags) {
770 if (star_flags) {
771 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
Damien George523b5752014-03-31 11:59:23 +0100772 // load dummy entry for non-existent pos_seq
773 emit_bc_load_null(emit);
774 emit_bc_rot_two(emit);
Damien George922ddd62014-04-09 12:43:17 +0100775 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
Damien George523b5752014-03-31 11:59:23 +0100776 // load dummy entry for non-existent kw_dict
777 emit_bc_load_null(emit);
Damien429d7192013-10-04 19:53:11 +0100778 }
Damien George523b5752014-03-31 11:59:23 +0100779 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
780 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 +0100781 } else {
Damien George523b5752014-03-31 11:59:23 +0100782 emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
783 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 +0100784 }
Damien George523b5752014-03-31 11:59:23 +0100785}
786
Damien George922ddd62014-04-09 12:43:17 +0100787STATIC void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
788 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damien George922ddd62014-04-09 12:43:17 +0100791STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
792 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +0100793}
794
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200795STATIC void emit_bc_return_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000796 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100797 emit->last_emit_was_return_value = true;
Damien George08335002014-01-18 23:24:36 +0000798 emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
Damien429d7192013-10-04 19:53:11 +0100799}
800
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200801STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
Damien George25042b12014-01-11 09:33:39 +0000802 assert(0 <= n_args && n_args <= 2);
Damien Georgece8f07a2014-03-27 23:30:26 +0000803 emit_bc_pre(emit, -n_args);
Damien George08335002014-01-18 23:24:36 +0000804 emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
Damien429d7192013-10-04 19:53:11 +0100805}
806
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200807STATIC void emit_bc_yield_value(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000808 emit_bc_pre(emit, 0);
Damien429d7192013-10-04 19:53:11 +0100809 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000810 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100811 }
Damien George08335002014-01-18 23:24:36 +0000812 emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
Damien429d7192013-10-04 19:53:11 +0100813}
814
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200815STATIC void emit_bc_yield_from(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000816 emit_bc_pre(emit, -1);
Damien429d7192013-10-04 19:53:11 +0100817 if (emit->pass == PASS_2) {
Damien George8725f8f2014-02-15 19:33:11 +0000818 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
Damien429d7192013-10-04 19:53:11 +0100819 }
Damien George08335002014-01-18 23:24:36 +0000820 emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
Damien429d7192013-10-04 19:53:11 +0100821}
822
Damien6cdd3af2013-10-05 18:08:26 +0100823const emit_method_table_t emit_bc_method_table = {
Damien415eb6f2013-10-05 12:19:06 +0100824 emit_bc_set_native_types,
825 emit_bc_start_pass,
826 emit_bc_end_pass,
827 emit_bc_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +0000828 emit_bc_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +0000829 emit_bc_set_source_line,
Damien415eb6f2013-10-05 12:19:06 +0100830
Damien4b03e772013-10-05 14:17:09 +0100831 emit_bc_load_id,
832 emit_bc_store_id,
833 emit_bc_delete_id,
834
Damien415eb6f2013-10-05 12:19:06 +0100835 emit_bc_label_assign,
836 emit_bc_import_name,
837 emit_bc_import_from,
838 emit_bc_import_star,
839 emit_bc_load_const_tok,
840 emit_bc_load_const_small_int,
841 emit_bc_load_const_int,
842 emit_bc_load_const_dec,
843 emit_bc_load_const_id,
844 emit_bc_load_const_str,
Damien George3558f622014-04-20 17:50:40 +0100845 emit_bc_load_null,
Damien415eb6f2013-10-05 12:19:06 +0100846 emit_bc_load_fast,
Damien415eb6f2013-10-05 12:19:06 +0100847 emit_bc_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +0000848 emit_bc_load_name,
849 emit_bc_load_global,
Damien415eb6f2013-10-05 12:19:06 +0100850 emit_bc_load_attr,
851 emit_bc_load_method,
852 emit_bc_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +0100853 emit_bc_load_subscr,
Damien415eb6f2013-10-05 12:19:06 +0100854 emit_bc_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000855 emit_bc_store_deref,
Damien415eb6f2013-10-05 12:19:06 +0100856 emit_bc_store_name,
857 emit_bc_store_global,
Damien415eb6f2013-10-05 12:19:06 +0100858 emit_bc_store_attr,
Damien415eb6f2013-10-05 12:19:06 +0100859 emit_bc_store_subscr,
860 emit_bc_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +0000861 emit_bc_delete_deref,
Damien415eb6f2013-10-05 12:19:06 +0100862 emit_bc_delete_name,
863 emit_bc_delete_global,
Damien415eb6f2013-10-05 12:19:06 +0100864 emit_bc_delete_attr,
865 emit_bc_delete_subscr,
866 emit_bc_dup_top,
867 emit_bc_dup_top_two,
868 emit_bc_pop_top,
869 emit_bc_rot_two,
870 emit_bc_rot_three,
871 emit_bc_jump,
872 emit_bc_pop_jump_if_true,
873 emit_bc_pop_jump_if_false,
874 emit_bc_jump_if_true_or_pop,
875 emit_bc_jump_if_false_or_pop,
Damien Georgecbddb272014-02-01 20:08:18 +0000876 emit_bc_unwind_jump,
877 emit_bc_unwind_jump,
Damien415eb6f2013-10-05 12:19:06 +0100878 emit_bc_setup_with,
879 emit_bc_with_cleanup,
880 emit_bc_setup_except,
881 emit_bc_setup_finally,
882 emit_bc_end_finally,
883 emit_bc_get_iter,
884 emit_bc_for_iter,
885 emit_bc_for_iter_end,
886 emit_bc_pop_block,
887 emit_bc_pop_except,
888 emit_bc_unary_op,
889 emit_bc_binary_op,
Damien415eb6f2013-10-05 12:19:06 +0100890 emit_bc_build_tuple,
891 emit_bc_build_list,
892 emit_bc_list_append,
893 emit_bc_build_map,
894 emit_bc_store_map,
895 emit_bc_map_add,
896 emit_bc_build_set,
897 emit_bc_set_add,
898 emit_bc_build_slice,
899 emit_bc_unpack_sequence,
900 emit_bc_unpack_ex,
901 emit_bc_make_function,
902 emit_bc_make_closure,
903 emit_bc_call_function,
904 emit_bc_call_method,
905 emit_bc_return_value,
906 emit_bc_raise_varargs,
907 emit_bc_yield_value,
908 emit_bc_yield_from,
909};
Damien George5f6a25f2014-04-20 18:02:27 +0100910
911#endif // !MICROPY_EMIT_CPYTHON