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