emitbc.c 34 KB

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