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 | # |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 7 | # Copyright (c) 2016-2019 Damien P. George |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 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 | 5716c5c | 2019-09-26 16:39:37 +1000 | [diff] [blame] | 60 | MPY_VERSION = 5 |
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 |
Josh Lloyd | 7d58a19 | 2019-09-25 17:53:30 +1200 | [diff] [blame] | 73 | global_qstrs = [None] # MP_QSTRnull should never be referenced |
Damien George | 4f0931b | 2019-03-01 14:33:03 +1100 | [diff] [blame] | 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: |
Damien George | 74ed068 | 2019-04-08 15:20:56 +1000 | [diff] [blame] | 78 | def __init__(self, size): |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 79 | self.window = [] |
Damien George | 74ed068 | 2019-04-08 15:20:56 +1000 | [diff] [blame] | 80 | self.size = size |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 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 | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 90 | MP_CODE_BYTECODE = 2 |
| 91 | MP_CODE_NATIVE_PY = 3 |
| 92 | MP_CODE_NATIVE_VIPER = 4 |
| 93 | MP_CODE_NATIVE_ASM = 5 |
| 94 | |
| 95 | MP_NATIVE_ARCH_NONE = 0 |
| 96 | MP_NATIVE_ARCH_X86 = 1 |
| 97 | MP_NATIVE_ARCH_X64 = 2 |
| 98 | MP_NATIVE_ARCH_ARMV6 = 3 |
| 99 | MP_NATIVE_ARCH_ARMV6M = 4 |
| 100 | MP_NATIVE_ARCH_ARMV7M = 5 |
| 101 | MP_NATIVE_ARCH_ARMV7EM = 6 |
| 102 | MP_NATIVE_ARCH_ARMV7EMSP = 7 |
| 103 | MP_NATIVE_ARCH_ARMV7EMDP = 8 |
| 104 | MP_NATIVE_ARCH_XTENSA = 9 |
Damien George | 9adedce | 2019-09-13 13:15:12 +1000 | [diff] [blame] | 105 | MP_NATIVE_ARCH_XTENSAWIN = 10 |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 106 | |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 107 | MP_BC_MASK_EXTRA_BYTE = 0x9e |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 108 | |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 109 | MP_BC_FORMAT_BYTE = 0 |
| 110 | MP_BC_FORMAT_QSTR = 1 |
| 111 | MP_BC_FORMAT_VAR_UINT = 2 |
| 112 | MP_BC_FORMAT_OFFSET = 3 |
| 113 | |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 114 | # extra byte if caching enabled: |
Damien George | 5889cf5 | 2019-09-02 20:24:01 +1000 | [diff] [blame] | 115 | MP_BC_LOAD_NAME = 0x11 |
| 116 | MP_BC_LOAD_GLOBAL = 0x12 |
| 117 | MP_BC_LOAD_ATTR = 0x13 |
| 118 | MP_BC_STORE_ATTR = 0x18 |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 119 | |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 120 | # this function mirrors that in py/bc.c |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 121 | def mp_opcode_format(bytecode, ip, count_var_uint): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 122 | opcode = bytecode[ip] |
| 123 | ip_start = ip |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 124 | f = ((0x000003a4 >> (2 * ((opcode) >> 4))) & 3) |
| 125 | if f == MP_BC_FORMAT_QSTR: |
Damien George | 814d580 | 2018-12-11 00:52:33 +1100 | [diff] [blame] | 126 | if config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE: |
| 127 | if (opcode == MP_BC_LOAD_NAME |
| 128 | or opcode == MP_BC_LOAD_GLOBAL |
| 129 | or opcode == MP_BC_LOAD_ATTR |
| 130 | or opcode == MP_BC_STORE_ATTR): |
| 131 | ip += 1 |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 132 | ip += 3 |
| 133 | else: |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 134 | extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0 |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 135 | ip += 1 |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 136 | if f == MP_BC_FORMAT_VAR_UINT: |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 137 | if count_var_uint: |
| 138 | while bytecode[ip] & 0x80 != 0: |
| 139 | ip += 1 |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 140 | ip += 1 |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 141 | elif f == MP_BC_FORMAT_OFFSET: |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 142 | ip += 2 |
| 143 | ip += extra_byte |
| 144 | return f, ip - ip_start |
| 145 | |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 146 | def read_prelude_sig(read_byte): |
| 147 | z = read_byte() |
| 148 | # xSSSSEAA |
| 149 | S = (z >> 3) & 0xf |
| 150 | E = (z >> 2) & 0x1 |
| 151 | F = 0 |
| 152 | A = z & 0x3 |
| 153 | K = 0 |
| 154 | D = 0 |
| 155 | n = 0 |
| 156 | while z & 0x80: |
| 157 | z = read_byte() |
| 158 | # xFSSKAED |
| 159 | S |= (z & 0x30) << (2 * n) |
| 160 | E |= (z & 0x02) << n |
| 161 | F |= ((z & 0x40) >> 6) << n |
| 162 | A |= (z & 0x4) << n |
| 163 | K |= ((z & 0x08) >> 3) << n |
| 164 | D |= (z & 0x1) << n |
| 165 | n += 1 |
| 166 | S += 1 |
| 167 | return S, E, F, A, K, D |
| 168 | |
Damien George | c8c0fd4 | 2019-09-25 15:45:47 +1000 | [diff] [blame] | 169 | def read_prelude_size(read_byte): |
| 170 | I = 0 |
| 171 | C = 0 |
| 172 | n = 0 |
| 173 | while True: |
| 174 | z = read_byte() |
| 175 | # xIIIIIIC |
| 176 | I |= ((z & 0x7e) >> 1) << (6 * n) |
| 177 | C |= (z & 1) << n |
| 178 | if not (z & 0x80): |
| 179 | break |
| 180 | n += 1 |
| 181 | return I, C |
| 182 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 183 | def extract_prelude(bytecode, ip): |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 184 | def local_read_byte(): |
| 185 | b = bytecode[ip_ref[0]] |
| 186 | ip_ref[0] += 1 |
| 187 | return b |
| 188 | ip_ref = [ip] # to close over ip in Python 2 and 3 |
| 189 | n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(local_read_byte) |
Damien George | c8c0fd4 | 2019-09-25 15:45:47 +1000 | [diff] [blame] | 190 | n_info, n_cell = read_prelude_size(local_read_byte) |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 191 | ip = ip_ref[0] |
| 192 | |
Damien George | c8c0fd4 | 2019-09-25 15:45:47 +1000 | [diff] [blame] | 193 | ip2 = ip |
| 194 | ip = ip2 + n_info + n_cell |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 195 | # ip now points to first opcode |
| 196 | # ip2 points to simple_name qstr |
Damien George | c8c0fd4 | 2019-09-25 15:45:47 +1000 | [diff] [blame] | 197 | return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 198 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 199 | class MPFunTable: |
| 200 | pass |
| 201 | |
Damien George | 643d2a0 | 2019-04-08 11:21:18 +1000 | [diff] [blame] | 202 | class RawCode(object): |
Damien George | 02fd83b | 2016-05-03 12:24:39 +0100 | [diff] [blame] | 203 | # a set of all escaped names, to make sure they are unique |
| 204 | escaped_names = set() |
| 205 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 206 | # convert code kind number to string |
| 207 | code_kind_str = { |
| 208 | MP_CODE_BYTECODE: 'MP_CODE_BYTECODE', |
| 209 | MP_CODE_NATIVE_PY: 'MP_CODE_NATIVE_PY', |
| 210 | MP_CODE_NATIVE_VIPER: 'MP_CODE_NATIVE_VIPER', |
| 211 | MP_CODE_NATIVE_ASM: 'MP_CODE_NATIVE_ASM', |
| 212 | } |
| 213 | |
| 214 | def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 215 | # set core variables |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 216 | self.code_kind = code_kind |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 217 | self.bytecode = bytecode |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 218 | self.prelude_offset = prelude_offset |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 219 | self.qstrs = qstrs |
| 220 | self.objs = objs |
| 221 | self.raw_codes = raw_codes |
| 222 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 223 | if self.prelude_offset is None: |
| 224 | # no prelude, assign a dummy simple_name |
| 225 | self.prelude_offset = 0 |
| 226 | self.simple_name = global_qstrs[1] |
| 227 | else: |
| 228 | # extract prelude |
| 229 | self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset) |
| 230 | self.simple_name = self._unpack_qstr(self.ip2) |
| 231 | self.source_file = self._unpack_qstr(self.ip2 + 2) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 232 | |
| 233 | def _unpack_qstr(self, ip): |
| 234 | qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8 |
| 235 | return global_qstrs[qst] |
| 236 | |
| 237 | def dump(self): |
| 238 | # dump children first |
| 239 | for rc in self.raw_codes: |
stijn | e4ab404 | 2017-08-16 10:37:00 +0200 | [diff] [blame] | 240 | rc.freeze('') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 241 | # TODO |
| 242 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 243 | def freeze_children(self, parent_name): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 244 | self.escaped_name = parent_name + self.simple_name.qstr_esc |
| 245 | |
Damien George | 02fd83b | 2016-05-03 12:24:39 +0100 | [diff] [blame] | 246 | # make sure the escaped name is unique |
| 247 | i = 2 |
| 248 | while self.escaped_name in RawCode.escaped_names: |
| 249 | self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i) |
| 250 | i += 1 |
| 251 | RawCode.escaped_names.add(self.escaped_name) |
| 252 | |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 253 | # emit children first |
| 254 | for rc in self.raw_codes: |
| 255 | rc.freeze(self.escaped_name + '_') |
| 256 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 257 | def freeze_constants(self): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 258 | # generate constant objects |
| 259 | for i, obj in enumerate(self.objs): |
| 260 | obj_name = 'const_obj_%s_%u' % (self.escaped_name, i) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 261 | if obj is MPFunTable: |
| 262 | pass |
| 263 | elif obj is Ellipsis: |
Damien George | 9ba3de6 | 2017-11-15 12:46:08 +1100 | [diff] [blame] | 264 | print('#define %s mp_const_ellipsis_obj' % obj_name) |
| 265 | elif is_str_type(obj) or is_bytes_type(obj): |
Damien George | b6bdf18 | 2016-09-02 15:10:45 +1000 | [diff] [blame] | 266 | if is_str_type(obj): |
| 267 | obj = bytes_cons(obj, 'utf8') |
| 268 | obj_type = 'mp_type_str' |
| 269 | else: |
| 270 | obj_type = 'mp_type_bytes' |
| 271 | print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};' |
| 272 | % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH), |
| 273 | len(obj), ''.join(('\\x%02x' % b) for b in obj))) |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 274 | elif is_int_type(obj): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 275 | if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE: |
| 276 | # TODO check if we can actually fit this long-int into a small-int |
| 277 | raise FreezeError(self, 'target does not support long int') |
| 278 | elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG: |
| 279 | # TODO |
| 280 | raise FreezeError(self, 'freezing int to long-long is not implemented') |
| 281 | elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: |
| 282 | neg = 0 |
| 283 | if obj < 0: |
| 284 | obj = -obj |
| 285 | neg = 1 |
| 286 | bits_per_dig = config.MPZ_DIG_SIZE |
| 287 | digs = [] |
| 288 | z = obj |
| 289 | while z: |
| 290 | digs.append(z & ((1 << bits_per_dig) - 1)) |
| 291 | z >>= bits_per_dig |
| 292 | ndigs = len(digs) |
| 293 | digs = ','.join(('%#x' % d) for d in digs) |
| 294 | print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, ' |
Damien George | 44fc92e | 2018-07-09 13:43:34 +1000 | [diff] [blame] | 295 | '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};' |
| 296 | % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs)) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 297 | elif type(obj) is float: |
Damien George | 72ae3c7 | 2016-08-10 13:26:11 +1000 | [diff] [blame] | 298 | 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] | 299 | print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};' |
| 300 | % (obj_name, obj)) |
Damien George | 72ae3c7 | 2016-08-10 13:26:11 +1000 | [diff] [blame] | 301 | print('#endif') |
Damien George | c51c883 | 2016-09-03 00:19:02 +1000 | [diff] [blame] | 302 | elif type(obj) is complex: |
| 303 | print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};' |
| 304 | % (obj_name, obj.real, obj.imag)) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 305 | else: |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 306 | raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,)) |
| 307 | |
Damien George | b6a3289 | 2017-08-12 22:26:18 +1000 | [diff] [blame] | 308 | # generate constant table, if it has any entries |
| 309 | const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes) |
| 310 | if const_table_len: |
| 311 | print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {' |
| 312 | % (self.escaped_name, const_table_len)) |
| 313 | for qst in self.qstrs: |
| 314 | print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id) |
| 315 | for i in range(len(self.objs)): |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 316 | if self.objs[i] is MPFunTable: |
| 317 | print(' mp_fun_table,') |
| 318 | elif type(self.objs[i]) is float: |
Damien George | b6a3289 | 2017-08-12 22:26:18 +1000 | [diff] [blame] | 319 | print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B') |
| 320 | print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i)) |
| 321 | print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C') |
| 322 | n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0] |
| 323 | n = ((n & ~0x3) | 2) + 0x80800000 |
| 324 | print(' (mp_rom_obj_t)(0x%08x),' % (n,)) |
Damien George | 929d10a | 2018-07-09 12:22:40 +1000 | [diff] [blame] | 325 | print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D') |
| 326 | n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0] |
| 327 | n += 0x8004000000000000 |
| 328 | print(' (mp_rom_obj_t)(0x%016x),' % (n,)) |
Damien George | b6a3289 | 2017-08-12 22:26:18 +1000 | [diff] [blame] | 329 | print('#endif') |
| 330 | else: |
| 331 | print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i)) |
| 332 | for rc in self.raw_codes: |
| 333 | print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name) |
| 334 | print('};') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 335 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 336 | def freeze_module(self, qstr_links=(), type_sig=0): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 337 | # generate module |
| 338 | if self.simple_name.str != '<module>': |
| 339 | print('STATIC ', end='') |
| 340 | print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 341 | print(' .kind = %s,' % RawCode.code_kind_str[self.code_kind]) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 342 | print(' .scope_flags = 0x%02x,' % self.prelude[2]) |
| 343 | print(' .n_pos_args = %u,' % self.prelude[3]) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 344 | print(' .fun_data = fun_data_%s,' % self.escaped_name) |
| 345 | if len(self.qstrs) + len(self.objs) + len(self.raw_codes): |
Damien George | 636ed0f | 2019-02-19 14:15:39 +1100 | [diff] [blame] | 346 | print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name) |
Damien George | b6a3289 | 2017-08-12 22:26:18 +1000 | [diff] [blame] | 347 | else: |
Damien George | 636ed0f | 2019-02-19 14:15:39 +1100 | [diff] [blame] | 348 | print(' .const_table = NULL,') |
| 349 | print(' #if MICROPY_PERSISTENT_CODE_SAVE') |
| 350 | print(' .fun_data_len = %u,' % len(self.bytecode)) |
| 351 | print(' .n_obj = %u,' % len(self.objs)) |
| 352 | print(' .n_raw_code = %u,' % len(self.raw_codes)) |
Damien George | c69f58e | 2019-09-06 23:55:15 +1000 | [diff] [blame] | 353 | if self.code_kind == MP_CODE_BYTECODE: |
| 354 | print(' #if MICROPY_PY_SYS_SETTRACE') |
| 355 | print(' .prelude = {') |
| 356 | print(' .n_state = %u,' % self.prelude[0]) |
| 357 | print(' .n_exc_stack = %u,' % self.prelude[1]) |
| 358 | print(' .scope_flags = %u,' % self.prelude[2]) |
| 359 | print(' .n_pos_args = %u,' % self.prelude[3]) |
| 360 | print(' .n_kwonly_args = %u,' % self.prelude[4]) |
| 361 | print(' .n_def_pos_args = %u,' % self.prelude[5]) |
| 362 | print(' .qstr_block_name = %s,' % self.simple_name.qstr_id) |
| 363 | print(' .qstr_source_file = %s,' % self.source_file.qstr_id) |
| 364 | print(' .line_info = fun_data_%s + %u,' % (self.escaped_name, 0)) # TODO |
Damien George | c69f58e | 2019-09-06 23:55:15 +1000 | [diff] [blame] | 365 | print(' .opcodes = fun_data_%s + %u,' % (self.escaped_name, self.ip)) |
| 366 | print(' },') |
| 367 | print(' .line_of_definition = %u,' % 0) # TODO |
| 368 | print(' #endif') |
Jun Wu | b152bbd | 2019-05-06 00:31:11 -0700 | [diff] [blame] | 369 | print(' #if MICROPY_EMIT_MACHINE_CODE') |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 370 | print(' .prelude_offset = %u,' % self.prelude_offset) |
| 371 | print(' .n_qstr = %u,' % len(qstr_links)) |
| 372 | print(' .qstr_link = NULL,') # TODO |
| 373 | print(' #endif') |
| 374 | print(' #endif') |
Jun Wu | b152bbd | 2019-05-06 00:31:11 -0700 | [diff] [blame] | 375 | print(' #if MICROPY_EMIT_MACHINE_CODE') |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 376 | print(' .type_sig = %u,' % type_sig) |
Damien George | 636ed0f | 2019-02-19 14:15:39 +1100 | [diff] [blame] | 377 | print(' #endif') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 378 | print('};') |
| 379 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 380 | class RawCodeBytecode(RawCode): |
| 381 | def __init__(self, bytecode, qstrs, objs, raw_codes): |
Damien George | 643d2a0 | 2019-04-08 11:21:18 +1000 | [diff] [blame] | 382 | super(RawCodeBytecode, self).__init__(MP_CODE_BYTECODE, bytecode, 0, qstrs, objs, raw_codes) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 383 | |
| 384 | def freeze(self, parent_name): |
| 385 | self.freeze_children(parent_name) |
| 386 | |
| 387 | # generate bytecode data |
| 388 | print() |
| 389 | print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str)) |
| 390 | print('STATIC ', end='') |
| 391 | if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE: |
| 392 | print('const ', end='') |
| 393 | print('byte fun_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode))) |
| 394 | print(' ', end='') |
| 395 | for i in range(self.ip2): |
| 396 | print(' 0x%02x,' % self.bytecode[i], end='') |
| 397 | print() |
| 398 | print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,') |
| 399 | print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,') |
| 400 | print(' ', end='') |
| 401 | for i in range(self.ip2 + 4, self.ip): |
| 402 | print(' 0x%02x,' % self.bytecode[i], end='') |
| 403 | print() |
| 404 | ip = self.ip |
| 405 | while ip < len(self.bytecode): |
| 406 | f, sz = mp_opcode_format(self.bytecode, ip, True) |
| 407 | if f == 1: |
| 408 | qst = self._unpack_qstr(ip + 1).qstr_id |
| 409 | extra = '' if sz == 3 else ' 0x%02x,' % self.bytecode[ip + 3] |
| 410 | print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,', extra) |
| 411 | else: |
| 412 | print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz))) |
| 413 | ip += sz |
| 414 | print('};') |
| 415 | |
| 416 | self.freeze_constants() |
| 417 | self.freeze_module() |
| 418 | |
| 419 | class RawCodeNative(RawCode): |
| 420 | def __init__(self, code_kind, fun_data, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig): |
Damien George | 643d2a0 | 2019-04-08 11:21:18 +1000 | [diff] [blame] | 421 | super(RawCodeNative, self).__init__(code_kind, fun_data, prelude_offset, qstrs, objs, raw_codes) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 422 | self.prelude = prelude |
| 423 | self.qstr_links = qstr_links |
| 424 | self.type_sig = type_sig |
| 425 | if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64): |
| 426 | self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))' |
| 427 | else: |
| 428 | self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))' |
| 429 | |
Jim Mussared | 4ab5156 | 2019-08-17 00:32:04 +1000 | [diff] [blame] | 430 | # Allow single-byte alignment by default for x86/x64/xtensa, but on ARM we need halfword- or word- alignment. |
| 431 | if config.native_arch == MP_NATIVE_ARCH_ARMV6: |
| 432 | # ARMV6 -- four byte align. |
| 433 | self.fun_data_attributes += ' __attribute__ ((aligned (4)))' |
| 434 | elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP: |
| 435 | # ARMVxxM -- two byte align. |
| 436 | self.fun_data_attributes += ' __attribute__ ((aligned (2)))' |
| 437 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 438 | def _asm_thumb_rewrite_mov(self, pc, val): |
| 439 | print(' (%u & 0xf0) | (%s >> 12),' % (self.bytecode[pc], val), end='') |
| 440 | print(' (%u & 0xfb) | (%s >> 9 & 0x04),' % (self.bytecode[pc + 1], val), end='') |
| 441 | print(' (%s & 0xff),' % (val,), end='') |
| 442 | print(' (%u & 0x07) | (%s >> 4 & 0x70),' % (self.bytecode[pc + 3], val)) |
| 443 | |
| 444 | def _link_qstr(self, pc, kind, qst): |
| 445 | if kind == 0: |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 446 | # Generic 16-bit link |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 447 | print(' %s & 0xff, %s >> 8,' % (qst, qst)) |
Damien George | 9d3031c | 2019-06-11 11:36:39 +1000 | [diff] [blame] | 448 | return 2 |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 449 | else: |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 450 | # Architecture-specific link |
| 451 | is_obj = kind == 2 |
| 452 | if is_obj: |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 453 | qst = '((uintptr_t)MP_OBJ_NEW_QSTR(%s))' % qst |
| 454 | if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64): |
| 455 | print(' %s & 0xff, %s >> 8, 0, 0,' % (qst, qst)) |
Damien George | 9d3031c | 2019-06-11 11:36:39 +1000 | [diff] [blame] | 456 | return 4 |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 457 | elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP: |
| 458 | if is_obj: |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 459 | # qstr object, movw and movt |
| 460 | self._asm_thumb_rewrite_mov(pc, qst) |
| 461 | self._asm_thumb_rewrite_mov(pc + 4, '(%s >> 16)' % qst) |
Damien George | 9d3031c | 2019-06-11 11:36:39 +1000 | [diff] [blame] | 462 | return 8 |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 463 | else: |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 464 | # qstr number, movw instruction |
| 465 | self._asm_thumb_rewrite_mov(pc, qst) |
Damien George | 9d3031c | 2019-06-11 11:36:39 +1000 | [diff] [blame] | 466 | return 4 |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 467 | else: |
| 468 | assert 0 |
| 469 | |
| 470 | def freeze(self, parent_name): |
| 471 | self.freeze_children(parent_name) |
| 472 | |
| 473 | # generate native code data |
| 474 | print() |
| 475 | if self.code_kind == MP_CODE_NATIVE_PY: |
| 476 | print('// frozen native code for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str)) |
| 477 | elif self.code_kind == MP_CODE_NATIVE_VIPER: |
| 478 | print('// frozen viper code for scope %s' % (parent_name,)) |
| 479 | else: |
| 480 | print('// frozen assembler code for scope %s' % (parent_name,)) |
| 481 | print('STATIC const byte fun_data_%s[%u] %s = {' % (self.escaped_name, len(self.bytecode), self.fun_data_attributes)) |
| 482 | |
| 483 | if self.code_kind == MP_CODE_NATIVE_PY: |
| 484 | i_top = self.prelude_offset |
| 485 | else: |
| 486 | i_top = len(self.bytecode) |
| 487 | i = 0 |
| 488 | qi = 0 |
| 489 | while i < i_top: |
| 490 | if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]: |
| 491 | # link qstr |
| 492 | qi_off, qi_kind, qi_val = self.qstr_links[qi] |
| 493 | qst = global_qstrs[qi_val].qstr_id |
Damien George | 9d3031c | 2019-06-11 11:36:39 +1000 | [diff] [blame] | 494 | i += self._link_qstr(i, qi_kind, qst) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 495 | qi += 1 |
| 496 | else: |
| 497 | # copy machine code (max 16 bytes) |
| 498 | i16 = min(i + 16, i_top) |
| 499 | if qi < len(self.qstr_links): |
| 500 | i16 = min(i16, self.qstr_links[qi][0]) |
| 501 | print(' ', end='') |
| 502 | for ii in range(i, i16): |
| 503 | print(' 0x%02x,' % self.bytecode[ii], end='') |
| 504 | print() |
| 505 | i = i16 |
| 506 | |
| 507 | if self.code_kind == MP_CODE_NATIVE_PY: |
| 508 | print(' ', end='') |
| 509 | for i in range(self.prelude_offset, self.ip2): |
| 510 | print(' 0x%02x,' % self.bytecode[i], end='') |
| 511 | print() |
| 512 | |
| 513 | print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,') |
| 514 | print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,') |
| 515 | |
| 516 | print(' ', end='') |
| 517 | for i in range(self.ip2 + 4, self.ip): |
| 518 | print(' 0x%02x,' % self.bytecode[i], end='') |
| 519 | print() |
| 520 | |
| 521 | print('};') |
| 522 | |
| 523 | self.freeze_constants() |
| 524 | self.freeze_module(self.qstr_links, self.type_sig) |
| 525 | |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 526 | class BytecodeBuffer: |
| 527 | def __init__(self, size): |
| 528 | self.buf = bytearray(size) |
| 529 | self.idx = 0 |
| 530 | |
| 531 | def is_full(self): |
| 532 | return self.idx == len(self.buf) |
| 533 | |
| 534 | def append(self, b): |
| 535 | self.buf[self.idx] = b |
| 536 | self.idx += 1 |
| 537 | |
| 538 | def read_byte(f, out=None): |
| 539 | b = bytes_cons(f.read(1))[0] |
| 540 | if out is not None: |
| 541 | out.append(b) |
| 542 | return b |
| 543 | |
| 544 | def read_uint(f, out=None): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 545 | i = 0 |
| 546 | while True: |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 547 | b = read_byte(f, out) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 548 | i = (i << 7) | (b & 0x7f) |
| 549 | if b & 0x80 == 0: |
| 550 | break |
| 551 | return i |
| 552 | |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 553 | def read_qstr(f, qstr_win): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 554 | ln = read_uint(f) |
Damien George | 4f0931b | 2019-03-01 14:33:03 +1100 | [diff] [blame] | 555 | if ln == 0: |
| 556 | # static qstr |
| 557 | return bytes_cons(f.read(1))[0] |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 558 | if ln & 1: |
| 559 | # qstr in table |
| 560 | return qstr_win.access(ln >> 1) |
| 561 | ln >>= 1 |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 562 | data = str_cons(f.read(ln), 'utf8') |
Damien George | 4f0931b | 2019-03-01 14:33:03 +1100 | [diff] [blame] | 563 | global_qstrs.append(QStrType(data)) |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 564 | qstr_win.push(len(global_qstrs) - 1) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 565 | return len(global_qstrs) - 1 |
| 566 | |
| 567 | def read_obj(f): |
| 568 | obj_type = f.read(1) |
| 569 | if obj_type == b'e': |
| 570 | return Ellipsis |
| 571 | else: |
| 572 | buf = f.read(read_uint(f)) |
| 573 | if obj_type == b's': |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 574 | return str_cons(buf, 'utf8') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 575 | elif obj_type == b'b': |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 576 | return bytes_cons(buf) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 577 | elif obj_type == b'i': |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 578 | return int(str_cons(buf, 'ascii'), 10) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 579 | elif obj_type == b'f': |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 580 | return float(str_cons(buf, 'ascii')) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 581 | elif obj_type == b'c': |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 582 | return complex(str_cons(buf, 'ascii')) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 583 | else: |
| 584 | assert 0 |
| 585 | |
Damien George | 23f0691 | 2019-10-10 15:30:16 +1100 | [diff] [blame^] | 586 | def read_prelude(f, bytecode, qstr_win): |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 587 | n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(lambda: read_byte(f, bytecode)) |
Damien George | c8c0fd4 | 2019-09-25 15:45:47 +1000 | [diff] [blame] | 588 | n_info, n_cell = read_prelude_size(lambda: read_byte(f, bytecode)) |
Damien George | 23f0691 | 2019-10-10 15:30:16 +1100 | [diff] [blame^] | 589 | read_qstr_and_pack(f, bytecode, qstr_win) # simple_name |
| 590 | read_qstr_and_pack(f, bytecode, qstr_win) # source_file |
| 591 | for _ in range(n_info - 4 + n_cell): |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 592 | read_byte(f, bytecode) |
Damien George | 23f0691 | 2019-10-10 15:30:16 +1100 | [diff] [blame^] | 593 | return n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 594 | |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 595 | def read_qstr_and_pack(f, bytecode, qstr_win): |
| 596 | qst = read_qstr(f, qstr_win) |
| 597 | bytecode.append(qst & 0xff) |
| 598 | bytecode.append(qst >> 8) |
| 599 | |
| 600 | def read_bytecode(file, bytecode, qstr_win): |
| 601 | while not bytecode.is_full(): |
| 602 | op = read_byte(file, bytecode) |
| 603 | f, sz = mp_opcode_format(bytecode.buf, bytecode.idx - 1, False) |
| 604 | sz -= 1 |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 605 | if f == MP_BC_FORMAT_QSTR: |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 606 | read_qstr_and_pack(file, bytecode, qstr_win) |
| 607 | sz -= 2 |
Damien George | 1f7202d | 2019-09-02 21:35:26 +1000 | [diff] [blame] | 608 | elif f == MP_BC_FORMAT_VAR_UINT: |
Damien George | 992a6e1 | 2019-03-01 14:03:10 +1100 | [diff] [blame] | 609 | while read_byte(file, bytecode) & 0x80: |
| 610 | pass |
| 611 | for _ in range(sz): |
| 612 | read_byte(file, bytecode) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 613 | |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 614 | def read_raw_code(f, qstr_win): |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 615 | kind_len = read_uint(f) |
| 616 | kind = (kind_len & 3) + MP_CODE_BYTECODE |
| 617 | fun_data_len = kind_len >> 2 |
| 618 | fun_data = BytecodeBuffer(fun_data_len) |
| 619 | |
| 620 | if kind == MP_CODE_BYTECODE: |
Damien George | 23f0691 | 2019-10-10 15:30:16 +1100 | [diff] [blame^] | 621 | prelude = read_prelude(f, fun_data, qstr_win) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 622 | read_bytecode(f, fun_data, qstr_win) |
| 623 | else: |
| 624 | fun_data.buf[:] = f.read(fun_data_len) |
| 625 | |
| 626 | qstr_links = [] |
| 627 | if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER): |
| 628 | # load qstr link table |
| 629 | n_qstr_link = read_uint(f) |
| 630 | for _ in range(n_qstr_link): |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 631 | off = read_uint(f) |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 632 | qst = read_qstr(f, qstr_win) |
| 633 | qstr_links.append((off >> 2, off & 3, qst)) |
| 634 | |
| 635 | type_sig = 0 |
| 636 | if kind == MP_CODE_NATIVE_PY: |
| 637 | prelude_offset = read_uint(f) |
| 638 | _, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset) |
Damien George | 23f0691 | 2019-10-10 15:30:16 +1100 | [diff] [blame^] | 639 | fun_data.idx = name_idx # rewind to where qstrs are in prelude |
| 640 | read_qstr_and_pack(f, fun_data, qstr_win) # simple_name |
| 641 | read_qstr_and_pack(f, fun_data, qstr_win) # source_file |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 642 | else: |
| 643 | prelude_offset = None |
| 644 | scope_flags = read_uint(f) |
| 645 | n_pos_args = 0 |
| 646 | if kind == MP_CODE_NATIVE_ASM: |
| 647 | n_pos_args = read_uint(f) |
| 648 | type_sig = read_uint(f) |
| 649 | prelude = (None, None, scope_flags, n_pos_args, 0) |
| 650 | |
Damien George | ea3c80a | 2019-02-21 15:18:59 +1100 | [diff] [blame] | 651 | qstrs = [] |
| 652 | objs = [] |
| 653 | raw_codes = [] |
| 654 | if kind != MP_CODE_NATIVE_ASM: |
| 655 | # load constant table |
| 656 | n_obj = read_uint(f) |
| 657 | n_raw_code = read_uint(f) |
| 658 | qstrs = [read_qstr(f, qstr_win) for _ in range(prelude[3] + prelude[4])] |
| 659 | if kind != MP_CODE_BYTECODE: |
| 660 | objs.append(MPFunTable) |
| 661 | objs.extend([read_obj(f) for _ in range(n_obj)]) |
| 662 | raw_codes = [read_raw_code(f, qstr_win) for _ in range(n_raw_code)] |
| 663 | |
| 664 | if kind == MP_CODE_BYTECODE: |
| 665 | return RawCodeBytecode(fun_data.buf, qstrs, objs, raw_codes) |
| 666 | else: |
| 667 | return RawCodeNative(kind, fun_data.buf, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 668 | |
| 669 | def read_mpy(filename): |
| 670 | with open(filename, 'rb') as f: |
Damien George | c3beb16 | 2016-04-15 11:56:10 +0100 | [diff] [blame] | 671 | header = bytes_cons(f.read(4)) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 672 | if header[0] != ord('M'): |
| 673 | raise Exception('not a valid .mpy file') |
Damien George | 6a11048 | 2017-02-17 00:19:34 +1100 | [diff] [blame] | 674 | if header[1] != config.MPY_VERSION: |
| 675 | raise Exception('incompatible .mpy version') |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 676 | feature_byte = header[2] |
| 677 | qw_size = read_uint(f) |
| 678 | config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_byte & 1) != 0 |
| 679 | config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0 |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 680 | mpy_native_arch = feature_byte >> 2 |
| 681 | if mpy_native_arch != MP_NATIVE_ARCH_NONE: |
| 682 | if config.native_arch == MP_NATIVE_ARCH_NONE: |
| 683 | config.native_arch = mpy_native_arch |
| 684 | elif config.native_arch != mpy_native_arch: |
| 685 | raise Exception('native architecture mismatch') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 686 | config.mp_small_int_bits = header[3] |
Damien George | 5996eeb | 2019-02-25 23:15:51 +1100 | [diff] [blame] | 687 | qstr_win = QStrWindow(qw_size) |
| 688 | return read_raw_code(f, qstr_win) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 689 | |
| 690 | def dump_mpy(raw_codes): |
| 691 | for rc in raw_codes: |
| 692 | rc.dump() |
| 693 | |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 694 | def freeze_mpy(base_qstrs, raw_codes): |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 695 | # add to qstrs |
| 696 | new = {} |
| 697 | for q in global_qstrs: |
| 698 | # don't add duplicates |
Damien George | 4f0931b | 2019-03-01 14:33:03 +1100 | [diff] [blame] | 699 | 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] | 700 | continue |
| 701 | new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) |
| 702 | new = sorted(new.values(), key=lambda x: x[0]) |
| 703 | |
| 704 | print('#include "py/mpconfig.h"') |
| 705 | print('#include "py/objint.h"') |
| 706 | print('#include "py/objstr.h"') |
| 707 | print('#include "py/emitglue.h"') |
| 708 | print() |
| 709 | |
Damien George | 98458a4 | 2017-01-05 15:52:52 +1100 | [diff] [blame] | 710 | print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) |
| 711 | print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"') |
Damien George | 99b4719 | 2016-05-16 23:13:30 +0100 | [diff] [blame] | 712 | print('#endif') |
| 713 | print() |
| 714 | |
| 715 | print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL) |
| 716 | print('#error "incompatible MICROPY_LONGINT_IMPL"') |
| 717 | print('#endif') |
| 718 | print() |
| 719 | |
| 720 | if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: |
| 721 | print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE) |
| 722 | print('#error "incompatible MPZ_DIG_SIZE"') |
| 723 | print('#endif') |
| 724 | print() |
| 725 | |
| 726 | |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 727 | print('#if MICROPY_PY_BUILTINS_FLOAT') |
| 728 | print('typedef struct _mp_obj_float_t {') |
| 729 | print(' mp_obj_base_t base;') |
| 730 | print(' mp_float_t value;') |
| 731 | print('} mp_obj_float_t;') |
| 732 | print('#endif') |
| 733 | print() |
| 734 | |
Damien George | c51c883 | 2016-09-03 00:19:02 +1000 | [diff] [blame] | 735 | print('#if MICROPY_PY_BUILTINS_COMPLEX') |
| 736 | print('typedef struct _mp_obj_complex_t {') |
| 737 | print(' mp_obj_base_t base;') |
| 738 | print(' mp_float_t real;') |
| 739 | print(' mp_float_t imag;') |
| 740 | print('} mp_obj_complex_t;') |
| 741 | print('#endif') |
| 742 | print() |
| 743 | |
Dave Hylands | 39eef27 | 2018-12-11 14:55:26 -0800 | [diff] [blame] | 744 | if len(new) > 0: |
| 745 | print('enum {') |
| 746 | for i in range(len(new)): |
| 747 | if i == 0: |
| 748 | print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) |
| 749 | else: |
| 750 | print(' MP_QSTR_%s,' % new[i][1]) |
| 751 | print('};') |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 752 | |
Rich Barlow | 6e5a40c | 2018-07-19 12:42:26 +0100 | [diff] [blame] | 753 | # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len |
| 754 | qstr_pool_alloc = min(len(new), 10) |
| 755 | |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 756 | print() |
| 757 | print('extern const qstr_pool_t mp_qstr_const_pool;'); |
| 758 | print('const qstr_pool_t mp_qstr_frozen_const_pool = {') |
| 759 | print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') |
| 760 | print(' MP_QSTRnumber_of, // previous pool size') |
Rich Barlow | 6e5a40c | 2018-07-19 12:42:26 +0100 | [diff] [blame] | 761 | print(' %u, // allocated entries' % qstr_pool_alloc) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 762 | print(' %u, // used entries' % len(new)) |
| 763 | print(' {') |
| 764 | for _, _, qstr in new: |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 765 | print(' %s,' |
| 766 | % 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] | 767 | print(' },') |
| 768 | print('};') |
| 769 | |
| 770 | for rc in raw_codes: |
| 771 | rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') |
| 772 | |
| 773 | print() |
| 774 | print('const char mp_frozen_mpy_names[] = {') |
| 775 | for rc in raw_codes: |
Damien George | 9b4c013 | 2016-05-23 12:46:02 +0100 | [diff] [blame] | 776 | module_name = rc.source_file.str |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 777 | print('"%s\\0"' % module_name) |
| 778 | print('"\\0"};') |
| 779 | |
| 780 | print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') |
| 781 | for rc in raw_codes: |
| 782 | print(' &raw_code_%s,' % rc.escaped_name) |
| 783 | print('};') |
| 784 | |
| 785 | def main(): |
| 786 | import argparse |
| 787 | cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.') |
| 788 | cmd_parser.add_argument('-d', '--dump', action='store_true', |
| 789 | help='dump contents of files') |
| 790 | cmd_parser.add_argument('-f', '--freeze', action='store_true', |
| 791 | help='freeze files') |
| 792 | cmd_parser.add_argument('-q', '--qstr-header', |
| 793 | help='qstr header file to freeze against') |
| 794 | cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz', |
| 795 | help='long-int implementation used by target (default mpz)') |
| 796 | cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16, |
| 797 | help='mpz digit size used by target (default 16)') |
| 798 | cmd_parser.add_argument('files', nargs='+', |
| 799 | help='input .mpy files') |
| 800 | args = cmd_parser.parse_args() |
| 801 | |
| 802 | # set config values relevant to target machine |
| 803 | config.MICROPY_LONGINT_IMPL = { |
| 804 | 'none':config.MICROPY_LONGINT_IMPL_NONE, |
| 805 | 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG, |
| 806 | 'mpz':config.MICROPY_LONGINT_IMPL_MPZ, |
| 807 | }[args.mlongint_impl] |
| 808 | config.MPZ_DIG_SIZE = args.mmpz_dig_size |
Damien George | faf3d3e | 2019-06-04 22:13:32 +1000 | [diff] [blame] | 809 | config.native_arch = MP_NATIVE_ARCH_NONE |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 810 | |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 811 | # 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] | 812 | if args.qstr_header: |
| 813 | qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header]) |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 814 | config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN']) |
| 815 | config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH']) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 816 | else: |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 817 | config.MICROPY_QSTR_BYTES_IN_LEN = 1 |
| 818 | config.MICROPY_QSTR_BYTES_IN_HASH = 1 |
| 819 | base_qstrs = {} |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 820 | |
| 821 | raw_codes = [read_mpy(file) for file in args.files] |
| 822 | |
| 823 | if args.dump: |
| 824 | dump_mpy(raw_codes) |
| 825 | elif args.freeze: |
| 826 | try: |
Damien George | b4790af | 2016-09-02 15:09:21 +1000 | [diff] [blame] | 827 | freeze_mpy(base_qstrs, raw_codes) |
Damien George | 0699c6b | 2016-01-31 21:45:22 +0000 | [diff] [blame] | 828 | except FreezeError as er: |
| 829 | print(er, file=sys.stderr) |
| 830 | sys.exit(1) |
| 831 | |
| 832 | if __name__ == '__main__': |
| 833 | main() |