2016-01-31 16:45:22 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
2019-06-04 08:13:32 -04:00
|
|
|
# Copyright (c) 2016-2019 Damien P. George
|
2016-01-31 16:45:22 -05:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
2016-04-15 06:56:10 -04:00
|
|
|
# Python 2/3 compatibility code
|
|
|
|
from __future__ import print_function
|
|
|
|
import platform
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
if platform.python_version_tuple()[0] == "2":
|
2016-04-15 06:56:10 -04:00
|
|
|
str_cons = lambda val, enc=None: val
|
|
|
|
bytes_cons = lambda val, enc=None: bytearray(val)
|
|
|
|
is_str_type = lambda o: type(o) is str
|
|
|
|
is_bytes_type = lambda o: type(o) is bytearray
|
|
|
|
is_int_type = lambda o: type(o) is int or type(o) is long
|
|
|
|
else:
|
|
|
|
str_cons = str
|
|
|
|
bytes_cons = bytes
|
|
|
|
is_str_type = lambda o: type(o) is str
|
|
|
|
is_bytes_type = lambda o: type(o) is bytes
|
|
|
|
is_int_type = lambda o: type(o) is int
|
|
|
|
# end compatibility code
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
import sys
|
2016-08-09 23:26:11 -04:00
|
|
|
import struct
|
2016-01-31 16:45:22 -05:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
sys.path.append(sys.path[0] + "/../py")
|
2016-01-31 16:45:22 -05:00
|
|
|
import makeqstrdata as qstrutil
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
class FreezeError(Exception):
|
|
|
|
def __init__(self, rawcode, msg):
|
|
|
|
self.rawcode = rawcode
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
return "error while freezing %s: %s" % (self.rawcode.source_file, self.msg)
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
class Config:
|
2019-09-26 02:39:37 -04:00
|
|
|
MPY_VERSION = 5
|
2016-01-31 16:45:22 -05:00
|
|
|
MICROPY_LONGINT_IMPL_NONE = 0
|
|
|
|
MICROPY_LONGINT_IMPL_LONGLONG = 1
|
|
|
|
MICROPY_LONGINT_IMPL_MPZ = 2
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
config = Config()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:33:03 -05:00
|
|
|
class QStrType:
|
|
|
|
def __init__(self, str):
|
|
|
|
self.str = str
|
|
|
|
self.qstr_esc = qstrutil.qstr_escape(self.str)
|
2020-02-26 23:36:53 -05:00
|
|
|
self.qstr_id = "MP_QSTR_" + self.qstr_esc
|
|
|
|
|
2019-02-28 22:33:03 -05:00
|
|
|
|
|
|
|
# Initialise global list of qstrs with static qstrs
|
2020-02-26 23:36:53 -05:00
|
|
|
global_qstrs = [None] # MP_QSTRnull should never be referenced
|
2019-02-28 22:33:03 -05:00
|
|
|
for n in qstrutil.static_qstr_list:
|
|
|
|
global_qstrs.append(QStrType(n))
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-25 07:15:51 -05:00
|
|
|
class QStrWindow:
|
2019-04-08 01:20:56 -04:00
|
|
|
def __init__(self, size):
|
2019-02-25 07:15:51 -05:00
|
|
|
self.window = []
|
2019-04-08 01:20:56 -04:00
|
|
|
self.size = size
|
2019-02-25 07:15:51 -05:00
|
|
|
|
|
|
|
def push(self, val):
|
2020-02-26 23:36:53 -05:00
|
|
|
self.window = [val] + self.window[: self.size - 1]
|
2019-02-25 07:15:51 -05:00
|
|
|
|
|
|
|
def access(self, idx):
|
|
|
|
val = self.window[idx]
|
2020-02-26 23:36:53 -05:00
|
|
|
self.window = [val] + self.window[:idx] + self.window[idx + 1 :]
|
2019-02-25 07:15:51 -05:00
|
|
|
return val
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
MP_CODE_BYTECODE = 2
|
|
|
|
MP_CODE_NATIVE_PY = 3
|
|
|
|
MP_CODE_NATIVE_VIPER = 4
|
|
|
|
MP_CODE_NATIVE_ASM = 5
|
|
|
|
|
|
|
|
MP_NATIVE_ARCH_NONE = 0
|
|
|
|
MP_NATIVE_ARCH_X86 = 1
|
|
|
|
MP_NATIVE_ARCH_X64 = 2
|
|
|
|
MP_NATIVE_ARCH_ARMV6 = 3
|
|
|
|
MP_NATIVE_ARCH_ARMV6M = 4
|
|
|
|
MP_NATIVE_ARCH_ARMV7M = 5
|
|
|
|
MP_NATIVE_ARCH_ARMV7EM = 6
|
|
|
|
MP_NATIVE_ARCH_ARMV7EMSP = 7
|
|
|
|
MP_NATIVE_ARCH_ARMV7EMDP = 8
|
|
|
|
MP_NATIVE_ARCH_XTENSA = 9
|
2019-09-12 23:15:12 -04:00
|
|
|
MP_NATIVE_ARCH_XTENSAWIN = 10
|
2019-02-20 23:18:59 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
MP_BC_MASK_EXTRA_BYTE = 0x9E
|
2019-09-02 07:35:26 -04:00
|
|
|
|
|
|
|
MP_BC_FORMAT_BYTE = 0
|
|
|
|
MP_BC_FORMAT_QSTR = 1
|
|
|
|
MP_BC_FORMAT_VAR_UINT = 2
|
|
|
|
MP_BC_FORMAT_OFFSET = 3
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
# extra byte if caching enabled:
|
2019-09-02 06:24:01 -04:00
|
|
|
MP_BC_LOAD_NAME = 0x11
|
|
|
|
MP_BC_LOAD_GLOBAL = 0x12
|
|
|
|
MP_BC_LOAD_ATTR = 0x13
|
|
|
|
MP_BC_STORE_ATTR = 0x18
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
# this function mirrors that in py/bc.c
|
2019-09-02 07:35:26 -04:00
|
|
|
def mp_opcode_format(bytecode, ip, count_var_uint):
|
2016-01-31 16:45:22 -05:00
|
|
|
opcode = bytecode[ip]
|
|
|
|
ip_start = ip
|
2020-02-26 23:36:53 -05:00
|
|
|
f = (0x000003A4 >> (2 * ((opcode) >> 4))) & 3
|
2019-09-02 07:35:26 -04:00
|
|
|
if f == MP_BC_FORMAT_QSTR:
|
2016-01-31 16:45:22 -05:00
|
|
|
ip += 3
|
|
|
|
else:
|
2019-09-02 07:35:26 -04:00
|
|
|
extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0
|
2016-01-31 16:45:22 -05:00
|
|
|
ip += 1
|
2019-09-02 07:35:26 -04:00
|
|
|
if f == MP_BC_FORMAT_VAR_UINT:
|
2019-02-28 22:03:10 -05:00
|
|
|
if count_var_uint:
|
|
|
|
while bytecode[ip] & 0x80 != 0:
|
|
|
|
ip += 1
|
2016-01-31 16:45:22 -05:00
|
|
|
ip += 1
|
2019-09-02 07:35:26 -04:00
|
|
|
elif f == MP_BC_FORMAT_OFFSET:
|
2016-01-31 16:45:22 -05:00
|
|
|
ip += 2
|
|
|
|
ip += extra_byte
|
|
|
|
return f, ip - ip_start
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-16 08:12:59 -04:00
|
|
|
def read_prelude_sig(read_byte):
|
|
|
|
z = read_byte()
|
|
|
|
# xSSSSEAA
|
2020-02-26 23:36:53 -05:00
|
|
|
S = (z >> 3) & 0xF
|
2019-09-16 08:12:59 -04:00
|
|
|
E = (z >> 2) & 0x1
|
|
|
|
F = 0
|
|
|
|
A = z & 0x3
|
|
|
|
K = 0
|
|
|
|
D = 0
|
|
|
|
n = 0
|
|
|
|
while z & 0x80:
|
|
|
|
z = read_byte()
|
|
|
|
# xFSSKAED
|
|
|
|
S |= (z & 0x30) << (2 * n)
|
|
|
|
E |= (z & 0x02) << n
|
|
|
|
F |= ((z & 0x40) >> 6) << n
|
|
|
|
A |= (z & 0x4) << n
|
|
|
|
K |= ((z & 0x08) >> 3) << n
|
|
|
|
D |= (z & 0x1) << n
|
|
|
|
n += 1
|
|
|
|
S += 1
|
|
|
|
return S, E, F, A, K, D
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-25 01:45:47 -04:00
|
|
|
def read_prelude_size(read_byte):
|
|
|
|
I = 0
|
|
|
|
C = 0
|
|
|
|
n = 0
|
|
|
|
while True:
|
|
|
|
z = read_byte()
|
|
|
|
# xIIIIIIC
|
2020-02-26 23:36:53 -05:00
|
|
|
I |= ((z & 0x7E) >> 1) << (6 * n)
|
2019-09-25 01:45:47 -04:00
|
|
|
C |= (z & 1) << n
|
|
|
|
if not (z & 0x80):
|
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
return I, C
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
def extract_prelude(bytecode, ip):
|
2019-09-16 08:12:59 -04:00
|
|
|
def local_read_byte():
|
|
|
|
b = bytecode[ip_ref[0]]
|
|
|
|
ip_ref[0] += 1
|
|
|
|
return b
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
ip_ref = [ip] # to close over ip in Python 2 and 3
|
|
|
|
(
|
|
|
|
n_state,
|
|
|
|
n_exc_stack,
|
|
|
|
scope_flags,
|
|
|
|
n_pos_args,
|
|
|
|
n_kwonly_args,
|
|
|
|
n_def_pos_args,
|
|
|
|
) = read_prelude_sig(local_read_byte)
|
2019-09-25 01:45:47 -04:00
|
|
|
n_info, n_cell = read_prelude_size(local_read_byte)
|
2019-09-16 08:12:59 -04:00
|
|
|
ip = ip_ref[0]
|
|
|
|
|
2019-09-25 01:45:47 -04:00
|
|
|
ip2 = ip
|
|
|
|
ip = ip2 + n_info + n_cell
|
2016-01-31 16:45:22 -05:00
|
|
|
# ip now points to first opcode
|
|
|
|
# ip2 points to simple_name qstr
|
2019-09-25 01:45:47 -04:00
|
|
|
return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args)
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
class MPFunTable:
|
|
|
|
pass
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-04-07 21:21:18 -04:00
|
|
|
class RawCode(object):
|
2016-05-03 07:24:39 -04:00
|
|
|
# a set of all escaped names, to make sure they are unique
|
|
|
|
escaped_names = set()
|
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
# convert code kind number to string
|
|
|
|
code_kind_str = {
|
2020-02-26 23:36:53 -05:00
|
|
|
MP_CODE_BYTECODE: "MP_CODE_BYTECODE",
|
|
|
|
MP_CODE_NATIVE_PY: "MP_CODE_NATIVE_PY",
|
|
|
|
MP_CODE_NATIVE_VIPER: "MP_CODE_NATIVE_VIPER",
|
|
|
|
MP_CODE_NATIVE_ASM: "MP_CODE_NATIVE_ASM",
|
2019-02-20 23:18:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes):
|
2016-01-31 16:45:22 -05:00
|
|
|
# set core variables
|
2019-02-20 23:18:59 -05:00
|
|
|
self.code_kind = code_kind
|
2016-01-31 16:45:22 -05:00
|
|
|
self.bytecode = bytecode
|
2019-02-20 23:18:59 -05:00
|
|
|
self.prelude_offset = prelude_offset
|
2016-01-31 16:45:22 -05:00
|
|
|
self.qstrs = qstrs
|
|
|
|
self.objs = objs
|
|
|
|
self.raw_codes = raw_codes
|
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
if self.prelude_offset is None:
|
|
|
|
# no prelude, assign a dummy simple_name
|
|
|
|
self.prelude_offset = 0
|
|
|
|
self.simple_name = global_qstrs[1]
|
|
|
|
else:
|
|
|
|
# extract prelude
|
|
|
|
self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset)
|
|
|
|
self.simple_name = self._unpack_qstr(self.ip2)
|
|
|
|
self.source_file = self._unpack_qstr(self.ip2 + 2)
|
2020-08-13 09:20:08 -04:00
|
|
|
self.line_info_offset = self.ip2 + 4
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
def _unpack_qstr(self, ip):
|
|
|
|
qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
|
|
|
|
return global_qstrs[qst]
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
# dump children first
|
|
|
|
for rc in self.raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
rc.freeze("")
|
2016-01-31 16:45:22 -05:00
|
|
|
# TODO
|
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
def freeze_children(self, parent_name):
|
2016-01-31 16:45:22 -05:00
|
|
|
self.escaped_name = parent_name + self.simple_name.qstr_esc
|
|
|
|
|
2016-05-03 07:24:39 -04:00
|
|
|
# make sure the escaped name is unique
|
|
|
|
i = 2
|
|
|
|
while self.escaped_name in RawCode.escaped_names:
|
|
|
|
self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
|
|
|
|
i += 1
|
|
|
|
RawCode.escaped_names.add(self.escaped_name)
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
# emit children first
|
|
|
|
for rc in self.raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
rc.freeze(self.escaped_name + "_")
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
def freeze_constants(self):
|
2016-01-31 16:45:22 -05:00
|
|
|
# generate constant objects
|
|
|
|
for i, obj in enumerate(self.objs):
|
2020-02-26 23:36:53 -05:00
|
|
|
obj_name = "const_obj_%s_%u" % (self.escaped_name, i)
|
2019-02-20 23:18:59 -05:00
|
|
|
if obj is MPFunTable:
|
|
|
|
pass
|
|
|
|
elif obj is Ellipsis:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#define %s mp_const_ellipsis_obj" % obj_name)
|
2017-11-14 20:46:08 -05:00
|
|
|
elif is_str_type(obj) or is_bytes_type(obj):
|
2016-09-02 01:10:45 -04:00
|
|
|
if is_str_type(obj):
|
2020-02-26 23:36:53 -05:00
|
|
|
obj = bytes_cons(obj, "utf8")
|
|
|
|
obj_type = "mp_type_str"
|
2016-09-02 01:10:45 -04:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
obj_type = "mp_type_bytes"
|
|
|
|
print(
|
|
|
|
'STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
|
|
|
|
% (
|
|
|
|
obj_name,
|
|
|
|
obj_type,
|
|
|
|
qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
|
|
|
|
len(obj),
|
|
|
|
"".join(("\\x%02x" % b) for b in obj),
|
|
|
|
)
|
|
|
|
)
|
2016-04-15 06:56:10 -04:00
|
|
|
elif is_int_type(obj):
|
2016-01-31 16:45:22 -05:00
|
|
|
if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
|
|
|
|
# TODO check if we can actually fit this long-int into a small-int
|
2020-02-26 23:36:53 -05:00
|
|
|
raise FreezeError(self, "target does not support long int")
|
2016-01-31 16:45:22 -05:00
|
|
|
elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
|
|
|
|
# TODO
|
2020-02-26 23:36:53 -05:00
|
|
|
raise FreezeError(self, "freezing int to long-long is not implemented")
|
2016-01-31 16:45:22 -05:00
|
|
|
elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
|
|
|
|
neg = 0
|
|
|
|
if obj < 0:
|
|
|
|
obj = -obj
|
|
|
|
neg = 1
|
|
|
|
bits_per_dig = config.MPZ_DIG_SIZE
|
|
|
|
digs = []
|
|
|
|
z = obj
|
|
|
|
while z:
|
|
|
|
digs.append(z & ((1 << bits_per_dig) - 1))
|
|
|
|
z >>= bits_per_dig
|
|
|
|
ndigs = len(digs)
|
2020-02-26 23:36:53 -05:00
|
|
|
digs = ",".join(("%#x" % d) for d in digs)
|
|
|
|
print(
|
|
|
|
"STATIC const mp_obj_int_t %s = {{&mp_type_int}, "
|
|
|
|
"{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};"
|
|
|
|
% (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs)
|
|
|
|
)
|
2016-01-31 16:45:22 -05:00
|
|
|
elif type(obj) is float:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B"
|
|
|
|
)
|
|
|
|
print(
|
2020-03-31 08:48:08 -04:00
|
|
|
"STATIC const mp_obj_float_t %s = {{&mp_type_float}, (mp_float_t)%.16g};"
|
|
|
|
% (obj_name, obj)
|
2020-02-26 23:36:53 -05:00
|
|
|
)
|
|
|
|
print("#endif")
|
2016-09-02 10:19:02 -04:00
|
|
|
elif type(obj) is complex:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
2020-03-31 08:48:08 -04:00
|
|
|
"STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, (mp_float_t)%.16g, (mp_float_t)%.16g};"
|
2020-02-26 23:36:53 -05:00
|
|
|
% (obj_name, obj.real, obj.imag)
|
|
|
|
)
|
2016-01-31 16:45:22 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
raise FreezeError(self, "freezing of object %r is not implemented" % (obj,))
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2017-08-12 08:26:18 -04:00
|
|
|
# generate constant table, if it has any entries
|
|
|
|
const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
|
|
|
|
if const_table_len:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"STATIC const mp_rom_obj_t const_table_data_%s[%u] = {"
|
|
|
|
% (self.escaped_name, const_table_len)
|
|
|
|
)
|
2017-08-12 08:26:18 -04:00
|
|
|
for qst in self.qstrs:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" MP_ROM_QSTR(%s)," % global_qstrs[qst].qstr_id)
|
2017-08-12 08:26:18 -04:00
|
|
|
for i in range(len(self.objs)):
|
2019-02-20 23:18:59 -05:00
|
|
|
if self.objs[i] is MPFunTable:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" &mp_fun_table,")
|
2019-02-20 23:18:59 -05:00
|
|
|
elif type(self.objs[i]) is float:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B"
|
|
|
|
)
|
|
|
|
print(" MP_ROM_PTR(&const_obj_%s_%u)," % (self.escaped_name, i))
|
|
|
|
print("#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C")
|
|
|
|
n = struct.unpack("<I", struct.pack("<f", self.objs[i]))[0]
|
2017-08-12 08:26:18 -04:00
|
|
|
n = ((n & ~0x3) | 2) + 0x80800000
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" (mp_rom_obj_t)(0x%08x)," % (n,))
|
|
|
|
print("#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D")
|
|
|
|
n = struct.unpack("<Q", struct.pack("<d", self.objs[i]))[0]
|
2018-07-08 22:22:40 -04:00
|
|
|
n += 0x8004000000000000
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" (mp_rom_obj_t)(0x%016x)," % (n,))
|
|
|
|
print("#endif")
|
2017-08-12 08:26:18 -04:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" MP_ROM_PTR(&const_obj_%s_%u)," % (self.escaped_name, i))
|
2017-08-12 08:26:18 -04:00
|
|
|
for rc in self.raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" MP_ROM_PTR(&raw_code_%s)," % rc.escaped_name)
|
|
|
|
print("};")
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
def freeze_module(self, qstr_links=(), type_sig=0):
|
2016-01-31 16:45:22 -05:00
|
|
|
# generate module
|
2020-02-26 23:36:53 -05:00
|
|
|
if self.simple_name.str != "<module>":
|
|
|
|
print("STATIC ", end="")
|
|
|
|
print("const mp_raw_code_t raw_code_%s = {" % self.escaped_name)
|
|
|
|
print(" .kind = %s," % RawCode.code_kind_str[self.code_kind])
|
|
|
|
print(" .scope_flags = 0x%02x," % self.prelude[2])
|
|
|
|
print(" .n_pos_args = %u," % self.prelude[3])
|
|
|
|
print(" .fun_data = fun_data_%s," % self.escaped_name)
|
2019-02-20 23:18:59 -05:00
|
|
|
if len(self.qstrs) + len(self.objs) + len(self.raw_codes):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" .const_table = (mp_uint_t*)const_table_data_%s," % self.escaped_name)
|
2017-08-12 08:26:18 -04:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" .const_table = NULL,")
|
|
|
|
print(" #if MICROPY_PERSISTENT_CODE_SAVE")
|
|
|
|
print(" .fun_data_len = %u," % len(self.bytecode))
|
|
|
|
print(" .n_obj = %u," % len(self.objs))
|
|
|
|
print(" .n_raw_code = %u," % len(self.raw_codes))
|
2019-09-06 09:55:15 -04:00
|
|
|
if self.code_kind == MP_CODE_BYTECODE:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" #if MICROPY_PY_SYS_SETTRACE")
|
|
|
|
print(" .prelude = {")
|
|
|
|
print(" .n_state = %u," % self.prelude[0])
|
|
|
|
print(" .n_exc_stack = %u," % self.prelude[1])
|
|
|
|
print(" .scope_flags = %u," % self.prelude[2])
|
|
|
|
print(" .n_pos_args = %u," % self.prelude[3])
|
|
|
|
print(" .n_kwonly_args = %u," % self.prelude[4])
|
|
|
|
print(" .n_def_pos_args = %u," % self.prelude[5])
|
|
|
|
print(" .qstr_block_name = %s," % self.simple_name.qstr_id)
|
|
|
|
print(" .qstr_source_file = %s," % self.source_file.qstr_id)
|
2020-08-13 09:20:08 -04:00
|
|
|
print(
|
|
|
|
" .line_info = fun_data_%s + %u,"
|
|
|
|
% (self.escaped_name, self.line_info_offset)
|
|
|
|
)
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" .opcodes = fun_data_%s + %u," % (self.escaped_name, self.ip))
|
|
|
|
print(" },")
|
|
|
|
print(" .line_of_definition = %u," % 0) # TODO
|
|
|
|
print(" #endif")
|
|
|
|
print(" #if MICROPY_EMIT_MACHINE_CODE")
|
|
|
|
print(" .prelude_offset = %u," % self.prelude_offset)
|
|
|
|
print(" .n_qstr = %u," % len(qstr_links))
|
|
|
|
print(" .qstr_link = NULL,") # TODO
|
|
|
|
print(" #endif")
|
|
|
|
print(" #endif")
|
|
|
|
print(" #if MICROPY_EMIT_MACHINE_CODE")
|
|
|
|
print(" .type_sig = %u," % type_sig)
|
|
|
|
print(" #endif")
|
|
|
|
print("};")
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
class RawCodeBytecode(RawCode):
|
|
|
|
def __init__(self, bytecode, qstrs, objs, raw_codes):
|
2020-02-26 23:36:53 -05:00
|
|
|
super(RawCodeBytecode, self).__init__(
|
|
|
|
MP_CODE_BYTECODE, bytecode, 0, qstrs, objs, raw_codes
|
|
|
|
)
|
2019-02-20 23:18:59 -05:00
|
|
|
|
|
|
|
def freeze(self, parent_name):
|
|
|
|
self.freeze_children(parent_name)
|
|
|
|
|
|
|
|
# generate bytecode data
|
|
|
|
print()
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"// frozen bytecode for file %s, scope %s%s"
|
|
|
|
% (self.source_file.str, parent_name, self.simple_name.str)
|
|
|
|
)
|
all: Remove MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE.
This commit removes all parts of code associated with the existing
MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE optimisation option, including the
-mcache-lookup-bc option to mpy-cross.
This feature originally provided a significant performance boost for Unix,
but wasn't able to be enabled for MCU targets (due to frozen bytecode), and
added significant extra complexity to generating and distributing .mpy
files.
The equivalent performance gain is now provided by the combination of
MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE (which has
been enabled on the unix port in the previous commit).
It's hard to provide precise performance numbers, but tests have been run
on a wide variety of architectures (x86-64, ARM Cortex, Aarch64, RISC-V,
xtensa) and they all generally agree on the qualitative improvements seen
by the combination of MICROPY_OPT_LOAD_ATTR_FAST_PATH and
MICROPY_OPT_MAP_LOOKUP_CACHE.
For example, on a "quiet" Linux x64 environment (i3-5010U @ 2.10GHz) the
change from CACHE_MAP_LOOKUP_IN_BYTECODE, to LOAD_ATTR_FAST_PATH combined
with MAP_LOOKUP_CACHE is:
diff of scores (higher is better)
N=2000 M=2000 bccache -> attrmapcache diff diff% (error%)
bm_chaos.py 13742.56 -> 13905.67 : +163.11 = +1.187% (+/-3.75%)
bm_fannkuch.py 60.13 -> 61.34 : +1.21 = +2.012% (+/-2.11%)
bm_fft.py 113083.20 -> 114793.68 : +1710.48 = +1.513% (+/-1.57%)
bm_float.py 256552.80 -> 243908.29 : -12644.51 = -4.929% (+/-1.90%)
bm_hexiom.py 521.93 -> 625.41 : +103.48 = +19.826% (+/-0.40%)
bm_nqueens.py 197544.25 -> 217713.12 : +20168.87 = +10.210% (+/-3.01%)
bm_pidigits.py 8072.98 -> 8198.75 : +125.77 = +1.558% (+/-3.22%)
misc_aes.py 17283.45 -> 16480.52 : -802.93 = -4.646% (+/-0.82%)
misc_mandel.py 99083.99 -> 128939.84 : +29855.85 = +30.132% (+/-5.88%)
misc_pystone.py 83860.10 -> 82592.56 : -1267.54 = -1.511% (+/-2.27%)
misc_raytrace.py 21490.40 -> 22227.23 : +736.83 = +3.429% (+/-1.88%)
This shows that the new optimisations are at least as good as the existing
inline-bytecode-caching, and are sometimes much better (because the new
ones apply caching to a wider variety of map lookups).
The new optimisations can also benefit code generated by the native
emitter, because they apply to the runtime rather than the generated code.
The improvement for the native emitter when LOAD_ATTR_FAST_PATH and
MAP_LOOKUP_CACHE are enabled is (same Linux environment as above):
diff of scores (higher is better)
N=2000 M=2000 native -> nat-attrmapcache diff diff% (error%)
bm_chaos.py 14130.62 -> 15464.68 : +1334.06 = +9.441% (+/-7.11%)
bm_fannkuch.py 74.96 -> 76.16 : +1.20 = +1.601% (+/-1.80%)
bm_fft.py 166682.99 -> 168221.86 : +1538.87 = +0.923% (+/-4.20%)
bm_float.py 233415.23 -> 265524.90 : +32109.67 = +13.756% (+/-2.57%)
bm_hexiom.py 628.59 -> 734.17 : +105.58 = +16.796% (+/-1.39%)
bm_nqueens.py 225418.44 -> 232926.45 : +7508.01 = +3.331% (+/-3.10%)
bm_pidigits.py 6322.00 -> 6379.52 : +57.52 = +0.910% (+/-5.62%)
misc_aes.py 20670.10 -> 27223.18 : +6553.08 = +31.703% (+/-1.56%)
misc_mandel.py 138221.11 -> 152014.01 : +13792.90 = +9.979% (+/-2.46%)
misc_pystone.py 85032.14 -> 105681.44 : +20649.30 = +24.284% (+/-2.25%)
misc_raytrace.py 19800.01 -> 23350.73 : +3550.72 = +17.933% (+/-2.79%)
In summary, compared to MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, the new
MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE options:
- are simpler;
- take less code size;
- are faster (generally);
- work with code generated by the native emitter;
- can be used on embedded targets with a small and constant RAM overhead;
- allow the same .mpy bytecode to run on all targets.
See #7680 for further discussion. And see also #7653 for a discussion
about simplifying mpy-cross options.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-05 22:28:06 -04:00
|
|
|
print("STATIC const byte fun_data_%s[%u] = {" % (self.escaped_name, len(self.bytecode)))
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
for i in range(self.ip2):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" 0x%02x," % self.bytecode[i], end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
print()
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", self.simple_name.qstr_id, "& 0xff,", self.simple_name.qstr_id, ">> 8,")
|
|
|
|
print(" ", self.source_file.qstr_id, "& 0xff,", self.source_file.qstr_id, ">> 8,")
|
|
|
|
print(" ", end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
for i in range(self.ip2 + 4, self.ip):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" 0x%02x," % self.bytecode[i], end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
print()
|
|
|
|
ip = self.ip
|
|
|
|
while ip < len(self.bytecode):
|
|
|
|
f, sz = mp_opcode_format(self.bytecode, ip, True)
|
|
|
|
if f == 1:
|
|
|
|
qst = self._unpack_qstr(ip + 1).qstr_id
|
2020-02-26 23:36:53 -05:00
|
|
|
extra = "" if sz == 3 else " 0x%02x," % self.bytecode[ip + 3]
|
|
|
|
print(" ", "0x%02x," % self.bytecode[ip], qst, "& 0xff,", qst, ">> 8,", extra)
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", "".join("0x%02x, " % self.bytecode[ip + i] for i in range(sz)))
|
2019-02-20 23:18:59 -05:00
|
|
|
ip += sz
|
2020-02-26 23:36:53 -05:00
|
|
|
print("};")
|
2019-02-20 23:18:59 -05:00
|
|
|
|
|
|
|
self.freeze_constants()
|
|
|
|
self.freeze_module()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
class RawCodeNative(RawCode):
|
2020-02-26 23:36:53 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
code_kind,
|
|
|
|
fun_data,
|
|
|
|
prelude_offset,
|
|
|
|
prelude,
|
|
|
|
qstr_links,
|
|
|
|
qstrs,
|
|
|
|
objs,
|
|
|
|
raw_codes,
|
|
|
|
type_sig,
|
|
|
|
):
|
|
|
|
super(RawCodeNative, self).__init__(
|
|
|
|
code_kind, fun_data, prelude_offset, qstrs, objs, raw_codes
|
|
|
|
)
|
2019-02-20 23:18:59 -05:00
|
|
|
self.prelude = prelude
|
|
|
|
self.qstr_links = qstr_links
|
|
|
|
self.type_sig = type_sig
|
2020-02-26 23:36:53 -05:00
|
|
|
if config.native_arch in (
|
|
|
|
MP_NATIVE_ARCH_X86,
|
|
|
|
MP_NATIVE_ARCH_X64,
|
|
|
|
MP_NATIVE_ARCH_XTENSA,
|
|
|
|
MP_NATIVE_ARCH_XTENSAWIN,
|
|
|
|
):
|
2019-02-20 23:18:59 -05:00
|
|
|
self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))'
|
|
|
|
else:
|
|
|
|
self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
|
|
|
|
|
2019-11-27 21:11:51 -05:00
|
|
|
# Allow single-byte alignment by default for x86/x64.
|
|
|
|
# ARM needs word alignment, ARM Thumb needs halfword, due to instruction size.
|
|
|
|
# Xtensa needs word alignment due to the 32-bit constant table embedded in the code.
|
2020-02-26 23:36:53 -05:00
|
|
|
if config.native_arch in (
|
|
|
|
MP_NATIVE_ARCH_ARMV6,
|
|
|
|
MP_NATIVE_ARCH_XTENSA,
|
|
|
|
MP_NATIVE_ARCH_XTENSAWIN,
|
|
|
|
):
|
2019-11-27 21:11:51 -05:00
|
|
|
# ARMV6 or Xtensa -- four byte align.
|
2020-02-26 23:36:53 -05:00
|
|
|
self.fun_data_attributes += " __attribute__ ((aligned (4)))"
|
2019-08-16 10:32:04 -04:00
|
|
|
elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
|
|
|
|
# ARMVxxM -- two byte align.
|
2020-02-26 23:36:53 -05:00
|
|
|
self.fun_data_attributes += " __attribute__ ((aligned (2)))"
|
2019-08-16 10:32:04 -04:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
def _asm_thumb_rewrite_mov(self, pc, val):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" (%u & 0xf0) | (%s >> 12)," % (self.bytecode[pc], val), end="")
|
|
|
|
print(" (%u & 0xfb) | (%s >> 9 & 0x04)," % (self.bytecode[pc + 1], val), end="")
|
|
|
|
print(" (%s & 0xff)," % (val,), end="")
|
|
|
|
print(" (%u & 0x07) | (%s >> 4 & 0x70)," % (self.bytecode[pc + 3], val))
|
2019-02-20 23:18:59 -05:00
|
|
|
|
|
|
|
def _link_qstr(self, pc, kind, qst):
|
|
|
|
if kind == 0:
|
2019-06-04 08:13:32 -04:00
|
|
|
# Generic 16-bit link
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" %s & 0xff, %s >> 8," % (qst, qst))
|
2019-06-10 21:36:39 -04:00
|
|
|
return 2
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
2019-06-04 08:13:32 -04:00
|
|
|
# Architecture-specific link
|
|
|
|
is_obj = kind == 2
|
|
|
|
if is_obj:
|
2020-02-26 23:36:53 -05:00
|
|
|
qst = "((uintptr_t)MP_OBJ_NEW_QSTR(%s))" % qst
|
2019-11-27 21:11:51 -05:00
|
|
|
if config.native_arch in (
|
2020-02-26 23:36:53 -05:00
|
|
|
MP_NATIVE_ARCH_X86,
|
|
|
|
MP_NATIVE_ARCH_X64,
|
2021-05-25 08:16:06 -04:00
|
|
|
MP_NATIVE_ARCH_ARMV6,
|
2020-02-26 23:36:53 -05:00
|
|
|
MP_NATIVE_ARCH_XTENSA,
|
|
|
|
MP_NATIVE_ARCH_XTENSAWIN,
|
|
|
|
):
|
|
|
|
print(
|
|
|
|
" %s & 0xff, (%s >> 8) & 0xff, (%s >> 16) & 0xff, %s >> 24,"
|
|
|
|
% (qst, qst, qst, qst)
|
|
|
|
)
|
2019-06-10 21:36:39 -04:00
|
|
|
return 4
|
2019-02-20 23:18:59 -05:00
|
|
|
elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
|
|
|
|
if is_obj:
|
2019-06-04 08:13:32 -04:00
|
|
|
# qstr object, movw and movt
|
|
|
|
self._asm_thumb_rewrite_mov(pc, qst)
|
2020-02-26 23:36:53 -05:00
|
|
|
self._asm_thumb_rewrite_mov(pc + 4, "(%s >> 16)" % qst)
|
2019-06-10 21:36:39 -04:00
|
|
|
return 8
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
2019-06-04 08:13:32 -04:00
|
|
|
# qstr number, movw instruction
|
|
|
|
self._asm_thumb_rewrite_mov(pc, qst)
|
2019-06-10 21:36:39 -04:00
|
|
|
return 4
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def freeze(self, parent_name):
|
2020-02-26 23:36:53 -05:00
|
|
|
if self.prelude[2] & ~0x0F:
|
|
|
|
raise FreezeError("unable to freeze code with relocations")
|
2019-12-09 22:57:12 -05:00
|
|
|
|
2019-02-20 23:18:59 -05:00
|
|
|
self.freeze_children(parent_name)
|
|
|
|
|
|
|
|
# generate native code data
|
|
|
|
print()
|
|
|
|
if self.code_kind == MP_CODE_NATIVE_PY:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"// frozen native code for file %s, scope %s%s"
|
|
|
|
% (self.source_file.str, parent_name, self.simple_name.str)
|
|
|
|
)
|
2019-02-20 23:18:59 -05:00
|
|
|
elif self.code_kind == MP_CODE_NATIVE_VIPER:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// frozen viper code for scope %s" % (parent_name,))
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// frozen assembler code for scope %s" % (parent_name,))
|
|
|
|
print(
|
|
|
|
"STATIC const byte fun_data_%s[%u] %s = {"
|
|
|
|
% (self.escaped_name, len(self.bytecode), self.fun_data_attributes)
|
|
|
|
)
|
2019-02-20 23:18:59 -05:00
|
|
|
|
|
|
|
if self.code_kind == MP_CODE_NATIVE_PY:
|
|
|
|
i_top = self.prelude_offset
|
|
|
|
else:
|
|
|
|
i_top = len(self.bytecode)
|
|
|
|
i = 0
|
|
|
|
qi = 0
|
|
|
|
while i < i_top:
|
|
|
|
if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]:
|
|
|
|
# link qstr
|
|
|
|
qi_off, qi_kind, qi_val = self.qstr_links[qi]
|
|
|
|
qst = global_qstrs[qi_val].qstr_id
|
2019-06-10 21:36:39 -04:00
|
|
|
i += self._link_qstr(i, qi_kind, qst)
|
2019-02-20 23:18:59 -05:00
|
|
|
qi += 1
|
|
|
|
else:
|
|
|
|
# copy machine code (max 16 bytes)
|
|
|
|
i16 = min(i + 16, i_top)
|
|
|
|
if qi < len(self.qstr_links):
|
|
|
|
i16 = min(i16, self.qstr_links[qi][0])
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
for ii in range(i, i16):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" 0x%02x," % self.bytecode[ii], end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
print()
|
|
|
|
i = i16
|
|
|
|
|
|
|
|
if self.code_kind == MP_CODE_NATIVE_PY:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
for i in range(self.prelude_offset, self.ip2):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" 0x%02x," % self.bytecode[i], end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
print()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", self.simple_name.qstr_id, "& 0xff,", self.simple_name.qstr_id, ">> 8,")
|
|
|
|
print(" ", self.source_file.qstr_id, "& 0xff,", self.source_file.qstr_id, ">> 8,")
|
2019-02-20 23:18:59 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ", end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
for i in range(self.ip2 + 4, self.ip):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" 0x%02x," % self.bytecode[i], end="")
|
2019-02-20 23:18:59 -05:00
|
|
|
print()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("};")
|
2019-02-20 23:18:59 -05:00
|
|
|
|
|
|
|
self.freeze_constants()
|
|
|
|
self.freeze_module(self.qstr_links, self.type_sig)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:03:10 -05:00
|
|
|
class BytecodeBuffer:
|
|
|
|
def __init__(self, size):
|
|
|
|
self.buf = bytearray(size)
|
|
|
|
self.idx = 0
|
|
|
|
|
|
|
|
def is_full(self):
|
|
|
|
return self.idx == len(self.buf)
|
|
|
|
|
|
|
|
def append(self, b):
|
|
|
|
self.buf[self.idx] = b
|
|
|
|
self.idx += 1
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:03:10 -05:00
|
|
|
def read_byte(f, out=None):
|
|
|
|
b = bytes_cons(f.read(1))[0]
|
|
|
|
if out is not None:
|
|
|
|
out.append(b)
|
|
|
|
return b
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:03:10 -05:00
|
|
|
def read_uint(f, out=None):
|
2016-01-31 16:45:22 -05:00
|
|
|
i = 0
|
|
|
|
while True:
|
2019-02-28 22:03:10 -05:00
|
|
|
b = read_byte(f, out)
|
2020-02-26 23:36:53 -05:00
|
|
|
i = (i << 7) | (b & 0x7F)
|
2016-01-31 16:45:22 -05:00
|
|
|
if b & 0x80 == 0:
|
|
|
|
break
|
|
|
|
return i
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-25 07:15:51 -05:00
|
|
|
def read_qstr(f, qstr_win):
|
2016-01-31 16:45:22 -05:00
|
|
|
ln = read_uint(f)
|
2019-02-28 22:33:03 -05:00
|
|
|
if ln == 0:
|
|
|
|
# static qstr
|
|
|
|
return bytes_cons(f.read(1))[0]
|
2019-02-25 07:15:51 -05:00
|
|
|
if ln & 1:
|
|
|
|
# qstr in table
|
|
|
|
return qstr_win.access(ln >> 1)
|
|
|
|
ln >>= 1
|
2020-02-26 23:36:53 -05:00
|
|
|
data = str_cons(f.read(ln), "utf8")
|
2019-02-28 22:33:03 -05:00
|
|
|
global_qstrs.append(QStrType(data))
|
2019-02-25 07:15:51 -05:00
|
|
|
qstr_win.push(len(global_qstrs) - 1)
|
2016-01-31 16:45:22 -05:00
|
|
|
return len(global_qstrs) - 1
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
def read_obj(f):
|
|
|
|
obj_type = f.read(1)
|
2020-02-26 23:36:53 -05:00
|
|
|
if obj_type == b"e":
|
2016-01-31 16:45:22 -05:00
|
|
|
return Ellipsis
|
|
|
|
else:
|
|
|
|
buf = f.read(read_uint(f))
|
2020-02-26 23:36:53 -05:00
|
|
|
if obj_type == b"s":
|
|
|
|
return str_cons(buf, "utf8")
|
|
|
|
elif obj_type == b"b":
|
2016-04-15 06:56:10 -04:00
|
|
|
return bytes_cons(buf)
|
2020-02-26 23:36:53 -05:00
|
|
|
elif obj_type == b"i":
|
|
|
|
return int(str_cons(buf, "ascii"), 10)
|
|
|
|
elif obj_type == b"f":
|
|
|
|
return float(str_cons(buf, "ascii"))
|
|
|
|
elif obj_type == b"c":
|
|
|
|
return complex(str_cons(buf, "ascii"))
|
2016-01-31 16:45:22 -05:00
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-10-10 00:30:16 -04:00
|
|
|
def read_prelude(f, bytecode, qstr_win):
|
2020-02-26 23:36:53 -05:00
|
|
|
(
|
|
|
|
n_state,
|
|
|
|
n_exc_stack,
|
|
|
|
scope_flags,
|
|
|
|
n_pos_args,
|
|
|
|
n_kwonly_args,
|
|
|
|
n_def_pos_args,
|
|
|
|
) = read_prelude_sig(lambda: read_byte(f, bytecode))
|
2019-09-25 01:45:47 -04:00
|
|
|
n_info, n_cell = read_prelude_size(lambda: read_byte(f, bytecode))
|
2020-02-26 23:36:53 -05:00
|
|
|
read_qstr_and_pack(f, bytecode, qstr_win) # simple_name
|
|
|
|
read_qstr_and_pack(f, bytecode, qstr_win) # source_file
|
2019-10-10 00:30:16 -04:00
|
|
|
for _ in range(n_info - 4 + n_cell):
|
2019-02-28 22:03:10 -05:00
|
|
|
read_byte(f, bytecode)
|
2019-10-10 00:30:16 -04:00
|
|
|
return n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args
|
2019-02-28 22:03:10 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:03:10 -05:00
|
|
|
def read_qstr_and_pack(f, bytecode, qstr_win):
|
2019-02-25 07:15:51 -05:00
|
|
|
qst = read_qstr(f, qstr_win)
|
2020-02-26 23:36:53 -05:00
|
|
|
bytecode.append(qst & 0xFF)
|
2019-02-28 22:03:10 -05:00
|
|
|
bytecode.append(qst >> 8)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-28 22:03:10 -05:00
|
|
|
def read_bytecode(file, bytecode, qstr_win):
|
|
|
|
while not bytecode.is_full():
|
|
|
|
op = read_byte(file, bytecode)
|
|
|
|
f, sz = mp_opcode_format(bytecode.buf, bytecode.idx - 1, False)
|
|
|
|
sz -= 1
|
2019-09-02 07:35:26 -04:00
|
|
|
if f == MP_BC_FORMAT_QSTR:
|
2019-02-28 22:03:10 -05:00
|
|
|
read_qstr_and_pack(file, bytecode, qstr_win)
|
|
|
|
sz -= 2
|
2019-09-02 07:35:26 -04:00
|
|
|
elif f == MP_BC_FORMAT_VAR_UINT:
|
2019-02-28 22:03:10 -05:00
|
|
|
while read_byte(file, bytecode) & 0x80:
|
|
|
|
pass
|
|
|
|
for _ in range(sz):
|
|
|
|
read_byte(file, bytecode)
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-25 07:15:51 -05:00
|
|
|
def read_raw_code(f, qstr_win):
|
2019-02-20 23:18:59 -05:00
|
|
|
kind_len = read_uint(f)
|
|
|
|
kind = (kind_len & 3) + MP_CODE_BYTECODE
|
|
|
|
fun_data_len = kind_len >> 2
|
|
|
|
fun_data = BytecodeBuffer(fun_data_len)
|
|
|
|
|
|
|
|
if kind == MP_CODE_BYTECODE:
|
2019-10-10 00:30:16 -04:00
|
|
|
prelude = read_prelude(f, fun_data, qstr_win)
|
2019-02-20 23:18:59 -05:00
|
|
|
read_bytecode(f, fun_data, qstr_win)
|
|
|
|
else:
|
|
|
|
fun_data.buf[:] = f.read(fun_data_len)
|
|
|
|
|
|
|
|
qstr_links = []
|
|
|
|
if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
|
|
|
|
# load qstr link table
|
|
|
|
n_qstr_link = read_uint(f)
|
|
|
|
for _ in range(n_qstr_link):
|
2019-06-04 08:13:32 -04:00
|
|
|
off = read_uint(f)
|
2019-02-20 23:18:59 -05:00
|
|
|
qst = read_qstr(f, qstr_win)
|
|
|
|
qstr_links.append((off >> 2, off & 3, qst))
|
|
|
|
|
|
|
|
type_sig = 0
|
|
|
|
if kind == MP_CODE_NATIVE_PY:
|
|
|
|
prelude_offset = read_uint(f)
|
|
|
|
_, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset)
|
2020-02-26 23:36:53 -05:00
|
|
|
fun_data.idx = name_idx # rewind to where qstrs are in prelude
|
|
|
|
read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
|
|
|
|
read_qstr_and_pack(f, fun_data, qstr_win) # source_file
|
2019-02-20 23:18:59 -05:00
|
|
|
else:
|
|
|
|
prelude_offset = None
|
|
|
|
scope_flags = read_uint(f)
|
|
|
|
n_pos_args = 0
|
|
|
|
if kind == MP_CODE_NATIVE_ASM:
|
|
|
|
n_pos_args = read_uint(f)
|
|
|
|
type_sig = read_uint(f)
|
|
|
|
prelude = (None, None, scope_flags, n_pos_args, 0)
|
|
|
|
|
|
|
|
qstrs = []
|
|
|
|
objs = []
|
|
|
|
raw_codes = []
|
|
|
|
if kind != MP_CODE_NATIVE_ASM:
|
|
|
|
# load constant table
|
|
|
|
n_obj = read_uint(f)
|
|
|
|
n_raw_code = read_uint(f)
|
|
|
|
qstrs = [read_qstr(f, qstr_win) for _ in range(prelude[3] + prelude[4])]
|
|
|
|
if kind != MP_CODE_BYTECODE:
|
|
|
|
objs.append(MPFunTable)
|
|
|
|
objs.extend([read_obj(f) for _ in range(n_obj)])
|
|
|
|
raw_codes = [read_raw_code(f, qstr_win) for _ in range(n_raw_code)]
|
|
|
|
|
|
|
|
if kind == MP_CODE_BYTECODE:
|
|
|
|
return RawCodeBytecode(fun_data.buf, qstrs, objs, raw_codes)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
return RawCodeNative(
|
|
|
|
kind,
|
|
|
|
fun_data.buf,
|
|
|
|
prelude_offset,
|
|
|
|
prelude,
|
|
|
|
qstr_links,
|
|
|
|
qstrs,
|
|
|
|
objs,
|
|
|
|
raw_codes,
|
|
|
|
type_sig,
|
|
|
|
)
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
def read_mpy(filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(filename, "rb") as f:
|
2016-04-15 06:56:10 -04:00
|
|
|
header = bytes_cons(f.read(4))
|
2020-02-26 23:36:53 -05:00
|
|
|
if header[0] != ord("M"):
|
|
|
|
raise Exception("not a valid .mpy file")
|
2017-02-16 08:19:34 -05:00
|
|
|
if header[1] != config.MPY_VERSION:
|
2020-02-26 23:36:53 -05:00
|
|
|
raise Exception("incompatible .mpy version")
|
2019-02-25 07:15:51 -05:00
|
|
|
feature_byte = header[2]
|
|
|
|
qw_size = read_uint(f)
|
|
|
|
config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0
|
2019-06-04 08:13:32 -04:00
|
|
|
mpy_native_arch = feature_byte >> 2
|
|
|
|
if mpy_native_arch != MP_NATIVE_ARCH_NONE:
|
|
|
|
if config.native_arch == MP_NATIVE_ARCH_NONE:
|
|
|
|
config.native_arch = mpy_native_arch
|
|
|
|
elif config.native_arch != mpy_native_arch:
|
2020-02-26 23:36:53 -05:00
|
|
|
raise Exception("native architecture mismatch")
|
2016-01-31 16:45:22 -05:00
|
|
|
config.mp_small_int_bits = header[3]
|
2019-02-25 07:15:51 -05:00
|
|
|
qstr_win = QStrWindow(qw_size)
|
2019-10-08 23:23:15 -04:00
|
|
|
rc = read_raw_code(f, qstr_win)
|
|
|
|
rc.mpy_source_file = filename
|
|
|
|
rc.qstr_win_size = qw_size
|
|
|
|
return rc
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
def dump_mpy(raw_codes):
|
|
|
|
for rc in raw_codes:
|
|
|
|
rc.dump()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-09-02 01:09:21 -04:00
|
|
|
def freeze_mpy(base_qstrs, raw_codes):
|
2016-01-31 16:45:22 -05:00
|
|
|
# add to qstrs
|
|
|
|
new = {}
|
|
|
|
for q in global_qstrs:
|
|
|
|
# don't add duplicates
|
2019-02-28 22:33:03 -05:00
|
|
|
if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new:
|
2016-01-31 16:45:22 -05:00
|
|
|
continue
|
|
|
|
new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
|
|
|
|
new = sorted(new.values(), key=lambda x: x[0])
|
|
|
|
|
|
|
|
print('#include "py/mpconfig.h"')
|
|
|
|
print('#include "py/objint.h"')
|
|
|
|
print('#include "py/objstr.h"')
|
|
|
|
print('#include "py/emitglue.h"')
|
2019-10-06 20:56:24 -04:00
|
|
|
print('#include "py/nativeglue.h"')
|
2016-01-31 16:45:22 -05:00
|
|
|
print()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#if MICROPY_LONGINT_IMPL != %u" % config.MICROPY_LONGINT_IMPL)
|
2016-05-16 18:13:30 -04:00
|
|
|
print('#error "incompatible MICROPY_LONGINT_IMPL"')
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#endif")
|
2016-05-16 18:13:30 -04:00
|
|
|
print()
|
|
|
|
|
|
|
|
if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#if MPZ_DIG_SIZE != %u" % config.MPZ_DIG_SIZE)
|
2016-05-16 18:13:30 -04:00
|
|
|
print('#error "incompatible MPZ_DIG_SIZE"')
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#endif")
|
2016-05-16 18:13:30 -04:00
|
|
|
print()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#if MICROPY_PY_BUILTINS_FLOAT")
|
|
|
|
print("typedef struct _mp_obj_float_t {")
|
|
|
|
print(" mp_obj_base_t base;")
|
|
|
|
print(" mp_float_t value;")
|
|
|
|
print("} mp_obj_float_t;")
|
|
|
|
print("#endif")
|
2016-01-31 16:45:22 -05:00
|
|
|
print()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#if MICROPY_PY_BUILTINS_COMPLEX")
|
|
|
|
print("typedef struct _mp_obj_complex_t {")
|
|
|
|
print(" mp_obj_base_t base;")
|
|
|
|
print(" mp_float_t real;")
|
|
|
|
print(" mp_float_t imag;")
|
|
|
|
print("} mp_obj_complex_t;")
|
|
|
|
print("#endif")
|
2016-09-02 10:19:02 -04:00
|
|
|
print()
|
|
|
|
|
2018-12-11 17:55:26 -05:00
|
|
|
if len(new) > 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("enum {")
|
2018-12-11 17:55:26 -05:00
|
|
|
for i in range(len(new)):
|
|
|
|
if i == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" MP_QSTR_%s = MP_QSTRnumber_of," % new[i][1])
|
2018-12-11 17:55:26 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" MP_QSTR_%s," % new[i][1])
|
|
|
|
print("};")
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2018-07-19 07:42:26 -04:00
|
|
|
# As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len
|
|
|
|
qstr_pool_alloc = min(len(new), 10)
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
print()
|
2020-02-26 23:36:53 -05:00
|
|
|
print("extern const qstr_pool_t mp_qstr_const_pool;")
|
|
|
|
print("const qstr_pool_t mp_qstr_frozen_const_pool = {")
|
|
|
|
print(" (qstr_pool_t*)&mp_qstr_const_pool, // previous pool")
|
|
|
|
print(" MP_QSTRnumber_of, // previous pool size")
|
|
|
|
print(" %u, // allocated entries" % qstr_pool_alloc)
|
|
|
|
print(" %u, // used entries" % len(new))
|
|
|
|
print(" {")
|
2016-01-31 16:45:22 -05:00
|
|
|
for _, _, qstr in new:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
" %s,"
|
|
|
|
% qstrutil.make_bytes(
|
|
|
|
config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(" },")
|
|
|
|
print("};")
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
for rc in raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
rc.freeze(rc.source_file.str.replace("/", "_")[:-3] + "_")
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
print()
|
2020-02-26 23:36:53 -05:00
|
|
|
print("const char mp_frozen_mpy_names[] = {")
|
2016-01-31 16:45:22 -05:00
|
|
|
for rc in raw_codes:
|
2016-05-23 07:46:02 -04:00
|
|
|
module_name = rc.source_file.str
|
2016-01-31 16:45:22 -05:00
|
|
|
print('"%s\\0"' % module_name)
|
|
|
|
print('"\\0"};')
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("const mp_raw_code_t *const mp_frozen_mpy_content[] = {")
|
2016-01-31 16:45:22 -05:00
|
|
|
for rc in raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" &raw_code_%s," % rc.escaped_name)
|
|
|
|
print("};")
|
|
|
|
|
2021-01-15 10:01:26 -05:00
|
|
|
# If a port defines MICROPY_FROZEN_LIST_ITEM then list all modules wrapped in that macro.
|
|
|
|
print("#ifdef MICROPY_FROZEN_LIST_ITEM")
|
|
|
|
for rc in raw_codes:
|
|
|
|
module_name = rc.source_file.str
|
|
|
|
if module_name.endswith("/__init__.py"):
|
|
|
|
short_name = module_name[: -len("/__init__.py")]
|
|
|
|
else:
|
|
|
|
short_name = module_name[: -len(".py")]
|
|
|
|
print('MICROPY_FROZEN_LIST_ITEM("%s", "%s")' % (short_name, module_name))
|
|
|
|
print("#endif")
|
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2019-10-08 23:23:15 -04:00
|
|
|
def merge_mpy(raw_codes, output_file):
|
2020-02-26 23:36:53 -05:00
|
|
|
assert len(raw_codes) <= 31 # so var-uints all fit in 1 byte
|
2019-10-08 23:23:15 -04:00
|
|
|
merged_mpy = bytearray()
|
|
|
|
|
|
|
|
if len(raw_codes) == 1:
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(raw_codes[0].mpy_source_file, "rb") as f:
|
2019-10-08 23:23:15 -04:00
|
|
|
merged_mpy.extend(f.read())
|
|
|
|
else:
|
|
|
|
header = bytearray(5)
|
2020-02-26 23:36:53 -05:00
|
|
|
header[0] = ord("M")
|
2019-10-08 23:23:15 -04:00
|
|
|
header[1] = config.MPY_VERSION
|
all: Remove MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE.
This commit removes all parts of code associated with the existing
MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE optimisation option, including the
-mcache-lookup-bc option to mpy-cross.
This feature originally provided a significant performance boost for Unix,
but wasn't able to be enabled for MCU targets (due to frozen bytecode), and
added significant extra complexity to generating and distributing .mpy
files.
The equivalent performance gain is now provided by the combination of
MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE (which has
been enabled on the unix port in the previous commit).
It's hard to provide precise performance numbers, but tests have been run
on a wide variety of architectures (x86-64, ARM Cortex, Aarch64, RISC-V,
xtensa) and they all generally agree on the qualitative improvements seen
by the combination of MICROPY_OPT_LOAD_ATTR_FAST_PATH and
MICROPY_OPT_MAP_LOOKUP_CACHE.
For example, on a "quiet" Linux x64 environment (i3-5010U @ 2.10GHz) the
change from CACHE_MAP_LOOKUP_IN_BYTECODE, to LOAD_ATTR_FAST_PATH combined
with MAP_LOOKUP_CACHE is:
diff of scores (higher is better)
N=2000 M=2000 bccache -> attrmapcache diff diff% (error%)
bm_chaos.py 13742.56 -> 13905.67 : +163.11 = +1.187% (+/-3.75%)
bm_fannkuch.py 60.13 -> 61.34 : +1.21 = +2.012% (+/-2.11%)
bm_fft.py 113083.20 -> 114793.68 : +1710.48 = +1.513% (+/-1.57%)
bm_float.py 256552.80 -> 243908.29 : -12644.51 = -4.929% (+/-1.90%)
bm_hexiom.py 521.93 -> 625.41 : +103.48 = +19.826% (+/-0.40%)
bm_nqueens.py 197544.25 -> 217713.12 : +20168.87 = +10.210% (+/-3.01%)
bm_pidigits.py 8072.98 -> 8198.75 : +125.77 = +1.558% (+/-3.22%)
misc_aes.py 17283.45 -> 16480.52 : -802.93 = -4.646% (+/-0.82%)
misc_mandel.py 99083.99 -> 128939.84 : +29855.85 = +30.132% (+/-5.88%)
misc_pystone.py 83860.10 -> 82592.56 : -1267.54 = -1.511% (+/-2.27%)
misc_raytrace.py 21490.40 -> 22227.23 : +736.83 = +3.429% (+/-1.88%)
This shows that the new optimisations are at least as good as the existing
inline-bytecode-caching, and are sometimes much better (because the new
ones apply caching to a wider variety of map lookups).
The new optimisations can also benefit code generated by the native
emitter, because they apply to the runtime rather than the generated code.
The improvement for the native emitter when LOAD_ATTR_FAST_PATH and
MAP_LOOKUP_CACHE are enabled is (same Linux environment as above):
diff of scores (higher is better)
N=2000 M=2000 native -> nat-attrmapcache diff diff% (error%)
bm_chaos.py 14130.62 -> 15464.68 : +1334.06 = +9.441% (+/-7.11%)
bm_fannkuch.py 74.96 -> 76.16 : +1.20 = +1.601% (+/-1.80%)
bm_fft.py 166682.99 -> 168221.86 : +1538.87 = +0.923% (+/-4.20%)
bm_float.py 233415.23 -> 265524.90 : +32109.67 = +13.756% (+/-2.57%)
bm_hexiom.py 628.59 -> 734.17 : +105.58 = +16.796% (+/-1.39%)
bm_nqueens.py 225418.44 -> 232926.45 : +7508.01 = +3.331% (+/-3.10%)
bm_pidigits.py 6322.00 -> 6379.52 : +57.52 = +0.910% (+/-5.62%)
misc_aes.py 20670.10 -> 27223.18 : +6553.08 = +31.703% (+/-1.56%)
misc_mandel.py 138221.11 -> 152014.01 : +13792.90 = +9.979% (+/-2.46%)
misc_pystone.py 85032.14 -> 105681.44 : +20649.30 = +24.284% (+/-2.25%)
misc_raytrace.py 19800.01 -> 23350.73 : +3550.72 = +17.933% (+/-2.79%)
In summary, compared to MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, the new
MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE options:
- are simpler;
- take less code size;
- are faster (generally);
- work with code generated by the native emitter;
- can be used on embedded targets with a small and constant RAM overhead;
- allow the same .mpy bytecode to run on all targets.
See #7680 for further discussion. And see also #7653 for a discussion
about simplifying mpy-cross options.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-05 22:28:06 -04:00
|
|
|
header[2] = config.native_arch << 2 | config.MICROPY_PY_BUILTINS_STR_UNICODE << 1
|
2019-10-08 23:23:15 -04:00
|
|
|
header[3] = config.mp_small_int_bits
|
2020-02-26 23:36:53 -05:00
|
|
|
header[4] = 32 # qstr_win_size
|
2019-10-08 23:23:15 -04:00
|
|
|
merged_mpy.extend(header)
|
|
|
|
|
|
|
|
bytecode = bytearray()
|
2020-09-04 02:12:09 -04:00
|
|
|
bytecode_len = 6 + len(raw_codes) * 5 + 2
|
2020-02-26 23:36:53 -05:00
|
|
|
bytecode.append(bytecode_len << 2) # kind and length
|
|
|
|
bytecode.append(0b00000000) # signature prelude
|
|
|
|
bytecode.append(0b00001000) # size prelude
|
|
|
|
bytecode.extend(b"\x00\x01") # MP_QSTR_
|
|
|
|
bytecode.extend(b"\x00\x01") # MP_QSTR_
|
2019-10-08 23:23:15 -04:00
|
|
|
for idx in range(len(raw_codes)):
|
2020-02-26 23:36:53 -05:00
|
|
|
bytecode.append(0x32) # MP_BC_MAKE_FUNCTION
|
|
|
|
bytecode.append(idx) # index raw code
|
2020-09-04 02:12:09 -04:00
|
|
|
bytecode.extend(b"\x34\x00\x59") # MP_BC_CALL_FUNCTION, 0 args, MP_BC_POP_TOP
|
2020-02-26 23:36:53 -05:00
|
|
|
bytecode.extend(b"\x51\x63") # MP_BC_LOAD_NONE, MP_BC_RETURN_VALUE
|
2019-10-08 23:23:15 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
bytecode.append(0) # n_obj
|
|
|
|
bytecode.append(len(raw_codes)) # n_raw_code
|
2019-10-08 23:23:15 -04:00
|
|
|
|
|
|
|
merged_mpy.extend(bytecode)
|
|
|
|
|
|
|
|
for rc in raw_codes:
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(rc.mpy_source_file, "rb") as f:
|
|
|
|
f.read(4) # skip header
|
|
|
|
read_uint(f) # skip qstr_win_size
|
|
|
|
data = f.read() # read rest of mpy file
|
2019-10-08 23:23:15 -04:00
|
|
|
merged_mpy.extend(data)
|
|
|
|
|
|
|
|
if output_file is None:
|
|
|
|
sys.stdout.buffer.write(merged_mpy)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(output_file, "wb") as f:
|
2019-10-08 23:23:15 -04:00
|
|
|
f.write(merged_mpy)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2016-01-31 16:45:22 -05:00
|
|
|
def main():
|
|
|
|
import argparse
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
cmd_parser = argparse.ArgumentParser(description="A tool to work with MicroPython .mpy files.")
|
|
|
|
cmd_parser.add_argument("-d", "--dump", action="store_true", help="dump contents of files")
|
|
|
|
cmd_parser.add_argument("-f", "--freeze", action="store_true", help="freeze files")
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"--merge", action="store_true", help="merge multiple .mpy files into one"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("-q", "--qstr-header", help="qstr header file to freeze against")
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-mlongint-impl",
|
|
|
|
choices=["none", "longlong", "mpz"],
|
|
|
|
default="mpz",
|
|
|
|
help="long-int implementation used by target (default mpz)",
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-mmpz-dig-size",
|
|
|
|
metavar="N",
|
|
|
|
type=int,
|
|
|
|
default=16,
|
|
|
|
help="mpz digit size used by target (default 16)",
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("-o", "--output", default=None, help="output file")
|
|
|
|
cmd_parser.add_argument("files", nargs="+", help="input .mpy files")
|
2016-01-31 16:45:22 -05:00
|
|
|
args = cmd_parser.parse_args()
|
|
|
|
|
|
|
|
# set config values relevant to target machine
|
|
|
|
config.MICROPY_LONGINT_IMPL = {
|
2020-02-26 23:36:53 -05:00
|
|
|
"none": config.MICROPY_LONGINT_IMPL_NONE,
|
|
|
|
"longlong": config.MICROPY_LONGINT_IMPL_LONGLONG,
|
|
|
|
"mpz": config.MICROPY_LONGINT_IMPL_MPZ,
|
2016-01-31 16:45:22 -05:00
|
|
|
}[args.mlongint_impl]
|
|
|
|
config.MPZ_DIG_SIZE = args.mmpz_dig_size
|
2019-06-04 08:13:32 -04:00
|
|
|
config.native_arch = MP_NATIVE_ARCH_NONE
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2016-09-02 01:09:21 -04:00
|
|
|
# set config values for qstrs, and get the existing base set of qstrs
|
2016-01-31 16:45:22 -05:00
|
|
|
if args.qstr_header:
|
|
|
|
qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
|
2020-02-26 23:36:53 -05:00
|
|
|
config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs["BYTES_IN_LEN"])
|
|
|
|
config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs["BYTES_IN_HASH"])
|
2016-01-31 16:45:22 -05:00
|
|
|
else:
|
2016-09-02 01:09:21 -04:00
|
|
|
config.MICROPY_QSTR_BYTES_IN_LEN = 1
|
|
|
|
config.MICROPY_QSTR_BYTES_IN_HASH = 1
|
|
|
|
base_qstrs = {}
|
2016-01-31 16:45:22 -05:00
|
|
|
|
|
|
|
raw_codes = [read_mpy(file) for file in args.files]
|
|
|
|
|
|
|
|
if args.dump:
|
|
|
|
dump_mpy(raw_codes)
|
|
|
|
elif args.freeze:
|
|
|
|
try:
|
2016-09-02 01:09:21 -04:00
|
|
|
freeze_mpy(base_qstrs, raw_codes)
|
2016-01-31 16:45:22 -05:00
|
|
|
except FreezeError as er:
|
|
|
|
print(er, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2019-10-08 23:23:15 -04:00
|
|
|
elif args.merge:
|
|
|
|
merged_mpy = merge_mpy(raw_codes, args.output)
|
2016-01-31 16:45:22 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-01-31 16:45:22 -05:00
|
|
|
main()
|