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