emitbc.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. if (pass < MP_PASS_EMIT) {
  281. memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(mp_uint_t));
  282. }
  283. emit->bytecode_offset = 0;
  284. emit->code_info_offset = 0;
  285. // Write local state size and exception stack size.
  286. {
  287. mp_uint_t n_state = scope->num_locals + scope->stack_size;
  288. if (n_state == 0) {
  289. // Need at least 1 entry in the state, in the case an exception is
  290. // propagated through this function, the exception is returned in
  291. // the highest slot in the state (fastn[0], see vm.c).
  292. n_state = 1;
  293. }
  294. emit_write_code_info_uint(emit, n_state);
  295. emit_write_code_info_uint(emit, scope->exc_stack_size);
  296. }
  297. // Write scope flags and number of arguments.
  298. // TODO check that num args all fit in a byte
  299. emit_write_code_info_byte(emit, emit->scope->scope_flags);
  300. emit_write_code_info_byte(emit, emit->scope->num_pos_args);
  301. emit_write_code_info_byte(emit, emit->scope->num_kwonly_args);
  302. emit_write_code_info_byte(emit, emit->scope->num_def_pos_args);
  303. // Write size of the rest of the code info. We don't know how big this
  304. // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
  305. // for it and hope that is enough! TODO assert this or something.
  306. if (pass == MP_PASS_EMIT) {
  307. emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
  308. } else {
  309. emit_get_cur_to_write_code_info(emit, 2);
  310. }
  311. // Write the name and source file of this function.
  312. emit_write_code_info_qstr(emit, scope->simple_name);
  313. emit_write_code_info_qstr(emit, scope->source_file);
  314. // bytecode prelude: initialise closed over variables
  315. for (int i = 0; i < scope->id_info_len; i++) {
  316. id_info_t *id = &scope->id_info[i];
  317. if (id->kind == ID_INFO_KIND_CELL) {
  318. assert(id->local_num < 255);
  319. emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
  320. }
  321. }
  322. emit_write_bytecode_byte(emit, 255); // end of list sentinel
  323. #if MICROPY_PERSISTENT_CODE
  324. emit->ct_cur_obj = 0;
  325. emit->ct_cur_raw_code = 0;
  326. #endif
  327. if (pass == MP_PASS_EMIT) {
  328. // Write argument names (needed to resolve positional args passed as
  329. // keywords). We store them as full word-sized objects for efficient access
  330. // in mp_setup_code_state this is the start of the prelude and is guaranteed
  331. // to be aligned on a word boundary.
  332. // For a given argument position (indexed by i) we need to find the
  333. // corresponding id_info which is a parameter, as it has the correct
  334. // qstr name to use as the argument name. Note that it's not a simple
  335. // 1-1 mapping (ie i!=j in general) because of possible closed-over
  336. // variables. In the case that the argument i has no corresponding
  337. // parameter we use "*" as its name (since no argument can ever be named
  338. // "*"). We could use a blank qstr but "*" is better for debugging.
  339. // Note: there is some wasted RAM here for the case of storing a qstr
  340. // for each closed-over variable, and maybe there is a better way to do
  341. // it, but that would require changes to mp_setup_code_state.
  342. for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
  343. qstr qst = MP_QSTR__star_;
  344. for (int j = 0; j < scope->id_info_len; ++j) {
  345. id_info_t *id = &scope->id_info[j];
  346. if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
  347. qst = id->qst;
  348. break;
  349. }
  350. }
  351. emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
  352. }
  353. }
  354. }
  355. void mp_emit_bc_end_pass(emit_t *emit) {
  356. if (emit->pass == MP_PASS_SCOPE) {
  357. return;
  358. }
  359. // check stack is back to zero size
  360. assert(emit->stack_size == 0);
  361. emit_write_code_info_byte(emit, 0); // end of line number info
  362. #if MICROPY_PERSISTENT_CODE
  363. assert(emit->pass <= MP_PASS_STACK_SIZE || (emit->ct_num_obj == emit->ct_cur_obj));
  364. emit->ct_num_obj = emit->ct_cur_obj;
  365. #endif
  366. if (emit->pass == MP_PASS_CODE_SIZE) {
  367. #if !MICROPY_PERSISTENT_CODE
  368. // so bytecode is aligned
  369. emit->code_info_offset = (size_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
  370. #endif
  371. // calculate size of total code-info + bytecode, in bytes
  372. emit->code_info_size = emit->code_info_offset;
  373. emit->bytecode_size = emit->bytecode_offset;
  374. emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
  375. #if MICROPY_PERSISTENT_CODE
  376. emit->const_table = m_new0(mp_uint_t,
  377. emit->scope->num_pos_args + emit->scope->num_kwonly_args
  378. + emit->ct_cur_obj + emit->ct_cur_raw_code);
  379. #else
  380. emit->const_table = m_new0(mp_uint_t,
  381. emit->scope->num_pos_args + emit->scope->num_kwonly_args);
  382. #endif
  383. } else if (emit->pass == MP_PASS_EMIT) {
  384. mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
  385. emit->code_info_size + emit->bytecode_size,
  386. emit->const_table,
  387. #if MICROPY_PERSISTENT_CODE_SAVE
  388. emit->ct_cur_obj, emit->ct_cur_raw_code,
  389. #endif
  390. emit->scope->scope_flags);
  391. }
  392. }
  393. bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
  394. return emit->last_emit_was_return_value;
  395. }
  396. void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
  397. if (emit->pass == MP_PASS_SCOPE) {
  398. return;
  399. }
  400. assert((mp_int_t)emit->stack_size + delta >= 0);
  401. emit->stack_size += delta;
  402. if (emit->stack_size > emit->scope->stack_size) {
  403. emit->scope->stack_size = emit->stack_size;
  404. }
  405. emit->last_emit_was_return_value = false;
  406. }
  407. static inline void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
  408. mp_emit_bc_adjust_stack_size(emit, stack_size_delta);
  409. }
  410. void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
  411. //printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
  412. #if MICROPY_ENABLE_SOURCE_LINE
  413. if (MP_STATE_VM(mp_optimise_value) >= 3) {
  414. // If we compile with -O3, don't store line numbers.
  415. return;
  416. }
  417. if (source_line > emit->last_source_line) {
  418. mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
  419. mp_uint_t lines_to_skip = source_line - emit->last_source_line;
  420. emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
  421. emit->last_source_line_offset = emit->bytecode_offset;
  422. emit->last_source_line = source_line;
  423. }
  424. #else
  425. (void)emit;
  426. (void)source_line;
  427. #endif
  428. }
  429. void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
  430. emit_bc_pre(emit, 0);
  431. if (emit->pass == MP_PASS_SCOPE) {
  432. return;
  433. }
  434. assert(l < emit->max_num_labels);
  435. if (emit->pass < MP_PASS_EMIT) {
  436. // assign label offset
  437. assert(emit->label_offsets[l] == (mp_uint_t)-1);
  438. emit->label_offsets[l] = emit->bytecode_offset;
  439. } else {
  440. // ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
  441. //printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
  442. assert(emit->label_offsets[l] == emit->bytecode_offset);
  443. }
  444. }
  445. void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
  446. emit_bc_pre(emit, -1);
  447. emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
  448. }
  449. void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
  450. emit_bc_pre(emit, 1);
  451. emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
  452. }
  453. void mp_emit_bc_import_star(emit_t *emit) {
  454. emit_bc_pre(emit, -1);
  455. emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
  456. }
  457. void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
  458. emit_bc_pre(emit, 1);
  459. switch (tok) {
  460. case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
  461. case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
  462. case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
  463. default:
  464. assert(tok == MP_TOKEN_ELLIPSIS);
  465. emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
  466. break;
  467. }
  468. }
  469. void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
  470. emit_bc_pre(emit, 1);
  471. if (-16 <= arg && arg <= 47) {
  472. emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
  473. } else {
  474. emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
  475. }
  476. }
  477. void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
  478. emit_bc_pre(emit, 1);
  479. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qst);
  480. }
  481. void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
  482. emit_bc_pre(emit, 1);
  483. emit_write_bytecode_byte_obj(emit, MP_BC_LOAD_CONST_OBJ, obj);
  484. }
  485. void mp_emit_bc_load_null(emit_t *emit) {
  486. emit_bc_pre(emit, 1);
  487. emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
  488. }
  489. void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
  490. (void)qst;
  491. emit_bc_pre(emit, 1);
  492. if (local_num <= 15) {
  493. emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
  494. } else {
  495. emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
  496. }
  497. }
  498. void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
  499. (void)qst;
  500. emit_bc_pre(emit, 1);
  501. emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
  502. }
  503. void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
  504. (void)qst;
  505. emit_bc_pre(emit, 1);
  506. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
  507. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  508. emit_write_bytecode_byte(emit, 0);
  509. }
  510. }
  511. void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
  512. (void)qst;
  513. emit_bc_pre(emit, 1);
  514. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
  515. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  516. emit_write_bytecode_byte(emit, 0);
  517. }
  518. }
  519. void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
  520. emit_bc_pre(emit, 0);
  521. emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
  522. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  523. emit_write_bytecode_byte(emit, 0);
  524. }
  525. }
  526. void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super) {
  527. emit_bc_pre(emit, 1 - 2 * is_super);
  528. emit_write_bytecode_byte_qstr(emit, is_super ? MP_BC_LOAD_SUPER_METHOD : MP_BC_LOAD_METHOD, qst);
  529. }
  530. void mp_emit_bc_load_build_class(emit_t *emit) {
  531. emit_bc_pre(emit, 1);
  532. emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
  533. }
  534. void mp_emit_bc_load_subscr(emit_t *emit) {
  535. emit_bc_pre(emit, -1);
  536. emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
  537. }
  538. void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
  539. (void)qst;
  540. emit_bc_pre(emit, -1);
  541. if (local_num <= 15) {
  542. emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
  543. } else {
  544. emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
  545. }
  546. }
  547. void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
  548. (void)qst;
  549. emit_bc_pre(emit, -1);
  550. emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
  551. }
  552. void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
  553. emit_bc_pre(emit, -1);
  554. emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
  555. }
  556. void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
  557. emit_bc_pre(emit, -1);
  558. emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
  559. }
  560. void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
  561. emit_bc_pre(emit, -2);
  562. emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
  563. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
  564. emit_write_bytecode_byte(emit, 0);
  565. }
  566. }
  567. void mp_emit_bc_store_subscr(emit_t *emit) {
  568. emit_bc_pre(emit, -3);
  569. emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
  570. }
  571. void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
  572. (void)qst;
  573. emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
  574. }
  575. void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
  576. (void)qst;
  577. emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
  578. }
  579. void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
  580. emit_bc_pre(emit, 0);
  581. emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
  582. }
  583. void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
  584. emit_bc_pre(emit, 0);
  585. emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
  586. }
  587. void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
  588. mp_emit_bc_load_null(emit);
  589. mp_emit_bc_rot_two(emit);
  590. mp_emit_bc_store_attr(emit, qst);
  591. }
  592. void mp_emit_bc_delete_subscr(emit_t *emit) {
  593. mp_emit_bc_load_null(emit);
  594. mp_emit_bc_rot_three(emit);
  595. mp_emit_bc_store_subscr(emit);
  596. }
  597. void mp_emit_bc_dup_top(emit_t *emit) {
  598. emit_bc_pre(emit, 1);
  599. emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
  600. }
  601. void mp_emit_bc_dup_top_two(emit_t *emit) {
  602. emit_bc_pre(emit, 2);
  603. emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
  604. }
  605. void mp_emit_bc_pop_top(emit_t *emit) {
  606. emit_bc_pre(emit, -1);
  607. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  608. }
  609. void mp_emit_bc_rot_two(emit_t *emit) {
  610. emit_bc_pre(emit, 0);
  611. emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
  612. }
  613. void mp_emit_bc_rot_three(emit_t *emit) {
  614. emit_bc_pre(emit, 0);
  615. emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
  616. }
  617. void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
  618. emit_bc_pre(emit, 0);
  619. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
  620. }
  621. void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
  622. emit_bc_pre(emit, -1);
  623. if (cond) {
  624. emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
  625. } else {
  626. emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
  627. }
  628. }
  629. void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
  630. emit_bc_pre(emit, -1);
  631. if (cond) {
  632. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
  633. } else {
  634. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
  635. }
  636. }
  637. void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
  638. if (except_depth == 0) {
  639. emit_bc_pre(emit, 0);
  640. if (label & MP_EMIT_BREAK_FROM_FOR) {
  641. // need to pop the iterator if we are breaking out of a for loop
  642. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  643. // also pop the iter_buf
  644. for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
  645. emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
  646. }
  647. }
  648. emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
  649. } else {
  650. emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
  651. emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
  652. }
  653. }
  654. void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
  655. // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
  656. // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
  657. emit_bc_pre(emit, 2);
  658. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
  659. }
  660. void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
  661. mp_emit_bc_pop_block(emit);
  662. mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
  663. mp_emit_bc_label_assign(emit, label);
  664. emit_bc_pre(emit, 2); // ensure we have enough stack space to call the __exit__ method
  665. emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
  666. emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_with
  667. }
  668. void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
  669. emit_bc_pre(emit, 0);
  670. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
  671. }
  672. void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
  673. emit_bc_pre(emit, 0);
  674. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
  675. }
  676. void mp_emit_bc_end_finally(emit_t *emit) {
  677. emit_bc_pre(emit, -1);
  678. emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
  679. }
  680. void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
  681. emit_bc_pre(emit, use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0);
  682. emit_write_bytecode_byte(emit, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
  683. }
  684. void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
  685. emit_bc_pre(emit, 1);
  686. emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
  687. }
  688. void mp_emit_bc_for_iter_end(emit_t *emit) {
  689. emit_bc_pre(emit, -MP_OBJ_ITER_BUF_NSLOTS);
  690. }
  691. void mp_emit_bc_pop_block(emit_t *emit) {
  692. emit_bc_pre(emit, 0);
  693. emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
  694. }
  695. void mp_emit_bc_pop_except(emit_t *emit) {
  696. emit_bc_pre(emit, 0);
  697. emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
  698. }
  699. void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
  700. emit_bc_pre(emit, 0);
  701. emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + op);
  702. }
  703. void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
  704. bool invert = false;
  705. if (op == MP_BINARY_OP_NOT_IN) {
  706. invert = true;
  707. op = MP_BINARY_OP_IN;
  708. } else if (op == MP_BINARY_OP_IS_NOT) {
  709. invert = true;
  710. op = MP_BINARY_OP_IS;
  711. }
  712. emit_bc_pre(emit, -1);
  713. emit_write_bytecode_byte(emit, MP_BC_BINARY_OP_MULTI + op);
  714. if (invert) {
  715. emit_bc_pre(emit, 0);
  716. emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
  717. }
  718. }
  719. void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
  720. emit_bc_pre(emit, 1 - n_args);
  721. emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
  722. }
  723. void mp_emit_bc_build_list(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_LIST, n_args);
  726. }
  727. void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
  728. emit_bc_pre(emit, 1);
  729. emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
  730. }
  731. void mp_emit_bc_store_map(emit_t *emit) {
  732. emit_bc_pre(emit, -2);
  733. emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
  734. }
  735. #if MICROPY_PY_BUILTINS_SET
  736. void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
  737. emit_bc_pre(emit, 1 - n_args);
  738. emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
  739. }
  740. #endif
  741. #if MICROPY_PY_BUILTINS_SLICE
  742. void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
  743. emit_bc_pre(emit, 1 - n_args);
  744. emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
  745. }
  746. #endif
  747. void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
  748. int t;
  749. int n;
  750. if (kind == SCOPE_LIST_COMP) {
  751. n = 0;
  752. t = 0;
  753. } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
  754. n = 1;
  755. t = 1;
  756. } else if (MICROPY_PY_BUILTINS_SET) {
  757. n = 0;
  758. t = 2;
  759. }
  760. emit_bc_pre(emit, -1 - n);
  761. // the lower 2 bits of the opcode argument indicate the collection type
  762. emit_write_bytecode_byte_uint(emit, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
  763. }
  764. void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
  765. emit_bc_pre(emit, -1 + n_args);
  766. emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
  767. }
  768. void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
  769. emit_bc_pre(emit, -1 + n_left + n_right + 1);
  770. emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
  771. }
  772. void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
  773. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  774. emit_bc_pre(emit, 1);
  775. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
  776. } else {
  777. emit_bc_pre(emit, -1);
  778. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
  779. }
  780. }
  781. 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) {
  782. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  783. emit_bc_pre(emit, -n_closed_over + 1);
  784. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
  785. emit_write_bytecode_byte(emit, n_closed_over);
  786. } else {
  787. assert(n_closed_over <= 255);
  788. emit_bc_pre(emit, -2 - (mp_int_t)n_closed_over + 1);
  789. emit_write_bytecode_byte_raw_code(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
  790. emit_write_bytecode_byte(emit, n_closed_over);
  791. }
  792. }
  793. 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) {
  794. if (star_flags) {
  795. emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
  796. emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
  797. } else {
  798. emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword);
  799. emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
  800. }
  801. }
  802. void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  803. emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
  804. }
  805. void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  806. emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
  807. }
  808. void mp_emit_bc_return_value(emit_t *emit) {
  809. emit_bc_pre(emit, -1);
  810. emit->last_emit_was_return_value = true;
  811. emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
  812. }
  813. void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
  814. assert(n_args <= 2);
  815. emit_bc_pre(emit, -n_args);
  816. emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
  817. }
  818. void mp_emit_bc_yield_value(emit_t *emit) {
  819. emit_bc_pre(emit, 0);
  820. emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
  821. emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
  822. }
  823. void mp_emit_bc_yield_from(emit_t *emit) {
  824. emit_bc_pre(emit, -1);
  825. emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
  826. emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
  827. }
  828. void mp_emit_bc_start_except_handler(emit_t *emit) {
  829. mp_emit_bc_adjust_stack_size(emit, 4); // stack adjust for the exception instance, +3 for possible UNWIND_JUMP state
  830. }
  831. void mp_emit_bc_end_except_handler(emit_t *emit) {
  832. mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
  833. }
  834. #if MICROPY_EMIT_NATIVE
  835. const emit_method_table_t emit_bc_method_table = {
  836. NULL, // set_native_type is never called when emitting bytecode
  837. mp_emit_bc_start_pass,
  838. mp_emit_bc_end_pass,
  839. mp_emit_bc_last_emit_was_return_value,
  840. mp_emit_bc_adjust_stack_size,
  841. mp_emit_bc_set_source_line,
  842. {
  843. mp_emit_bc_load_fast,
  844. mp_emit_bc_load_deref,
  845. mp_emit_bc_load_name,
  846. mp_emit_bc_load_global,
  847. },
  848. {
  849. mp_emit_bc_store_fast,
  850. mp_emit_bc_store_deref,
  851. mp_emit_bc_store_name,
  852. mp_emit_bc_store_global,
  853. },
  854. {
  855. mp_emit_bc_delete_fast,
  856. mp_emit_bc_delete_deref,
  857. mp_emit_bc_delete_name,
  858. mp_emit_bc_delete_global,
  859. },
  860. mp_emit_bc_label_assign,
  861. mp_emit_bc_import_name,
  862. mp_emit_bc_import_from,
  863. mp_emit_bc_import_star,
  864. mp_emit_bc_load_const_tok,
  865. mp_emit_bc_load_const_small_int,
  866. mp_emit_bc_load_const_str,
  867. mp_emit_bc_load_const_obj,
  868. mp_emit_bc_load_null,
  869. mp_emit_bc_load_attr,
  870. mp_emit_bc_load_method,
  871. mp_emit_bc_load_build_class,
  872. mp_emit_bc_load_subscr,
  873. mp_emit_bc_store_attr,
  874. mp_emit_bc_store_subscr,
  875. mp_emit_bc_delete_attr,
  876. mp_emit_bc_delete_subscr,
  877. mp_emit_bc_dup_top,
  878. mp_emit_bc_dup_top_two,
  879. mp_emit_bc_pop_top,
  880. mp_emit_bc_rot_two,
  881. mp_emit_bc_rot_three,
  882. mp_emit_bc_jump,
  883. mp_emit_bc_pop_jump_if,
  884. mp_emit_bc_jump_if_or_pop,
  885. mp_emit_bc_unwind_jump,
  886. mp_emit_bc_unwind_jump,
  887. mp_emit_bc_setup_with,
  888. mp_emit_bc_with_cleanup,
  889. mp_emit_bc_setup_except,
  890. mp_emit_bc_setup_finally,
  891. mp_emit_bc_end_finally,
  892. mp_emit_bc_get_iter,
  893. mp_emit_bc_for_iter,
  894. mp_emit_bc_for_iter_end,
  895. mp_emit_bc_pop_block,
  896. mp_emit_bc_pop_except,
  897. mp_emit_bc_unary_op,
  898. mp_emit_bc_binary_op,
  899. mp_emit_bc_build_tuple,
  900. mp_emit_bc_build_list,
  901. mp_emit_bc_build_map,
  902. mp_emit_bc_store_map,
  903. #if MICROPY_PY_BUILTINS_SET
  904. mp_emit_bc_build_set,
  905. #endif
  906. #if MICROPY_PY_BUILTINS_SLICE
  907. mp_emit_bc_build_slice,
  908. #endif
  909. mp_emit_bc_store_comp,
  910. mp_emit_bc_unpack_sequence,
  911. mp_emit_bc_unpack_ex,
  912. mp_emit_bc_make_function,
  913. mp_emit_bc_make_closure,
  914. mp_emit_bc_call_function,
  915. mp_emit_bc_call_method,
  916. mp_emit_bc_return_value,
  917. mp_emit_bc_raise_varargs,
  918. mp_emit_bc_yield_value,
  919. mp_emit_bc_yield_from,
  920. mp_emit_bc_start_except_handler,
  921. mp_emit_bc_end_except_handler,
  922. };
  923. #else
  924. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
  925. mp_emit_bc_load_fast,
  926. mp_emit_bc_load_deref,
  927. mp_emit_bc_load_name,
  928. mp_emit_bc_load_global,
  929. };
  930. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
  931. mp_emit_bc_store_fast,
  932. mp_emit_bc_store_deref,
  933. mp_emit_bc_store_name,
  934. mp_emit_bc_store_global,
  935. };
  936. const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
  937. mp_emit_bc_delete_fast,
  938. mp_emit_bc_delete_deref,
  939. mp_emit_bc_delete_name,
  940. mp_emit_bc_delete_global,
  941. };
  942. #endif
  943. #endif //MICROPY_ENABLE_COMPILER