emitbc.c 33 KB

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