bc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #ifndef MICROPY_INCLUDED_PY_BC_H
  28. #define MICROPY_INCLUDED_PY_BC_H
  29. #include "py/runtime.h"
  30. // bytecode layout:
  31. //
  32. // func signature : var uint
  33. // contains six values interleaved bit-wise as: xSSSSEAA [xFSSKAED repeated]
  34. // x = extension another byte follows
  35. // S = n_state - 1 number of entries in Python value stack
  36. // E = n_exc_stack number of entries in exception stack
  37. // F = scope_flags four bits of flags, MP_SCOPE_FLAG_xxx
  38. // A = n_pos_args number of arguments this function takes
  39. // K = n_kwonly_args number of keyword-only arguments this function takes
  40. // D = n_def_pos_args number of default positional arguments
  41. //
  42. // prelude size : var uint
  43. // contains two values interleaved bit-wise as: xIIIIIIC repeated
  44. // x = extension another byte follows
  45. // I = n_info number of bytes in source info section (always > 0)
  46. // C = n_cells number of bytes/cells in closure section
  47. //
  48. // source info section:
  49. // simple_name : var qstr always exists
  50. // argname0 : var qstr
  51. // ... : var qstr
  52. // argnameN : var qstr N = num_pos_args + num_kwonly_args - 1
  53. // <line number info>
  54. //
  55. // closure section:
  56. // local_num0 : byte
  57. // ... : byte
  58. // local_numN : byte N = n_cells-1
  59. //
  60. // <bytecode>
  61. //
  62. //
  63. // constant table layout:
  64. //
  65. // const0 : obj
  66. // constN : obj
  67. #define MP_ENCODE_UINT_MAX_BYTES ((MP_BYTES_PER_OBJ_WORD * 8 + 6) / 7)
  68. #define MP_BC_PRELUDE_SIG_ENCODE(S, E, scope, out_byte, out_env) \
  69. do { \
  70. /*// Get values to store in prelude */ \
  71. size_t F = scope->scope_flags & MP_SCOPE_FLAG_ALL_SIG; \
  72. size_t A = scope->num_pos_args; \
  73. size_t K = scope->num_kwonly_args; \
  74. size_t D = scope->num_def_pos_args; \
  75. \
  76. /* Adjust S to shrink range, to compress better */ \
  77. S -= 1; \
  78. \
  79. /* Encode prelude */ \
  80. /* xSSSSEAA */ \
  81. uint8_t z = (S & 0xf) << 3 | (E & 1) << 2 | (A & 3); \
  82. S >>= 4; \
  83. E >>= 1; \
  84. A >>= 2; \
  85. while (S | E | F | A | K | D) { \
  86. out_byte(out_env, 0x80 | z); \
  87. /* xFSSKAED */ \
  88. z = (F & 1) << 6 | (S & 3) << 4 | (K & 1) << 3 \
  89. | (A & 1) << 2 | (E & 1) << 1 | (D & 1); \
  90. S >>= 2; \
  91. E >>= 1; \
  92. F >>= 1; \
  93. A >>= 1; \
  94. K >>= 1; \
  95. D >>= 1; \
  96. } \
  97. out_byte(out_env, z); \
  98. } while (0)
  99. #define MP_BC_PRELUDE_SIG_DECODE_INTO(ip, S, E, F, A, K, D) \
  100. do { \
  101. uint8_t z = *(ip)++; \
  102. /* xSSSSEAA */ \
  103. S = (z >> 3) & 0xf; \
  104. E = (z >> 2) & 0x1; \
  105. F = 0; \
  106. A = z & 0x3; \
  107. K = 0; \
  108. D = 0; \
  109. for (unsigned n = 0; z & 0x80; ++n) { \
  110. z = *(ip)++; \
  111. /* xFSSKAED */ \
  112. S |= (z & 0x30) << (2 * n); \
  113. E |= (z & 0x02) << n; \
  114. F |= ((z & 0x40) >> 6) << n; \
  115. A |= (z & 0x4) << n; \
  116. K |= ((z & 0x08) >> 3) << n; \
  117. D |= (z & 0x1) << n; \
  118. } \
  119. S += 1; \
  120. } while (0)
  121. #define MP_BC_PRELUDE_SIG_DECODE(ip) \
  122. size_t n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args; \
  123. MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args); \
  124. (void)n_state; (void)n_exc_stack; (void)scope_flags; \
  125. (void)n_pos_args; (void)n_kwonly_args; (void)n_def_pos_args
  126. #define MP_BC_PRELUDE_SIZE_ENCODE(I, C, out_byte, out_env) \
  127. do { \
  128. /* Encode bit-wise as: xIIIIIIC */ \
  129. uint8_t z = 0; \
  130. do { \
  131. z = (I & 0x3f) << 1 | (C & 1); \
  132. C >>= 1; \
  133. I >>= 6; \
  134. if (C | I) { \
  135. z |= 0x80; \
  136. } \
  137. out_byte(out_env, z); \
  138. } while (C | I); \
  139. } while (0)
  140. #define MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, I, C) \
  141. do { \
  142. uint8_t z; \
  143. C = 0; \
  144. I = 0; \
  145. for (unsigned n = 0;; ++n) { \
  146. z = *(ip)++; \
  147. /* xIIIIIIC */ \
  148. C |= (z & 1) << n; \
  149. I |= ((z & 0x7e) >> 1) << (6 * n); \
  150. if (!(z & 0x80)) { \
  151. break; \
  152. } \
  153. } \
  154. } while (0)
  155. #define MP_BC_PRELUDE_SIZE_DECODE(ip) \
  156. size_t n_info, n_cell; \
  157. MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, n_info, n_cell); \
  158. (void)n_info; (void)n_cell
  159. // Sentinel value for mp_code_state_t.exc_sp_idx
  160. #define MP_CODE_STATE_EXC_SP_IDX_SENTINEL ((uint16_t)-1)
  161. // To convert mp_code_state_t.exc_sp_idx to/from a pointer to mp_exc_stack_t
  162. #define MP_CODE_STATE_EXC_SP_IDX_FROM_PTR(exc_stack, exc_sp) ((exc_sp) + 1 - (exc_stack))
  163. #define MP_CODE_STATE_EXC_SP_IDX_TO_PTR(exc_stack, exc_sp_idx) ((exc_stack) + (exc_sp_idx) - 1)
  164. typedef struct _mp_bytecode_prelude_t {
  165. uint n_state;
  166. uint n_exc_stack;
  167. uint scope_flags;
  168. uint n_pos_args;
  169. uint n_kwonly_args;
  170. uint n_def_pos_args;
  171. qstr qstr_block_name_idx;
  172. const byte *line_info;
  173. const byte *line_info_top;
  174. const byte *opcodes;
  175. } mp_bytecode_prelude_t;
  176. // Exception stack entry
  177. typedef struct _mp_exc_stack_t {
  178. const byte *handler;
  179. // bit 0 is currently unused
  180. // bit 1 is whether the opcode was SETUP_WITH or SETUP_FINALLY
  181. mp_obj_t *val_sp;
  182. // Saved exception
  183. mp_obj_base_t *prev_exc;
  184. } mp_exc_stack_t;
  185. // Constants associated with a module, to interface bytecode with runtime.
  186. typedef struct _mp_module_constants_t {
  187. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  188. qstr_short_t *qstr_table;
  189. #else
  190. qstr source_file;
  191. #endif
  192. mp_obj_t *obj_table;
  193. } mp_module_constants_t;
  194. // State associated with a module.
  195. typedef struct _mp_module_context_t {
  196. mp_obj_module_t module;
  197. mp_module_constants_t constants;
  198. } mp_module_context_t;
  199. // Outer level struct defining a compiled module.
  200. typedef struct _mp_compiled_module_t {
  201. mp_module_context_t *context;
  202. const struct _mp_raw_code_t *rc;
  203. #if MICROPY_PERSISTENT_CODE_SAVE
  204. bool has_native;
  205. size_t n_qstr;
  206. size_t n_obj;
  207. #endif
  208. } mp_compiled_module_t;
  209. // Outer level struct defining a frozen module.
  210. typedef struct _mp_frozen_module_t {
  211. const mp_module_constants_t constants;
  212. const void *proto_fun;
  213. } mp_frozen_module_t;
  214. // State for an executing function.
  215. typedef struct _mp_code_state_t {
  216. // The fun_bc entry points to the underlying function object that is being executed.
  217. // It is needed to access the start of bytecode and the const_table.
  218. // It is also needed to prevent the GC from reclaiming the bytecode during execution,
  219. // because the ip pointer below will always point to the interior of the bytecode.
  220. struct _mp_obj_fun_bc_t *fun_bc;
  221. const byte *ip;
  222. mp_obj_t *sp;
  223. uint16_t n_state;
  224. uint16_t exc_sp_idx;
  225. mp_obj_dict_t *old_globals;
  226. #if MICROPY_STACKLESS
  227. struct _mp_code_state_t *prev;
  228. #endif
  229. #if MICROPY_PY_SYS_SETTRACE
  230. struct _mp_code_state_t *prev_state;
  231. struct _mp_obj_frame_t *frame;
  232. #endif
  233. // Variable-length
  234. mp_obj_t state[0];
  235. // Variable-length, never accessed by name, only as (void*)(state + n_state)
  236. // mp_exc_stack_t exc_state[0];
  237. } mp_code_state_t;
  238. // State for an executing native function (based on mp_code_state_t).
  239. typedef struct _mp_code_state_native_t {
  240. struct _mp_obj_fun_bc_t *fun_bc;
  241. const byte *ip;
  242. mp_obj_t *sp;
  243. uint16_t n_state;
  244. uint16_t exc_sp_idx;
  245. mp_obj_dict_t *old_globals;
  246. mp_obj_t state[0];
  247. } mp_code_state_native_t;
  248. // Allocator may return NULL, in which case data is not stored (can be used to compute size).
  249. typedef uint8_t *(*mp_encode_uint_allocator_t)(void *env, size_t nbytes);
  250. void mp_encode_uint(void *env, mp_encode_uint_allocator_t allocator, mp_uint_t val);
  251. mp_uint_t mp_decode_uint(const byte **ptr);
  252. mp_uint_t mp_decode_uint_value(const byte *ptr);
  253. const byte *mp_decode_uint_skip(const byte *ptr);
  254. mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state,
  255. #ifndef __cplusplus
  256. volatile
  257. #endif
  258. mp_obj_t inject_exc);
  259. mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args);
  260. void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
  261. void mp_setup_code_state_native(mp_code_state_native_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
  262. void mp_bytecode_print(const mp_print_t *print, const struct _mp_raw_code_t *rc, size_t fun_data_len, const mp_module_constants_t *cm);
  263. void mp_bytecode_print2(const mp_print_t *print, const byte *ip, size_t len, struct _mp_raw_code_t *const *child_table, const mp_module_constants_t *cm);
  264. const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start, const byte *ip, struct _mp_raw_code_t *const *child_table, const mp_module_constants_t *cm);
  265. #define mp_bytecode_print_inst(print, code, x_table) mp_bytecode_print2(print, code, 1, x_table)
  266. // Helper macros to access pointer with least significant bits holding flags
  267. #define MP_TAGPTR_PTR(x) ((void *)((uintptr_t)(x) & ~((uintptr_t)3)))
  268. #define MP_TAGPTR_TAG0(x) ((uintptr_t)(x) & 1)
  269. #define MP_TAGPTR_TAG1(x) ((uintptr_t)(x) & 2)
  270. #define MP_TAGPTR_MAKE(ptr, tag) ((void *)((uintptr_t)(ptr) | (tag)))
  271. static inline void mp_module_context_alloc_tables(mp_module_context_t *context, size_t n_qstr, size_t n_obj) {
  272. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  273. size_t nq = (n_qstr * sizeof(qstr_short_t) + sizeof(mp_uint_t) - 1) / sizeof(mp_uint_t);
  274. size_t no = n_obj;
  275. mp_uint_t *mem = m_new(mp_uint_t, nq + no);
  276. context->constants.qstr_table = (qstr_short_t *)mem;
  277. context->constants.obj_table = (mp_obj_t *)(mem + nq);
  278. #else
  279. if (n_obj == 0) {
  280. context->constants.obj_table = NULL;
  281. } else {
  282. context->constants.obj_table = m_new(mp_obj_t, n_obj);
  283. }
  284. #endif
  285. }
  286. static inline size_t mp_bytecode_get_source_line(const byte *line_info, const byte *line_info_top, size_t bc_offset) {
  287. size_t source_line = 1;
  288. while (line_info < line_info_top) {
  289. size_t c = *line_info;
  290. size_t b, l;
  291. if ((c & 0x80) == 0) {
  292. // 0b0LLBBBBB encoding
  293. b = c & 0x1f;
  294. l = c >> 5;
  295. line_info += 1;
  296. } else {
  297. // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
  298. b = c & 0xf;
  299. l = ((c << 4) & 0x700) | line_info[1];
  300. line_info += 2;
  301. }
  302. if (bc_offset >= b) {
  303. bc_offset -= b;
  304. source_line += l;
  305. } else {
  306. // found source line corresponding to bytecode offset
  307. break;
  308. }
  309. }
  310. return source_line;
  311. }
  312. #endif // MICROPY_INCLUDED_PY_BC_H