emitbc.c 36 KB

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