1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013-2019 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
33#include "py/mpstate.h"
34#include "py/emit.h"
35#include "py/bc0.h"
36
37#if MICROPY_ENABLE_COMPILER
38
39#define BYTES_FOR_INT ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7)
40#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
41
42struct _emit_t {
43 // Accessed as mp_obj_t, so must be aligned as such, and we rely on the
44 // memory allocator returning a suitably aligned pointer.
45 // Should work for cases when mp_obj_t is 64-bit on a 32-bit machine.
46 byte dummy_data[DUMMY_DATA_SIZE];
47
48 pass_kind_t pass : 8;
49 mp_uint_t last_emit_was_return_value : 8;
50
51 int stack_size;
52
53 scope_t *scope;
54
55 mp_uint_t last_source_line_offset;
56 mp_uint_t last_source_line;
57
58 mp_uint_t max_num_labels;
59 mp_uint_t *label_offsets;
60
61 size_t code_info_offset;
62 size_t code_info_size;
63 size_t bytecode_offset;
64 size_t bytecode_size;
65 byte *code_base; // stores both byte code and code info
66
67 size_t n_info;
68 size_t n_cell;
69
70 #if MICROPY_PERSISTENT_CODE
71 uint16_t ct_cur_obj;
72 uint16_t ct_num_obj;
73 uint16_t ct_cur_raw_code;
74 #endif
75 mp_uint_t *const_table;
76};
77
78emit_t *emit_bc_new(void) {
79 emit_t *emit = m_new0(emit_t, 1);
80 return emit;
81}
82
83void emit_bc_set_max_num_labels(emit_t *emit, mp_uint_t max_num_labels) {
84 emit->max_num_labels = max_num_labels;
85 emit->label_offsets = m_new(mp_uint_t, emit->max_num_labels);
86}
87
88void emit_bc_free(emit_t *emit) {
89 m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
90 m_del_obj(emit_t, emit);
91}
92
93typedef byte *(*emit_allocator_t)(emit_t *emit, int nbytes);
94
95STATIC void emit_write_uint(emit_t *emit, emit_allocator_t allocator, mp_uint_t val) {
96 // We store each 7 bits in a separate byte, and that's how many bytes needed
97 byte buf[BYTES_FOR_INT];
98 byte *p = buf + sizeof(buf);
99 // We encode in little-ending order, but store in big-endian, to help decoding
100 do {
101 *--p = val & 0x7f;
102 val >>= 7;
103 } while (val != 0);
104 byte *c = allocator(emit, buf + sizeof(buf) - p);
105 while (p != buf + sizeof(buf) - 1) {
106 *c++ = *p++ | 0x80;
107 }
108 *c = *p;
109}
110
111// all functions must go through this one to emit code info
112STATIC byte *emit_get_cur_to_write_code_info(emit_t *emit, int num_bytes_to_write) {
113 if (emit->pass < MP_PASS_EMIT) {
114 emit->code_info_offset += num_bytes_to_write;
115 return emit->dummy_data;
116 } else {
117 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
118 byte *c = emit->code_base + emit->code_info_offset;
119 emit->code_info_offset += num_bytes_to_write;
120 return c;
121 }
122}
123
124STATIC void emit_write_code_info_byte(emit_t *emit, byte val) {
125 *emit_get_cur_to_write_code_info(emit, 1) = val;
126}
127
128STATIC void emit_write_code_info_qstr(emit_t *emit, qstr qst) {
129 #if MICROPY_PERSISTENT_CODE
130 assert((qst >> 16) == 0);
131 byte *c = emit_get_cur_to_write_code_info(emit, 2);
132 c[0] = qst;
133 c[1] = qst >> 8;
134 #else
135 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
136 #endif
137}
138
139#if MICROPY_ENABLE_SOURCE_LINE
140STATIC void emit_write_code_info_bytes_lines(emit_t *emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) {
141 assert(bytes_to_skip > 0 || lines_to_skip > 0);
142 while (bytes_to_skip > 0 || lines_to_skip > 0) {
143 mp_uint_t b, l;
144 if (lines_to_skip <= 6 || bytes_to_skip > 0xf) {
145 // use 0b0LLBBBBB encoding
146 b = MIN(bytes_to_skip, 0x1f);
147 if (b < bytes_to_skip) {
148 // we can't skip any lines until we skip all the bytes
149 l = 0;
150 } else {
151 l = MIN(lines_to_skip, 0x3);
152 }
153 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
154 } else {
155 // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
156 b = MIN(bytes_to_skip, 0xf);
157 l = MIN(lines_to_skip, 0x7ff);
158 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
159 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
160 ci[1] = l;
161 }
162 bytes_to_skip -= b;
163 lines_to_skip -= l;
164 }
165}
166#endif
167
168// all functions must go through this one to emit byte code
169STATIC byte *emit_get_cur_to_write_bytecode(emit_t *emit, int num_bytes_to_write) {
170 if (emit->pass < MP_PASS_EMIT) {
171 emit->bytecode_offset += num_bytes_to_write;
172 return emit->dummy_data;
173 } else {
174 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
175 byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
176 emit->bytecode_offset += num_bytes_to_write;
177 return c;
178 }
179}
180
181STATIC void emit_write_bytecode_raw_byte(emit_t *emit, byte b1) {
182 byte *c = emit_get_cur_to_write_bytecode(emit, 1);
183 c[0] = b1;
184}
185
186STATIC void emit_write_bytecode_byte(emit_t *emit, int stack_adj, byte b1) {
187 mp_emit_bc_adjust_stack_size(emit, stack_adj);
188 byte *c = emit_get_cur_to_write_bytecode(emit, 1);
189 c[0] = b1;
190}
191
192// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
193STATIC void emit_write_bytecode_byte_int(emit_t *emit, int stack_adj, byte b1, mp_int_t num) {
194 emit_write_bytecode_byte(emit, stack_adj, b1);
195
196 // We store each 7 bits in a separate byte, and that's how many bytes needed
197 byte buf[BYTES_FOR_INT];
198 byte *p = buf + sizeof(buf);
199 // We encode in little-ending order, but store in big-endian, to help decoding
200 do {
201 *--p = num & 0x7f;
202 num >>= 7;
203 } while (num != 0 && num != -1);
204 // Make sure that highest bit we stored (mask 0x40) matches sign
205 // of the number. If not, store extra byte just to encode sign
206 if (num == -1 && (*p & 0x40) == 0) {
207 *--p = 0x7f;
208 } else if (num == 0 && (*p & 0x40) != 0) {
209 *--p = 0;
210 }
211
212 byte *c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
213 while (p != buf + sizeof(buf) - 1) {
214 *c++ = *p++ | 0x80;
215 }
216 *c = *p;
217}
218
219STATIC void emit_write_bytecode_byte_uint(emit_t *emit, int stack_adj, byte b, mp_uint_t val) {
220 emit_write_bytecode_byte(emit, stack_adj, b);
221 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
222}
223
224#if MICROPY_PERSISTENT_CODE
225STATIC void emit_write_bytecode_byte_const(emit_t *emit, int stack_adj, byte b, mp_uint_t n, mp_uint_t c) {
226 if (emit->pass == MP_PASS_EMIT) {
227 emit->const_table[n] = c;
228 }
229 emit_write_bytecode_byte_uint(emit, stack_adj, b, n);
230}
231#endif
232
233STATIC void emit_write_bytecode_byte_qstr(emit_t *emit, int stack_adj, byte b, qstr qst) {
234 #if MICROPY_PERSISTENT_CODE
235 assert((qst >> 16) == 0);
236 mp_emit_bc_adjust_stack_size(emit, stack_adj);
237 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
238 c[0] = b;
239 c[1] = qst;
240 c[2] = qst >> 8;
241 #else
242 emit_write_bytecode_byte_uint(emit, stack_adj, b, qst);
243 #endif
244}
245
246STATIC void emit_write_bytecode_byte_obj(emit_t *emit, int stack_adj, byte b, mp_obj_t obj) {
247 #if MICROPY_PERSISTENT_CODE
248 emit_write_bytecode_byte_const(emit, stack_adj, b,
249 emit->scope->num_pos_args + emit->scope->num_kwonly_args
250 + emit->ct_cur_obj++, (mp_uint_t)obj);
251 #else
252 // aligns the pointer so it is friendly to GC
253 emit_write_bytecode_byte(emit, stack_adj, b);
254 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(mp_obj_t));
255 mp_obj_t *c = (mp_obj_t *)emit_get_cur_to_write_bytecode(emit, sizeof(mp_obj_t));
256 // Verify thar c is already uint-aligned
257 assert(c == MP_ALIGN(c, sizeof(mp_obj_t)));
258 *c = obj;
259 #endif
260}
261
262STATIC void emit_write_bytecode_byte_raw_code(emit_t *emit, int stack_adj, byte b, mp_raw_code_t *rc) {
263 #if MICROPY_PERSISTENT_CODE
264 emit_write_bytecode_byte_const(emit, stack_adj, b,
265 emit->scope->num_pos_args + emit->scope->num_kwonly_args
266 + emit->ct_num_obj + emit->ct_cur_raw_code++, (mp_uint_t)(uintptr_t)rc);
267 #else
268 // aligns the pointer so it is friendly to GC
269 emit_write_bytecode_byte(emit, stack_adj, b);
270 emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(void *));
271 void **c = (void **)emit_get_cur_to_write_bytecode(emit, sizeof(void *));
272 // Verify thar c is already uint-aligned
273 assert(c == MP_ALIGN(c, sizeof(void *)));
274 *c = rc;
275 #endif
276 #if MICROPY_PY_SYS_SETTRACE
277 rc->line_of_definition = emit->last_source_line;
278 #endif
279}
280
281// unsigned labels are relative to ip following this instruction, stored as 16 bits
282STATIC void emit_write_bytecode_byte_unsigned_label(emit_t *emit, int stack_adj, byte b1, mp_uint_t label) {
283 mp_emit_bc_adjust_stack_size(emit, stack_adj);
284 mp_uint_t bytecode_offset;
285 if (emit->pass < MP_PASS_EMIT) {
286 bytecode_offset = 0;
287 } else {
288 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
289 }
290 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
291 c[0] = b1;
292 c[1] = bytecode_offset;
293 c[2] = bytecode_offset >> 8;
294}
295
296// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
297STATIC void emit_write_bytecode_byte_signed_label(emit_t *emit, int stack_adj, byte b1, mp_uint_t label) {
298 mp_emit_bc_adjust_stack_size(emit, stack_adj);
299 int bytecode_offset;
300 if (emit->pass < MP_PASS_EMIT) {
301 bytecode_offset = 0;
302 } else {
303 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
304 }
305 byte *c = emit_get_cur_to_write_bytecode(emit, 3);
306 c[0] = b1;
307 c[1] = bytecode_offset;
308 c[2] = bytecode_offset >> 8;
309}
310
311void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
312 emit->pass = pass;
313 emit->stack_size = 0;
314 emit->last_emit_was_return_value = false;
315 emit->scope = scope;
316 emit->last_source_line_offset = 0;
317 emit->last_source_line = 1;
318 #ifndef NDEBUG
319 // With debugging enabled labels are checked for unique assignment
320 if (pass < MP_PASS_EMIT && emit->label_offsets != NULL) {
321 memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
322 }
323 #endif
324 emit->bytecode_offset = 0;
325 emit->code_info_offset = 0;
326
327 // Write local state size, exception stack size, scope flags and number of arguments
328 {
329 mp_uint_t n_state = scope->num_locals + scope->stack_size;
330 if (n_state == 0) {
331 // Need at least 1 entry in the state, in the case an exception is
332 // propagated through this function, the exception is returned in
333 // the highest slot in the state (fastn[0], see vm.c).
334 n_state = 1;
335 }
336 #if MICROPY_DEBUG_VM_STACK_OVERFLOW
337 // An extra slot in the stack is needed to detect VM stack overflow
338 n_state += 1;
339 #endif
340
341 size_t n_exc_stack = scope->exc_stack_size;
342 MP_BC_PRELUDE_SIG_ENCODE(n_state, n_exc_stack, scope, emit_write_code_info_byte, emit);
343 }
344
345 // Write number of cells and size of the source code info
346 if (pass >= MP_PASS_CODE_SIZE) {
347 MP_BC_PRELUDE_SIZE_ENCODE(emit->n_info, emit->n_cell, emit_write_code_info_byte, emit);
348 }
349
350 emit->n_info = emit->code_info_offset;
351
352 // Write the name and source file of this function.
353 emit_write_code_info_qstr(emit, scope->simple_name);
354 emit_write_code_info_qstr(emit, scope->source_file);
355
356 #if MICROPY_PERSISTENT_CODE
357 emit->ct_cur_obj = 0;
358 emit->ct_cur_raw_code = 0;
359 #endif
360
361 if (pass == MP_PASS_EMIT) {
362 // Write argument names (needed to resolve positional args passed as
363 // keywords). We store them as full word-sized objects for efficient access
364 // in mp_setup_code_state this is the start of the prelude and is guaranteed
365 // to be aligned on a word boundary.
366
367 // For a given argument position (indexed by i) we need to find the
368 // corresponding id_info which is a parameter, as it has the correct
369 // qstr name to use as the argument name. Note that it's not a simple
370 // 1-1 mapping (ie i!=j in general) because of possible closed-over
371 // variables. In the case that the argument i has no corresponding
372 // parameter we use "*" as its name (since no argument can ever be named
373 // "*"). We could use a blank qstr but "*" is better for debugging.
374 // Note: there is some wasted RAM here for the case of storing a qstr
375 // for each closed-over variable, and maybe there is a better way to do
376 // it, but that would require changes to mp_setup_code_state.
377 for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
378 qstr qst = MP_QSTR__star_;
379 for (int j = 0; j < scope->id_info_len; ++j) {
380 id_info_t *id = &scope->id_info[j];
381 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
382 qst = id->qst;
383 break;
384 }
385 }
386 emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
387 }
388 }
389}
390
391void mp_emit_bc_end_pass(emit_t *emit) {
392 if (emit->pass == MP_PASS_SCOPE) {
393 return;
394 }
395
396 // check stack is back to zero size
397 assert(emit->stack_size == 0);
398
399 emit_write_code_info_byte(emit, 0); // end of line number info
400
401 // Calculate size of source code info section
402 emit->n_info = emit->code_info_offset - emit->n_info;
403
404 // Emit closure section of prelude
405 emit->n_cell = 0;
406 for (size_t i = 0; i < emit->scope->id_info_len; ++i) {
407 id_info_t *id = &emit->scope->id_info[i];
408 if (id->kind == ID_INFO_KIND_CELL) {
409 assert(id->local_num <= 255);
410 emit_write_code_info_byte(emit, id->local_num); // write the local which should be converted to a cell
411 ++emit->n_cell;
412 }
413 }
414
415 #if MICROPY_PERSISTENT_CODE
416 assert(emit->pass <= MP_PASS_STACK_SIZE || (emit->ct_num_obj == emit->ct_cur_obj));
417 emit->ct_num_obj = emit->ct_cur_obj;
418 #endif
419
420 if (emit->pass == MP_PASS_CODE_SIZE) {
421 #if !MICROPY_PERSISTENT_CODE
422 // so bytecode is aligned
423 emit->code_info_offset = (size_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
424 #endif
425
426 // calculate size of total code-info + bytecode, in bytes
427 emit->code_info_size = emit->code_info_offset;
428 emit->bytecode_size = emit->bytecode_offset;
429 emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
430
431 #if MICROPY_PERSISTENT_CODE
432 emit->const_table = m_new0(mp_uint_t,
433 emit->scope->num_pos_args + emit->scope->num_kwonly_args
434 + emit->ct_cur_obj + emit->ct_cur_raw_code);
435 #else
436 emit->const_table = m_new0(mp_uint_t,
437 emit->scope->num_pos_args + emit->scope->num_kwonly_args);
438 #endif
439
440 } else if (emit->pass == MP_PASS_EMIT) {
441 mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
442 #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
443 emit->code_info_size + emit->bytecode_size,
444 #endif
445 emit->const_table,
446 #if MICROPY_PERSISTENT_CODE_SAVE
447 emit->ct_cur_obj, emit->ct_cur_raw_code,
448 #endif
449 emit->scope->scope_flags);
450 }
451}
452
453bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
454 return emit->last_emit_was_return_value;
455}
456
457void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
458 if (emit->pass == MP_PASS_SCOPE) {
459 return;
460 }
461 assert((mp_int_t)emit->stack_size + delta >= 0);
462 emit->stack_size += delta;
463 if (emit->stack_size > emit->scope->stack_size) {
464 emit->scope->stack_size = emit->stack_size;
465 }
466 emit->last_emit_was_return_value = false;
467}
468
469void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
470 #if MICROPY_ENABLE_SOURCE_LINE
471 if (MP_STATE_VM(mp_optimise_value) >= 3) {
472 // If we compile with -O3, don't store line numbers.
473 return;
474 }
475 if (source_line > emit->last_source_line) {
476 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
477 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
478 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
479 emit->last_source_line_offset = emit->bytecode_offset;
480 emit->last_source_line = source_line;
481 }
482 #else
483 (void)emit;
484 (void)source_line;
485 #endif
486}
487
488void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
489 mp_emit_bc_adjust_stack_size(emit, 0);
490 if (emit->pass == MP_PASS_SCOPE) {
491 return;
492 }
493 assert(l < emit->max_num_labels);
494 if (emit->pass < MP_PASS_EMIT) {
495 // assign label offset
496 assert(emit->label_offsets[l] == (mp_uint_t)-1);
497 emit->label_offsets[l] = emit->bytecode_offset;
498 } else {
499 // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
500 assert(emit->label_offsets[l] == emit->bytecode_offset);
501 }
502}
503
504void mp_emit_bc_import(emit_t *emit, qstr qst, int kind) {
505 MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_NAME == MP_BC_IMPORT_NAME);
506 MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_FROM == MP_BC_IMPORT_FROM);
507 int stack_adj = kind == MP_EMIT_IMPORT_FROM ? 1 : -1;
508 if (kind == MP_EMIT_IMPORT_STAR) {
509 emit_write_bytecode_byte(emit, stack_adj, MP_BC_IMPORT_STAR);
510 } else {
511 emit_write_bytecode_byte_qstr(emit, stack_adj, MP_BC_IMPORT_NAME + kind, qst);
512 }
513}
514
515void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
516 MP_STATIC_ASSERT(MP_BC_LOAD_CONST_FALSE + (MP_TOKEN_KW_NONE - MP_TOKEN_KW_FALSE) == MP_BC_LOAD_CONST_NONE);
517 MP_STATIC_ASSERT(MP_BC_LOAD_CONST_FALSE + (MP_TOKEN_KW_TRUE - MP_TOKEN_KW_FALSE) == MP_BC_LOAD_CONST_TRUE);
518 if (tok == MP_TOKEN_ELLIPSIS) {
519 emit_write_bytecode_byte_obj(emit, 1, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
520 } else {
521 emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_CONST_FALSE + (tok - MP_TOKEN_KW_FALSE));
522 }
523}
524
525void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
526 if (-MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS <= arg
527 && arg < MP_BC_LOAD_CONST_SMALL_INT_MULTI_NUM - MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS) {
528 emit_write_bytecode_byte(emit, 1,
529 MP_BC_LOAD_CONST_SMALL_INT_MULTI + MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS + arg);
530 } else {
531 emit_write_bytecode_byte_int(emit, 1, MP_BC_LOAD_CONST_SMALL_INT, arg);
532 }
533}
534
535void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
536 emit_write_bytecode_byte_qstr(emit, 1, MP_BC_LOAD_CONST_STRING, qst);
537}
538
539void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
540 emit_write_bytecode_byte_obj(emit, 1, MP_BC_LOAD_CONST_OBJ, obj);
541}
542
543void mp_emit_bc_load_null(emit_t *emit) {
544 emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_NULL);
545}
546
547void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
548 MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_LOAD_FAST_N);
549 MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_LOAD_DEREF);
550 (void)qst;
551 if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
552 emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_FAST_MULTI + local_num);
553 } else {
554 emit_write_bytecode_byte_uint(emit, 1, MP_BC_LOAD_FAST_N + kind, local_num);
555 }
556}
557
558void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind) {
559 MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_LOAD_NAME);
560 MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_LOAD_GLOBAL);
561 (void)qst;
562 emit_write_bytecode_byte_qstr(emit, 1, MP_BC_LOAD_NAME + kind, qst);
563 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
564 emit_write_bytecode_raw_byte(emit, 0);
565 }
566}
567
568void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super) {
569 int stack_adj = 1 - 2 * is_super;
570 emit_write_bytecode_byte_qstr(emit, stack_adj, is_super ? MP_BC_LOAD_SUPER_METHOD : MP_BC_LOAD_METHOD, qst);
571}
572
573void mp_emit_bc_load_build_class(emit_t *emit) {
574 emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_BUILD_CLASS);
575}
576
577void mp_emit_bc_subscr(emit_t *emit, int kind) {
578 if (kind == MP_EMIT_SUBSCR_LOAD) {
579 emit_write_bytecode_byte(emit, -1, MP_BC_LOAD_SUBSCR);
580 } else {
581 if (kind == MP_EMIT_SUBSCR_DELETE) {
582 mp_emit_bc_load_null(emit);
583 mp_emit_bc_rot_three(emit);
584 }
585 emit_write_bytecode_byte(emit, -3, MP_BC_STORE_SUBSCR);
586 }
587}
588
589void mp_emit_bc_attr(emit_t *emit, qstr qst, int kind) {
590 if (kind == MP_EMIT_ATTR_LOAD) {
591 emit_write_bytecode_byte_qstr(emit, 0, MP_BC_LOAD_ATTR, qst);
592 } else {
593 if (kind == MP_EMIT_ATTR_DELETE) {
594 mp_emit_bc_load_null(emit);
595 mp_emit_bc_rot_two(emit);
596 }
597 emit_write_bytecode_byte_qstr(emit, -2, MP_BC_STORE_ATTR, qst);
598 }
599 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
600 emit_write_bytecode_raw_byte(emit, 0);
601 }
602}
603
604void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
605 MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_STORE_FAST_N);
606 MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_STORE_DEREF);
607 (void)qst;
608 if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
609 emit_write_bytecode_byte(emit, -1, MP_BC_STORE_FAST_MULTI + local_num);
610 } else {
611 emit_write_bytecode_byte_uint(emit, -1, MP_BC_STORE_FAST_N + kind, local_num);
612 }
613}
614
615void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind) {
616 MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_STORE_NAME);
617 MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_STORE_GLOBAL);
618 emit_write_bytecode_byte_qstr(emit, -1, MP_BC_STORE_NAME + kind, qst);
619}
620
621void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
622 MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
623 MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
624 (void)qst;
625 emit_write_bytecode_byte_uint(emit, 0, MP_BC_DELETE_FAST + kind, local_num);
626}
627
628void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind) {
629 MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_DELETE_NAME);
630 MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_DELETE_GLOBAL);
631 emit_write_bytecode_byte_qstr(emit, 0, MP_BC_DELETE_NAME + kind, qst);
632}
633
634void mp_emit_bc_dup_top(emit_t *emit) {
635 emit_write_bytecode_byte(emit, 1, MP_BC_DUP_TOP);
636}
637
638void mp_emit_bc_dup_top_two(emit_t *emit) {
639 emit_write_bytecode_byte(emit, 2, MP_BC_DUP_TOP_TWO);
640}
641
642void mp_emit_bc_pop_top(emit_t *emit) {
643 emit_write_bytecode_byte(emit, -1, MP_BC_POP_TOP);
644}
645
646void mp_emit_bc_rot_two(emit_t *emit) {
647 emit_write_bytecode_byte(emit, 0, MP_BC_ROT_TWO);
648}
649
650void mp_emit_bc_rot_three(emit_t *emit) {
651 emit_write_bytecode_byte(emit, 0, MP_BC_ROT_THREE);
652}
653
654void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
655 emit_write_bytecode_byte_signed_label(emit, 0, MP_BC_JUMP, label);
656}
657
658void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
659 if (cond) {
660 emit_write_bytecode_byte_signed_label(emit, -1, MP_BC_POP_JUMP_IF_TRUE, label);
661 } else {
662 emit_write_bytecode_byte_signed_label(emit, -1, MP_BC_POP_JUMP_IF_FALSE, label);
663 }
664}
665
666void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
667 if (cond) {
668 emit_write_bytecode_byte_signed_label(emit, -1, MP_BC_JUMP_IF_TRUE_OR_POP, label);
669 } else {
670 emit_write_bytecode_byte_signed_label(emit, -1, MP_BC_JUMP_IF_FALSE_OR_POP, label);
671 }
672}
673
674void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
675 if (except_depth == 0) {
676 if (label & MP_EMIT_BREAK_FROM_FOR) {
677 // need to pop the iterator if we are breaking out of a for loop
678 emit_write_bytecode_raw_byte(emit, MP_BC_POP_TOP);
679 // also pop the iter_buf
680 for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
681 emit_write_bytecode_raw_byte(emit, MP_BC_POP_TOP);
682 }
683 }
684 emit_write_bytecode_byte_signed_label(emit, 0, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
685 } else {
686 emit_write_bytecode_byte_signed_label(emit, 0, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
687 emit_write_bytecode_raw_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
688 }
689}
690
691void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind) {
692 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_WITH == MP_BC_SETUP_WITH);
693 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_EXCEPT == MP_BC_SETUP_EXCEPT);
694 MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_FINALLY == MP_BC_SETUP_FINALLY);
695 // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
696 // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
697 int stack_adj = kind == MP_EMIT_SETUP_BLOCK_WITH ? 2 : 0;
698 emit_write_bytecode_byte_unsigned_label(emit, stack_adj, MP_BC_SETUP_WITH + kind, label);
699}
700
701void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
702 mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
703 mp_emit_bc_label_assign(emit, label);
704 // The +2 is to ensure we have enough stack space to call the __exit__ method
705 emit_write_bytecode_byte(emit, 2, MP_BC_WITH_CLEANUP);
706 // Cancel the +2 above, plus the +2 from mp_emit_bc_setup_block(MP_EMIT_SETUP_BLOCK_WITH)
707 mp_emit_bc_adjust_stack_size(emit, -4);
708}
709
710void mp_emit_bc_end_finally(emit_t *emit) {
711 emit_write_bytecode_byte(emit, -1, MP_BC_END_FINALLY);
712}
713
714void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
715 int stack_adj = use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0;
716 emit_write_bytecode_byte(emit, stack_adj, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
717}
718
719void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
720 emit_write_bytecode_byte_unsigned_label(emit, 1, MP_BC_FOR_ITER, label);
721}
722
723void mp_emit_bc_for_iter_end(emit_t *emit) {
724 mp_emit_bc_adjust_stack_size(emit, -MP_OBJ_ITER_BUF_NSLOTS);
725}
726
727void mp_emit_bc_pop_except_jump(emit_t *emit, mp_uint_t label, bool within_exc_handler) {
728 (void)within_exc_handler;
729 emit_write_bytecode_byte_unsigned_label(emit, 0, MP_BC_POP_EXCEPT_JUMP, label);
730}
731
732void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
733 emit_write_bytecode_byte(emit, 0, MP_BC_UNARY_OP_MULTI + op);
734}
735
736void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
737 bool invert = false;
738 if (op == MP_BINARY_OP_NOT_IN) {
739 invert = true;
740 op = MP_BINARY_OP_IN;
741 } else if (op == MP_BINARY_OP_IS_NOT) {
742 invert = true;
743 op = MP_BINARY_OP_IS;
744 }
745 emit_write_bytecode_byte(emit, -1, MP_BC_BINARY_OP_MULTI + op);
746 if (invert) {
747 emit_write_bytecode_byte(emit, 0, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
748 }
749}
750
751void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
752 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
753 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
754 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
755 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_BC_BUILD_SET);
756 MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SLICE == MP_BC_BUILD_SLICE);
757 int stack_adj = kind == MP_EMIT_BUILD_MAP ? 1 : 1 - n_args;
758 emit_write_bytecode_byte_uint(emit, stack_adj, MP_BC_BUILD_TUPLE + kind, n_args);
759}
760
761void mp_emit_bc_store_map(emit_t *emit) {
762 emit_write_bytecode_byte(emit, -2, MP_BC_STORE_MAP);
763}
764
765void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
766 int t;
767 int n;
768 if (kind == SCOPE_LIST_COMP) {
769 n = 0;
770 t = 0;
771 } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
772 n = 1;
773 t = 1;
774 } else if (MICROPY_PY_BUILTINS_SET) {
775 n = 0;
776 t = 2;
777 }
778 // the lower 2 bits of the opcode argument indicate the collection type
779 emit_write_bytecode_byte_uint(emit, -1 - n, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
780}
781
782void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
783 emit_write_bytecode_byte_uint(emit, -1 + n_args, MP_BC_UNPACK_SEQUENCE, n_args);
784}
785
786void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
787 emit_write_bytecode_byte_uint(emit, -1 + n_left + n_right + 1, MP_BC_UNPACK_EX, n_left | (n_right << 8));
788}
789
790void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
791 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
792 emit_write_bytecode_byte_raw_code(emit, 1, MP_BC_MAKE_FUNCTION, scope->raw_code);
793 } else {
794 emit_write_bytecode_byte_raw_code(emit, -1, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
795 }
796}
797
798void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
799 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
800 int stack_adj = -n_closed_over + 1;
801 emit_write_bytecode_byte_raw_code(emit, stack_adj, MP_BC_MAKE_CLOSURE, scope->raw_code);
802 emit_write_bytecode_raw_byte(emit, n_closed_over);
803 } else {
804 assert(n_closed_over <= 255);
805 int stack_adj = -2 - (mp_int_t)n_closed_over + 1;
806 emit_write_bytecode_byte_raw_code(emit, stack_adj, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
807 emit_write_bytecode_raw_byte(emit, n_closed_over);
808 }
809}
810
811STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, mp_uint_t bytecode_base, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
812 if (star_flags) {
813 stack_adj -= (int)n_positional + 2 * (int)n_keyword + 2;
814 emit_write_bytecode_byte_uint(emit, stack_adj, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
815 } else {
816 stack_adj -= (int)n_positional + 2 * (int)n_keyword;
817 emit_write_bytecode_byte_uint(emit, stack_adj, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
818 }
819}
820
821void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
822 emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
823}
824
825void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
826 emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
827}
828
829void mp_emit_bc_return_value(emit_t *emit) {
830 emit_write_bytecode_byte(emit, -1, MP_BC_RETURN_VALUE);
831 emit->last_emit_was_return_value = true;
832}
833
834void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
835 MP_STATIC_ASSERT(MP_BC_RAISE_LAST + 1 == MP_BC_RAISE_OBJ);
836 MP_STATIC_ASSERT(MP_BC_RAISE_LAST + 2 == MP_BC_RAISE_FROM);
837 assert(n_args <= 2);
838 emit_write_bytecode_byte(emit, -n_args, MP_BC_RAISE_LAST + n_args);
839}
840
841void mp_emit_bc_yield(emit_t *emit, int kind) {
842 MP_STATIC_ASSERT(MP_BC_YIELD_VALUE + 1 == MP_BC_YIELD_FROM);
843 emit_write_bytecode_byte(emit, -kind, MP_BC_YIELD_VALUE + kind);
844 emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
845}
846
847void mp_emit_bc_start_except_handler(emit_t *emit) {
848 mp_emit_bc_adjust_stack_size(emit, 4); // stack adjust for the exception instance, +3 for possible UNWIND_JUMP state
849}
850
851void mp_emit_bc_end_except_handler(emit_t *emit) {
852 mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
853}
854
855#if MICROPY_EMIT_NATIVE
856const emit_method_table_t emit_bc_method_table = {
857 #if MICROPY_DYNAMIC_COMPILER
858 NULL,
859 NULL,
860 #endif
861
862 mp_emit_bc_start_pass,
863 mp_emit_bc_end_pass,
864 mp_emit_bc_last_emit_was_return_value,
865 mp_emit_bc_adjust_stack_size,
866 mp_emit_bc_set_source_line,
867
868 {
869 mp_emit_bc_load_local,
870 mp_emit_bc_load_global,
871 },
872 {
873 mp_emit_bc_store_local,
874 mp_emit_bc_store_global,
875 },
876 {
877 mp_emit_bc_delete_local,
878 mp_emit_bc_delete_global,
879 },
880
881 mp_emit_bc_label_assign,
882 mp_emit_bc_import,
883 mp_emit_bc_load_const_tok,
884 mp_emit_bc_load_const_small_int,
885 mp_emit_bc_load_const_str,
886 mp_emit_bc_load_const_obj,
887 mp_emit_bc_load_null,
888 mp_emit_bc_load_method,
889 mp_emit_bc_load_build_class,
890 mp_emit_bc_subscr,
891 mp_emit_bc_attr,
892 mp_emit_bc_dup_top,
893 mp_emit_bc_dup_top_two,
894 mp_emit_bc_pop_top,
895 mp_emit_bc_rot_two,
896 mp_emit_bc_rot_three,
897 mp_emit_bc_jump,
898 mp_emit_bc_pop_jump_if,
899 mp_emit_bc_jump_if_or_pop,
900 mp_emit_bc_unwind_jump,
901 mp_emit_bc_setup_block,
902 mp_emit_bc_with_cleanup,
903 mp_emit_bc_end_finally,
904 mp_emit_bc_get_iter,
905 mp_emit_bc_for_iter,
906 mp_emit_bc_for_iter_end,
907 mp_emit_bc_pop_except_jump,
908 mp_emit_bc_unary_op,
909 mp_emit_bc_binary_op,
910 mp_emit_bc_build,
911 mp_emit_bc_store_map,
912 mp_emit_bc_store_comp,
913 mp_emit_bc_unpack_sequence,
914 mp_emit_bc_unpack_ex,
915 mp_emit_bc_make_function,
916 mp_emit_bc_make_closure,
917 mp_emit_bc_call_function,
918 mp_emit_bc_call_method,
919 mp_emit_bc_return_value,
920 mp_emit_bc_raise_varargs,
921 mp_emit_bc_yield,
922
923 mp_emit_bc_start_except_handler,
924 mp_emit_bc_end_except_handler,
925};
926#else
927const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
928 mp_emit_bc_load_local,
929 mp_emit_bc_load_global,
930};
931
932const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
933 mp_emit_bc_store_local,
934 mp_emit_bc_store_global,
935};
936
937const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
938 mp_emit_bc_delete_local,
939 mp_emit_bc_delete_global,
940};
941#endif
942
943#endif // MICROPY_ENABLE_COMPILER
944