emitbc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 && emit->label_offsets != NULL) {
  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(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. if (kind == MP_EMIT_IMPORT_FROM) {
  453. emit_bc_pre(emit, 1);
  454. } else {
  455. emit_bc_pre(emit, -1);
  456. }
  457. if (kind == MP_EMIT_IMPORT_STAR) {
  458. emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
  459. } else {
  460. emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME + kind, qst);
  461. }
  462. }
  463. void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
  464. emit_bc_pre(emit, 1);
  465. switch (tok) {
  466. case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
  467. case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
  468. case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
  469. default:
  470. assert(tok == MP_TOKEN_ELLIPSIS);
  471. emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
  472. break;
  473. }
  474. }
  475. void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
  476. emit_bc_pre(emit, 1);
  477. if (-16 <= arg && arg <= 47) {
  478. emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
  479. } else {
  480. emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
  481. }
  482. }
  483. void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
  484. emit_bc_pre(emit, 1);
  485. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
  486. }
  487. void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
  488. emit_bc_pre(emit, 1);
  489. emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, obj);
  490. }
  491. void mp_emit_bc_load_null(emit_t *emit) {
  492. emit_bc_pre(emit, 1);
  493. emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
  494. }
  495. void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  496. MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_LOAD_FAST_N);
  497. MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_LOAD_DEREF);
  498. (void)qst;
  499. emit_bc_pre(emit, 1);
  500. if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
  501. emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
  502. } else {
  503. emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N + kind, local_num);
  504. }
  505. }
  506. void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind) {
  507. MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_LOAD_NAME);
  508. MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_LOAD_GLOBAL);
  509. (void)qst;
  510. emit_bc_pre(emit, 1);
  511. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME + kind, qst);
  512. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  513. emit_write_bytecode_byte(emit, 0);
  514. }
  515. }
  516. void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super) {
  517. emit_bc_pre(emit, 1 - 2 * is_super);
  518. emit_write_bytecode_byte_qstr(emit, is_super ? MP_BC_LOAD_SUPER_METHOD : MP_BC_LOAD_METHOD, qst);
  519. }
  520. void mp_emit_bc_load_build_class(emit_t *emit) {
  521. emit_bc_pre(emit, 1);
  522. emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
  523. }
  524. void mp_emit_bc_subscr(emit_t *emit, int kind) {
  525. if (kind == MP_EMIT_SUBSCR_LOAD) {
  526. emit_bc_pre(emit, -1);
  527. emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
  528. } else {
  529. if (kind == MP_EMIT_SUBSCR_DELETE) {
  530. mp_emit_bc_load_null(emit);
  531. mp_emit_bc_rot_three(emit);
  532. }
  533. emit_bc_pre(emit, -3);
  534. emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
  535. }
  536. }
  537. void mp_emit_bc_attr(emit_t *emit, qstr qst, int kind) {
  538. if (kind == MP_EMIT_ATTR_LOAD) {
  539. emit_bc_pre(emit, 0);
  540. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
  541. } else {
  542. if (kind == MP_EMIT_ATTR_DELETE) {
  543. mp_emit_bc_load_null(emit);
  544. mp_emit_bc_rot_two(emit);
  545. }
  546. emit_bc_pre(emit, -2);
  547. emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
  548. }
  549. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  550. emit_write_bytecode_byte(emit, 0);
  551. }
  552. }
  553. void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  554. MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_STORE_FAST_N);
  555. MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_STORE_DEREF);
  556. (void)qst;
  557. emit_bc_pre(emit, -1);
  558. if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
  559. emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
  560. } else {
  561. emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N + kind, local_num);
  562. }
  563. }
  564. void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind) {
  565. MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_STORE_NAME);
  566. MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_STORE_GLOBAL);
  567. emit_bc_pre(emit, -1);
  568. emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME + kind, qst);
  569. }
  570. void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  571. MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
  572. MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
  573. (void)qst;
  574. emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST + kind, local_num);
  575. }
  576. void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind) {
  577. MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_DELETE_NAME);
  578. MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_DELETE_GLOBAL);
  579. emit_bc_pre(emit, 0);
  580. emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME + kind, qst);
  581. }
  582. void mp_emit_bc_dup_top(emit_t *emit) {
  583. emit_bc_pre(emit, 1);
  584. emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
  585. }
  586. void mp_emit_bc_dup_top_two(emit_t *emit) {
  587. emit_bc_pre(emit, 2);
  588. emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
  589. }
  590. void mp_emit_bc_pop_top(emit_t *emit) {
  591. emit_bc_pre(emit, -1);
  592. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  593. }
  594. void mp_emit_bc_rot_two(emit_t *emit) {
  595. emit_bc_pre(emit, 0);
  596. emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
  597. }
  598. void mp_emit_bc_rot_three(emit_t *emit) {
  599. emit_bc_pre(emit, 0);
  600. emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
  601. }
  602. void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
  603. emit_bc_pre(emit, 0);
  604. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
  605. }
  606. void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
  607. emit_bc_pre(emit, -1);
  608. if (cond) {
  609. emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
  610. } else {
  611. emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
  612. }
  613. }
  614. void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
  615. emit_bc_pre(emit, -1);
  616. if (cond) {
  617. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
  618. } else {
  619. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
  620. }
  621. }
  622. void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
  623. if (except_depth == 0) {
  624. emit_bc_pre(emit, 0);
  625. if (label & MP_EMIT_BREAK_FROM_FOR) {
  626. // need to pop the iterator if we are breaking out of a for loop
  627. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  628. // also pop the iter_buf
  629. for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
  630. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  631. }
  632. }
  633. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
  634. } else {
  635. emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
  636. emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
  637. }
  638. }
  639. void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind) {
  640. MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_WITH == MP_BC_SETUP_WITH);
  641. MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_EXCEPT == MP_BC_SETUP_EXCEPT);
  642. MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_FINALLY == MP_BC_SETUP_FINALLY);
  643. if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
  644. // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
  645. // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
  646. emit_bc_pre(emit, 2);
  647. } else {
  648. emit_bc_pre(emit, 0);
  649. }
  650. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH + kind, label);
  651. }
  652. void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
  653. mp_emit_bc_pop_block(emit);
  654. mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
  655. mp_emit_bc_label_assign(emit, label);
  656. emit_bc_pre(emit, 2); // ensure we have enough stack space to call the __exit__ method
  657. emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
  658. emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_block(MP_EMIT_SETUP_BLOCK_WITH)
  659. }
  660. void mp_emit_bc_end_finally(emit_t *emit) {
  661. emit_bc_pre(emit, -1);
  662. emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
  663. }
  664. void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
  665. emit_bc_pre(emit, use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0);
  666. emit_write_bytecode_byte(emit, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
  667. }
  668. void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
  669. emit_bc_pre(emit, 1);
  670. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
  671. }
  672. void mp_emit_bc_for_iter_end(emit_t *emit) {
  673. emit_bc_pre(emit, -MP_OBJ_ITER_BUF_NSLOTS);
  674. }
  675. void mp_emit_bc_pop_block(emit_t *emit) {
  676. emit_bc_pre(emit, 0);
  677. emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
  678. }
  679. void mp_emit_bc_pop_except(emit_t *emit) {
  680. emit_bc_pre(emit, 0);
  681. emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
  682. }
  683. void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
  684. emit_bc_pre(emit, 0);
  685. emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
  686. }
  687. void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
  688. bool invert = false;
  689. if (op == MP_BINARY_OP_NOT_IN) {
  690. invert = true;
  691. op = MP_BINARY_OP_IN;
  692. } else if (op == MP_BINARY_OP_IS_NOT) {
  693. invert = true;
  694. op = MP_BINARY_OP_IS;
  695. }
  696. emit_bc_pre(emit, -1);
  697. emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
  698. if (invert) {
  699. emit_bc_pre(emit, 0);
  700. emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
  701. }
  702. }
  703. void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
  704. MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
  705. MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
  706. MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
  707. MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_BC_BUILD_SET);
  708. MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SLICE == MP_BC_BUILD_SLICE);
  709. if (kind == MP_EMIT_BUILD_MAP) {
  710. emit_bc_pre(emit, 1);
  711. } else {
  712. emit_bc_pre(emit, 1 - n_args);
  713. }
  714. emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE + kind, n_args);
  715. }
  716. void mp_emit_bc_store_map(emit_t *emit) {
  717. emit_bc_pre(emit, -2);
  718. emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
  719. }
  720. void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
  721. int t;
  722. int n;
  723. if (kind == SCOPE_LIST_COMP) {
  724. n = 0;
  725. t = 0;
  726. } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
  727. n = 1;
  728. t = 1;
  729. } else if (MICROPY_PY_BUILTINS_SET) {
  730. n = 0;
  731. t = 2;
  732. }
  733. emit_bc_pre(emit, -1 - n);
  734. // the lower 2 bits of the opcode argument indicate the collection type
  735. emit_write_bytecode_byte_uint(emit, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
  736. }
  737. void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
  738. emit_bc_pre(emit, -1 + n_args);
  739. emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
  740. }
  741. void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
  742. emit_bc_pre(emit, -1 + n_left + n_right + 1);
  743. emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
  744. }
  745. void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
  746. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  747. emit_bc_pre(emit, 1);
  748. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
  749. } else {
  750. emit_bc_pre(emit, -1);
  751. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
  752. }
  753. }
  754. 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) {
  755. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  756. emit_bc_pre(emit, -n_closed_over + 1);
  757. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
  758. emit_write_bytecode_byte(emit, n_closed_over);
  759. } else {
  760. assert(n_closed_over <= 255);
  761. emit_bc_pre(emit, -2 - (mp_int_t)n_closed_over + 1);
  762. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
  763. emit_write_bytecode_byte(emit, n_closed_over);
  764. }
  765. }
  766. 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) {
  767. if (star_flags) {
  768. emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
  769. emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
  770. } else {
  771. emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
  772. emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
  773. }
  774. }
  775. void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  776. emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
  777. }
  778. void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  779. emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
  780. }
  781. void mp_emit_bc_return_value(emit_t *emit) {
  782. emit_bc_pre(emit, -1);
  783. emit->last_emit_was_return_value = true;
  784. emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
  785. }
  786. void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
  787. assert(n_args <= 2);
  788. emit_bc_pre(emit, -n_args);
  789. emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
  790. }
  791. void mp_emit_bc_yield(emit_t *emit, int kind) {
  792. MP_STATIC_ASSERT(MP_BC_YIELD_VALUE + 1 == MP_BC_YIELD_FROM);
  793. emit_bc_pre(emit, -kind);
  794. emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
  795. emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE + kind);
  796. }
  797. void mp_emit_bc_start_except_handler(emit_t *emit) {
  798. mp_emit_bc_adjust_stack_size(emit, 4); // stack adjust for the exception instance, +3 for possible UNWIND_JUMP state
  799. }
  800. void mp_emit_bc_end_except_handler(emit_t *emit) {
  801. mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
  802. }
  803. #if MICROPY_EMIT_NATIVE
  804. const emit_method_table_t emit_bc_method_table = {
  805. NULL, // set_native_type is never called when emitting bytecode
  806. mp_emit_bc_start_pass,
  807. mp_emit_bc_end_pass,
  808. mp_emit_bc_last_emit_was_return_value,
  809. mp_emit_bc_adjust_stack_size,
  810. mp_emit_bc_set_source_line,
  811. {
  812. mp_emit_bc_load_local,
  813. mp_emit_bc_load_global,
  814. },
  815. {
  816. mp_emit_bc_store_local,
  817. mp_emit_bc_store_global,
  818. },
  819. {
  820. mp_emit_bc_delete_local,
  821. mp_emit_bc_delete_global,
  822. },
  823. mp_emit_bc_label_assign,
  824. mp_emit_bc_import,
  825. mp_emit_bc_load_const_tok,
  826. mp_emit_bc_load_const_small_int,
  827. mp_emit_bc_load_const_str,
  828. mp_emit_bc_load_const_obj,
  829. mp_emit_bc_load_null,
  830. mp_emit_bc_load_method,
  831. mp_emit_bc_load_build_class,
  832. mp_emit_bc_subscr,
  833. mp_emit_bc_attr,
  834. mp_emit_bc_dup_top,
  835. mp_emit_bc_dup_top_two,
  836. mp_emit_bc_pop_top,
  837. mp_emit_bc_rot_two,
  838. mp_emit_bc_rot_three,
  839. mp_emit_bc_jump,
  840. mp_emit_bc_pop_jump_if,
  841. mp_emit_bc_jump_if_or_pop,
  842. mp_emit_bc_unwind_jump,
  843. mp_emit_bc_setup_block,
  844. mp_emit_bc_with_cleanup,
  845. mp_emit_bc_end_finally,
  846. mp_emit_bc_get_iter,
  847. mp_emit_bc_for_iter,
  848. mp_emit_bc_for_iter_end,
  849. mp_emit_bc_pop_block,
  850. mp_emit_bc_pop_except,
  851. mp_emit_bc_unary_op,
  852. mp_emit_bc_binary_op,
  853. mp_emit_bc_build,
  854. mp_emit_bc_store_map,
  855. mp_emit_bc_store_comp,
  856. mp_emit_bc_unpack_sequence,
  857. mp_emit_bc_unpack_ex,
  858. mp_emit_bc_make_function,
  859. mp_emit_bc_make_closure,
  860. mp_emit_bc_call_function,
  861. mp_emit_bc_call_method,
  862. mp_emit_bc_return_value,
  863. mp_emit_bc_raise_varargs,
  864. mp_emit_bc_yield,
  865. mp_emit_bc_start_except_handler,
  866. mp_emit_bc_end_except_handler,
  867. };
  868. #else
  869. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
  870. mp_emit_bc_load_local,
  871. mp_emit_bc_load_global,
  872. };
  873. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
  874. mp_emit_bc_store_local,
  875. mp_emit_bc_store_global,
  876. };
  877. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
  878. mp_emit_bc_delete_local,
  879. mp_emit_bc_delete_global,
  880. };
  881. #endif
  882. #endif //MICROPY_ENABLE_COMPILER