blob: 39362bc09265268f138c72df4bd13eba1ecd11ae [file] [log] [blame]
Damien George0699c6b2016-01-31 21:45:22 +00001#!/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 Georgefaf3d3e2019-06-04 22:13:32 +10007# Copyright (c) 2016-2019 Damien P. George
Damien George0699c6b2016-01-31 21:45:22 +00008#
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 Georgec3beb162016-04-15 11:56:10 +010027# Python 2/3 compatibility code
28from __future__ import print_function
29import platform
30if 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
36else:
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 George0699c6b2016-01-31 21:45:22 +000044import sys
Damien George72ae3c72016-08-10 13:26:11 +100045import struct
Damien George0699c6b2016-01-31 21:45:22 +000046from collections import namedtuple
47
Paul Sokolovsky473e85e2017-05-01 00:01:30 +030048sys.path.append(sys.path[0] + '/../py')
Damien George0699c6b2016-01-31 21:45:22 +000049import makeqstrdata as qstrutil
50
51class 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
59class Config:
Damien George5716c5c2019-09-26 16:39:37 +100060 MPY_VERSION = 5
Damien George0699c6b2016-01-31 21:45:22 +000061 MICROPY_LONGINT_IMPL_NONE = 0
62 MICROPY_LONGINT_IMPL_LONGLONG = 1
63 MICROPY_LONGINT_IMPL_MPZ = 2
64config = Config()
65
Damien George4f0931b2019-03-01 14:33:03 +110066class QStrType:
67 def __init__(self, str):
68 self.str = str
69 self.qstr_esc = qstrutil.qstr_escape(self.str)
70 self.qstr_id = 'MP_QSTR_' + self.qstr_esc
71
72# Initialise global list of qstrs with static qstrs
Josh Lloyd7d58a192019-09-25 17:53:30 +120073global_qstrs = [None] # MP_QSTRnull should never be referenced
Damien George4f0931b2019-03-01 14:33:03 +110074for n in qstrutil.static_qstr_list:
75 global_qstrs.append(QStrType(n))
76
Damien George5996eeb2019-02-25 23:15:51 +110077class QStrWindow:
Damien George74ed0682019-04-08 15:20:56 +100078 def __init__(self, size):
Damien George5996eeb2019-02-25 23:15:51 +110079 self.window = []
Damien George74ed0682019-04-08 15:20:56 +100080 self.size = size
Damien George5996eeb2019-02-25 23:15:51 +110081
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 Georgeea3c80a2019-02-21 15:18:59 +110090MP_CODE_BYTECODE = 2
91MP_CODE_NATIVE_PY = 3
92MP_CODE_NATIVE_VIPER = 4
93MP_CODE_NATIVE_ASM = 5
94
95MP_NATIVE_ARCH_NONE = 0
96MP_NATIVE_ARCH_X86 = 1
97MP_NATIVE_ARCH_X64 = 2
98MP_NATIVE_ARCH_ARMV6 = 3
99MP_NATIVE_ARCH_ARMV6M = 4
100MP_NATIVE_ARCH_ARMV7M = 5
101MP_NATIVE_ARCH_ARMV7EM = 6
102MP_NATIVE_ARCH_ARMV7EMSP = 7
103MP_NATIVE_ARCH_ARMV7EMDP = 8
104MP_NATIVE_ARCH_XTENSA = 9
Damien George9adedce2019-09-13 13:15:12 +1000105MP_NATIVE_ARCH_XTENSAWIN = 10
Damien Georgeea3c80a2019-02-21 15:18:59 +1100106
Damien George1f7202d2019-09-02 21:35:26 +1000107MP_BC_MASK_EXTRA_BYTE = 0x9e
Damien George0699c6b2016-01-31 21:45:22 +0000108
Damien George1f7202d2019-09-02 21:35:26 +1000109MP_BC_FORMAT_BYTE = 0
110MP_BC_FORMAT_QSTR = 1
111MP_BC_FORMAT_VAR_UINT = 2
112MP_BC_FORMAT_OFFSET = 3
113
Damien George0699c6b2016-01-31 21:45:22 +0000114# extra byte if caching enabled:
Damien George5889cf52019-09-02 20:24:01 +1000115MP_BC_LOAD_NAME = 0x11
116MP_BC_LOAD_GLOBAL = 0x12
117MP_BC_LOAD_ATTR = 0x13
118MP_BC_STORE_ATTR = 0x18
Damien George0699c6b2016-01-31 21:45:22 +0000119
Damien George0699c6b2016-01-31 21:45:22 +0000120# this function mirrors that in py/bc.c
Damien George1f7202d2019-09-02 21:35:26 +1000121def mp_opcode_format(bytecode, ip, count_var_uint):
Damien George0699c6b2016-01-31 21:45:22 +0000122 opcode = bytecode[ip]
123 ip_start = ip
Damien George1f7202d2019-09-02 21:35:26 +1000124 f = ((0x000003a4 >> (2 * ((opcode) >> 4))) & 3)
125 if f == MP_BC_FORMAT_QSTR:
Damien George814d5802018-12-11 00:52:33 +1100126 if config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
127 if (opcode == MP_BC_LOAD_NAME
128 or opcode == MP_BC_LOAD_GLOBAL
129 or opcode == MP_BC_LOAD_ATTR
130 or opcode == MP_BC_STORE_ATTR):
131 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000132 ip += 3
133 else:
Damien George1f7202d2019-09-02 21:35:26 +1000134 extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0
Damien George0699c6b2016-01-31 21:45:22 +0000135 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000136 if f == MP_BC_FORMAT_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100137 if count_var_uint:
138 while bytecode[ip] & 0x80 != 0:
139 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000140 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000141 elif f == MP_BC_FORMAT_OFFSET:
Damien George0699c6b2016-01-31 21:45:22 +0000142 ip += 2
143 ip += extra_byte
144 return f, ip - ip_start
145
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000146def read_prelude_sig(read_byte):
147 z = read_byte()
148 # xSSSSEAA
149 S = (z >> 3) & 0xf
150 E = (z >> 2) & 0x1
151 F = 0
152 A = z & 0x3
153 K = 0
154 D = 0
155 n = 0
156 while z & 0x80:
157 z = read_byte()
158 # xFSSKAED
159 S |= (z & 0x30) << (2 * n)
160 E |= (z & 0x02) << n
161 F |= ((z & 0x40) >> 6) << n
162 A |= (z & 0x4) << n
163 K |= ((z & 0x08) >> 3) << n
164 D |= (z & 0x1) << n
165 n += 1
166 S += 1
167 return S, E, F, A, K, D
168
Damien Georgec8c0fd42019-09-25 15:45:47 +1000169def read_prelude_size(read_byte):
170 I = 0
171 C = 0
172 n = 0
173 while True:
174 z = read_byte()
175 # xIIIIIIC
176 I |= ((z & 0x7e) >> 1) << (6 * n)
177 C |= (z & 1) << n
178 if not (z & 0x80):
179 break
180 n += 1
181 return I, C
182
Damien Georgeea3c80a2019-02-21 15:18:59 +1100183def extract_prelude(bytecode, ip):
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000184 def local_read_byte():
185 b = bytecode[ip_ref[0]]
186 ip_ref[0] += 1
187 return b
188 ip_ref = [ip] # to close over ip in Python 2 and 3
189 n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(local_read_byte)
Damien Georgec8c0fd42019-09-25 15:45:47 +1000190 n_info, n_cell = read_prelude_size(local_read_byte)
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000191 ip = ip_ref[0]
192
Damien Georgec8c0fd42019-09-25 15:45:47 +1000193 ip2 = ip
194 ip = ip2 + n_info + n_cell
Damien George0699c6b2016-01-31 21:45:22 +0000195 # ip now points to first opcode
196 # ip2 points to simple_name qstr
Damien Georgec8c0fd42019-09-25 15:45:47 +1000197 return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args)
Damien George0699c6b2016-01-31 21:45:22 +0000198
Damien Georgeea3c80a2019-02-21 15:18:59 +1100199class MPFunTable:
200 pass
201
Damien George643d2a02019-04-08 11:21:18 +1000202class RawCode(object):
Damien George02fd83b2016-05-03 12:24:39 +0100203 # a set of all escaped names, to make sure they are unique
204 escaped_names = set()
205
Damien Georgeea3c80a2019-02-21 15:18:59 +1100206 # convert code kind number to string
207 code_kind_str = {
208 MP_CODE_BYTECODE: 'MP_CODE_BYTECODE',
209 MP_CODE_NATIVE_PY: 'MP_CODE_NATIVE_PY',
210 MP_CODE_NATIVE_VIPER: 'MP_CODE_NATIVE_VIPER',
211 MP_CODE_NATIVE_ASM: 'MP_CODE_NATIVE_ASM',
212 }
213
214 def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000215 # set core variables
Damien Georgeea3c80a2019-02-21 15:18:59 +1100216 self.code_kind = code_kind
Damien George0699c6b2016-01-31 21:45:22 +0000217 self.bytecode = bytecode
Damien Georgeea3c80a2019-02-21 15:18:59 +1100218 self.prelude_offset = prelude_offset
Damien George0699c6b2016-01-31 21:45:22 +0000219 self.qstrs = qstrs
220 self.objs = objs
221 self.raw_codes = raw_codes
222
Damien Georgeea3c80a2019-02-21 15:18:59 +1100223 if self.prelude_offset is None:
224 # no prelude, assign a dummy simple_name
225 self.prelude_offset = 0
226 self.simple_name = global_qstrs[1]
227 else:
228 # extract prelude
229 self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset)
230 self.simple_name = self._unpack_qstr(self.ip2)
231 self.source_file = self._unpack_qstr(self.ip2 + 2)
Damien George0699c6b2016-01-31 21:45:22 +0000232
233 def _unpack_qstr(self, ip):
234 qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
235 return global_qstrs[qst]
236
237 def dump(self):
238 # dump children first
239 for rc in self.raw_codes:
stijne4ab4042017-08-16 10:37:00 +0200240 rc.freeze('')
Damien George0699c6b2016-01-31 21:45:22 +0000241 # TODO
242
Damien Georgeea3c80a2019-02-21 15:18:59 +1100243 def freeze_children(self, parent_name):
Damien George0699c6b2016-01-31 21:45:22 +0000244 self.escaped_name = parent_name + self.simple_name.qstr_esc
245
Damien George02fd83b2016-05-03 12:24:39 +0100246 # make sure the escaped name is unique
247 i = 2
248 while self.escaped_name in RawCode.escaped_names:
249 self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
250 i += 1
251 RawCode.escaped_names.add(self.escaped_name)
252
Damien George0699c6b2016-01-31 21:45:22 +0000253 # emit children first
254 for rc in self.raw_codes:
255 rc.freeze(self.escaped_name + '_')
256
Damien Georgeea3c80a2019-02-21 15:18:59 +1100257 def freeze_constants(self):
Damien George0699c6b2016-01-31 21:45:22 +0000258 # generate constant objects
259 for i, obj in enumerate(self.objs):
260 obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100261 if obj is MPFunTable:
262 pass
263 elif obj is Ellipsis:
Damien George9ba3de62017-11-15 12:46:08 +1100264 print('#define %s mp_const_ellipsis_obj' % obj_name)
265 elif is_str_type(obj) or is_bytes_type(obj):
Damien Georgeb6bdf182016-09-02 15:10:45 +1000266 if is_str_type(obj):
267 obj = bytes_cons(obj, 'utf8')
268 obj_type = 'mp_type_str'
269 else:
270 obj_type = 'mp_type_bytes'
271 print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
272 % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
273 len(obj), ''.join(('\\x%02x' % b) for b in obj)))
Damien Georgec3beb162016-04-15 11:56:10 +0100274 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000275 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
276 # TODO check if we can actually fit this long-int into a small-int
277 raise FreezeError(self, 'target does not support long int')
278 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
279 # TODO
280 raise FreezeError(self, 'freezing int to long-long is not implemented')
281 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
282 neg = 0
283 if obj < 0:
284 obj = -obj
285 neg = 1
286 bits_per_dig = config.MPZ_DIG_SIZE
287 digs = []
288 z = obj
289 while z:
290 digs.append(z & ((1 << bits_per_dig) - 1))
291 z >>= bits_per_dig
292 ndigs = len(digs)
293 digs = ','.join(('%#x' % d) for d in digs)
294 print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
Damien George44fc92e2018-07-09 13:43:34 +1000295 '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};'
296 % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs))
Damien George0699c6b2016-01-31 21:45:22 +0000297 elif type(obj) is float:
Damien George72ae3c72016-08-10 13:26:11 +1000298 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
Damien George0699c6b2016-01-31 21:45:22 +0000299 print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
300 % (obj_name, obj))
Damien George72ae3c72016-08-10 13:26:11 +1000301 print('#endif')
Damien Georgec51c8832016-09-03 00:19:02 +1000302 elif type(obj) is complex:
303 print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
304 % (obj_name, obj.real, obj.imag))
Damien George0699c6b2016-01-31 21:45:22 +0000305 else:
Damien George0699c6b2016-01-31 21:45:22 +0000306 raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
307
Damien Georgeb6a32892017-08-12 22:26:18 +1000308 # generate constant table, if it has any entries
309 const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
310 if const_table_len:
311 print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
312 % (self.escaped_name, const_table_len))
313 for qst in self.qstrs:
314 print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
315 for i in range(len(self.objs)):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100316 if self.objs[i] is MPFunTable:
317 print(' mp_fun_table,')
318 elif type(self.objs[i]) is float:
Damien Georgeb6a32892017-08-12 22:26:18 +1000319 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
320 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
321 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
322 n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
323 n = ((n & ~0x3) | 2) + 0x80800000
324 print(' (mp_rom_obj_t)(0x%08x),' % (n,))
Damien George929d10a2018-07-09 12:22:40 +1000325 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D')
326 n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0]
327 n += 0x8004000000000000
328 print(' (mp_rom_obj_t)(0x%016x),' % (n,))
Damien Georgeb6a32892017-08-12 22:26:18 +1000329 print('#endif')
330 else:
331 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
332 for rc in self.raw_codes:
333 print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
334 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000335
Damien Georgeea3c80a2019-02-21 15:18:59 +1100336 def freeze_module(self, qstr_links=(), type_sig=0):
Damien George0699c6b2016-01-31 21:45:22 +0000337 # generate module
338 if self.simple_name.str != '<module>':
339 print('STATIC ', end='')
340 print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100341 print(' .kind = %s,' % RawCode.code_kind_str[self.code_kind])
Damien George0699c6b2016-01-31 21:45:22 +0000342 print(' .scope_flags = 0x%02x,' % self.prelude[2])
343 print(' .n_pos_args = %u,' % self.prelude[3])
Damien Georgeea3c80a2019-02-21 15:18:59 +1100344 print(' .fun_data = fun_data_%s,' % self.escaped_name)
345 if len(self.qstrs) + len(self.objs) + len(self.raw_codes):
Damien George636ed0f2019-02-19 14:15:39 +1100346 print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
Damien Georgeb6a32892017-08-12 22:26:18 +1000347 else:
Damien George636ed0f2019-02-19 14:15:39 +1100348 print(' .const_table = NULL,')
349 print(' #if MICROPY_PERSISTENT_CODE_SAVE')
350 print(' .fun_data_len = %u,' % len(self.bytecode))
351 print(' .n_obj = %u,' % len(self.objs))
352 print(' .n_raw_code = %u,' % len(self.raw_codes))
Damien Georgec69f58e2019-09-06 23:55:15 +1000353 if self.code_kind == MP_CODE_BYTECODE:
354 print(' #if MICROPY_PY_SYS_SETTRACE')
355 print(' .prelude = {')
356 print(' .n_state = %u,' % self.prelude[0])
357 print(' .n_exc_stack = %u,' % self.prelude[1])
358 print(' .scope_flags = %u,' % self.prelude[2])
359 print(' .n_pos_args = %u,' % self.prelude[3])
360 print(' .n_kwonly_args = %u,' % self.prelude[4])
361 print(' .n_def_pos_args = %u,' % self.prelude[5])
362 print(' .qstr_block_name = %s,' % self.simple_name.qstr_id)
363 print(' .qstr_source_file = %s,' % self.source_file.qstr_id)
364 print(' .line_info = fun_data_%s + %u,' % (self.escaped_name, 0)) # TODO
Damien Georgec69f58e2019-09-06 23:55:15 +1000365 print(' .opcodes = fun_data_%s + %u,' % (self.escaped_name, self.ip))
366 print(' },')
367 print(' .line_of_definition = %u,' % 0) # TODO
368 print(' #endif')
Jun Wub152bbd2019-05-06 00:31:11 -0700369 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100370 print(' .prelude_offset = %u,' % self.prelude_offset)
371 print(' .n_qstr = %u,' % len(qstr_links))
372 print(' .qstr_link = NULL,') # TODO
373 print(' #endif')
374 print(' #endif')
Jun Wub152bbd2019-05-06 00:31:11 -0700375 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100376 print(' .type_sig = %u,' % type_sig)
Damien George636ed0f2019-02-19 14:15:39 +1100377 print(' #endif')
Damien George0699c6b2016-01-31 21:45:22 +0000378 print('};')
379
Damien Georgeea3c80a2019-02-21 15:18:59 +1100380class RawCodeBytecode(RawCode):
381 def __init__(self, bytecode, qstrs, objs, raw_codes):
Damien George643d2a02019-04-08 11:21:18 +1000382 super(RawCodeBytecode, self).__init__(MP_CODE_BYTECODE, bytecode, 0, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100383
384 def freeze(self, parent_name):
385 self.freeze_children(parent_name)
386
387 # generate bytecode data
388 print()
389 print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
390 print('STATIC ', end='')
391 if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
392 print('const ', end='')
393 print('byte fun_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode)))
394 print(' ', end='')
395 for i in range(self.ip2):
396 print(' 0x%02x,' % self.bytecode[i], end='')
397 print()
398 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
399 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
400 print(' ', end='')
401 for i in range(self.ip2 + 4, self.ip):
402 print(' 0x%02x,' % self.bytecode[i], end='')
403 print()
404 ip = self.ip
405 while ip < len(self.bytecode):
406 f, sz = mp_opcode_format(self.bytecode, ip, True)
407 if f == 1:
408 qst = self._unpack_qstr(ip + 1).qstr_id
409 extra = '' if sz == 3 else ' 0x%02x,' % self.bytecode[ip + 3]
410 print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,', extra)
411 else:
412 print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
413 ip += sz
414 print('};')
415
416 self.freeze_constants()
417 self.freeze_module()
418
419class RawCodeNative(RawCode):
420 def __init__(self, code_kind, fun_data, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig):
Damien George643d2a02019-04-08 11:21:18 +1000421 super(RawCodeNative, self).__init__(code_kind, fun_data, prelude_offset, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100422 self.prelude = prelude
423 self.qstr_links = qstr_links
424 self.type_sig = type_sig
425 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
426 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))'
427 else:
428 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
429
Jim Mussared4ab51562019-08-17 00:32:04 +1000430 # Allow single-byte alignment by default for x86/x64/xtensa, but on ARM we need halfword- or word- alignment.
431 if config.native_arch == MP_NATIVE_ARCH_ARMV6:
432 # ARMV6 -- four byte align.
433 self.fun_data_attributes += ' __attribute__ ((aligned (4)))'
434 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
435 # ARMVxxM -- two byte align.
436 self.fun_data_attributes += ' __attribute__ ((aligned (2)))'
437
Damien Georgeea3c80a2019-02-21 15:18:59 +1100438 def _asm_thumb_rewrite_mov(self, pc, val):
439 print(' (%u & 0xf0) | (%s >> 12),' % (self.bytecode[pc], val), end='')
440 print(' (%u & 0xfb) | (%s >> 9 & 0x04),' % (self.bytecode[pc + 1], val), end='')
441 print(' (%s & 0xff),' % (val,), end='')
442 print(' (%u & 0x07) | (%s >> 4 & 0x70),' % (self.bytecode[pc + 3], val))
443
444 def _link_qstr(self, pc, kind, qst):
445 if kind == 0:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000446 # Generic 16-bit link
Damien Georgeea3c80a2019-02-21 15:18:59 +1100447 print(' %s & 0xff, %s >> 8,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000448 return 2
Damien Georgeea3c80a2019-02-21 15:18:59 +1100449 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000450 # Architecture-specific link
451 is_obj = kind == 2
452 if is_obj:
Damien Georgeea3c80a2019-02-21 15:18:59 +1100453 qst = '((uintptr_t)MP_OBJ_NEW_QSTR(%s))' % qst
454 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
455 print(' %s & 0xff, %s >> 8, 0, 0,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000456 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100457 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
458 if is_obj:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000459 # qstr object, movw and movt
460 self._asm_thumb_rewrite_mov(pc, qst)
461 self._asm_thumb_rewrite_mov(pc + 4, '(%s >> 16)' % qst)
Damien George9d3031c2019-06-11 11:36:39 +1000462 return 8
Damien Georgeea3c80a2019-02-21 15:18:59 +1100463 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000464 # qstr number, movw instruction
465 self._asm_thumb_rewrite_mov(pc, qst)
Damien George9d3031c2019-06-11 11:36:39 +1000466 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100467 else:
468 assert 0
469
470 def freeze(self, parent_name):
471 self.freeze_children(parent_name)
472
473 # generate native code data
474 print()
475 if self.code_kind == MP_CODE_NATIVE_PY:
476 print('// frozen native code for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
477 elif self.code_kind == MP_CODE_NATIVE_VIPER:
478 print('// frozen viper code for scope %s' % (parent_name,))
479 else:
480 print('// frozen assembler code for scope %s' % (parent_name,))
481 print('STATIC const byte fun_data_%s[%u] %s = {' % (self.escaped_name, len(self.bytecode), self.fun_data_attributes))
482
483 if self.code_kind == MP_CODE_NATIVE_PY:
484 i_top = self.prelude_offset
485 else:
486 i_top = len(self.bytecode)
487 i = 0
488 qi = 0
489 while i < i_top:
490 if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]:
491 # link qstr
492 qi_off, qi_kind, qi_val = self.qstr_links[qi]
493 qst = global_qstrs[qi_val].qstr_id
Damien George9d3031c2019-06-11 11:36:39 +1000494 i += self._link_qstr(i, qi_kind, qst)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100495 qi += 1
496 else:
497 # copy machine code (max 16 bytes)
498 i16 = min(i + 16, i_top)
499 if qi < len(self.qstr_links):
500 i16 = min(i16, self.qstr_links[qi][0])
501 print(' ', end='')
502 for ii in range(i, i16):
503 print(' 0x%02x,' % self.bytecode[ii], end='')
504 print()
505 i = i16
506
507 if self.code_kind == MP_CODE_NATIVE_PY:
508 print(' ', end='')
509 for i in range(self.prelude_offset, self.ip2):
510 print(' 0x%02x,' % self.bytecode[i], end='')
511 print()
512
513 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
514 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
515
516 print(' ', end='')
517 for i in range(self.ip2 + 4, self.ip):
518 print(' 0x%02x,' % self.bytecode[i], end='')
519 print()
520
521 print('};')
522
523 self.freeze_constants()
524 self.freeze_module(self.qstr_links, self.type_sig)
525
Damien George992a6e12019-03-01 14:03:10 +1100526class BytecodeBuffer:
527 def __init__(self, size):
528 self.buf = bytearray(size)
529 self.idx = 0
530
531 def is_full(self):
532 return self.idx == len(self.buf)
533
534 def append(self, b):
535 self.buf[self.idx] = b
536 self.idx += 1
537
538def read_byte(f, out=None):
539 b = bytes_cons(f.read(1))[0]
540 if out is not None:
541 out.append(b)
542 return b
543
544def read_uint(f, out=None):
Damien George0699c6b2016-01-31 21:45:22 +0000545 i = 0
546 while True:
Damien George992a6e12019-03-01 14:03:10 +1100547 b = read_byte(f, out)
Damien George0699c6b2016-01-31 21:45:22 +0000548 i = (i << 7) | (b & 0x7f)
549 if b & 0x80 == 0:
550 break
551 return i
552
Damien George5996eeb2019-02-25 23:15:51 +1100553def read_qstr(f, qstr_win):
Damien George0699c6b2016-01-31 21:45:22 +0000554 ln = read_uint(f)
Damien George4f0931b2019-03-01 14:33:03 +1100555 if ln == 0:
556 # static qstr
557 return bytes_cons(f.read(1))[0]
Damien George5996eeb2019-02-25 23:15:51 +1100558 if ln & 1:
559 # qstr in table
560 return qstr_win.access(ln >> 1)
561 ln >>= 1
Damien Georgec3beb162016-04-15 11:56:10 +0100562 data = str_cons(f.read(ln), 'utf8')
Damien George4f0931b2019-03-01 14:33:03 +1100563 global_qstrs.append(QStrType(data))
Damien George5996eeb2019-02-25 23:15:51 +1100564 qstr_win.push(len(global_qstrs) - 1)
Damien George0699c6b2016-01-31 21:45:22 +0000565 return len(global_qstrs) - 1
566
567def read_obj(f):
568 obj_type = f.read(1)
569 if obj_type == b'e':
570 return Ellipsis
571 else:
572 buf = f.read(read_uint(f))
573 if obj_type == b's':
Damien Georgec3beb162016-04-15 11:56:10 +0100574 return str_cons(buf, 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000575 elif obj_type == b'b':
Damien Georgec3beb162016-04-15 11:56:10 +0100576 return bytes_cons(buf)
Damien George0699c6b2016-01-31 21:45:22 +0000577 elif obj_type == b'i':
Damien Georgec3beb162016-04-15 11:56:10 +0100578 return int(str_cons(buf, 'ascii'), 10)
Damien George0699c6b2016-01-31 21:45:22 +0000579 elif obj_type == b'f':
Damien Georgec3beb162016-04-15 11:56:10 +0100580 return float(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000581 elif obj_type == b'c':
Damien Georgec3beb162016-04-15 11:56:10 +0100582 return complex(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000583 else:
584 assert 0
585
Damien George23f06912019-10-10 15:30:16 +1100586def read_prelude(f, bytecode, qstr_win):
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000587 n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(lambda: read_byte(f, bytecode))
Damien Georgec8c0fd42019-09-25 15:45:47 +1000588 n_info, n_cell = read_prelude_size(lambda: read_byte(f, bytecode))
Damien George23f06912019-10-10 15:30:16 +1100589 read_qstr_and_pack(f, bytecode, qstr_win) # simple_name
590 read_qstr_and_pack(f, bytecode, qstr_win) # source_file
591 for _ in range(n_info - 4 + n_cell):
Damien George992a6e12019-03-01 14:03:10 +1100592 read_byte(f, bytecode)
Damien George23f06912019-10-10 15:30:16 +1100593 return n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args
Damien George0699c6b2016-01-31 21:45:22 +0000594
Damien George992a6e12019-03-01 14:03:10 +1100595def read_qstr_and_pack(f, bytecode, qstr_win):
596 qst = read_qstr(f, qstr_win)
597 bytecode.append(qst & 0xff)
598 bytecode.append(qst >> 8)
599
600def read_bytecode(file, bytecode, qstr_win):
601 while not bytecode.is_full():
602 op = read_byte(file, bytecode)
603 f, sz = mp_opcode_format(bytecode.buf, bytecode.idx - 1, False)
604 sz -= 1
Damien George1f7202d2019-09-02 21:35:26 +1000605 if f == MP_BC_FORMAT_QSTR:
Damien George992a6e12019-03-01 14:03:10 +1100606 read_qstr_and_pack(file, bytecode, qstr_win)
607 sz -= 2
Damien George1f7202d2019-09-02 21:35:26 +1000608 elif f == MP_BC_FORMAT_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100609 while read_byte(file, bytecode) & 0x80:
610 pass
611 for _ in range(sz):
612 read_byte(file, bytecode)
Damien George0699c6b2016-01-31 21:45:22 +0000613
Damien George5996eeb2019-02-25 23:15:51 +1100614def read_raw_code(f, qstr_win):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100615 kind_len = read_uint(f)
616 kind = (kind_len & 3) + MP_CODE_BYTECODE
617 fun_data_len = kind_len >> 2
618 fun_data = BytecodeBuffer(fun_data_len)
619
620 if kind == MP_CODE_BYTECODE:
Damien George23f06912019-10-10 15:30:16 +1100621 prelude = read_prelude(f, fun_data, qstr_win)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100622 read_bytecode(f, fun_data, qstr_win)
623 else:
624 fun_data.buf[:] = f.read(fun_data_len)
625
626 qstr_links = []
627 if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
628 # load qstr link table
629 n_qstr_link = read_uint(f)
630 for _ in range(n_qstr_link):
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000631 off = read_uint(f)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100632 qst = read_qstr(f, qstr_win)
633 qstr_links.append((off >> 2, off & 3, qst))
634
635 type_sig = 0
636 if kind == MP_CODE_NATIVE_PY:
637 prelude_offset = read_uint(f)
638 _, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset)
Damien George23f06912019-10-10 15:30:16 +1100639 fun_data.idx = name_idx # rewind to where qstrs are in prelude
640 read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
641 read_qstr_and_pack(f, fun_data, qstr_win) # source_file
Damien Georgeea3c80a2019-02-21 15:18:59 +1100642 else:
643 prelude_offset = None
644 scope_flags = read_uint(f)
645 n_pos_args = 0
646 if kind == MP_CODE_NATIVE_ASM:
647 n_pos_args = read_uint(f)
648 type_sig = read_uint(f)
649 prelude = (None, None, scope_flags, n_pos_args, 0)
650
Damien Georgeea3c80a2019-02-21 15:18:59 +1100651 qstrs = []
652 objs = []
653 raw_codes = []
654 if kind != MP_CODE_NATIVE_ASM:
655 # load constant table
656 n_obj = read_uint(f)
657 n_raw_code = read_uint(f)
658 qstrs = [read_qstr(f, qstr_win) for _ in range(prelude[3] + prelude[4])]
659 if kind != MP_CODE_BYTECODE:
660 objs.append(MPFunTable)
661 objs.extend([read_obj(f) for _ in range(n_obj)])
662 raw_codes = [read_raw_code(f, qstr_win) for _ in range(n_raw_code)]
663
664 if kind == MP_CODE_BYTECODE:
665 return RawCodeBytecode(fun_data.buf, qstrs, objs, raw_codes)
666 else:
667 return RawCodeNative(kind, fun_data.buf, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig)
Damien George0699c6b2016-01-31 21:45:22 +0000668
669def read_mpy(filename):
670 with open(filename, 'rb') as f:
Damien Georgec3beb162016-04-15 11:56:10 +0100671 header = bytes_cons(f.read(4))
Damien George0699c6b2016-01-31 21:45:22 +0000672 if header[0] != ord('M'):
673 raise Exception('not a valid .mpy file')
Damien George6a110482017-02-17 00:19:34 +1100674 if header[1] != config.MPY_VERSION:
675 raise Exception('incompatible .mpy version')
Damien George5996eeb2019-02-25 23:15:51 +1100676 feature_byte = header[2]
677 qw_size = read_uint(f)
678 config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_byte & 1) != 0
679 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000680 mpy_native_arch = feature_byte >> 2
681 if mpy_native_arch != MP_NATIVE_ARCH_NONE:
682 if config.native_arch == MP_NATIVE_ARCH_NONE:
683 config.native_arch = mpy_native_arch
684 elif config.native_arch != mpy_native_arch:
685 raise Exception('native architecture mismatch')
Damien George0699c6b2016-01-31 21:45:22 +0000686 config.mp_small_int_bits = header[3]
Damien George5996eeb2019-02-25 23:15:51 +1100687 qstr_win = QStrWindow(qw_size)
688 return read_raw_code(f, qstr_win)
Damien George0699c6b2016-01-31 21:45:22 +0000689
690def dump_mpy(raw_codes):
691 for rc in raw_codes:
692 rc.dump()
693
Damien Georgeb4790af2016-09-02 15:09:21 +1000694def freeze_mpy(base_qstrs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000695 # add to qstrs
696 new = {}
697 for q in global_qstrs:
698 # don't add duplicates
Damien George4f0931b2019-03-01 14:33:03 +1100699 if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new:
Damien George0699c6b2016-01-31 21:45:22 +0000700 continue
701 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
702 new = sorted(new.values(), key=lambda x: x[0])
703
704 print('#include "py/mpconfig.h"')
705 print('#include "py/objint.h"')
706 print('#include "py/objstr.h"')
707 print('#include "py/emitglue.h"')
708 print()
709
Damien George98458a42017-01-05 15:52:52 +1100710 print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
711 print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
Damien George99b47192016-05-16 23:13:30 +0100712 print('#endif')
713 print()
714
715 print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
716 print('#error "incompatible MICROPY_LONGINT_IMPL"')
717 print('#endif')
718 print()
719
720 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
721 print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
722 print('#error "incompatible MPZ_DIG_SIZE"')
723 print('#endif')
724 print()
725
726
Damien George0699c6b2016-01-31 21:45:22 +0000727 print('#if MICROPY_PY_BUILTINS_FLOAT')
728 print('typedef struct _mp_obj_float_t {')
729 print(' mp_obj_base_t base;')
730 print(' mp_float_t value;')
731 print('} mp_obj_float_t;')
732 print('#endif')
733 print()
734
Damien Georgec51c8832016-09-03 00:19:02 +1000735 print('#if MICROPY_PY_BUILTINS_COMPLEX')
736 print('typedef struct _mp_obj_complex_t {')
737 print(' mp_obj_base_t base;')
738 print(' mp_float_t real;')
739 print(' mp_float_t imag;')
740 print('} mp_obj_complex_t;')
741 print('#endif')
742 print()
743
Dave Hylands39eef272018-12-11 14:55:26 -0800744 if len(new) > 0:
745 print('enum {')
746 for i in range(len(new)):
747 if i == 0:
748 print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
749 else:
750 print(' MP_QSTR_%s,' % new[i][1])
751 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000752
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100753 # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len
754 qstr_pool_alloc = min(len(new), 10)
755
Damien George0699c6b2016-01-31 21:45:22 +0000756 print()
757 print('extern const qstr_pool_t mp_qstr_const_pool;');
758 print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
759 print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
760 print(' MP_QSTRnumber_of, // previous pool size')
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100761 print(' %u, // allocated entries' % qstr_pool_alloc)
Damien George0699c6b2016-01-31 21:45:22 +0000762 print(' %u, // used entries' % len(new))
763 print(' {')
764 for _, _, qstr in new:
Damien Georgeb4790af2016-09-02 15:09:21 +1000765 print(' %s,'
766 % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
Damien George0699c6b2016-01-31 21:45:22 +0000767 print(' },')
768 print('};')
769
770 for rc in raw_codes:
771 rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
772
773 print()
774 print('const char mp_frozen_mpy_names[] = {')
775 for rc in raw_codes:
Damien George9b4c0132016-05-23 12:46:02 +0100776 module_name = rc.source_file.str
Damien George0699c6b2016-01-31 21:45:22 +0000777 print('"%s\\0"' % module_name)
778 print('"\\0"};')
779
780 print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
781 for rc in raw_codes:
782 print(' &raw_code_%s,' % rc.escaped_name)
783 print('};')
784
785def main():
786 import argparse
787 cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
788 cmd_parser.add_argument('-d', '--dump', action='store_true',
789 help='dump contents of files')
790 cmd_parser.add_argument('-f', '--freeze', action='store_true',
791 help='freeze files')
792 cmd_parser.add_argument('-q', '--qstr-header',
793 help='qstr header file to freeze against')
794 cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
795 help='long-int implementation used by target (default mpz)')
796 cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
797 help='mpz digit size used by target (default 16)')
798 cmd_parser.add_argument('files', nargs='+',
799 help='input .mpy files')
800 args = cmd_parser.parse_args()
801
802 # set config values relevant to target machine
803 config.MICROPY_LONGINT_IMPL = {
804 'none':config.MICROPY_LONGINT_IMPL_NONE,
805 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
806 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
807 }[args.mlongint_impl]
808 config.MPZ_DIG_SIZE = args.mmpz_dig_size
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000809 config.native_arch = MP_NATIVE_ARCH_NONE
Damien George0699c6b2016-01-31 21:45:22 +0000810
Damien Georgeb4790af2016-09-02 15:09:21 +1000811 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +0000812 if args.qstr_header:
813 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien Georgeb4790af2016-09-02 15:09:21 +1000814 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
815 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
Damien George0699c6b2016-01-31 21:45:22 +0000816 else:
Damien Georgeb4790af2016-09-02 15:09:21 +1000817 config.MICROPY_QSTR_BYTES_IN_LEN = 1
818 config.MICROPY_QSTR_BYTES_IN_HASH = 1
819 base_qstrs = {}
Damien George0699c6b2016-01-31 21:45:22 +0000820
821 raw_codes = [read_mpy(file) for file in args.files]
822
823 if args.dump:
824 dump_mpy(raw_codes)
825 elif args.freeze:
826 try:
Damien Georgeb4790af2016-09-02 15:09:21 +1000827 freeze_mpy(base_qstrs, raw_codes)
Damien George0699c6b2016-01-31 21:45:22 +0000828 except FreezeError as er:
829 print(er, file=sys.stderr)
830 sys.exit(1)
831
832if __name__ == '__main__':
833 main()