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