mpy-tool.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #!/usr/bin/env python3
  2. #
  3. # This file is part of the MicroPython project, http://micropython.org/
  4. #
  5. # The MIT License (MIT)
  6. #
  7. # Copyright (c) 2016 Damien P. George
  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. # Python 2/3 compatibility code
  27. from __future__ import print_function
  28. import platform
  29. if platform.python_version_tuple()[0] == '2':
  30. str_cons = lambda val, enc=None: val
  31. bytes_cons = lambda val, enc=None: bytearray(val)
  32. is_str_type = lambda o: type(o) is str
  33. is_bytes_type = lambda o: type(o) is bytearray
  34. is_int_type = lambda o: type(o) is int or type(o) is long
  35. else:
  36. str_cons = str
  37. bytes_cons = bytes
  38. is_str_type = lambda o: type(o) is str
  39. is_bytes_type = lambda o: type(o) is bytes
  40. is_int_type = lambda o: type(o) is int
  41. # end compatibility code
  42. import sys
  43. import struct
  44. from collections import namedtuple
  45. sys.path.append(sys.path[0] + '/../py')
  46. import makeqstrdata as qstrutil
  47. class FreezeError(Exception):
  48. def __init__(self, rawcode, msg):
  49. self.rawcode = rawcode
  50. self.msg = msg
  51. def __str__(self):
  52. return 'error while freezing %s: %s' % (self.rawcode.source_file, self.msg)
  53. class Config:
  54. MPY_VERSION = 3
  55. MICROPY_LONGINT_IMPL_NONE = 0
  56. MICROPY_LONGINT_IMPL_LONGLONG = 1
  57. MICROPY_LONGINT_IMPL_MPZ = 2
  58. config = Config()
  59. MP_OPCODE_BYTE = 0
  60. MP_OPCODE_QSTR = 1
  61. MP_OPCODE_VAR_UINT = 2
  62. MP_OPCODE_OFFSET = 3
  63. # extra bytes:
  64. MP_BC_MAKE_CLOSURE = 0x62
  65. MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
  66. MP_BC_RAISE_VARARGS = 0x5c
  67. # extra byte if caching enabled:
  68. MP_BC_LOAD_NAME = 0x1c
  69. MP_BC_LOAD_GLOBAL = 0x1d
  70. MP_BC_LOAD_ATTR = 0x1e
  71. MP_BC_STORE_ATTR = 0x26
  72. def make_opcode_format():
  73. def OC4(a, b, c, d):
  74. return a | (b << 2) | (c << 4) | (d << 6)
  75. U = 0
  76. B = 0
  77. Q = 1
  78. V = 2
  79. O = 3
  80. return bytes_cons((
  81. # this table is taken verbatim from py/bc.c
  82. OC4(U, U, U, U), # 0x00-0x03
  83. OC4(U, U, U, U), # 0x04-0x07
  84. OC4(U, U, U, U), # 0x08-0x0b
  85. OC4(U, U, U, U), # 0x0c-0x0f
  86. OC4(B, B, B, U), # 0x10-0x13
  87. OC4(V, U, Q, V), # 0x14-0x17
  88. OC4(B, V, V, Q), # 0x18-0x1b
  89. OC4(Q, Q, Q, Q), # 0x1c-0x1f
  90. OC4(B, B, V, V), # 0x20-0x23
  91. OC4(Q, Q, Q, B), # 0x24-0x27
  92. OC4(V, V, Q, Q), # 0x28-0x2b
  93. OC4(U, U, U, U), # 0x2c-0x2f
  94. OC4(B, B, B, B), # 0x30-0x33
  95. OC4(B, O, O, O), # 0x34-0x37
  96. OC4(O, O, U, U), # 0x38-0x3b
  97. OC4(U, O, B, O), # 0x3c-0x3f
  98. OC4(O, B, B, O), # 0x40-0x43
  99. OC4(B, B, O, B), # 0x44-0x47
  100. OC4(U, U, U, U), # 0x48-0x4b
  101. OC4(U, U, U, U), # 0x4c-0x4f
  102. OC4(V, V, U, V), # 0x50-0x53
  103. OC4(B, U, V, V), # 0x54-0x57
  104. OC4(V, V, V, B), # 0x58-0x5b
  105. OC4(B, B, B, U), # 0x5c-0x5f
  106. OC4(V, V, V, V), # 0x60-0x63
  107. OC4(V, V, V, V), # 0x64-0x67
  108. OC4(Q, Q, B, U), # 0x68-0x6b
  109. OC4(U, U, U, U), # 0x6c-0x6f
  110. OC4(B, B, B, B), # 0x70-0x73
  111. OC4(B, B, B, B), # 0x74-0x77
  112. OC4(B, B, B, B), # 0x78-0x7b
  113. OC4(B, B, B, B), # 0x7c-0x7f
  114. OC4(B, B, B, B), # 0x80-0x83
  115. OC4(B, B, B, B), # 0x84-0x87
  116. OC4(B, B, B, B), # 0x88-0x8b
  117. OC4(B, B, B, B), # 0x8c-0x8f
  118. OC4(B, B, B, B), # 0x90-0x93
  119. OC4(B, B, B, B), # 0x94-0x97
  120. OC4(B, B, B, B), # 0x98-0x9b
  121. OC4(B, B, B, B), # 0x9c-0x9f
  122. OC4(B, B, B, B), # 0xa0-0xa3
  123. OC4(B, B, B, B), # 0xa4-0xa7
  124. OC4(B, B, B, B), # 0xa8-0xab
  125. OC4(B, B, B, B), # 0xac-0xaf
  126. OC4(B, B, B, B), # 0xb0-0xb3
  127. OC4(B, B, B, B), # 0xb4-0xb7
  128. OC4(B, B, B, B), # 0xb8-0xbb
  129. OC4(B, B, B, B), # 0xbc-0xbf
  130. OC4(B, B, B, B), # 0xc0-0xc3
  131. OC4(B, B, B, B), # 0xc4-0xc7
  132. OC4(B, B, B, B), # 0xc8-0xcb
  133. OC4(B, B, B, B), # 0xcc-0xcf
  134. OC4(B, B, B, B), # 0xd0-0xd3
  135. OC4(U, U, U, B), # 0xd4-0xd7
  136. OC4(B, B, B, B), # 0xd8-0xdb
  137. OC4(B, B, B, B), # 0xdc-0xdf
  138. OC4(B, B, B, B), # 0xe0-0xe3
  139. OC4(B, B, B, B), # 0xe4-0xe7
  140. OC4(B, B, B, B), # 0xe8-0xeb
  141. OC4(B, B, B, B), # 0xec-0xef
  142. OC4(B, B, B, B), # 0xf0-0xf3
  143. OC4(B, B, B, B), # 0xf4-0xf7
  144. OC4(U, U, U, U), # 0xf8-0xfb
  145. OC4(U, U, U, U), # 0xfc-0xff
  146. ))
  147. # this function mirrors that in py/bc.c
  148. def mp_opcode_format(bytecode, ip, opcode_format=make_opcode_format()):
  149. opcode = bytecode[ip]
  150. ip_start = ip
  151. f = (opcode_format[opcode >> 2] >> (2 * (opcode & 3))) & 3
  152. if f == MP_OPCODE_QSTR:
  153. ip += 3
  154. else:
  155. extra_byte = (
  156. opcode == MP_BC_RAISE_VARARGS
  157. or opcode == MP_BC_MAKE_CLOSURE
  158. or opcode == MP_BC_MAKE_CLOSURE_DEFARGS
  159. or config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE and (
  160. opcode == MP_BC_LOAD_NAME
  161. or opcode == MP_BC_LOAD_GLOBAL
  162. or opcode == MP_BC_LOAD_ATTR
  163. or opcode == MP_BC_STORE_ATTR
  164. )
  165. )
  166. ip += 1
  167. if f == MP_OPCODE_VAR_UINT:
  168. while bytecode[ip] & 0x80 != 0:
  169. ip += 1
  170. ip += 1
  171. elif f == MP_OPCODE_OFFSET:
  172. ip += 2
  173. ip += extra_byte
  174. return f, ip - ip_start
  175. def decode_uint(bytecode, ip):
  176. unum = 0
  177. while True:
  178. val = bytecode[ip]
  179. ip += 1
  180. unum = (unum << 7) | (val & 0x7f)
  181. if not (val & 0x80):
  182. break
  183. return ip, unum
  184. def extract_prelude(bytecode):
  185. ip = 0
  186. ip, n_state = decode_uint(bytecode, ip)
  187. ip, n_exc_stack = decode_uint(bytecode, ip)
  188. scope_flags = bytecode[ip]; ip += 1
  189. n_pos_args = bytecode[ip]; ip += 1
  190. n_kwonly_args = bytecode[ip]; ip += 1
  191. n_def_pos_args = bytecode[ip]; ip += 1
  192. ip2, code_info_size = decode_uint(bytecode, ip)
  193. ip += code_info_size
  194. while bytecode[ip] != 0xff:
  195. ip += 1
  196. ip += 1
  197. # ip now points to first opcode
  198. # ip2 points to simple_name qstr
  199. return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
  200. class RawCode:
  201. # a set of all escaped names, to make sure they are unique
  202. escaped_names = set()
  203. def __init__(self, bytecode, qstrs, objs, raw_codes):
  204. # set core variables
  205. self.bytecode = bytecode
  206. self.qstrs = qstrs
  207. self.objs = objs
  208. self.raw_codes = raw_codes
  209. # extract prelude
  210. self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode)
  211. self.simple_name = self._unpack_qstr(self.ip2)
  212. self.source_file = self._unpack_qstr(self.ip2 + 2)
  213. def _unpack_qstr(self, ip):
  214. qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
  215. return global_qstrs[qst]
  216. def dump(self):
  217. # dump children first
  218. for rc in self.raw_codes:
  219. rc.freeze('')
  220. # TODO
  221. def freeze(self, parent_name):
  222. self.escaped_name = parent_name + self.simple_name.qstr_esc
  223. # make sure the escaped name is unique
  224. i = 2
  225. while self.escaped_name in RawCode.escaped_names:
  226. self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
  227. i += 1
  228. RawCode.escaped_names.add(self.escaped_name)
  229. # emit children first
  230. for rc in self.raw_codes:
  231. rc.freeze(self.escaped_name + '_')
  232. # generate bytecode data
  233. print()
  234. print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
  235. print('STATIC ', end='')
  236. if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
  237. print('const ', end='')
  238. print('byte bytecode_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode)))
  239. print(' ', end='')
  240. for i in range(self.ip2):
  241. print(' 0x%02x,' % self.bytecode[i], end='')
  242. print()
  243. print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
  244. print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
  245. print(' ', end='')
  246. for i in range(self.ip2 + 4, self.ip):
  247. print(' 0x%02x,' % self.bytecode[i], end='')
  248. print()
  249. ip = self.ip
  250. while ip < len(self.bytecode):
  251. f, sz = mp_opcode_format(self.bytecode, ip)
  252. if f == 1:
  253. qst = self._unpack_qstr(ip + 1).qstr_id
  254. print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,')
  255. else:
  256. print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
  257. ip += sz
  258. print('};')
  259. # generate constant objects
  260. for i, obj in enumerate(self.objs):
  261. obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
  262. if obj is Ellipsis:
  263. print('#define %s mp_const_ellipsis_obj' % obj_name)
  264. elif is_str_type(obj) or is_bytes_type(obj):
  265. if is_str_type(obj):
  266. obj = bytes_cons(obj, 'utf8')
  267. obj_type = 'mp_type_str'
  268. else:
  269. obj_type = 'mp_type_bytes'
  270. print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
  271. % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
  272. len(obj), ''.join(('\\x%02x' % b) for b in obj)))
  273. elif is_int_type(obj):
  274. if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
  275. # TODO check if we can actually fit this long-int into a small-int
  276. raise FreezeError(self, 'target does not support long int')
  277. elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
  278. # TODO
  279. raise FreezeError(self, 'freezing int to long-long is not implemented')
  280. elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
  281. neg = 0
  282. if obj < 0:
  283. obj = -obj
  284. neg = 1
  285. bits_per_dig = config.MPZ_DIG_SIZE
  286. digs = []
  287. z = obj
  288. while z:
  289. digs.append(z & ((1 << bits_per_dig) - 1))
  290. z >>= bits_per_dig
  291. ndigs = len(digs)
  292. digs = ','.join(('%#x' % d) for d in digs)
  293. print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
  294. '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};'
  295. % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs))
  296. elif type(obj) is float:
  297. print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
  298. print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
  299. % (obj_name, obj))
  300. print('#endif')
  301. elif type(obj) is complex:
  302. print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
  303. % (obj_name, obj.real, obj.imag))
  304. else:
  305. raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
  306. # generate constant table, if it has any entries
  307. const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
  308. if const_table_len:
  309. print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
  310. % (self.escaped_name, const_table_len))
  311. for qst in self.qstrs:
  312. print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
  313. for i in range(len(self.objs)):
  314. if type(self.objs[i]) is float:
  315. print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
  316. print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
  317. print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
  318. n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
  319. n = ((n & ~0x3) | 2) + 0x80800000
  320. print(' (mp_rom_obj_t)(0x%08x),' % (n,))
  321. print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D')
  322. n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0]
  323. n += 0x8004000000000000
  324. print(' (mp_rom_obj_t)(0x%016x),' % (n,))
  325. print('#endif')
  326. else:
  327. print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
  328. for rc in self.raw_codes:
  329. print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
  330. print('};')
  331. # generate module
  332. if self.simple_name.str != '<module>':
  333. print('STATIC ', end='')
  334. print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
  335. print(' .kind = MP_CODE_BYTECODE,')
  336. print(' .scope_flags = 0x%02x,' % self.prelude[2])
  337. print(' .n_pos_args = %u,' % self.prelude[3])
  338. print(' .data.u_byte = {')
  339. print(' .bytecode = bytecode_data_%s,' % self.escaped_name)
  340. if const_table_len:
  341. print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
  342. else:
  343. print(' .const_table = NULL,')
  344. print(' #if MICROPY_PERSISTENT_CODE_SAVE')
  345. print(' .bc_len = %u,' % len(self.bytecode))
  346. print(' .n_obj = %u,' % len(self.objs))
  347. print(' .n_raw_code = %u,' % len(self.raw_codes))
  348. print(' #endif')
  349. print(' },')
  350. print('};')
  351. def read_uint(f):
  352. i = 0
  353. while True:
  354. b = bytes_cons(f.read(1))[0]
  355. i = (i << 7) | (b & 0x7f)
  356. if b & 0x80 == 0:
  357. break
  358. return i
  359. global_qstrs = []
  360. qstr_type = namedtuple('qstr', ('str', 'qstr_esc', 'qstr_id'))
  361. def read_qstr(f):
  362. ln = read_uint(f)
  363. data = str_cons(f.read(ln), 'utf8')
  364. qstr_esc = qstrutil.qstr_escape(data)
  365. global_qstrs.append(qstr_type(data, qstr_esc, 'MP_QSTR_' + qstr_esc))
  366. return len(global_qstrs) - 1
  367. def read_obj(f):
  368. obj_type = f.read(1)
  369. if obj_type == b'e':
  370. return Ellipsis
  371. else:
  372. buf = f.read(read_uint(f))
  373. if obj_type == b's':
  374. return str_cons(buf, 'utf8')
  375. elif obj_type == b'b':
  376. return bytes_cons(buf)
  377. elif obj_type == b'i':
  378. return int(str_cons(buf, 'ascii'), 10)
  379. elif obj_type == b'f':
  380. return float(str_cons(buf, 'ascii'))
  381. elif obj_type == b'c':
  382. return complex(str_cons(buf, 'ascii'))
  383. else:
  384. assert 0
  385. def read_qstr_and_pack(f, bytecode, ip):
  386. qst = read_qstr(f)
  387. bytecode[ip] = qst & 0xff
  388. bytecode[ip + 1] = qst >> 8
  389. def read_bytecode_qstrs(file, bytecode, ip):
  390. while ip < len(bytecode):
  391. f, sz = mp_opcode_format(bytecode, ip)
  392. if f == 1:
  393. read_qstr_and_pack(file, bytecode, ip + 1)
  394. ip += sz
  395. def read_raw_code(f):
  396. bc_len = read_uint(f)
  397. bytecode = bytearray(f.read(bc_len))
  398. ip, ip2, prelude = extract_prelude(bytecode)
  399. read_qstr_and_pack(f, bytecode, ip2) # simple_name
  400. read_qstr_and_pack(f, bytecode, ip2 + 2) # source_file
  401. read_bytecode_qstrs(f, bytecode, ip)
  402. n_obj = read_uint(f)
  403. n_raw_code = read_uint(f)
  404. qstrs = [read_qstr(f) for _ in range(prelude[3] + prelude[4])]
  405. objs = [read_obj(f) for _ in range(n_obj)]
  406. raw_codes = [read_raw_code(f) for _ in range(n_raw_code)]
  407. return RawCode(bytecode, qstrs, objs, raw_codes)
  408. def read_mpy(filename):
  409. with open(filename, 'rb') as f:
  410. header = bytes_cons(f.read(4))
  411. if header[0] != ord('M'):
  412. raise Exception('not a valid .mpy file')
  413. if header[1] != config.MPY_VERSION:
  414. raise Exception('incompatible .mpy version')
  415. feature_flags = header[2]
  416. config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_flags & 1) != 0
  417. config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_flags & 2) != 0
  418. config.mp_small_int_bits = header[3]
  419. return read_raw_code(f)
  420. def dump_mpy(raw_codes):
  421. for rc in raw_codes:
  422. rc.dump()
  423. def freeze_mpy(base_qstrs, raw_codes):
  424. # add to qstrs
  425. new = {}
  426. for q in global_qstrs:
  427. # don't add duplicates
  428. if q.qstr_esc in base_qstrs or q.qstr_esc in new:
  429. continue
  430. new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
  431. new = sorted(new.values(), key=lambda x: x[0])
  432. print('#include "py/mpconfig.h"')
  433. print('#include "py/objint.h"')
  434. print('#include "py/objstr.h"')
  435. print('#include "py/emitglue.h"')
  436. print()
  437. print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
  438. print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
  439. print('#endif')
  440. print()
  441. print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
  442. print('#error "incompatible MICROPY_LONGINT_IMPL"')
  443. print('#endif')
  444. print()
  445. if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
  446. print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
  447. print('#error "incompatible MPZ_DIG_SIZE"')
  448. print('#endif')
  449. print()
  450. print('#if MICROPY_PY_BUILTINS_FLOAT')
  451. print('typedef struct _mp_obj_float_t {')
  452. print(' mp_obj_base_t base;')
  453. print(' mp_float_t value;')
  454. print('} mp_obj_float_t;')
  455. print('#endif')
  456. print()
  457. print('#if MICROPY_PY_BUILTINS_COMPLEX')
  458. print('typedef struct _mp_obj_complex_t {')
  459. print(' mp_obj_base_t base;')
  460. print(' mp_float_t real;')
  461. print(' mp_float_t imag;')
  462. print('} mp_obj_complex_t;')
  463. print('#endif')
  464. print()
  465. print('enum {')
  466. for i in range(len(new)):
  467. if i == 0:
  468. print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
  469. else:
  470. print(' MP_QSTR_%s,' % new[i][1])
  471. print('};')
  472. print()
  473. print('extern const qstr_pool_t mp_qstr_const_pool;');
  474. print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
  475. print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
  476. print(' MP_QSTRnumber_of, // previous pool size')
  477. print(' %u, // allocated entries' % len(new))
  478. print(' %u, // used entries' % len(new))
  479. print(' {')
  480. for _, _, qstr in new:
  481. print(' %s,'
  482. % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
  483. print(' },')
  484. print('};')
  485. for rc in raw_codes:
  486. rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
  487. print()
  488. print('const char mp_frozen_mpy_names[] = {')
  489. for rc in raw_codes:
  490. module_name = rc.source_file.str
  491. print('"%s\\0"' % module_name)
  492. print('"\\0"};')
  493. print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
  494. for rc in raw_codes:
  495. print(' &raw_code_%s,' % rc.escaped_name)
  496. print('};')
  497. def main():
  498. import argparse
  499. cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
  500. cmd_parser.add_argument('-d', '--dump', action='store_true',
  501. help='dump contents of files')
  502. cmd_parser.add_argument('-f', '--freeze', action='store_true',
  503. help='freeze files')
  504. cmd_parser.add_argument('-q', '--qstr-header',
  505. help='qstr header file to freeze against')
  506. cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
  507. help='long-int implementation used by target (default mpz)')
  508. cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
  509. help='mpz digit size used by target (default 16)')
  510. cmd_parser.add_argument('files', nargs='+',
  511. help='input .mpy files')
  512. args = cmd_parser.parse_args()
  513. # set config values relevant to target machine
  514. config.MICROPY_LONGINT_IMPL = {
  515. 'none':config.MICROPY_LONGINT_IMPL_NONE,
  516. 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
  517. 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
  518. }[args.mlongint_impl]
  519. config.MPZ_DIG_SIZE = args.mmpz_dig_size
  520. # set config values for qstrs, and get the existing base set of qstrs
  521. if args.qstr_header:
  522. qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
  523. config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
  524. config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
  525. else:
  526. config.MICROPY_QSTR_BYTES_IN_LEN = 1
  527. config.MICROPY_QSTR_BYTES_IN_HASH = 1
  528. base_qstrs = {}
  529. raw_codes = [read_mpy(file) for file in args.files]
  530. if args.dump:
  531. dump_mpy(raw_codes)
  532. elif args.freeze:
  533. try:
  534. freeze_mpy(base_qstrs, raw_codes)
  535. except FreezeError as er:
  536. print(er, file=sys.stderr)
  537. sys.exit(1)
  538. if __name__ == '__main__':
  539. main()