blob: 648d56fe057e984fc156e0e49e34158ca2e09875 [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 George9a5f92e2019-03-07 17:13:24 +110060 MPY_VERSION = 4
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
73global_qstrs = [None] # MP_QSTR_NULL should never be referenced
74for 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
105
Damien George0699c6b2016-01-31 21:45:22 +0000106MP_OPCODE_BYTE = 0
107MP_OPCODE_QSTR = 1
108MP_OPCODE_VAR_UINT = 2
109MP_OPCODE_OFFSET = 3
110
111# extra bytes:
112MP_BC_MAKE_CLOSURE = 0x62
113MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
114MP_BC_RAISE_VARARGS = 0x5c
115# extra byte if caching enabled:
Damien George814d5802018-12-11 00:52:33 +1100116MP_BC_LOAD_NAME = 0x1b
117MP_BC_LOAD_GLOBAL = 0x1c
118MP_BC_LOAD_ATTR = 0x1d
Damien George0699c6b2016-01-31 21:45:22 +0000119MP_BC_STORE_ATTR = 0x26
120
121def make_opcode_format():
122 def OC4(a, b, c, d):
123 return a | (b << 2) | (c << 4) | (d << 6)
124 U = 0
125 B = 0
126 Q = 1
127 V = 2
128 O = 3
Damien Georgec3beb162016-04-15 11:56:10 +0100129 return bytes_cons((
Damien George0699c6b2016-01-31 21:45:22 +0000130 # this table is taken verbatim from py/bc.c
131 OC4(U, U, U, U), # 0x00-0x03
132 OC4(U, U, U, U), # 0x04-0x07
133 OC4(U, U, U, U), # 0x08-0x0b
134 OC4(U, U, U, U), # 0x0c-0x0f
135 OC4(B, B, B, U), # 0x10-0x13
136 OC4(V, U, Q, V), # 0x14-0x17
Damien Georgedd11af22017-04-19 09:45:59 +1000137 OC4(B, V, V, Q), # 0x18-0x1b
Damien George0699c6b2016-01-31 21:45:22 +0000138 OC4(Q, Q, Q, Q), # 0x1c-0x1f
139 OC4(B, B, V, V), # 0x20-0x23
140 OC4(Q, Q, Q, B), # 0x24-0x27
141 OC4(V, V, Q, Q), # 0x28-0x2b
142 OC4(U, U, U, U), # 0x2c-0x2f
143 OC4(B, B, B, B), # 0x30-0x33
144 OC4(B, O, O, O), # 0x34-0x37
145 OC4(O, O, U, U), # 0x38-0x3b
146 OC4(U, O, B, O), # 0x3c-0x3f
147 OC4(O, B, B, O), # 0x40-0x43
Damien George5a2599d2019-02-15 12:18:59 +1100148 OC4(O, U, O, B), # 0x44-0x47
Damien George0699c6b2016-01-31 21:45:22 +0000149 OC4(U, U, U, U), # 0x48-0x4b
150 OC4(U, U, U, U), # 0x4c-0x4f
Damien George7df92912016-09-23 12:48:57 +1000151 OC4(V, V, U, V), # 0x50-0x53
152 OC4(B, U, V, V), # 0x54-0x57
Damien George0699c6b2016-01-31 21:45:22 +0000153 OC4(V, V, V, B), # 0x58-0x5b
154 OC4(B, B, B, U), # 0x5c-0x5f
155 OC4(V, V, V, V), # 0x60-0x63
156 OC4(V, V, V, V), # 0x64-0x67
157 OC4(Q, Q, B, U), # 0x68-0x6b
158 OC4(U, U, U, U), # 0x6c-0x6f
159
160 OC4(B, B, B, B), # 0x70-0x73
161 OC4(B, B, B, B), # 0x74-0x77
162 OC4(B, B, B, B), # 0x78-0x7b
163 OC4(B, B, B, B), # 0x7c-0x7f
164 OC4(B, B, B, B), # 0x80-0x83
165 OC4(B, B, B, B), # 0x84-0x87
166 OC4(B, B, B, B), # 0x88-0x8b
167 OC4(B, B, B, B), # 0x8c-0x8f
168 OC4(B, B, B, B), # 0x90-0x93
169 OC4(B, B, B, B), # 0x94-0x97
170 OC4(B, B, B, B), # 0x98-0x9b
171 OC4(B, B, B, B), # 0x9c-0x9f
172 OC4(B, B, B, B), # 0xa0-0xa3
173 OC4(B, B, B, B), # 0xa4-0xa7
174 OC4(B, B, B, B), # 0xa8-0xab
175 OC4(B, B, B, B), # 0xac-0xaf
176
177 OC4(B, B, B, B), # 0xb0-0xb3
178 OC4(B, B, B, B), # 0xb4-0xb7
179 OC4(B, B, B, B), # 0xb8-0xbb
180 OC4(B, B, B, B), # 0xbc-0xbf
181
182 OC4(B, B, B, B), # 0xc0-0xc3
183 OC4(B, B, B, B), # 0xc4-0xc7
184 OC4(B, B, B, B), # 0xc8-0xcb
185 OC4(B, B, B, B), # 0xcc-0xcf
186
187 OC4(B, B, B, B), # 0xd0-0xd3
Damien George933eab42017-10-10 10:37:38 +1100188 OC4(U, U, U, B), # 0xd4-0xd7
Damien George0699c6b2016-01-31 21:45:22 +0000189 OC4(B, B, B, B), # 0xd8-0xdb
190 OC4(B, B, B, B), # 0xdc-0xdf
191
192 OC4(B, B, B, B), # 0xe0-0xe3
193 OC4(B, B, B, B), # 0xe4-0xe7
194 OC4(B, B, B, B), # 0xe8-0xeb
195 OC4(B, B, B, B), # 0xec-0xef
196
197 OC4(B, B, B, B), # 0xf0-0xf3
198 OC4(B, B, B, B), # 0xf4-0xf7
Damien George933eab42017-10-10 10:37:38 +1100199 OC4(U, U, U, U), # 0xf8-0xfb
Damien George0699c6b2016-01-31 21:45:22 +0000200 OC4(U, U, U, U), # 0xfc-0xff
201 ))
202
203# this function mirrors that in py/bc.c
Damien George992a6e12019-03-01 14:03:10 +1100204def mp_opcode_format(bytecode, ip, count_var_uint, opcode_format=make_opcode_format()):
Damien George0699c6b2016-01-31 21:45:22 +0000205 opcode = bytecode[ip]
206 ip_start = ip
207 f = (opcode_format[opcode >> 2] >> (2 * (opcode & 3))) & 3
208 if f == MP_OPCODE_QSTR:
Damien George814d5802018-12-11 00:52:33 +1100209 if config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
210 if (opcode == MP_BC_LOAD_NAME
211 or opcode == MP_BC_LOAD_GLOBAL
212 or opcode == MP_BC_LOAD_ATTR
213 or opcode == MP_BC_STORE_ATTR):
214 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000215 ip += 3
216 else:
217 extra_byte = (
218 opcode == MP_BC_RAISE_VARARGS
219 or opcode == MP_BC_MAKE_CLOSURE
220 or opcode == MP_BC_MAKE_CLOSURE_DEFARGS
Damien George0699c6b2016-01-31 21:45:22 +0000221 )
222 ip += 1
223 if f == MP_OPCODE_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100224 if count_var_uint:
225 while bytecode[ip] & 0x80 != 0:
226 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000227 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000228 elif f == MP_OPCODE_OFFSET:
229 ip += 2
230 ip += extra_byte
231 return f, ip - ip_start
232
233def decode_uint(bytecode, ip):
234 unum = 0
235 while True:
236 val = bytecode[ip]
237 ip += 1
238 unum = (unum << 7) | (val & 0x7f)
239 if not (val & 0x80):
240 break
241 return ip, unum
242
Damien Georgeea3c80a2019-02-21 15:18:59 +1100243def extract_prelude(bytecode, ip):
Damien George0699c6b2016-01-31 21:45:22 +0000244 ip, n_state = decode_uint(bytecode, ip)
245 ip, n_exc_stack = decode_uint(bytecode, ip)
246 scope_flags = bytecode[ip]; ip += 1
247 n_pos_args = bytecode[ip]; ip += 1
248 n_kwonly_args = bytecode[ip]; ip += 1
249 n_def_pos_args = bytecode[ip]; ip += 1
250 ip2, code_info_size = decode_uint(bytecode, ip)
251 ip += code_info_size
252 while bytecode[ip] != 0xff:
253 ip += 1
254 ip += 1
255 # ip now points to first opcode
256 # ip2 points to simple_name qstr
257 return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
258
Damien Georgeea3c80a2019-02-21 15:18:59 +1100259class MPFunTable:
260 pass
261
Damien George643d2a02019-04-08 11:21:18 +1000262class RawCode(object):
Damien George02fd83b2016-05-03 12:24:39 +0100263 # a set of all escaped names, to make sure they are unique
264 escaped_names = set()
265
Damien Georgeea3c80a2019-02-21 15:18:59 +1100266 # convert code kind number to string
267 code_kind_str = {
268 MP_CODE_BYTECODE: 'MP_CODE_BYTECODE',
269 MP_CODE_NATIVE_PY: 'MP_CODE_NATIVE_PY',
270 MP_CODE_NATIVE_VIPER: 'MP_CODE_NATIVE_VIPER',
271 MP_CODE_NATIVE_ASM: 'MP_CODE_NATIVE_ASM',
272 }
273
274 def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000275 # set core variables
Damien Georgeea3c80a2019-02-21 15:18:59 +1100276 self.code_kind = code_kind
Damien George0699c6b2016-01-31 21:45:22 +0000277 self.bytecode = bytecode
Damien Georgeea3c80a2019-02-21 15:18:59 +1100278 self.prelude_offset = prelude_offset
Damien George0699c6b2016-01-31 21:45:22 +0000279 self.qstrs = qstrs
280 self.objs = objs
281 self.raw_codes = raw_codes
282
Damien Georgeea3c80a2019-02-21 15:18:59 +1100283 if self.prelude_offset is None:
284 # no prelude, assign a dummy simple_name
285 self.prelude_offset = 0
286 self.simple_name = global_qstrs[1]
287 else:
288 # extract prelude
289 self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset)
290 self.simple_name = self._unpack_qstr(self.ip2)
291 self.source_file = self._unpack_qstr(self.ip2 + 2)
Damien George0699c6b2016-01-31 21:45:22 +0000292
293 def _unpack_qstr(self, ip):
294 qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
295 return global_qstrs[qst]
296
297 def dump(self):
298 # dump children first
299 for rc in self.raw_codes:
stijne4ab4042017-08-16 10:37:00 +0200300 rc.freeze('')
Damien George0699c6b2016-01-31 21:45:22 +0000301 # TODO
302
Damien Georgeea3c80a2019-02-21 15:18:59 +1100303 def freeze_children(self, parent_name):
Damien George0699c6b2016-01-31 21:45:22 +0000304 self.escaped_name = parent_name + self.simple_name.qstr_esc
305
Damien George02fd83b2016-05-03 12:24:39 +0100306 # make sure the escaped name is unique
307 i = 2
308 while self.escaped_name in RawCode.escaped_names:
309 self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
310 i += 1
311 RawCode.escaped_names.add(self.escaped_name)
312
Damien George0699c6b2016-01-31 21:45:22 +0000313 # emit children first
314 for rc in self.raw_codes:
315 rc.freeze(self.escaped_name + '_')
316
Damien Georgeea3c80a2019-02-21 15:18:59 +1100317 def freeze_constants(self):
Damien George0699c6b2016-01-31 21:45:22 +0000318 # generate constant objects
319 for i, obj in enumerate(self.objs):
320 obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100321 if obj is MPFunTable:
322 pass
323 elif obj is Ellipsis:
Damien George9ba3de62017-11-15 12:46:08 +1100324 print('#define %s mp_const_ellipsis_obj' % obj_name)
325 elif is_str_type(obj) or is_bytes_type(obj):
Damien Georgeb6bdf182016-09-02 15:10:45 +1000326 if is_str_type(obj):
327 obj = bytes_cons(obj, 'utf8')
328 obj_type = 'mp_type_str'
329 else:
330 obj_type = 'mp_type_bytes'
331 print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
332 % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
333 len(obj), ''.join(('\\x%02x' % b) for b in obj)))
Damien Georgec3beb162016-04-15 11:56:10 +0100334 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000335 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
336 # TODO check if we can actually fit this long-int into a small-int
337 raise FreezeError(self, 'target does not support long int')
338 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
339 # TODO
340 raise FreezeError(self, 'freezing int to long-long is not implemented')
341 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
342 neg = 0
343 if obj < 0:
344 obj = -obj
345 neg = 1
346 bits_per_dig = config.MPZ_DIG_SIZE
347 digs = []
348 z = obj
349 while z:
350 digs.append(z & ((1 << bits_per_dig) - 1))
351 z >>= bits_per_dig
352 ndigs = len(digs)
353 digs = ','.join(('%#x' % d) for d in digs)
354 print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
Damien George44fc92e2018-07-09 13:43:34 +1000355 '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};'
356 % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs))
Damien George0699c6b2016-01-31 21:45:22 +0000357 elif type(obj) is float:
Damien George72ae3c72016-08-10 13:26:11 +1000358 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
Damien George0699c6b2016-01-31 21:45:22 +0000359 print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
360 % (obj_name, obj))
Damien George72ae3c72016-08-10 13:26:11 +1000361 print('#endif')
Damien Georgec51c8832016-09-03 00:19:02 +1000362 elif type(obj) is complex:
363 print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
364 % (obj_name, obj.real, obj.imag))
Damien George0699c6b2016-01-31 21:45:22 +0000365 else:
Damien George0699c6b2016-01-31 21:45:22 +0000366 raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
367
Damien Georgeb6a32892017-08-12 22:26:18 +1000368 # generate constant table, if it has any entries
369 const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
370 if const_table_len:
371 print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
372 % (self.escaped_name, const_table_len))
373 for qst in self.qstrs:
374 print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
375 for i in range(len(self.objs)):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100376 if self.objs[i] is MPFunTable:
377 print(' mp_fun_table,')
378 elif type(self.objs[i]) is float:
Damien Georgeb6a32892017-08-12 22:26:18 +1000379 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
380 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
381 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
382 n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
383 n = ((n & ~0x3) | 2) + 0x80800000
384 print(' (mp_rom_obj_t)(0x%08x),' % (n,))
Damien George929d10a2018-07-09 12:22:40 +1000385 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D')
386 n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0]
387 n += 0x8004000000000000
388 print(' (mp_rom_obj_t)(0x%016x),' % (n,))
Damien Georgeb6a32892017-08-12 22:26:18 +1000389 print('#endif')
390 else:
391 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
392 for rc in self.raw_codes:
393 print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
394 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000395
Damien Georgeea3c80a2019-02-21 15:18:59 +1100396 def freeze_module(self, qstr_links=(), type_sig=0):
Damien George0699c6b2016-01-31 21:45:22 +0000397 # generate module
398 if self.simple_name.str != '<module>':
399 print('STATIC ', end='')
400 print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100401 print(' .kind = %s,' % RawCode.code_kind_str[self.code_kind])
Damien George0699c6b2016-01-31 21:45:22 +0000402 print(' .scope_flags = 0x%02x,' % self.prelude[2])
403 print(' .n_pos_args = %u,' % self.prelude[3])
Damien Georgeea3c80a2019-02-21 15:18:59 +1100404 print(' .fun_data = fun_data_%s,' % self.escaped_name)
405 if len(self.qstrs) + len(self.objs) + len(self.raw_codes):
Damien George636ed0f2019-02-19 14:15:39 +1100406 print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
Damien Georgeb6a32892017-08-12 22:26:18 +1000407 else:
Damien George636ed0f2019-02-19 14:15:39 +1100408 print(' .const_table = NULL,')
409 print(' #if MICROPY_PERSISTENT_CODE_SAVE')
410 print(' .fun_data_len = %u,' % len(self.bytecode))
411 print(' .n_obj = %u,' % len(self.objs))
412 print(' .n_raw_code = %u,' % len(self.raw_codes))
Jun Wub152bbd2019-05-06 00:31:11 -0700413 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100414 print(' .prelude_offset = %u,' % self.prelude_offset)
415 print(' .n_qstr = %u,' % len(qstr_links))
416 print(' .qstr_link = NULL,') # TODO
417 print(' #endif')
418 print(' #endif')
Jun Wub152bbd2019-05-06 00:31:11 -0700419 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100420 print(' .type_sig = %u,' % type_sig)
Damien George636ed0f2019-02-19 14:15:39 +1100421 print(' #endif')
Damien George0699c6b2016-01-31 21:45:22 +0000422 print('};')
423
Damien Georgeea3c80a2019-02-21 15:18:59 +1100424class RawCodeBytecode(RawCode):
425 def __init__(self, bytecode, qstrs, objs, raw_codes):
Damien George643d2a02019-04-08 11:21:18 +1000426 super(RawCodeBytecode, self).__init__(MP_CODE_BYTECODE, bytecode, 0, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100427
428 def freeze(self, parent_name):
429 self.freeze_children(parent_name)
430
431 # generate bytecode data
432 print()
433 print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
434 print('STATIC ', end='')
435 if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
436 print('const ', end='')
437 print('byte fun_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode)))
438 print(' ', end='')
439 for i in range(self.ip2):
440 print(' 0x%02x,' % self.bytecode[i], end='')
441 print()
442 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
443 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
444 print(' ', end='')
445 for i in range(self.ip2 + 4, self.ip):
446 print(' 0x%02x,' % self.bytecode[i], end='')
447 print()
448 ip = self.ip
449 while ip < len(self.bytecode):
450 f, sz = mp_opcode_format(self.bytecode, ip, True)
451 if f == 1:
452 qst = self._unpack_qstr(ip + 1).qstr_id
453 extra = '' if sz == 3 else ' 0x%02x,' % self.bytecode[ip + 3]
454 print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,', extra)
455 else:
456 print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
457 ip += sz
458 print('};')
459
460 self.freeze_constants()
461 self.freeze_module()
462
463class RawCodeNative(RawCode):
464 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 +1000465 super(RawCodeNative, self).__init__(code_kind, fun_data, prelude_offset, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100466 self.prelude = prelude
467 self.qstr_links = qstr_links
468 self.type_sig = type_sig
469 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
470 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))'
471 else:
472 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
473
474 def _asm_thumb_rewrite_mov(self, pc, val):
475 print(' (%u & 0xf0) | (%s >> 12),' % (self.bytecode[pc], val), end='')
476 print(' (%u & 0xfb) | (%s >> 9 & 0x04),' % (self.bytecode[pc + 1], val), end='')
477 print(' (%s & 0xff),' % (val,), end='')
478 print(' (%u & 0x07) | (%s >> 4 & 0x70),' % (self.bytecode[pc + 3], val))
479
480 def _link_qstr(self, pc, kind, qst):
481 if kind == 0:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000482 # Generic 16-bit link
Damien Georgeea3c80a2019-02-21 15:18:59 +1100483 print(' %s & 0xff, %s >> 8,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000484 return 2
Damien Georgeea3c80a2019-02-21 15:18:59 +1100485 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000486 # Architecture-specific link
487 is_obj = kind == 2
488 if is_obj:
Damien Georgeea3c80a2019-02-21 15:18:59 +1100489 qst = '((uintptr_t)MP_OBJ_NEW_QSTR(%s))' % qst
490 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
491 print(' %s & 0xff, %s >> 8, 0, 0,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000492 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100493 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
494 if is_obj:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000495 # qstr object, movw and movt
496 self._asm_thumb_rewrite_mov(pc, qst)
497 self._asm_thumb_rewrite_mov(pc + 4, '(%s >> 16)' % qst)
Damien George9d3031c2019-06-11 11:36:39 +1000498 return 8
Damien Georgeea3c80a2019-02-21 15:18:59 +1100499 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000500 # qstr number, movw instruction
501 self._asm_thumb_rewrite_mov(pc, qst)
Damien George9d3031c2019-06-11 11:36:39 +1000502 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100503 else:
504 assert 0
505
506 def freeze(self, parent_name):
507 self.freeze_children(parent_name)
508
509 # generate native code data
510 print()
511 if self.code_kind == MP_CODE_NATIVE_PY:
512 print('// frozen native code for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
513 elif self.code_kind == MP_CODE_NATIVE_VIPER:
514 print('// frozen viper code for scope %s' % (parent_name,))
515 else:
516 print('// frozen assembler code for scope %s' % (parent_name,))
517 print('STATIC const byte fun_data_%s[%u] %s = {' % (self.escaped_name, len(self.bytecode), self.fun_data_attributes))
518
519 if self.code_kind == MP_CODE_NATIVE_PY:
520 i_top = self.prelude_offset
521 else:
522 i_top = len(self.bytecode)
523 i = 0
524 qi = 0
525 while i < i_top:
526 if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]:
527 # link qstr
528 qi_off, qi_kind, qi_val = self.qstr_links[qi]
529 qst = global_qstrs[qi_val].qstr_id
Damien George9d3031c2019-06-11 11:36:39 +1000530 i += self._link_qstr(i, qi_kind, qst)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100531 qi += 1
532 else:
533 # copy machine code (max 16 bytes)
534 i16 = min(i + 16, i_top)
535 if qi < len(self.qstr_links):
536 i16 = min(i16, self.qstr_links[qi][0])
537 print(' ', end='')
538 for ii in range(i, i16):
539 print(' 0x%02x,' % self.bytecode[ii], end='')
540 print()
541 i = i16
542
543 if self.code_kind == MP_CODE_NATIVE_PY:
544 print(' ', end='')
545 for i in range(self.prelude_offset, self.ip2):
546 print(' 0x%02x,' % self.bytecode[i], end='')
547 print()
548
549 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
550 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
551
552 print(' ', end='')
553 for i in range(self.ip2 + 4, self.ip):
554 print(' 0x%02x,' % self.bytecode[i], end='')
555 print()
556
557 print('};')
558
559 self.freeze_constants()
560 self.freeze_module(self.qstr_links, self.type_sig)
561
Damien George992a6e12019-03-01 14:03:10 +1100562class BytecodeBuffer:
563 def __init__(self, size):
564 self.buf = bytearray(size)
565 self.idx = 0
566
567 def is_full(self):
568 return self.idx == len(self.buf)
569
570 def append(self, b):
571 self.buf[self.idx] = b
572 self.idx += 1
573
574def read_byte(f, out=None):
575 b = bytes_cons(f.read(1))[0]
576 if out is not None:
577 out.append(b)
578 return b
579
580def read_uint(f, out=None):
Damien George0699c6b2016-01-31 21:45:22 +0000581 i = 0
582 while True:
Damien George992a6e12019-03-01 14:03:10 +1100583 b = read_byte(f, out)
Damien George0699c6b2016-01-31 21:45:22 +0000584 i = (i << 7) | (b & 0x7f)
585 if b & 0x80 == 0:
586 break
587 return i
588
Damien George5996eeb2019-02-25 23:15:51 +1100589def read_qstr(f, qstr_win):
Damien George0699c6b2016-01-31 21:45:22 +0000590 ln = read_uint(f)
Damien George4f0931b2019-03-01 14:33:03 +1100591 if ln == 0:
592 # static qstr
593 return bytes_cons(f.read(1))[0]
Damien George5996eeb2019-02-25 23:15:51 +1100594 if ln & 1:
595 # qstr in table
596 return qstr_win.access(ln >> 1)
597 ln >>= 1
Damien Georgec3beb162016-04-15 11:56:10 +0100598 data = str_cons(f.read(ln), 'utf8')
Damien George4f0931b2019-03-01 14:33:03 +1100599 global_qstrs.append(QStrType(data))
Damien George5996eeb2019-02-25 23:15:51 +1100600 qstr_win.push(len(global_qstrs) - 1)
Damien George0699c6b2016-01-31 21:45:22 +0000601 return len(global_qstrs) - 1
602
603def read_obj(f):
604 obj_type = f.read(1)
605 if obj_type == b'e':
606 return Ellipsis
607 else:
608 buf = f.read(read_uint(f))
609 if obj_type == b's':
Damien Georgec3beb162016-04-15 11:56:10 +0100610 return str_cons(buf, 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000611 elif obj_type == b'b':
Damien Georgec3beb162016-04-15 11:56:10 +0100612 return bytes_cons(buf)
Damien George0699c6b2016-01-31 21:45:22 +0000613 elif obj_type == b'i':
Damien Georgec3beb162016-04-15 11:56:10 +0100614 return int(str_cons(buf, 'ascii'), 10)
Damien George0699c6b2016-01-31 21:45:22 +0000615 elif obj_type == b'f':
Damien Georgec3beb162016-04-15 11:56:10 +0100616 return float(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000617 elif obj_type == b'c':
Damien Georgec3beb162016-04-15 11:56:10 +0100618 return complex(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000619 else:
620 assert 0
621
Damien George992a6e12019-03-01 14:03:10 +1100622def read_prelude(f, bytecode):
623 n_state = read_uint(f, bytecode)
624 n_exc_stack = read_uint(f, bytecode)
625 scope_flags = read_byte(f, bytecode)
626 n_pos_args = read_byte(f, bytecode)
627 n_kwonly_args = read_byte(f, bytecode)
628 n_def_pos_args = read_byte(f, bytecode)
629 l1 = bytecode.idx
630 code_info_size = read_uint(f, bytecode)
631 l2 = bytecode.idx
632 for _ in range(code_info_size - (l2 - l1)):
633 read_byte(f, bytecode)
634 while read_byte(f, bytecode) != 255:
635 pass
636 return l2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
Damien George0699c6b2016-01-31 21:45:22 +0000637
Damien George992a6e12019-03-01 14:03:10 +1100638def read_qstr_and_pack(f, bytecode, qstr_win):
639 qst = read_qstr(f, qstr_win)
640 bytecode.append(qst & 0xff)
641 bytecode.append(qst >> 8)
642
643def read_bytecode(file, bytecode, qstr_win):
644 while not bytecode.is_full():
645 op = read_byte(file, bytecode)
646 f, sz = mp_opcode_format(bytecode.buf, bytecode.idx - 1, False)
647 sz -= 1
648 if f == MP_OPCODE_QSTR:
649 read_qstr_and_pack(file, bytecode, qstr_win)
650 sz -= 2
651 elif f == MP_OPCODE_VAR_UINT:
652 while read_byte(file, bytecode) & 0x80:
653 pass
654 for _ in range(sz):
655 read_byte(file, bytecode)
Damien George0699c6b2016-01-31 21:45:22 +0000656
Damien George5996eeb2019-02-25 23:15:51 +1100657def read_raw_code(f, qstr_win):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100658 kind_len = read_uint(f)
659 kind = (kind_len & 3) + MP_CODE_BYTECODE
660 fun_data_len = kind_len >> 2
661 fun_data = BytecodeBuffer(fun_data_len)
662
663 if kind == MP_CODE_BYTECODE:
664 name_idx, prelude = read_prelude(f, fun_data)
665 read_bytecode(f, fun_data, qstr_win)
666 else:
667 fun_data.buf[:] = f.read(fun_data_len)
668
669 qstr_links = []
670 if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
671 # load qstr link table
672 n_qstr_link = read_uint(f)
673 for _ in range(n_qstr_link):
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000674 off = read_uint(f)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100675 qst = read_qstr(f, qstr_win)
676 qstr_links.append((off >> 2, off & 3, qst))
677
678 type_sig = 0
679 if kind == MP_CODE_NATIVE_PY:
680 prelude_offset = read_uint(f)
681 _, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset)
682 else:
683 prelude_offset = None
684 scope_flags = read_uint(f)
685 n_pos_args = 0
686 if kind == MP_CODE_NATIVE_ASM:
687 n_pos_args = read_uint(f)
688 type_sig = read_uint(f)
689 prelude = (None, None, scope_flags, n_pos_args, 0)
690
691 if kind in (MP_CODE_BYTECODE, MP_CODE_NATIVE_PY):
692 fun_data.idx = name_idx # rewind to where qstrs are in prelude
693 read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
694 read_qstr_and_pack(f, fun_data, qstr_win) # source_file
695
696 qstrs = []
697 objs = []
698 raw_codes = []
699 if kind != MP_CODE_NATIVE_ASM:
700 # load constant table
701 n_obj = read_uint(f)
702 n_raw_code = read_uint(f)
703 qstrs = [read_qstr(f, qstr_win) for _ in range(prelude[3] + prelude[4])]
704 if kind != MP_CODE_BYTECODE:
705 objs.append(MPFunTable)
706 objs.extend([read_obj(f) for _ in range(n_obj)])
707 raw_codes = [read_raw_code(f, qstr_win) for _ in range(n_raw_code)]
708
709 if kind == MP_CODE_BYTECODE:
710 return RawCodeBytecode(fun_data.buf, qstrs, objs, raw_codes)
711 else:
712 return RawCodeNative(kind, fun_data.buf, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig)
Damien George0699c6b2016-01-31 21:45:22 +0000713
714def read_mpy(filename):
715 with open(filename, 'rb') as f:
Damien Georgec3beb162016-04-15 11:56:10 +0100716 header = bytes_cons(f.read(4))
Damien George0699c6b2016-01-31 21:45:22 +0000717 if header[0] != ord('M'):
718 raise Exception('not a valid .mpy file')
Damien George6a110482017-02-17 00:19:34 +1100719 if header[1] != config.MPY_VERSION:
720 raise Exception('incompatible .mpy version')
Damien George5996eeb2019-02-25 23:15:51 +1100721 feature_byte = header[2]
722 qw_size = read_uint(f)
723 config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_byte & 1) != 0
724 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000725 mpy_native_arch = feature_byte >> 2
726 if mpy_native_arch != MP_NATIVE_ARCH_NONE:
727 if config.native_arch == MP_NATIVE_ARCH_NONE:
728 config.native_arch = mpy_native_arch
729 elif config.native_arch != mpy_native_arch:
730 raise Exception('native architecture mismatch')
Damien George0699c6b2016-01-31 21:45:22 +0000731 config.mp_small_int_bits = header[3]
Damien George5996eeb2019-02-25 23:15:51 +1100732 qstr_win = QStrWindow(qw_size)
733 return read_raw_code(f, qstr_win)
Damien George0699c6b2016-01-31 21:45:22 +0000734
735def dump_mpy(raw_codes):
736 for rc in raw_codes:
737 rc.dump()
738
Damien Georgeb4790af2016-09-02 15:09:21 +1000739def freeze_mpy(base_qstrs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000740 # add to qstrs
741 new = {}
742 for q in global_qstrs:
743 # don't add duplicates
Damien George4f0931b2019-03-01 14:33:03 +1100744 if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new:
Damien George0699c6b2016-01-31 21:45:22 +0000745 continue
746 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
747 new = sorted(new.values(), key=lambda x: x[0])
748
749 print('#include "py/mpconfig.h"')
750 print('#include "py/objint.h"')
751 print('#include "py/objstr.h"')
752 print('#include "py/emitglue.h"')
753 print()
754
Damien George98458a42017-01-05 15:52:52 +1100755 print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
756 print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
Damien George99b47192016-05-16 23:13:30 +0100757 print('#endif')
758 print()
759
760 print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
761 print('#error "incompatible MICROPY_LONGINT_IMPL"')
762 print('#endif')
763 print()
764
765 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
766 print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
767 print('#error "incompatible MPZ_DIG_SIZE"')
768 print('#endif')
769 print()
770
771
Damien George0699c6b2016-01-31 21:45:22 +0000772 print('#if MICROPY_PY_BUILTINS_FLOAT')
773 print('typedef struct _mp_obj_float_t {')
774 print(' mp_obj_base_t base;')
775 print(' mp_float_t value;')
776 print('} mp_obj_float_t;')
777 print('#endif')
778 print()
779
Damien Georgec51c8832016-09-03 00:19:02 +1000780 print('#if MICROPY_PY_BUILTINS_COMPLEX')
781 print('typedef struct _mp_obj_complex_t {')
782 print(' mp_obj_base_t base;')
783 print(' mp_float_t real;')
784 print(' mp_float_t imag;')
785 print('} mp_obj_complex_t;')
786 print('#endif')
787 print()
788
Dave Hylands39eef272018-12-11 14:55:26 -0800789 if len(new) > 0:
790 print('enum {')
791 for i in range(len(new)):
792 if i == 0:
793 print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
794 else:
795 print(' MP_QSTR_%s,' % new[i][1])
796 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000797
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100798 # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len
799 qstr_pool_alloc = min(len(new), 10)
800
Damien George0699c6b2016-01-31 21:45:22 +0000801 print()
802 print('extern const qstr_pool_t mp_qstr_const_pool;');
803 print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
804 print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
805 print(' MP_QSTRnumber_of, // previous pool size')
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100806 print(' %u, // allocated entries' % qstr_pool_alloc)
Damien George0699c6b2016-01-31 21:45:22 +0000807 print(' %u, // used entries' % len(new))
808 print(' {')
809 for _, _, qstr in new:
Damien Georgeb4790af2016-09-02 15:09:21 +1000810 print(' %s,'
811 % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
Damien George0699c6b2016-01-31 21:45:22 +0000812 print(' },')
813 print('};')
814
815 for rc in raw_codes:
816 rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
817
818 print()
819 print('const char mp_frozen_mpy_names[] = {')
820 for rc in raw_codes:
Damien George9b4c0132016-05-23 12:46:02 +0100821 module_name = rc.source_file.str
Damien George0699c6b2016-01-31 21:45:22 +0000822 print('"%s\\0"' % module_name)
823 print('"\\0"};')
824
825 print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
826 for rc in raw_codes:
827 print(' &raw_code_%s,' % rc.escaped_name)
828 print('};')
829
830def main():
831 import argparse
832 cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
833 cmd_parser.add_argument('-d', '--dump', action='store_true',
834 help='dump contents of files')
835 cmd_parser.add_argument('-f', '--freeze', action='store_true',
836 help='freeze files')
837 cmd_parser.add_argument('-q', '--qstr-header',
838 help='qstr header file to freeze against')
839 cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
840 help='long-int implementation used by target (default mpz)')
841 cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
842 help='mpz digit size used by target (default 16)')
843 cmd_parser.add_argument('files', nargs='+',
844 help='input .mpy files')
845 args = cmd_parser.parse_args()
846
847 # set config values relevant to target machine
848 config.MICROPY_LONGINT_IMPL = {
849 'none':config.MICROPY_LONGINT_IMPL_NONE,
850 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
851 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
852 }[args.mlongint_impl]
853 config.MPZ_DIG_SIZE = args.mmpz_dig_size
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000854 config.native_arch = MP_NATIVE_ARCH_NONE
Damien George0699c6b2016-01-31 21:45:22 +0000855
Damien Georgeb4790af2016-09-02 15:09:21 +1000856 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +0000857 if args.qstr_header:
858 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien Georgeb4790af2016-09-02 15:09:21 +1000859 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
860 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
Damien George0699c6b2016-01-31 21:45:22 +0000861 else:
Damien Georgeb4790af2016-09-02 15:09:21 +1000862 config.MICROPY_QSTR_BYTES_IN_LEN = 1
863 config.MICROPY_QSTR_BYTES_IN_HASH = 1
864 base_qstrs = {}
Damien George0699c6b2016-01-31 21:45:22 +0000865
866 raw_codes = [read_mpy(file) for file in args.files]
867
868 if args.dump:
869 dump_mpy(raw_codes)
870 elif args.freeze:
871 try:
Damien Georgeb4790af2016-09-02 15:09:21 +1000872 freeze_mpy(base_qstrs, raw_codes)
Damien George0699c6b2016-01-31 21:45:22 +0000873 except FreezeError as er:
874 print(er, file=sys.stderr)
875 sys.exit(1)
876
877if __name__ == '__main__':
878 main()