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