persistentcode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 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 <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "py/reader.h"
  31. #include "py/emitglue.h"
  32. #include "py/persistentcode.h"
  33. #include "py/bc.h"
  34. #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
  35. #include "py/smallint.h"
  36. // The current version of .mpy files
  37. #define MPY_VERSION (3)
  38. // The feature flags byte encodes the compile-time config options that
  39. // affect the generate bytecode.
  40. #define MPY_FEATURE_FLAGS ( \
  41. ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \
  42. | ((MICROPY_PY_BUILTINS_STR_UNICODE) << 1) \
  43. )
  44. // This is a version of the flags that can be configured at runtime.
  45. #define MPY_FEATURE_FLAGS_DYNAMIC ( \
  46. ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) << 0) \
  47. | ((MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) << 1) \
  48. )
  49. #if MICROPY_PERSISTENT_CODE_LOAD || (MICROPY_PERSISTENT_CODE_SAVE && !MICROPY_DYNAMIC_COMPILER)
  50. // The bytecode will depend on the number of bits in a small-int, and
  51. // this function computes that (could make it a fixed constant, but it
  52. // would need to be defined in mpconfigport.h).
  53. STATIC int mp_small_int_bits(void) {
  54. mp_int_t i = MP_SMALL_INT_MAX;
  55. int n = 1;
  56. while (i != 0) {
  57. i >>= 1;
  58. ++n;
  59. }
  60. return n;
  61. }
  62. #endif
  63. typedef struct _bytecode_prelude_t {
  64. uint n_state;
  65. uint n_exc_stack;
  66. uint scope_flags;
  67. uint n_pos_args;
  68. uint n_kwonly_args;
  69. uint n_def_pos_args;
  70. uint code_info_size;
  71. } bytecode_prelude_t;
  72. // ip will point to start of opcodes
  73. // ip2 will point to simple_name, source_file qstrs
  74. STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
  75. prelude->n_state = mp_decode_uint(ip);
  76. prelude->n_exc_stack = mp_decode_uint(ip);
  77. prelude->scope_flags = *(*ip)++;
  78. prelude->n_pos_args = *(*ip)++;
  79. prelude->n_kwonly_args = *(*ip)++;
  80. prelude->n_def_pos_args = *(*ip)++;
  81. *ip2 = *ip;
  82. prelude->code_info_size = mp_decode_uint(ip2);
  83. *ip += prelude->code_info_size;
  84. while (*(*ip)++ != 255) {
  85. }
  86. }
  87. #endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
  88. #if MICROPY_PERSISTENT_CODE_LOAD
  89. #include "py/parsenum.h"
  90. STATIC int read_byte(mp_reader_t *reader) {
  91. return reader->readbyte(reader->data);
  92. }
  93. STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
  94. while (len-- > 0) {
  95. *buf++ = reader->readbyte(reader->data);
  96. }
  97. }
  98. STATIC size_t read_uint(mp_reader_t *reader) {
  99. size_t unum = 0;
  100. for (;;) {
  101. byte b = reader->readbyte(reader->data);
  102. unum = (unum << 7) | (b & 0x7f);
  103. if ((b & 0x80) == 0) {
  104. break;
  105. }
  106. }
  107. return unum;
  108. }
  109. STATIC qstr load_qstr(mp_reader_t *reader) {
  110. size_t len = read_uint(reader);
  111. char *str = m_new(char, len);
  112. read_bytes(reader, (byte*)str, len);
  113. qstr qst = qstr_from_strn(str, len);
  114. m_del(char, str, len);
  115. return qst;
  116. }
  117. STATIC mp_obj_t load_obj(mp_reader_t *reader) {
  118. byte obj_type = read_byte(reader);
  119. if (obj_type == 'e') {
  120. return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj);
  121. } else {
  122. size_t len = read_uint(reader);
  123. vstr_t vstr;
  124. vstr_init_len(&vstr, len);
  125. read_bytes(reader, (byte*)vstr.buf, len);
  126. if (obj_type == 's' || obj_type == 'b') {
  127. return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
  128. } else if (obj_type == 'i') {
  129. return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
  130. } else {
  131. assert(obj_type == 'f' || obj_type == 'c');
  132. return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
  133. }
  134. }
  135. }
  136. STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
  137. while (ip < ip_top) {
  138. size_t sz;
  139. uint f = mp_opcode_format(ip, &sz);
  140. if (f == MP_OPCODE_QSTR) {
  141. qstr qst = load_qstr(reader);
  142. ip[1] = qst;
  143. ip[2] = qst >> 8;
  144. }
  145. ip += sz;
  146. }
  147. }
  148. STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
  149. // load bytecode
  150. size_t bc_len = read_uint(reader);
  151. byte *bytecode = m_new(byte, bc_len);
  152. read_bytes(reader, bytecode, bc_len);
  153. // extract prelude
  154. const byte *ip = bytecode;
  155. const byte *ip2;
  156. bytecode_prelude_t prelude;
  157. extract_prelude(&ip, &ip2, &prelude);
  158. // load qstrs and link global qstr ids into bytecode
  159. qstr simple_name = load_qstr(reader);
  160. qstr source_file = load_qstr(reader);
  161. ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8;
  162. ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8;
  163. load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len);
  164. // load constant table
  165. size_t n_obj = read_uint(reader);
  166. size_t n_raw_code = read_uint(reader);
  167. mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code);
  168. mp_uint_t *ct = const_table;
  169. for (size_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
  170. *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader));
  171. }
  172. for (size_t i = 0; i < n_obj; ++i) {
  173. *ct++ = (mp_uint_t)load_obj(reader);
  174. }
  175. for (size_t i = 0; i < n_raw_code; ++i) {
  176. *ct++ = (mp_uint_t)(uintptr_t)load_raw_code(reader);
  177. }
  178. // create raw_code and return it
  179. mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
  180. mp_emit_glue_assign_bytecode(rc, bytecode,
  181. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  182. bc_len,
  183. #endif
  184. const_table,
  185. #if MICROPY_PERSISTENT_CODE_SAVE
  186. n_obj, n_raw_code,
  187. #endif
  188. prelude.scope_flags);
  189. return rc;
  190. }
  191. mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
  192. byte header[4];
  193. read_bytes(reader, header, sizeof(header));
  194. if (header[0] != 'M'
  195. || header[1] != MPY_VERSION
  196. || header[2] != MPY_FEATURE_FLAGS
  197. || header[3] > mp_small_int_bits()) {
  198. mp_raise_ValueError("incompatible .mpy file");
  199. }
  200. mp_raw_code_t *rc = load_raw_code(reader);
  201. reader->close(reader->data);
  202. return rc;
  203. }
  204. mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) {
  205. mp_reader_t reader;
  206. mp_reader_new_mem(&reader, buf, len, 0);
  207. return mp_raw_code_load(&reader);
  208. }
  209. mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
  210. mp_reader_t reader;
  211. mp_reader_new_file(&reader, filename);
  212. return mp_raw_code_load(&reader);
  213. }
  214. #endif // MICROPY_PERSISTENT_CODE_LOAD
  215. #if MICROPY_PERSISTENT_CODE_SAVE
  216. #include "py/objstr.h"
  217. STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
  218. print->print_strn(print->data, (const char*)data, len);
  219. }
  220. #define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
  221. STATIC void mp_print_uint(mp_print_t *print, size_t n) {
  222. byte buf[BYTES_FOR_INT];
  223. byte *p = buf + sizeof(buf);
  224. *--p = n & 0x7f;
  225. n >>= 7;
  226. for (; n != 0; n >>= 7) {
  227. *--p = 0x80 | (n & 0x7f);
  228. }
  229. print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p);
  230. }
  231. STATIC void save_qstr(mp_print_t *print, qstr qst) {
  232. size_t len;
  233. const byte *str = qstr_data(qst, &len);
  234. mp_print_uint(print, len);
  235. mp_print_bytes(print, str, len);
  236. }
  237. STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
  238. if (MP_OBJ_IS_STR_OR_BYTES(o)) {
  239. byte obj_type;
  240. if (MP_OBJ_IS_STR(o)) {
  241. obj_type = 's';
  242. } else {
  243. obj_type = 'b';
  244. }
  245. mp_uint_t len;
  246. const char *str = mp_obj_str_get_data(o, &len);
  247. mp_print_bytes(print, &obj_type, 1);
  248. mp_print_uint(print, len);
  249. mp_print_bytes(print, (const byte*)str, len);
  250. } else if (MP_OBJ_TO_PTR(o) == &mp_const_ellipsis_obj) {
  251. byte obj_type = 'e';
  252. mp_print_bytes(print, &obj_type, 1);
  253. } else {
  254. // we save numbers using a simplistic text representation
  255. // TODO could be improved
  256. byte obj_type;
  257. if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
  258. obj_type = 'i';
  259. #if MICROPY_PY_BUILTINS_COMPLEX
  260. } else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
  261. obj_type = 'c';
  262. #endif
  263. } else {
  264. assert(mp_obj_is_float(o));
  265. obj_type = 'f';
  266. }
  267. vstr_t vstr;
  268. mp_print_t pr;
  269. vstr_init_print(&vstr, 10, &pr);
  270. mp_obj_print_helper(&pr, o, PRINT_REPR);
  271. mp_print_bytes(print, &obj_type, 1);
  272. mp_print_uint(print, vstr.len);
  273. mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
  274. vstr_clear(&vstr);
  275. }
  276. }
  277. STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) {
  278. while (ip < ip_top) {
  279. size_t sz;
  280. uint f = mp_opcode_format(ip, &sz);
  281. if (f == MP_OPCODE_QSTR) {
  282. qstr qst = ip[1] | (ip[2] << 8);
  283. save_qstr(print, qst);
  284. }
  285. ip += sz;
  286. }
  287. }
  288. STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
  289. if (rc->kind != MP_CODE_BYTECODE) {
  290. mp_raise_ValueError("can only save bytecode");
  291. }
  292. // save bytecode
  293. mp_print_uint(print, rc->data.u_byte.bc_len);
  294. mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len);
  295. // extract prelude
  296. const byte *ip = rc->data.u_byte.bytecode;
  297. const byte *ip2;
  298. bytecode_prelude_t prelude;
  299. extract_prelude(&ip, &ip2, &prelude);
  300. // save qstrs
  301. save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name
  302. save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file
  303. save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len);
  304. // save constant table
  305. mp_print_uint(print, rc->data.u_byte.n_obj);
  306. mp_print_uint(print, rc->data.u_byte.n_raw_code);
  307. const mp_uint_t *const_table = rc->data.u_byte.const_table;
  308. for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
  309. mp_obj_t o = (mp_obj_t)*const_table++;
  310. save_qstr(print, MP_OBJ_QSTR_VALUE(o));
  311. }
  312. for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
  313. save_obj(print, (mp_obj_t)*const_table++);
  314. }
  315. for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
  316. save_raw_code(print, (mp_raw_code_t*)(uintptr_t)*const_table++);
  317. }
  318. }
  319. void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
  320. // header contains:
  321. // byte 'M'
  322. // byte version
  323. // byte feature flags
  324. // byte number of bits in a small int
  325. byte header[4] = {'M', MPY_VERSION, MPY_FEATURE_FLAGS_DYNAMIC,
  326. #if MICROPY_DYNAMIC_COMPILER
  327. mp_dynamic_compiler.small_int_bits,
  328. #else
  329. mp_small_int_bits(),
  330. #endif
  331. };
  332. mp_print_bytes(print, header, sizeof(header));
  333. save_raw_code(print, rc);
  334. }
  335. // here we define mp_raw_code_save_file depending on the port
  336. // TODO abstract this away properly
  337. #if defined(__i386__) || defined(__x86_64__) || defined(__unix__)
  338. #include <unistd.h>
  339. #include <sys/stat.h>
  340. #include <fcntl.h>
  341. STATIC void fd_print_strn(void *env, const char *str, size_t len) {
  342. int fd = (intptr_t)env;
  343. ssize_t ret = write(fd, str, len);
  344. (void)ret;
  345. }
  346. void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
  347. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  348. mp_print_t fd_print = {(void*)(intptr_t)fd, fd_print_strn};
  349. mp_raw_code_save(rc, &fd_print);
  350. close(fd);
  351. }
  352. #else
  353. #error mp_raw_code_save_file not implemented for this platform
  354. #endif
  355. #endif // MICROPY_PERSISTENT_CODE_SAVE