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