blob: 4f8e965d79850b42395a3999355d339caf6275cf [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
105
Damien George1f7202d2019-09-02 21:35:26 +1000106MP_BC_MASK_EXTRA_BYTE = 0x9e
Damien George0699c6b2016-01-31 21:45:22 +0000107
Damien George1f7202d2019-09-02 21:35:26 +1000108MP_BC_FORMAT_BYTE = 0
109MP_BC_FORMAT_QSTR = 1
110MP_BC_FORMAT_VAR_UINT = 2
111MP_BC_FORMAT_OFFSET = 3
112
Damien George0699c6b2016-01-31 21:45:22 +0000113# extra byte if caching enabled:
Damien George5889cf52019-09-02 20:24:01 +1000114MP_BC_LOAD_NAME = 0x11
115MP_BC_LOAD_GLOBAL = 0x12
116MP_BC_LOAD_ATTR = 0x13
117MP_BC_STORE_ATTR = 0x18
Damien George0699c6b2016-01-31 21:45:22 +0000118
Damien George0699c6b2016-01-31 21:45:22 +0000119# this function mirrors that in py/bc.c
Damien George1f7202d2019-09-02 21:35:26 +1000120def mp_opcode_format(bytecode, ip, count_var_uint):
Damien George0699c6b2016-01-31 21:45:22 +0000121 opcode = bytecode[ip]
122 ip_start = ip
Damien George1f7202d2019-09-02 21:35:26 +1000123 f = ((0x000003a4 >> (2 * ((opcode) >> 4))) & 3)
124 if f == MP_BC_FORMAT_QSTR:
Damien George814d5802018-12-11 00:52:33 +1100125 if config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
126 if (opcode == MP_BC_LOAD_NAME
127 or opcode == MP_BC_LOAD_GLOBAL
128 or opcode == MP_BC_LOAD_ATTR
129 or opcode == MP_BC_STORE_ATTR):
130 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000131 ip += 3
132 else:
Damien George1f7202d2019-09-02 21:35:26 +1000133 extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0
Damien George0699c6b2016-01-31 21:45:22 +0000134 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000135 if f == MP_BC_FORMAT_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100136 if count_var_uint:
137 while bytecode[ip] & 0x80 != 0:
138 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000139 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000140 elif f == MP_BC_FORMAT_OFFSET:
Damien George0699c6b2016-01-31 21:45:22 +0000141 ip += 2
142 ip += extra_byte
143 return f, ip - ip_start
144
145def decode_uint(bytecode, ip):
146 unum = 0
147 while True:
148 val = bytecode[ip]
149 ip += 1
150 unum = (unum << 7) | (val & 0x7f)
151 if not (val & 0x80):
152 break
153 return ip, unum
154
Damien Georgeea3c80a2019-02-21 15:18:59 +1100155def extract_prelude(bytecode, ip):
Damien George0699c6b2016-01-31 21:45:22 +0000156 ip, n_state = decode_uint(bytecode, ip)
157 ip, n_exc_stack = decode_uint(bytecode, ip)
158 scope_flags = bytecode[ip]; ip += 1
159 n_pos_args = bytecode[ip]; ip += 1
160 n_kwonly_args = bytecode[ip]; ip += 1
161 n_def_pos_args = bytecode[ip]; ip += 1
162 ip2, code_info_size = decode_uint(bytecode, ip)
163 ip += code_info_size
164 while bytecode[ip] != 0xff:
165 ip += 1
166 ip += 1
167 # ip now points to first opcode
168 # ip2 points to simple_name qstr
169 return ip, ip2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
170
Damien Georgeea3c80a2019-02-21 15:18:59 +1100171class MPFunTable:
172 pass
173
Damien George643d2a02019-04-08 11:21:18 +1000174class RawCode(object):
Damien George02fd83b2016-05-03 12:24:39 +0100175 # a set of all escaped names, to make sure they are unique
176 escaped_names = set()
177
Damien Georgeea3c80a2019-02-21 15:18:59 +1100178 # convert code kind number to string
179 code_kind_str = {
180 MP_CODE_BYTECODE: 'MP_CODE_BYTECODE',
181 MP_CODE_NATIVE_PY: 'MP_CODE_NATIVE_PY',
182 MP_CODE_NATIVE_VIPER: 'MP_CODE_NATIVE_VIPER',
183 MP_CODE_NATIVE_ASM: 'MP_CODE_NATIVE_ASM',
184 }
185
186 def __init__(self, code_kind, bytecode, prelude_offset, qstrs, objs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000187 # set core variables
Damien Georgeea3c80a2019-02-21 15:18:59 +1100188 self.code_kind = code_kind
Damien George0699c6b2016-01-31 21:45:22 +0000189 self.bytecode = bytecode
Damien Georgeea3c80a2019-02-21 15:18:59 +1100190 self.prelude_offset = prelude_offset
Damien George0699c6b2016-01-31 21:45:22 +0000191 self.qstrs = qstrs
192 self.objs = objs
193 self.raw_codes = raw_codes
194
Damien Georgeea3c80a2019-02-21 15:18:59 +1100195 if self.prelude_offset is None:
196 # no prelude, assign a dummy simple_name
197 self.prelude_offset = 0
198 self.simple_name = global_qstrs[1]
199 else:
200 # extract prelude
201 self.ip, self.ip2, self.prelude = extract_prelude(self.bytecode, self.prelude_offset)
202 self.simple_name = self._unpack_qstr(self.ip2)
203 self.source_file = self._unpack_qstr(self.ip2 + 2)
Damien George0699c6b2016-01-31 21:45:22 +0000204
205 def _unpack_qstr(self, ip):
206 qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
207 return global_qstrs[qst]
208
209 def dump(self):
210 # dump children first
211 for rc in self.raw_codes:
stijne4ab4042017-08-16 10:37:00 +0200212 rc.freeze('')
Damien George0699c6b2016-01-31 21:45:22 +0000213 # TODO
214
Damien Georgeea3c80a2019-02-21 15:18:59 +1100215 def freeze_children(self, parent_name):
Damien George0699c6b2016-01-31 21:45:22 +0000216 self.escaped_name = parent_name + self.simple_name.qstr_esc
217
Damien George02fd83b2016-05-03 12:24:39 +0100218 # make sure the escaped name is unique
219 i = 2
220 while self.escaped_name in RawCode.escaped_names:
221 self.escaped_name = parent_name + self.simple_name.qstr_esc + str(i)
222 i += 1
223 RawCode.escaped_names.add(self.escaped_name)
224
Damien George0699c6b2016-01-31 21:45:22 +0000225 # emit children first
226 for rc in self.raw_codes:
227 rc.freeze(self.escaped_name + '_')
228
Damien Georgeea3c80a2019-02-21 15:18:59 +1100229 def freeze_constants(self):
Damien George0699c6b2016-01-31 21:45:22 +0000230 # generate constant objects
231 for i, obj in enumerate(self.objs):
232 obj_name = 'const_obj_%s_%u' % (self.escaped_name, i)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100233 if obj is MPFunTable:
234 pass
235 elif obj is Ellipsis:
Damien George9ba3de62017-11-15 12:46:08 +1100236 print('#define %s mp_const_ellipsis_obj' % obj_name)
237 elif is_str_type(obj) or is_bytes_type(obj):
Damien Georgeb6bdf182016-09-02 15:10:45 +1000238 if is_str_type(obj):
239 obj = bytes_cons(obj, 'utf8')
240 obj_type = 'mp_type_str'
241 else:
242 obj_type = 'mp_type_bytes'
243 print('STATIC const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
244 % (obj_name, obj_type, qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
245 len(obj), ''.join(('\\x%02x' % b) for b in obj)))
Damien Georgec3beb162016-04-15 11:56:10 +0100246 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000247 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
248 # TODO check if we can actually fit this long-int into a small-int
249 raise FreezeError(self, 'target does not support long int')
250 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
251 # TODO
252 raise FreezeError(self, 'freezing int to long-long is not implemented')
253 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
254 neg = 0
255 if obj < 0:
256 obj = -obj
257 neg = 1
258 bits_per_dig = config.MPZ_DIG_SIZE
259 digs = []
260 z = obj
261 while z:
262 digs.append(z & ((1 << bits_per_dig) - 1))
263 z >>= bits_per_dig
264 ndigs = len(digs)
265 digs = ','.join(('%#x' % d) for d in digs)
266 print('STATIC const mp_obj_int_t %s = {{&mp_type_int}, '
Damien George44fc92e2018-07-09 13:43:34 +1000267 '{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};'
268 % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs))
Damien George0699c6b2016-01-31 21:45:22 +0000269 elif type(obj) is float:
Damien George72ae3c72016-08-10 13:26:11 +1000270 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
Damien George0699c6b2016-01-31 21:45:22 +0000271 print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
272 % (obj_name, obj))
Damien George72ae3c72016-08-10 13:26:11 +1000273 print('#endif')
Damien Georgec51c8832016-09-03 00:19:02 +1000274 elif type(obj) is complex:
275 print('STATIC const mp_obj_complex_t %s = {{&mp_type_complex}, %.16g, %.16g};'
276 % (obj_name, obj.real, obj.imag))
Damien George0699c6b2016-01-31 21:45:22 +0000277 else:
Damien George0699c6b2016-01-31 21:45:22 +0000278 raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
279
Damien Georgeb6a32892017-08-12 22:26:18 +1000280 # generate constant table, if it has any entries
281 const_table_len = len(self.qstrs) + len(self.objs) + len(self.raw_codes)
282 if const_table_len:
283 print('STATIC const mp_rom_obj_t const_table_data_%s[%u] = {'
284 % (self.escaped_name, const_table_len))
285 for qst in self.qstrs:
286 print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
287 for i in range(len(self.objs)):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100288 if self.objs[i] is MPFunTable:
289 print(' mp_fun_table,')
290 elif type(self.objs[i]) is float:
Damien Georgeb6a32892017-08-12 22:26:18 +1000291 print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
292 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
293 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
294 n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
295 n = ((n & ~0x3) | 2) + 0x80800000
296 print(' (mp_rom_obj_t)(0x%08x),' % (n,))
Damien George929d10a2018-07-09 12:22:40 +1000297 print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D')
298 n = struct.unpack('<Q', struct.pack('<d', self.objs[i]))[0]
299 n += 0x8004000000000000
300 print(' (mp_rom_obj_t)(0x%016x),' % (n,))
Damien Georgeb6a32892017-08-12 22:26:18 +1000301 print('#endif')
302 else:
303 print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
304 for rc in self.raw_codes:
305 print(' MP_ROM_PTR(&raw_code_%s),' % rc.escaped_name)
306 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000307
Damien Georgeea3c80a2019-02-21 15:18:59 +1100308 def freeze_module(self, qstr_links=(), type_sig=0):
Damien George0699c6b2016-01-31 21:45:22 +0000309 # generate module
310 if self.simple_name.str != '<module>':
311 print('STATIC ', end='')
312 print('const mp_raw_code_t raw_code_%s = {' % self.escaped_name)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100313 print(' .kind = %s,' % RawCode.code_kind_str[self.code_kind])
Damien George0699c6b2016-01-31 21:45:22 +0000314 print(' .scope_flags = 0x%02x,' % self.prelude[2])
315 print(' .n_pos_args = %u,' % self.prelude[3])
Damien Georgeea3c80a2019-02-21 15:18:59 +1100316 print(' .fun_data = fun_data_%s,' % self.escaped_name)
317 if len(self.qstrs) + len(self.objs) + len(self.raw_codes):
Damien George636ed0f2019-02-19 14:15:39 +1100318 print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
Damien Georgeb6a32892017-08-12 22:26:18 +1000319 else:
Damien George636ed0f2019-02-19 14:15:39 +1100320 print(' .const_table = NULL,')
321 print(' #if MICROPY_PERSISTENT_CODE_SAVE')
322 print(' .fun_data_len = %u,' % len(self.bytecode))
323 print(' .n_obj = %u,' % len(self.objs))
324 print(' .n_raw_code = %u,' % len(self.raw_codes))
Damien Georgec69f58e2019-09-06 23:55:15 +1000325 if self.code_kind == MP_CODE_BYTECODE:
326 print(' #if MICROPY_PY_SYS_SETTRACE')
327 print(' .prelude = {')
328 print(' .n_state = %u,' % self.prelude[0])
329 print(' .n_exc_stack = %u,' % self.prelude[1])
330 print(' .scope_flags = %u,' % self.prelude[2])
331 print(' .n_pos_args = %u,' % self.prelude[3])
332 print(' .n_kwonly_args = %u,' % self.prelude[4])
333 print(' .n_def_pos_args = %u,' % self.prelude[5])
334 print(' .qstr_block_name = %s,' % self.simple_name.qstr_id)
335 print(' .qstr_source_file = %s,' % self.source_file.qstr_id)
336 print(' .line_info = fun_data_%s + %u,' % (self.escaped_name, 0)) # TODO
337 print(' .locals = fun_data_%s + %u,' % (self.escaped_name, 0)) # TODO
338 print(' .opcodes = fun_data_%s + %u,' % (self.escaped_name, self.ip))
339 print(' },')
340 print(' .line_of_definition = %u,' % 0) # TODO
341 print(' #endif')
Jun Wub152bbd2019-05-06 00:31:11 -0700342 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100343 print(' .prelude_offset = %u,' % self.prelude_offset)
344 print(' .n_qstr = %u,' % len(qstr_links))
345 print(' .qstr_link = NULL,') # TODO
346 print(' #endif')
347 print(' #endif')
Jun Wub152bbd2019-05-06 00:31:11 -0700348 print(' #if MICROPY_EMIT_MACHINE_CODE')
Damien Georgeea3c80a2019-02-21 15:18:59 +1100349 print(' .type_sig = %u,' % type_sig)
Damien George636ed0f2019-02-19 14:15:39 +1100350 print(' #endif')
Damien George0699c6b2016-01-31 21:45:22 +0000351 print('};')
352
Damien Georgeea3c80a2019-02-21 15:18:59 +1100353class RawCodeBytecode(RawCode):
354 def __init__(self, bytecode, qstrs, objs, raw_codes):
Damien George643d2a02019-04-08 11:21:18 +1000355 super(RawCodeBytecode, self).__init__(MP_CODE_BYTECODE, bytecode, 0, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100356
357 def freeze(self, parent_name):
358 self.freeze_children(parent_name)
359
360 # generate bytecode data
361 print()
362 print('// frozen bytecode for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
363 print('STATIC ', end='')
364 if not config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE:
365 print('const ', end='')
366 print('byte fun_data_%s[%u] = {' % (self.escaped_name, len(self.bytecode)))
367 print(' ', end='')
368 for i in range(self.ip2):
369 print(' 0x%02x,' % self.bytecode[i], end='')
370 print()
371 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
372 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
373 print(' ', end='')
374 for i in range(self.ip2 + 4, self.ip):
375 print(' 0x%02x,' % self.bytecode[i], end='')
376 print()
377 ip = self.ip
378 while ip < len(self.bytecode):
379 f, sz = mp_opcode_format(self.bytecode, ip, True)
380 if f == 1:
381 qst = self._unpack_qstr(ip + 1).qstr_id
382 extra = '' if sz == 3 else ' 0x%02x,' % self.bytecode[ip + 3]
383 print(' ', '0x%02x,' % self.bytecode[ip], qst, '& 0xff,', qst, '>> 8,', extra)
384 else:
385 print(' ', ''.join('0x%02x, ' % self.bytecode[ip + i] for i in range(sz)))
386 ip += sz
387 print('};')
388
389 self.freeze_constants()
390 self.freeze_module()
391
392class RawCodeNative(RawCode):
393 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 +1000394 super(RawCodeNative, self).__init__(code_kind, fun_data, prelude_offset, qstrs, objs, raw_codes)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100395 self.prelude = prelude
396 self.qstr_links = qstr_links
397 self.type_sig = type_sig
398 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
399 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))'
400 else:
401 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
402
Jim Mussared4ab51562019-08-17 00:32:04 +1000403 # Allow single-byte alignment by default for x86/x64/xtensa, but on ARM we need halfword- or word- alignment.
404 if config.native_arch == MP_NATIVE_ARCH_ARMV6:
405 # ARMV6 -- four byte align.
406 self.fun_data_attributes += ' __attribute__ ((aligned (4)))'
407 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
408 # ARMVxxM -- two byte align.
409 self.fun_data_attributes += ' __attribute__ ((aligned (2)))'
410
Damien Georgeea3c80a2019-02-21 15:18:59 +1100411 def _asm_thumb_rewrite_mov(self, pc, val):
412 print(' (%u & 0xf0) | (%s >> 12),' % (self.bytecode[pc], val), end='')
413 print(' (%u & 0xfb) | (%s >> 9 & 0x04),' % (self.bytecode[pc + 1], val), end='')
414 print(' (%s & 0xff),' % (val,), end='')
415 print(' (%u & 0x07) | (%s >> 4 & 0x70),' % (self.bytecode[pc + 3], val))
416
417 def _link_qstr(self, pc, kind, qst):
418 if kind == 0:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000419 # Generic 16-bit link
Damien Georgeea3c80a2019-02-21 15:18:59 +1100420 print(' %s & 0xff, %s >> 8,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000421 return 2
Damien Georgeea3c80a2019-02-21 15:18:59 +1100422 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000423 # Architecture-specific link
424 is_obj = kind == 2
425 if is_obj:
Damien Georgeea3c80a2019-02-21 15:18:59 +1100426 qst = '((uintptr_t)MP_OBJ_NEW_QSTR(%s))' % qst
427 if config.native_arch in (MP_NATIVE_ARCH_X86, MP_NATIVE_ARCH_X64):
428 print(' %s & 0xff, %s >> 8, 0, 0,' % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000429 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100430 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
431 if is_obj:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000432 # qstr object, movw and movt
433 self._asm_thumb_rewrite_mov(pc, qst)
434 self._asm_thumb_rewrite_mov(pc + 4, '(%s >> 16)' % qst)
Damien George9d3031c2019-06-11 11:36:39 +1000435 return 8
Damien Georgeea3c80a2019-02-21 15:18:59 +1100436 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000437 # qstr number, movw instruction
438 self._asm_thumb_rewrite_mov(pc, qst)
Damien George9d3031c2019-06-11 11:36:39 +1000439 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100440 else:
441 assert 0
442
443 def freeze(self, parent_name):
444 self.freeze_children(parent_name)
445
446 # generate native code data
447 print()
448 if self.code_kind == MP_CODE_NATIVE_PY:
449 print('// frozen native code for file %s, scope %s%s' % (self.source_file.str, parent_name, self.simple_name.str))
450 elif self.code_kind == MP_CODE_NATIVE_VIPER:
451 print('// frozen viper code for scope %s' % (parent_name,))
452 else:
453 print('// frozen assembler code for scope %s' % (parent_name,))
454 print('STATIC const byte fun_data_%s[%u] %s = {' % (self.escaped_name, len(self.bytecode), self.fun_data_attributes))
455
456 if self.code_kind == MP_CODE_NATIVE_PY:
457 i_top = self.prelude_offset
458 else:
459 i_top = len(self.bytecode)
460 i = 0
461 qi = 0
462 while i < i_top:
463 if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]:
464 # link qstr
465 qi_off, qi_kind, qi_val = self.qstr_links[qi]
466 qst = global_qstrs[qi_val].qstr_id
Damien George9d3031c2019-06-11 11:36:39 +1000467 i += self._link_qstr(i, qi_kind, qst)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100468 qi += 1
469 else:
470 # copy machine code (max 16 bytes)
471 i16 = min(i + 16, i_top)
472 if qi < len(self.qstr_links):
473 i16 = min(i16, self.qstr_links[qi][0])
474 print(' ', end='')
475 for ii in range(i, i16):
476 print(' 0x%02x,' % self.bytecode[ii], end='')
477 print()
478 i = i16
479
480 if self.code_kind == MP_CODE_NATIVE_PY:
481 print(' ', end='')
482 for i in range(self.prelude_offset, self.ip2):
483 print(' 0x%02x,' % self.bytecode[i], end='')
484 print()
485
486 print(' ', self.simple_name.qstr_id, '& 0xff,', self.simple_name.qstr_id, '>> 8,')
487 print(' ', self.source_file.qstr_id, '& 0xff,', self.source_file.qstr_id, '>> 8,')
488
489 print(' ', end='')
490 for i in range(self.ip2 + 4, self.ip):
491 print(' 0x%02x,' % self.bytecode[i], end='')
492 print()
493
494 print('};')
495
496 self.freeze_constants()
497 self.freeze_module(self.qstr_links, self.type_sig)
498
Damien George992a6e12019-03-01 14:03:10 +1100499class BytecodeBuffer:
500 def __init__(self, size):
501 self.buf = bytearray(size)
502 self.idx = 0
503
504 def is_full(self):
505 return self.idx == len(self.buf)
506
507 def append(self, b):
508 self.buf[self.idx] = b
509 self.idx += 1
510
511def read_byte(f, out=None):
512 b = bytes_cons(f.read(1))[0]
513 if out is not None:
514 out.append(b)
515 return b
516
517def read_uint(f, out=None):
Damien George0699c6b2016-01-31 21:45:22 +0000518 i = 0
519 while True:
Damien George992a6e12019-03-01 14:03:10 +1100520 b = read_byte(f, out)
Damien George0699c6b2016-01-31 21:45:22 +0000521 i = (i << 7) | (b & 0x7f)
522 if b & 0x80 == 0:
523 break
524 return i
525
Damien George5996eeb2019-02-25 23:15:51 +1100526def read_qstr(f, qstr_win):
Damien George0699c6b2016-01-31 21:45:22 +0000527 ln = read_uint(f)
Damien George4f0931b2019-03-01 14:33:03 +1100528 if ln == 0:
529 # static qstr
530 return bytes_cons(f.read(1))[0]
Damien George5996eeb2019-02-25 23:15:51 +1100531 if ln & 1:
532 # qstr in table
533 return qstr_win.access(ln >> 1)
534 ln >>= 1
Damien Georgec3beb162016-04-15 11:56:10 +0100535 data = str_cons(f.read(ln), 'utf8')
Damien George4f0931b2019-03-01 14:33:03 +1100536 global_qstrs.append(QStrType(data))
Damien George5996eeb2019-02-25 23:15:51 +1100537 qstr_win.push(len(global_qstrs) - 1)
Damien George0699c6b2016-01-31 21:45:22 +0000538 return len(global_qstrs) - 1
539
540def read_obj(f):
541 obj_type = f.read(1)
542 if obj_type == b'e':
543 return Ellipsis
544 else:
545 buf = f.read(read_uint(f))
546 if obj_type == b's':
Damien Georgec3beb162016-04-15 11:56:10 +0100547 return str_cons(buf, 'utf8')
Damien George0699c6b2016-01-31 21:45:22 +0000548 elif obj_type == b'b':
Damien Georgec3beb162016-04-15 11:56:10 +0100549 return bytes_cons(buf)
Damien George0699c6b2016-01-31 21:45:22 +0000550 elif obj_type == b'i':
Damien Georgec3beb162016-04-15 11:56:10 +0100551 return int(str_cons(buf, 'ascii'), 10)
Damien George0699c6b2016-01-31 21:45:22 +0000552 elif obj_type == b'f':
Damien Georgec3beb162016-04-15 11:56:10 +0100553 return float(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000554 elif obj_type == b'c':
Damien Georgec3beb162016-04-15 11:56:10 +0100555 return complex(str_cons(buf, 'ascii'))
Damien George0699c6b2016-01-31 21:45:22 +0000556 else:
557 assert 0
558
Damien George992a6e12019-03-01 14:03:10 +1100559def read_prelude(f, bytecode):
560 n_state = read_uint(f, bytecode)
561 n_exc_stack = read_uint(f, bytecode)
562 scope_flags = read_byte(f, bytecode)
563 n_pos_args = read_byte(f, bytecode)
564 n_kwonly_args = read_byte(f, bytecode)
565 n_def_pos_args = read_byte(f, bytecode)
566 l1 = bytecode.idx
567 code_info_size = read_uint(f, bytecode)
568 l2 = bytecode.idx
569 for _ in range(code_info_size - (l2 - l1)):
570 read_byte(f, bytecode)
571 while read_byte(f, bytecode) != 255:
572 pass
573 return l2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args, code_info_size)
Damien George0699c6b2016-01-31 21:45:22 +0000574
Damien George992a6e12019-03-01 14:03:10 +1100575def read_qstr_and_pack(f, bytecode, qstr_win):
576 qst = read_qstr(f, qstr_win)
577 bytecode.append(qst & 0xff)
578 bytecode.append(qst >> 8)
579
580def read_bytecode(file, bytecode, qstr_win):
581 while not bytecode.is_full():
582 op = read_byte(file, bytecode)
583 f, sz = mp_opcode_format(bytecode.buf, bytecode.idx - 1, False)
584 sz -= 1
Damien George1f7202d2019-09-02 21:35:26 +1000585 if f == MP_BC_FORMAT_QSTR:
Damien George992a6e12019-03-01 14:03:10 +1100586 read_qstr_and_pack(file, bytecode, qstr_win)
587 sz -= 2
Damien George1f7202d2019-09-02 21:35:26 +1000588 elif f == MP_BC_FORMAT_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100589 while read_byte(file, bytecode) & 0x80:
590 pass
591 for _ in range(sz):
592 read_byte(file, bytecode)
Damien George0699c6b2016-01-31 21:45:22 +0000593
Damien George5996eeb2019-02-25 23:15:51 +1100594def read_raw_code(f, qstr_win):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100595 kind_len = read_uint(f)
596 kind = (kind_len & 3) + MP_CODE_BYTECODE
597 fun_data_len = kind_len >> 2
598 fun_data = BytecodeBuffer(fun_data_len)
599
600 if kind == MP_CODE_BYTECODE:
601 name_idx, prelude = read_prelude(f, fun_data)
602 read_bytecode(f, fun_data, qstr_win)
603 else:
604 fun_data.buf[:] = f.read(fun_data_len)
605
606 qstr_links = []
607 if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
608 # load qstr link table
609 n_qstr_link = read_uint(f)
610 for _ in range(n_qstr_link):
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000611 off = read_uint(f)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100612 qst = read_qstr(f, qstr_win)
613 qstr_links.append((off >> 2, off & 3, qst))
614
615 type_sig = 0
616 if kind == MP_CODE_NATIVE_PY:
617 prelude_offset = read_uint(f)
618 _, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset)
619 else:
620 prelude_offset = None
621 scope_flags = read_uint(f)
622 n_pos_args = 0
623 if kind == MP_CODE_NATIVE_ASM:
624 n_pos_args = read_uint(f)
625 type_sig = read_uint(f)
626 prelude = (None, None, scope_flags, n_pos_args, 0)
627
628 if kind in (MP_CODE_BYTECODE, MP_CODE_NATIVE_PY):
629 fun_data.idx = name_idx # rewind to where qstrs are in prelude
630 read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
631 read_qstr_and_pack(f, fun_data, qstr_win) # source_file
632
633 qstrs = []
634 objs = []
635 raw_codes = []
636 if kind != MP_CODE_NATIVE_ASM:
637 # load constant table
638 n_obj = read_uint(f)
639 n_raw_code = read_uint(f)
640 qstrs = [read_qstr(f, qstr_win) for _ in range(prelude[3] + prelude[4])]
641 if kind != MP_CODE_BYTECODE:
642 objs.append(MPFunTable)
643 objs.extend([read_obj(f) for _ in range(n_obj)])
644 raw_codes = [read_raw_code(f, qstr_win) for _ in range(n_raw_code)]
645
646 if kind == MP_CODE_BYTECODE:
647 return RawCodeBytecode(fun_data.buf, qstrs, objs, raw_codes)
648 else:
649 return RawCodeNative(kind, fun_data.buf, prelude_offset, prelude, qstr_links, qstrs, objs, raw_codes, type_sig)
Damien George0699c6b2016-01-31 21:45:22 +0000650
651def read_mpy(filename):
652 with open(filename, 'rb') as f:
Damien Georgec3beb162016-04-15 11:56:10 +0100653 header = bytes_cons(f.read(4))
Damien George0699c6b2016-01-31 21:45:22 +0000654 if header[0] != ord('M'):
655 raise Exception('not a valid .mpy file')
Damien George6a110482017-02-17 00:19:34 +1100656 if header[1] != config.MPY_VERSION:
657 raise Exception('incompatible .mpy version')
Damien George5996eeb2019-02-25 23:15:51 +1100658 feature_byte = header[2]
659 qw_size = read_uint(f)
660 config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE = (feature_byte & 1) != 0
661 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000662 mpy_native_arch = feature_byte >> 2
663 if mpy_native_arch != MP_NATIVE_ARCH_NONE:
664 if config.native_arch == MP_NATIVE_ARCH_NONE:
665 config.native_arch = mpy_native_arch
666 elif config.native_arch != mpy_native_arch:
667 raise Exception('native architecture mismatch')
Damien George0699c6b2016-01-31 21:45:22 +0000668 config.mp_small_int_bits = header[3]
Damien George5996eeb2019-02-25 23:15:51 +1100669 qstr_win = QStrWindow(qw_size)
670 return read_raw_code(f, qstr_win)
Damien George0699c6b2016-01-31 21:45:22 +0000671
672def dump_mpy(raw_codes):
673 for rc in raw_codes:
674 rc.dump()
675
Damien Georgeb4790af2016-09-02 15:09:21 +1000676def freeze_mpy(base_qstrs, raw_codes):
Damien George0699c6b2016-01-31 21:45:22 +0000677 # add to qstrs
678 new = {}
679 for q in global_qstrs:
680 # don't add duplicates
Damien George4f0931b2019-03-01 14:33:03 +1100681 if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new:
Damien George0699c6b2016-01-31 21:45:22 +0000682 continue
683 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str)
684 new = sorted(new.values(), key=lambda x: x[0])
685
686 print('#include "py/mpconfig.h"')
687 print('#include "py/objint.h"')
688 print('#include "py/objstr.h"')
689 print('#include "py/emitglue.h"')
690 print()
691
Damien George98458a42017-01-05 15:52:52 +1100692 print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
693 print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"')
Damien George99b47192016-05-16 23:13:30 +0100694 print('#endif')
695 print()
696
697 print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL)
698 print('#error "incompatible MICROPY_LONGINT_IMPL"')
699 print('#endif')
700 print()
701
702 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
703 print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE)
704 print('#error "incompatible MPZ_DIG_SIZE"')
705 print('#endif')
706 print()
707
708
Damien George0699c6b2016-01-31 21:45:22 +0000709 print('#if MICROPY_PY_BUILTINS_FLOAT')
710 print('typedef struct _mp_obj_float_t {')
711 print(' mp_obj_base_t base;')
712 print(' mp_float_t value;')
713 print('} mp_obj_float_t;')
714 print('#endif')
715 print()
716
Damien Georgec51c8832016-09-03 00:19:02 +1000717 print('#if MICROPY_PY_BUILTINS_COMPLEX')
718 print('typedef struct _mp_obj_complex_t {')
719 print(' mp_obj_base_t base;')
720 print(' mp_float_t real;')
721 print(' mp_float_t imag;')
722 print('} mp_obj_complex_t;')
723 print('#endif')
724 print()
725
Dave Hylands39eef272018-12-11 14:55:26 -0800726 if len(new) > 0:
727 print('enum {')
728 for i in range(len(new)):
729 if i == 0:
730 print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1])
731 else:
732 print(' MP_QSTR_%s,' % new[i][1])
733 print('};')
Damien George0699c6b2016-01-31 21:45:22 +0000734
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100735 # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len
736 qstr_pool_alloc = min(len(new), 10)
737
Damien George0699c6b2016-01-31 21:45:22 +0000738 print()
739 print('extern const qstr_pool_t mp_qstr_const_pool;');
740 print('const qstr_pool_t mp_qstr_frozen_const_pool = {')
741 print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool')
742 print(' MP_QSTRnumber_of, // previous pool size')
Rich Barlow6e5a40c2018-07-19 12:42:26 +0100743 print(' %u, // allocated entries' % qstr_pool_alloc)
Damien George0699c6b2016-01-31 21:45:22 +0000744 print(' %u, // used entries' % len(new))
745 print(' {')
746 for _, _, qstr in new:
Damien Georgeb4790af2016-09-02 15:09:21 +1000747 print(' %s,'
748 % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr))
Damien George0699c6b2016-01-31 21:45:22 +0000749 print(' },')
750 print('};')
751
752 for rc in raw_codes:
753 rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_')
754
755 print()
756 print('const char mp_frozen_mpy_names[] = {')
757 for rc in raw_codes:
Damien George9b4c0132016-05-23 12:46:02 +0100758 module_name = rc.source_file.str
Damien George0699c6b2016-01-31 21:45:22 +0000759 print('"%s\\0"' % module_name)
760 print('"\\0"};')
761
762 print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {')
763 for rc in raw_codes:
764 print(' &raw_code_%s,' % rc.escaped_name)
765 print('};')
766
767def main():
768 import argparse
769 cmd_parser = argparse.ArgumentParser(description='A tool to work with MicroPython .mpy files.')
770 cmd_parser.add_argument('-d', '--dump', action='store_true',
771 help='dump contents of files')
772 cmd_parser.add_argument('-f', '--freeze', action='store_true',
773 help='freeze files')
774 cmd_parser.add_argument('-q', '--qstr-header',
775 help='qstr header file to freeze against')
776 cmd_parser.add_argument('-mlongint-impl', choices=['none', 'longlong', 'mpz'], default='mpz',
777 help='long-int implementation used by target (default mpz)')
778 cmd_parser.add_argument('-mmpz-dig-size', metavar='N', type=int, default=16,
779 help='mpz digit size used by target (default 16)')
780 cmd_parser.add_argument('files', nargs='+',
781 help='input .mpy files')
782 args = cmd_parser.parse_args()
783
784 # set config values relevant to target machine
785 config.MICROPY_LONGINT_IMPL = {
786 'none':config.MICROPY_LONGINT_IMPL_NONE,
787 'longlong':config.MICROPY_LONGINT_IMPL_LONGLONG,
788 'mpz':config.MICROPY_LONGINT_IMPL_MPZ,
789 }[args.mlongint_impl]
790 config.MPZ_DIG_SIZE = args.mmpz_dig_size
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000791 config.native_arch = MP_NATIVE_ARCH_NONE
Damien George0699c6b2016-01-31 21:45:22 +0000792
Damien Georgeb4790af2016-09-02 15:09:21 +1000793 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +0000794 if args.qstr_header:
795 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien Georgeb4790af2016-09-02 15:09:21 +1000796 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs['BYTES_IN_LEN'])
797 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs['BYTES_IN_HASH'])
Damien George0699c6b2016-01-31 21:45:22 +0000798 else:
Damien Georgeb4790af2016-09-02 15:09:21 +1000799 config.MICROPY_QSTR_BYTES_IN_LEN = 1
800 config.MICROPY_QSTR_BYTES_IN_HASH = 1
801 base_qstrs = {}
Damien George0699c6b2016-01-31 21:45:22 +0000802
803 raw_codes = [read_mpy(file) for file in args.files]
804
805 if args.dump:
806 dump_mpy(raw_codes)
807 elif args.freeze:
808 try:
Damien Georgeb4790af2016-09-02 15:09:21 +1000809 freeze_mpy(base_qstrs, raw_codes)
Damien George0699c6b2016-01-31 21:45:22 +0000810 except FreezeError as er:
811 print(er, file=sys.stderr)
812 sys.exit(1)
813
814if __name__ == '__main__':
815 main()