emitbc.c 35 KB

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