blob: 84c09a0c688d2da23e744cf509fbfb82f78bfdd2 [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
Damien George69661f32020-02-27 15:36:53 +110030
31if platform.python_version_tuple()[0] == "2":
Damien Georgef2040bf2021-10-22 22:22:47 +110032 from binascii import hexlify as hexlify_py2
33
34 str_cons = lambda val, enc=None: str(val)
Damien Georgec3beb162016-04-15 11:56:10 +010035 bytes_cons = lambda val, enc=None: bytearray(val)
36 is_str_type = lambda o: type(o) is str
37 is_bytes_type = lambda o: type(o) is bytearray
38 is_int_type = lambda o: type(o) is int or type(o) is long
Damien Georgef2040bf2021-10-22 22:22:47 +110039
40 def hexlify_to_str(b):
41 x = hexlify_py2(b)
42 return ":".join(x[i : i + 2] for i in range(0, len(x), 2))
43
Damien Georgec3beb162016-04-15 11:56:10 +010044else:
Damien Georgef2040bf2021-10-22 22:22:47 +110045 from binascii import hexlify
46
Damien Georgec3beb162016-04-15 11:56:10 +010047 str_cons = str
48 bytes_cons = bytes
49 is_str_type = lambda o: type(o) is str
50 is_bytes_type = lambda o: type(o) is bytes
51 is_int_type = lambda o: type(o) is int
Damien Georgef2040bf2021-10-22 22:22:47 +110052
53 def hexlify_to_str(b):
54 return str(hexlify(b, ":"), "ascii")
55
56
Damien Georgec3beb162016-04-15 11:56:10 +010057# end compatibility code
58
Damien George0699c6b2016-01-31 21:45:22 +000059import sys
Damien George72ae3c72016-08-10 13:26:11 +100060import struct
Damien George0699c6b2016-01-31 21:45:22 +000061
Damien George69661f32020-02-27 15:36:53 +110062sys.path.append(sys.path[0] + "/../py")
Damien George0699c6b2016-01-31 21:45:22 +000063import makeqstrdata as qstrutil
64
Damien George69661f32020-02-27 15:36:53 +110065
Damien Georgef2040bf2021-10-22 22:22:47 +110066class MPYReadError(Exception):
67 def __init__(self, filename, msg):
68 self.filename = filename
69 self.msg = msg
70
71 def __str__(self):
72 return "%s: %s" % (self.filename, self.msg)
73
74
Damien George0699c6b2016-01-31 21:45:22 +000075class FreezeError(Exception):
76 def __init__(self, rawcode, msg):
77 self.rawcode = rawcode
78 self.msg = msg
79
80 def __str__(self):
Damien George69661f32020-02-27 15:36:53 +110081 return "error while freezing %s: %s" % (self.rawcode.source_file, self.msg)
82
Damien George0699c6b2016-01-31 21:45:22 +000083
84class Config:
Damien Georgef2040bf2021-10-22 22:22:47 +110085 MPY_VERSION = 6
Damien George0699c6b2016-01-31 21:45:22 +000086 MICROPY_LONGINT_IMPL_NONE = 0
87 MICROPY_LONGINT_IMPL_LONGLONG = 1
88 MICROPY_LONGINT_IMPL_MPZ = 2
Damien George69661f32020-02-27 15:36:53 +110089
90
Damien George0699c6b2016-01-31 21:45:22 +000091config = Config()
92
Damien George69661f32020-02-27 15:36:53 +110093
Damien George4f0931b2019-03-01 14:33:03 +110094class QStrType:
95 def __init__(self, str):
96 self.str = str
97 self.qstr_esc = qstrutil.qstr_escape(self.str)
Damien George69661f32020-02-27 15:36:53 +110098 self.qstr_id = "MP_QSTR_" + self.qstr_esc
99
Damien George4f0931b2019-03-01 14:33:03 +1100100
101# Initialise global list of qstrs with static qstrs
Damien George69661f32020-02-27 15:36:53 +1100102global_qstrs = [None] # MP_QSTRnull should never be referenced
Damien George4f0931b2019-03-01 14:33:03 +1100103for n in qstrutil.static_qstr_list:
104 global_qstrs.append(QStrType(n))
105
Damien George69661f32020-02-27 15:36:53 +1100106
Damien Georgeea3c80a2019-02-21 15:18:59 +1100107MP_CODE_BYTECODE = 2
108MP_CODE_NATIVE_PY = 3
109MP_CODE_NATIVE_VIPER = 4
110MP_CODE_NATIVE_ASM = 5
111
112MP_NATIVE_ARCH_NONE = 0
113MP_NATIVE_ARCH_X86 = 1
114MP_NATIVE_ARCH_X64 = 2
115MP_NATIVE_ARCH_ARMV6 = 3
116MP_NATIVE_ARCH_ARMV6M = 4
117MP_NATIVE_ARCH_ARMV7M = 5
118MP_NATIVE_ARCH_ARMV7EM = 6
119MP_NATIVE_ARCH_ARMV7EMSP = 7
120MP_NATIVE_ARCH_ARMV7EMDP = 8
121MP_NATIVE_ARCH_XTENSA = 9
Damien George9adedce2019-09-13 13:15:12 +1000122MP_NATIVE_ARCH_XTENSAWIN = 10
Damien Georgeea3c80a2019-02-21 15:18:59 +1100123
Damien Georgef2040bf2021-10-22 22:22:47 +1100124MP_SCOPE_FLAG_VIPERRELOC = 0x10
125MP_SCOPE_FLAG_VIPERRODATA = 0x20
126MP_SCOPE_FLAG_VIPERBSS = 0x40
127
Damien George69661f32020-02-27 15:36:53 +1100128MP_BC_MASK_EXTRA_BYTE = 0x9E
Damien George0699c6b2016-01-31 21:45:22 +0000129
Damien George1f7202d2019-09-02 21:35:26 +1000130MP_BC_FORMAT_BYTE = 0
131MP_BC_FORMAT_QSTR = 1
132MP_BC_FORMAT_VAR_UINT = 2
133MP_BC_FORMAT_OFFSET = 3
134
Damien Georgef2040bf2021-10-22 22:22:47 +1100135mp_unary_op_method_name = (
136 "__pos__",
137 "__neg__",
138 "__invert__",
139 "<not>",
140)
141
142mp_binary_op_method_name = (
143 "__lt__",
144 "__gt__",
145 "__eq__",
146 "__le__",
147 "__ge__",
148 "__ne__",
149 "<in>",
150 "<is>",
151 "<exception match>",
152 "__ior__",
153 "__ixor__",
154 "__iand__",
155 "__ilshift__",
156 "__irshift__",
157 "__iadd__",
158 "__isub__",
159 "__imul__",
160 "__imatmul__",
161 "__ifloordiv__",
162 "__itruediv__",
163 "__imod__",
164 "__ipow__",
165 "__or__",
166 "__xor__",
167 "__and__",
168 "__lshift__",
169 "__rshift__",
170 "__add__",
171 "__sub__",
172 "__mul__",
173 "__matmul__",
174 "__floordiv__",
175 "__truediv__",
176 "__mod__",
177 "__pow__",
178)
179
180
181class Opcodes:
182 # fmt: off
183 # Load, Store, Delete, Import, Make, Build, Unpack, Call, Jump, Exception, For, sTack, Return, Yield, Op
184 MP_BC_BASE_RESERVED = (0x00) # ----------------
185 MP_BC_BASE_QSTR_O = (0x10) # LLLLLLSSSDDII---
186 MP_BC_BASE_VINT_E = (0x20) # MMLLLLSSDDBBBBBB
187 MP_BC_BASE_VINT_O = (0x30) # UUMMCCCC--------
188 MP_BC_BASE_JUMP_E = (0x40) # J-JJJJJEEEEF----
189 MP_BC_BASE_BYTE_O = (0x50) # LLLLSSDTTTTTEEFF
190 MP_BC_BASE_BYTE_E = (0x60) # --BREEEYYI------
191 MP_BC_LOAD_CONST_SMALL_INT_MULTI = (0x70) # LLLLLLLLLLLLLLLL
192 # = (0x80) # LLLLLLLLLLLLLLLL
193 # = (0x90) # LLLLLLLLLLLLLLLL
194 # = (0xa0) # LLLLLLLLLLLLLLLL
195 MP_BC_LOAD_FAST_MULTI = (0xb0) # LLLLLLLLLLLLLLLL
196 MP_BC_STORE_FAST_MULTI = (0xc0) # SSSSSSSSSSSSSSSS
197 MP_BC_UNARY_OP_MULTI = (0xd0) # OOOOOOO
198 MP_BC_BINARY_OP_MULTI = (0xd7) # OOOOOOOOO
199 # = (0xe0) # OOOOOOOOOOOOOOOO
200 # = (0xf0) # OOOOOOOOOO------
201
202 MP_BC_LOAD_CONST_SMALL_INT_MULTI_NUM = 64
203 MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS = 16
204 MP_BC_LOAD_FAST_MULTI_NUM = 16
205 MP_BC_STORE_FAST_MULTI_NUM = 16
206 MP_BC_UNARY_OP_MULTI_NUM = 4 # MP_UNARY_OP_NUM_BYTECODE
207 MP_BC_BINARY_OP_MULTI_NUM = 35 # MP_BINARY_OP_NUM_BYTECODE
208
209 MP_BC_LOAD_CONST_FALSE = (MP_BC_BASE_BYTE_O + 0x00)
210 MP_BC_LOAD_CONST_NONE = (MP_BC_BASE_BYTE_O + 0x01)
211 MP_BC_LOAD_CONST_TRUE = (MP_BC_BASE_BYTE_O + 0x02)
212 MP_BC_LOAD_CONST_SMALL_INT = (MP_BC_BASE_VINT_E + 0x02) # signed var-int
213 MP_BC_LOAD_CONST_STRING = (MP_BC_BASE_QSTR_O + 0x00) # qstr
214 MP_BC_LOAD_CONST_OBJ = (MP_BC_BASE_VINT_E + 0x03) # ptr
215 MP_BC_LOAD_NULL = (MP_BC_BASE_BYTE_O + 0x03)
216
217 MP_BC_LOAD_FAST_N = (MP_BC_BASE_VINT_E + 0x04) # uint
218 MP_BC_LOAD_DEREF = (MP_BC_BASE_VINT_E + 0x05) # uint
219 MP_BC_LOAD_NAME = (MP_BC_BASE_QSTR_O + 0x01) # qstr
220 MP_BC_LOAD_GLOBAL = (MP_BC_BASE_QSTR_O + 0x02) # qstr
221 MP_BC_LOAD_ATTR = (MP_BC_BASE_QSTR_O + 0x03) # qstr
222 MP_BC_LOAD_METHOD = (MP_BC_BASE_QSTR_O + 0x04) # qstr
223 MP_BC_LOAD_SUPER_METHOD = (MP_BC_BASE_QSTR_O + 0x05) # qstr
224 MP_BC_LOAD_BUILD_CLASS = (MP_BC_BASE_BYTE_O + 0x04)
225 MP_BC_LOAD_SUBSCR = (MP_BC_BASE_BYTE_O + 0x05)
226
227 MP_BC_STORE_FAST_N = (MP_BC_BASE_VINT_E + 0x06) # uint
228 MP_BC_STORE_DEREF = (MP_BC_BASE_VINT_E + 0x07) # uint
229 MP_BC_STORE_NAME = (MP_BC_BASE_QSTR_O + 0x06) # qstr
230 MP_BC_STORE_GLOBAL = (MP_BC_BASE_QSTR_O + 0x07) # qstr
231 MP_BC_STORE_ATTR = (MP_BC_BASE_QSTR_O + 0x08) # qstr
232 MP_BC_STORE_SUBSCR = (MP_BC_BASE_BYTE_O + 0x06)
233
234 MP_BC_DELETE_FAST = (MP_BC_BASE_VINT_E + 0x08) # uint
235 MP_BC_DELETE_DEREF = (MP_BC_BASE_VINT_E + 0x09) # uint
236 MP_BC_DELETE_NAME = (MP_BC_BASE_QSTR_O + 0x09) # qstr
237 MP_BC_DELETE_GLOBAL = (MP_BC_BASE_QSTR_O + 0x0a) # qstr
238
239 MP_BC_DUP_TOP = (MP_BC_BASE_BYTE_O + 0x07)
240 MP_BC_DUP_TOP_TWO = (MP_BC_BASE_BYTE_O + 0x08)
241 MP_BC_POP_TOP = (MP_BC_BASE_BYTE_O + 0x09)
242 MP_BC_ROT_TWO = (MP_BC_BASE_BYTE_O + 0x0a)
243 MP_BC_ROT_THREE = (MP_BC_BASE_BYTE_O + 0x0b)
244
Damien George538c3c02022-03-16 09:37:58 +1100245 MP_BC_UNWIND_JUMP = (MP_BC_BASE_JUMP_E + 0x00) # signed relative bytecode offset; then a byte
246 MP_BC_JUMP = (MP_BC_BASE_JUMP_E + 0x02) # signed relative bytecode offset
247 MP_BC_POP_JUMP_IF_TRUE = (MP_BC_BASE_JUMP_E + 0x03) # signed relative bytecode offset
248 MP_BC_POP_JUMP_IF_FALSE = (MP_BC_BASE_JUMP_E + 0x04) # signed relative bytecode offset
Damien George6d11c692022-03-21 16:36:13 +1100249 MP_BC_JUMP_IF_TRUE_OR_POP = (MP_BC_BASE_JUMP_E + 0x05) # unsigned relative bytecode offset
250 MP_BC_JUMP_IF_FALSE_OR_POP = (MP_BC_BASE_JUMP_E + 0x06) # unsigned relative bytecode offset
Damien George538c3c02022-03-16 09:37:58 +1100251 MP_BC_SETUP_WITH = (MP_BC_BASE_JUMP_E + 0x07) # unsigned relative bytecode offset
252 MP_BC_SETUP_EXCEPT = (MP_BC_BASE_JUMP_E + 0x08) # unsigned relative bytecode offset
253 MP_BC_SETUP_FINALLY = (MP_BC_BASE_JUMP_E + 0x09) # unsigned relative bytecode offset
254 MP_BC_POP_EXCEPT_JUMP = (MP_BC_BASE_JUMP_E + 0x0a) # unsigned relative bytecode offset
255 MP_BC_FOR_ITER = (MP_BC_BASE_JUMP_E + 0x0b) # unsigned relative bytecode offset
Damien Georgef2040bf2021-10-22 22:22:47 +1100256 MP_BC_WITH_CLEANUP = (MP_BC_BASE_BYTE_O + 0x0c)
257 MP_BC_END_FINALLY = (MP_BC_BASE_BYTE_O + 0x0d)
258 MP_BC_GET_ITER = (MP_BC_BASE_BYTE_O + 0x0e)
259 MP_BC_GET_ITER_STACK = (MP_BC_BASE_BYTE_O + 0x0f)
260
261 MP_BC_BUILD_TUPLE = (MP_BC_BASE_VINT_E + 0x0a) # uint
262 MP_BC_BUILD_LIST = (MP_BC_BASE_VINT_E + 0x0b) # uint
263 MP_BC_BUILD_MAP = (MP_BC_BASE_VINT_E + 0x0c) # uint
264 MP_BC_STORE_MAP = (MP_BC_BASE_BYTE_E + 0x02)
265 MP_BC_BUILD_SET = (MP_BC_BASE_VINT_E + 0x0d) # uint
266 MP_BC_BUILD_SLICE = (MP_BC_BASE_VINT_E + 0x0e) # uint
267 MP_BC_STORE_COMP = (MP_BC_BASE_VINT_E + 0x0f) # uint
268 MP_BC_UNPACK_SEQUENCE = (MP_BC_BASE_VINT_O + 0x00) # uint
269 MP_BC_UNPACK_EX = (MP_BC_BASE_VINT_O + 0x01) # uint
270
271 MP_BC_RETURN_VALUE = (MP_BC_BASE_BYTE_E + 0x03)
272 MP_BC_RAISE_LAST = (MP_BC_BASE_BYTE_E + 0x04)
273 MP_BC_RAISE_OBJ = (MP_BC_BASE_BYTE_E + 0x05)
274 MP_BC_RAISE_FROM = (MP_BC_BASE_BYTE_E + 0x06)
275 MP_BC_YIELD_VALUE = (MP_BC_BASE_BYTE_E + 0x07)
276 MP_BC_YIELD_FROM = (MP_BC_BASE_BYTE_E + 0x08)
277
278 MP_BC_MAKE_FUNCTION = (MP_BC_BASE_VINT_O + 0x02) # uint
279 MP_BC_MAKE_FUNCTION_DEFARGS = (MP_BC_BASE_VINT_O + 0x03) # uint
280 MP_BC_MAKE_CLOSURE = (MP_BC_BASE_VINT_E + 0x00) # uint; extra byte
281 MP_BC_MAKE_CLOSURE_DEFARGS = (MP_BC_BASE_VINT_E + 0x01) # uint; extra byte
282 MP_BC_CALL_FUNCTION = (MP_BC_BASE_VINT_O + 0x04) # uint
283 MP_BC_CALL_FUNCTION_VAR_KW = (MP_BC_BASE_VINT_O + 0x05) # uint
284 MP_BC_CALL_METHOD = (MP_BC_BASE_VINT_O + 0x06) # uint
285 MP_BC_CALL_METHOD_VAR_KW = (MP_BC_BASE_VINT_O + 0x07) # uint
286
287 MP_BC_IMPORT_NAME = (MP_BC_BASE_QSTR_O + 0x0b) # qstr
288 MP_BC_IMPORT_FROM = (MP_BC_BASE_QSTR_O + 0x0c) # qstr
289 MP_BC_IMPORT_STAR = (MP_BC_BASE_BYTE_E + 0x09)
290 # fmt: on
291
Damien George538c3c02022-03-16 09:37:58 +1100292 # Create sets of related opcodes.
293 ALL_OFFSET_SIGNED = (
294 MP_BC_UNWIND_JUMP,
295 MP_BC_JUMP,
296 MP_BC_POP_JUMP_IF_TRUE,
297 MP_BC_POP_JUMP_IF_FALSE,
Damien George538c3c02022-03-16 09:37:58 +1100298 )
299
Damien Georgef2040bf2021-10-22 22:22:47 +1100300 # Create a dict mapping opcode value to opcode name.
301 mapping = ["unknown" for _ in range(256)]
302 for op_name in list(locals()):
303 if op_name.startswith("MP_BC_"):
304 mapping[locals()[op_name]] = op_name[len("MP_BC_") :]
305 for i in range(MP_BC_LOAD_CONST_SMALL_INT_MULTI_NUM):
306 name = "LOAD_CONST_SMALL_INT %d" % (i - MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS)
307 mapping[MP_BC_LOAD_CONST_SMALL_INT_MULTI + i] = name
308 for i in range(MP_BC_LOAD_FAST_MULTI_NUM):
309 mapping[MP_BC_LOAD_FAST_MULTI + i] = "LOAD_FAST %d" % i
310 for i in range(MP_BC_STORE_FAST_MULTI_NUM):
311 mapping[MP_BC_STORE_FAST_MULTI + i] = "STORE_FAST %d" % i
312 for i in range(MP_BC_UNARY_OP_MULTI_NUM):
313 mapping[MP_BC_UNARY_OP_MULTI + i] = "UNARY_OP %d %s" % (i, mp_unary_op_method_name[i])
314 for i in range(MP_BC_BINARY_OP_MULTI_NUM):
315 mapping[MP_BC_BINARY_OP_MULTI + i] = "BINARY_OP %d %s" % (i, mp_binary_op_method_name[i])
316
Damien George0699c6b2016-01-31 21:45:22 +0000317
Damien George0699c6b2016-01-31 21:45:22 +0000318# this function mirrors that in py/bc.c
Damien George1f7202d2019-09-02 21:35:26 +1000319def mp_opcode_format(bytecode, ip, count_var_uint):
Damien George0699c6b2016-01-31 21:45:22 +0000320 opcode = bytecode[ip]
321 ip_start = ip
Damien George69661f32020-02-27 15:36:53 +1100322 f = (0x000003A4 >> (2 * ((opcode) >> 4))) & 3
Damien George1f7202d2019-09-02 21:35:26 +1000323 if f == MP_BC_FORMAT_QSTR:
Damien George0699c6b2016-01-31 21:45:22 +0000324 ip += 3
325 else:
Damien George1f7202d2019-09-02 21:35:26 +1000326 extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0
Damien George0699c6b2016-01-31 21:45:22 +0000327 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000328 if f == MP_BC_FORMAT_VAR_UINT:
Damien George992a6e12019-03-01 14:03:10 +1100329 if count_var_uint:
330 while bytecode[ip] & 0x80 != 0:
331 ip += 1
Damien George0699c6b2016-01-31 21:45:22 +0000332 ip += 1
Damien George1f7202d2019-09-02 21:35:26 +1000333 elif f == MP_BC_FORMAT_OFFSET:
Damien George538c3c02022-03-16 09:37:58 +1100334 if bytecode[ip] & 0x80 == 0:
335 ip += 1
336 else:
337 ip += 2
Damien George0699c6b2016-01-31 21:45:22 +0000338 ip += extra_byte
339 return f, ip - ip_start
340
Damien George69661f32020-02-27 15:36:53 +1100341
Damien Georgef2040bf2021-10-22 22:22:47 +1100342def mp_opcode_decode(bytecode, ip):
343 opcode = bytecode[ip]
344 ip_start = ip
345 f = (0x000003A4 >> (2 * ((opcode) >> 4))) & 3
346 extra_byte = (opcode & MP_BC_MASK_EXTRA_BYTE) == 0
347 ip += 1
348 arg = 0
349 if f in (MP_BC_FORMAT_QSTR, MP_BC_FORMAT_VAR_UINT):
350 arg = bytecode[ip] & 0x7F
351 while bytecode[ip] & 0x80 != 0:
352 ip += 1
353 arg = arg << 7 | bytecode[ip] & 0x7F
354 ip += 1
355 elif f == MP_BC_FORMAT_OFFSET:
Damien George538c3c02022-03-16 09:37:58 +1100356 if bytecode[ip] & 0x80 == 0:
357 arg = bytecode[ip]
358 ip += 1
359 if opcode in Opcodes.ALL_OFFSET_SIGNED:
360 arg -= 0x40
361 else:
362 arg = bytecode[ip] & 0x7F | bytecode[ip + 1] << 7
363 ip += 2
364 if opcode in Opcodes.ALL_OFFSET_SIGNED:
365 arg -= 0x4000
Damien Georgef2040bf2021-10-22 22:22:47 +1100366 ip += extra_byte
367 return f, ip - ip_start, arg
368
369
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000370def read_prelude_sig(read_byte):
371 z = read_byte()
372 # xSSSSEAA
Damien George69661f32020-02-27 15:36:53 +1100373 S = (z >> 3) & 0xF
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000374 E = (z >> 2) & 0x1
375 F = 0
376 A = z & 0x3
377 K = 0
378 D = 0
379 n = 0
380 while z & 0x80:
381 z = read_byte()
382 # xFSSKAED
383 S |= (z & 0x30) << (2 * n)
384 E |= (z & 0x02) << n
385 F |= ((z & 0x40) >> 6) << n
386 A |= (z & 0x4) << n
387 K |= ((z & 0x08) >> 3) << n
388 D |= (z & 0x1) << n
389 n += 1
390 S += 1
391 return S, E, F, A, K, D
392
Damien George69661f32020-02-27 15:36:53 +1100393
Damien Georgec8c0fd42019-09-25 15:45:47 +1000394def read_prelude_size(read_byte):
395 I = 0
396 C = 0
397 n = 0
398 while True:
399 z = read_byte()
400 # xIIIIIIC
Damien George69661f32020-02-27 15:36:53 +1100401 I |= ((z & 0x7E) >> 1) << (6 * n)
Damien Georgec8c0fd42019-09-25 15:45:47 +1000402 C |= (z & 1) << n
403 if not (z & 0x80):
404 break
405 n += 1
406 return I, C
407
Damien George69661f32020-02-27 15:36:53 +1100408
Damien Georgeea3c80a2019-02-21 15:18:59 +1100409def extract_prelude(bytecode, ip):
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000410 def local_read_byte():
411 b = bytecode[ip_ref[0]]
412 ip_ref[0] += 1
413 return b
Damien George69661f32020-02-27 15:36:53 +1100414
415 ip_ref = [ip] # to close over ip in Python 2 and 3
416 (
417 n_state,
418 n_exc_stack,
419 scope_flags,
420 n_pos_args,
421 n_kwonly_args,
422 n_def_pos_args,
423 ) = read_prelude_sig(local_read_byte)
Damien Georgef2040bf2021-10-22 22:22:47 +1100424
Damien Georgec8c0fd42019-09-25 15:45:47 +1000425 n_info, n_cell = read_prelude_size(local_read_byte)
Damien Georgeb5ebfad2019-09-16 22:12:59 +1000426 ip = ip_ref[0]
427
Damien Georgec8c0fd42019-09-25 15:45:47 +1000428 ip2 = ip
429 ip = ip2 + n_info + n_cell
Damien George0699c6b2016-01-31 21:45:22 +0000430 # ip now points to first opcode
431 # ip2 points to simple_name qstr
Damien Georgef2040bf2021-10-22 22:22:47 +1100432
433 # Extract simple_name and argument qstrs (var uints).
434 args = []
435 for arg_num in range(1 + n_pos_args + n_kwonly_args):
436 value = 0
437 while True:
438 b = local_read_byte()
439 value = (value << 7) | (b & 0x7F)
440 if b & 0x80 == 0:
441 break
442 args.append(value)
443
444 return (
445 ip2,
446 ip,
447 ip_ref[0],
448 (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args),
449 args,
450 )
Damien George0699c6b2016-01-31 21:45:22 +0000451
Damien George69661f32020-02-27 15:36:53 +1100452
Damien Georgeea3c80a2019-02-21 15:18:59 +1100453class MPFunTable:
Damien Georgef2040bf2021-10-22 22:22:47 +1100454 def __repr__(self):
455 return "mp_fun_table"
Damien Georgeea3c80a2019-02-21 15:18:59 +1100456
Damien George69661f32020-02-27 15:36:53 +1100457
Damien Georgef2040bf2021-10-22 22:22:47 +1100458class CompiledModule:
459 def __init__(
460 self,
461 mpy_source_file,
462 mpy_segments,
463 header,
464 qstr_table,
465 obj_table,
466 raw_code,
467 raw_code_file_offset,
468 escaped_name,
469 ):
470 self.mpy_source_file = mpy_source_file
471 self.mpy_segments = mpy_segments
472 self.source_file = qstr_table[0]
473 self.header = header
474 self.qstr_table = qstr_table
475 self.obj_table = obj_table
476 self.raw_code_file_offset = raw_code_file_offset
477 self.raw_code = raw_code
478 self.escaped_name = escaped_name
Damien George0699c6b2016-01-31 21:45:22 +0000479
480 def _unpack_qstr(self, ip):
481 qst = self.bytecode[ip] | self.bytecode[ip + 1] << 8
482 return global_qstrs[qst]
483
Damien Georgef2040bf2021-10-22 22:22:47 +1100484 def hexdump(self):
485 with open(self.mpy_source_file, "rb") as f:
486 WIDTH = 16
487 COL_OFF = "\033[0m"
488 COL_TABLE = (
489 ("", ""), # META
490 ("\033[0;31m", "\033[0;91m"), # QSTR
491 ("\033[0;32m", "\033[0;92m"), # OBJ
492 ("\033[0;34m", "\033[0;94m"), # CODE
493 )
494 cur_col = ""
495 cur_col_index = 0
496 offset = 0
497 segment_index = 0
498 while True:
499 data = bytes_cons(f.read(WIDTH))
500 if not data:
501 break
Damien George0699c6b2016-01-31 21:45:22 +0000502
Damien Georgef2040bf2021-10-22 22:22:47 +1100503 # Print out the hex dump of this line of data.
504 line_hex = cur_col
505 line_chr = cur_col
506 line_comment = ""
507 for i in range(len(data)):
508 # Determine the colour of the data, if any, and the line comment.
509 while segment_index < len(self.mpy_segments):
510 if offset + i == self.mpy_segments[segment_index].start:
511 cur_col = COL_TABLE[self.mpy_segments[segment_index].kind][
512 cur_col_index
513 ]
514 cur_col_index = 1 - cur_col_index
515 line_hex += cur_col
516 line_chr += cur_col
517 line_comment += " %s%s%s" % (
518 cur_col,
519 self.mpy_segments[segment_index].name,
520 COL_OFF,
521 )
522 if offset + i == self.mpy_segments[segment_index].end:
523 cur_col = ""
524 line_hex += COL_OFF
525 line_chr += COL_OFF
526 segment_index += 1
527 else:
528 break
Damien George0699c6b2016-01-31 21:45:22 +0000529
Damien Georgef2040bf2021-10-22 22:22:47 +1100530 # Add to the hex part of the line.
531 if i % 2 == 0:
532 line_hex += " "
533 line_hex += "%02x" % data[i]
Damien George02fd83b2016-05-03 12:24:39 +0100534
Damien Georgef2040bf2021-10-22 22:22:47 +1100535 # Add to the characters part of the line.
536 if 0x20 <= data[i] <= 0x7E:
537 line_chr += "%s" % chr(data[i])
538 else:
539 line_chr += "."
540
541 # Print out this line.
542 if cur_col:
543 line_hex += COL_OFF
544 line_chr += COL_OFF
545 pad = " " * ((WIDTH - len(data)) * 5 // 2)
546 print("%08x:%s%s %s %s" % (offset, line_hex, pad, line_chr, line_comment))
547 offset += WIDTH
548
549 def disassemble(self):
550 print("mpy_source_file:", self.mpy_source_file)
551 print("source_file:", self.source_file.str)
552 print("header:", hexlify_to_str(self.header))
553 print("qstr_table[%u]:" % len(self.qstr_table))
554 for q in self.qstr_table:
555 print(" %s" % q.str)
556 print("obj_table:", self.obj_table)
557 self.raw_code.disassemble()
558
559 def freeze(self, compiled_module_index):
560 print()
561 print("/" * 80)
562 print("// frozen module %s" % self.escaped_name)
563 print("// - original source file: %s" % self.mpy_source_file)
564 print("// - frozen file name: %s" % self.source_file.str)
565 print("// - .mpy header: %s" % ":".join("%02x" % b for b in self.header))
566 print()
567
568 self.raw_code.freeze()
569 print()
570
571 self.freeze_constants()
572
573 print()
574 print("static const mp_frozen_module_t frozen_module_%s = {" % self.escaped_name)
575 print(" .constants = {")
576 if len(self.qstr_table):
577 print(
578 " .qstr_table = (qstr_short_t *)&const_qstr_table_data_%s,"
579 % self.escaped_name
580 )
581 else:
582 print(" .qstr_table = NULL,")
583 if len(self.obj_table):
584 print(" .obj_table = (mp_obj_t *)&const_obj_table_data_%s," % self.escaped_name)
585 else:
586 print(" .obj_table = NULL,")
587 print(" },")
588 print(" .rc = &raw_code_%s," % self.raw_code.escaped_name)
589 print("};")
Damien George0699c6b2016-01-31 21:45:22 +0000590
Damien Georgeea3c80a2019-02-21 15:18:59 +1100591 def freeze_constants(self):
Damien Georgef2040bf2021-10-22 22:22:47 +1100592 global const_str_content, const_int_content, const_obj_content
593
594 if len(self.qstr_table):
595 print(
596 "static const qstr_short_t const_qstr_table_data_%s[%u] = {"
597 % (self.escaped_name, len(self.qstr_table))
598 )
599 for q in self.qstr_table:
600 print(" %s," % q.qstr_id)
601 print("};")
602
603 if not len(self.obj_table):
604 return
605
Damien George0699c6b2016-01-31 21:45:22 +0000606 # generate constant objects
Damien Georgef2040bf2021-10-22 22:22:47 +1100607 print()
608 print("// constants")
609 for i, obj in enumerate(self.obj_table):
Damien George69661f32020-02-27 15:36:53 +1100610 obj_name = "const_obj_%s_%u" % (self.escaped_name, i)
Damien Georgef2040bf2021-10-22 22:22:47 +1100611 if isinstance(obj, MPFunTable):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100612 pass
613 elif obj is Ellipsis:
Damien George69661f32020-02-27 15:36:53 +1100614 print("#define %s mp_const_ellipsis_obj" % obj_name)
Damien George9ba3de62017-11-15 12:46:08 +1100615 elif is_str_type(obj) or is_bytes_type(obj):
Damien Georgeb6bdf182016-09-02 15:10:45 +1000616 if is_str_type(obj):
Damien George69661f32020-02-27 15:36:53 +1100617 obj = bytes_cons(obj, "utf8")
618 obj_type = "mp_type_str"
Damien Georgeb6bdf182016-09-02 15:10:45 +1000619 else:
Damien George69661f32020-02-27 15:36:53 +1100620 obj_type = "mp_type_bytes"
621 print(
Damien Georgef2040bf2021-10-22 22:22:47 +1100622 'static const mp_obj_str_t %s = {{&%s}, %u, %u, (const byte*)"%s"};'
Damien George69661f32020-02-27 15:36:53 +1100623 % (
624 obj_name,
625 obj_type,
626 qstrutil.compute_hash(obj, config.MICROPY_QSTR_BYTES_IN_HASH),
627 len(obj),
628 "".join(("\\x%02x" % b) for b in obj),
629 )
630 )
Damien Georgef2040bf2021-10-22 22:22:47 +1100631 const_str_content += len(obj)
632 const_obj_content += 4 * 4
Damien Georgec3beb162016-04-15 11:56:10 +0100633 elif is_int_type(obj):
Damien George0699c6b2016-01-31 21:45:22 +0000634 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
635 # TODO check if we can actually fit this long-int into a small-int
Damien George69661f32020-02-27 15:36:53 +1100636 raise FreezeError(self, "target does not support long int")
Damien George0699c6b2016-01-31 21:45:22 +0000637 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
638 # TODO
Damien George69661f32020-02-27 15:36:53 +1100639 raise FreezeError(self, "freezing int to long-long is not implemented")
Damien George0699c6b2016-01-31 21:45:22 +0000640 elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
641 neg = 0
642 if obj < 0:
643 obj = -obj
644 neg = 1
645 bits_per_dig = config.MPZ_DIG_SIZE
646 digs = []
647 z = obj
648 while z:
649 digs.append(z & ((1 << bits_per_dig) - 1))
650 z >>= bits_per_dig
651 ndigs = len(digs)
Damien George69661f32020-02-27 15:36:53 +1100652 digs = ",".join(("%#x" % d) for d in digs)
653 print(
Damien Georgef2040bf2021-10-22 22:22:47 +1100654 "static const mp_obj_int_t %s = {{&mp_type_int}, "
Damien George69661f32020-02-27 15:36:53 +1100655 "{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t*)(const uint%u_t[]){%s}}};"
656 % (obj_name, neg, ndigs, ndigs, bits_per_dig, bits_per_dig, digs)
657 )
Damien Georgef2040bf2021-10-22 22:22:47 +1100658 const_int_content += (digs.count(",") + 1) * bits_per_dig // 8
659 const_obj_content += 4 * 4
Damien George0699c6b2016-01-31 21:45:22 +0000660 elif type(obj) is float:
Damien George69661f32020-02-27 15:36:53 +1100661 print(
662 "#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B"
663 )
664 print(
Damien Georgef2040bf2021-10-22 22:22:47 +1100665 "static const mp_obj_float_t %s = {{&mp_type_float}, (mp_float_t)%.16g};"
stijnbcf01d12020-03-31 14:48:08 +0200666 % (obj_name, obj)
Damien George69661f32020-02-27 15:36:53 +1100667 )
668 print("#endif")
Damien Georgef2040bf2021-10-22 22:22:47 +1100669 const_obj_content += 3 * 4
Damien Georgec51c8832016-09-03 00:19:02 +1000670 elif type(obj) is complex:
Damien George69661f32020-02-27 15:36:53 +1100671 print(
Damien Georgef2040bf2021-10-22 22:22:47 +1100672 "static const mp_obj_complex_t %s = {{&mp_type_complex}, (mp_float_t)%.16g, (mp_float_t)%.16g};"
Damien George69661f32020-02-27 15:36:53 +1100673 % (obj_name, obj.real, obj.imag)
674 )
Damien George0699c6b2016-01-31 21:45:22 +0000675 else:
Damien George69661f32020-02-27 15:36:53 +1100676 raise FreezeError(self, "freezing of object %r is not implemented" % (obj,))
Damien George0699c6b2016-01-31 21:45:22 +0000677
Damien Georgef2040bf2021-10-22 22:22:47 +1100678 # generate constant table
679 print()
680 print("// constant table")
681 print(
682 "static const mp_rom_obj_t const_obj_table_data_%s[%u] = {"
683 % (self.escaped_name, len(self.obj_table))
684 )
685 for i in range(len(self.obj_table)):
686 if isinstance(self.obj_table[i], MPFunTable):
687 print(" &mp_fun_table,")
688 elif type(self.obj_table[i]) is float:
689 print(
690 "#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B"
691 )
692 print(" MP_ROM_PTR(&const_obj_%s_%u)," % (self.escaped_name, i))
693 print("#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C")
694 n = struct.unpack("<I", struct.pack("<f", self.obj_table[i]))[0]
695 n = ((n & ~0x3) | 2) + 0x80800000
696 print(" (mp_rom_obj_t)(0x%08x)," % (n,))
697 print("#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D")
698 n = struct.unpack("<Q", struct.pack("<d", self.obj_table[i]))[0]
699 n += 0x8004000000000000
700 print(" (mp_rom_obj_t)(0x%016x)," % (n,))
701 print("#endif")
702 else:
703 print(" MP_ROM_PTR(&const_obj_%s_%u)," % (self.escaped_name, i))
704 print("};")
Damien George0699c6b2016-01-31 21:45:22 +0000705
Damien Georgef2040bf2021-10-22 22:22:47 +1100706 global const_table_ptr_content
707 const_table_ptr_content += len(self.obj_table)
708
709
710class RawCode(object):
711 # a set of all escaped names, to make sure they are unique
712 escaped_names = set()
713
714 # convert code kind number to string
715 code_kind_str = {
716 MP_CODE_BYTECODE: "MP_CODE_BYTECODE",
717 MP_CODE_NATIVE_PY: "MP_CODE_NATIVE_PY",
718 MP_CODE_NATIVE_VIPER: "MP_CODE_NATIVE_VIPER",
719 MP_CODE_NATIVE_ASM: "MP_CODE_NATIVE_ASM",
720 }
721
722 def __init__(self, cm_escaped_name, qstr_table, fun_data, prelude_offset, code_kind):
723 self.qstr_table = qstr_table
724 self.fun_data = fun_data
725 self.prelude_offset = prelude_offset
726 self.code_kind = code_kind
727
728 if code_kind in (MP_CODE_BYTECODE, MP_CODE_NATIVE_PY):
729 (
730 self.offset_names,
731 self.offset_opcodes,
732 self.offset_line_info,
733 self.prelude,
734 self.names,
735 ) = extract_prelude(self.fun_data, prelude_offset)
736 self.scope_flags = self.prelude[2]
737 self.n_pos_args = self.prelude[3]
738 self.simple_name = self.qstr_table[self.names[0]]
Damien Georgeb6a32892017-08-12 22:26:18 +1000739 else:
Damien Georgef2040bf2021-10-22 22:22:47 +1100740 self.simple_name = self.qstr_table[0]
741
742 escaped_name = cm_escaped_name + "_" + self.simple_name.qstr_esc
743
744 # make sure the escaped name is unique
745 i = 2
746 unique_escaped_name = escaped_name
747 while unique_escaped_name in self.escaped_names:
748 unique_escaped_name = escaped_name + str(i)
749 i += 1
750 self.escaped_names.add(unique_escaped_name)
751 self.escaped_name = unique_escaped_name
752
753 def disassemble_children(self):
754 print(" children:", [rc.simple_name.str for rc in self.children])
755 for rc in self.children:
756 rc.disassemble()
757
758 def freeze_children(self):
759 # Freeze children and generate table of children.
760 if len(self.children):
761 for rc in self.children:
762 print("// child of %s" % self.escaped_name)
763 rc.freeze()
764 print()
765 print("static const mp_raw_code_t *const children_%s[] = {" % self.escaped_name)
766 for rc in self.children:
767 print(" &raw_code_%s," % rc.escaped_name)
768 print("};")
769 print()
770
771 def freeze_raw_code(self, qstr_links=(), type_sig=0):
772 # Generate mp_raw_code_t.
773 print("static const mp_raw_code_t raw_code_%s = {" % self.escaped_name)
774 print(" .kind = %s," % RawCode.code_kind_str[self.code_kind])
775 print(" .scope_flags = 0x%02x," % self.scope_flags)
776 print(" .n_pos_args = %u," % self.n_pos_args)
777 print(" .fun_data = fun_data_%s," % self.escaped_name)
778 print(" #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS")
779 print(" .fun_data_len = %u," % len(self.fun_data))
780 print(" #endif")
781 if len(self.children):
782 print(" .children = (void *)&children_%s," % self.escaped_name)
783 else:
784 print(" .children = NULL,")
Damien George69661f32020-02-27 15:36:53 +1100785 print(" #if MICROPY_PERSISTENT_CODE_SAVE")
Damien Georgef2040bf2021-10-22 22:22:47 +1100786 print(" .n_children = %u," % len(self.children))
Damien Georgec69f58e2019-09-06 23:55:15 +1000787 if self.code_kind == MP_CODE_BYTECODE:
Damien George69661f32020-02-27 15:36:53 +1100788 print(" #if MICROPY_PY_SYS_SETTRACE")
789 print(" .prelude = {")
790 print(" .n_state = %u," % self.prelude[0])
791 print(" .n_exc_stack = %u," % self.prelude[1])
792 print(" .scope_flags = %u," % self.prelude[2])
793 print(" .n_pos_args = %u," % self.prelude[3])
794 print(" .n_kwonly_args = %u," % self.prelude[4])
795 print(" .n_def_pos_args = %u," % self.prelude[5])
Damien Georgef2040bf2021-10-22 22:22:47 +1100796 print(" .qstr_block_name_idx = %u," % self.names[0])
Martin Milata492cf342020-08-13 15:20:08 +0200797 print(
798 " .line_info = fun_data_%s + %u,"
Damien Georgef2040bf2021-10-22 22:22:47 +1100799 % (self.escaped_name, self.offset_line_info)
Martin Milata492cf342020-08-13 15:20:08 +0200800 )
Damien Georgef2040bf2021-10-22 22:22:47 +1100801 print(
802 " .opcodes = fun_data_%s + %u," % (self.escaped_name, self.offset_opcodes)
803 )
Damien George69661f32020-02-27 15:36:53 +1100804 print(" },")
805 print(" .line_of_definition = %u," % 0) # TODO
806 print(" #endif")
807 print(" #if MICROPY_EMIT_MACHINE_CODE")
808 print(" .prelude_offset = %u," % self.prelude_offset)
809 print(" .n_qstr = %u," % len(qstr_links))
810 print(" .qstr_link = NULL,") # TODO
811 print(" #endif")
812 print(" #endif")
813 print(" #if MICROPY_EMIT_MACHINE_CODE")
814 print(" .type_sig = %u," % type_sig)
815 print(" #endif")
816 print("};")
817
Damien Georgef2040bf2021-10-22 22:22:47 +1100818 global raw_code_count, raw_code_content
819 raw_code_count += 1
820 raw_code_content += 4 * 4
821
Damien George0699c6b2016-01-31 21:45:22 +0000822
Damien Georgeea3c80a2019-02-21 15:18:59 +1100823class RawCodeBytecode(RawCode):
Damien Georgef2040bf2021-10-22 22:22:47 +1100824 def __init__(self, cm_escaped_name, qstr_table, obj_table, fun_data):
825 self.obj_table = obj_table
Damien George69661f32020-02-27 15:36:53 +1100826 super(RawCodeBytecode, self).__init__(
Damien Georgef2040bf2021-10-22 22:22:47 +1100827 cm_escaped_name, qstr_table, fun_data, 0, MP_CODE_BYTECODE
Damien George69661f32020-02-27 15:36:53 +1100828 )
Damien Georgeea3c80a2019-02-21 15:18:59 +1100829
Damien Georgef2040bf2021-10-22 22:22:47 +1100830 def disassemble(self):
831 bc = self.fun_data
832 print("simple_name:", self.simple_name.str)
833 print(" raw bytecode:", len(bc), hexlify_to_str(bc))
834 print(" prelude:", self.prelude)
835 print(" args:", [self.qstr_table[i].str for i in self.names[1:]])
836 print(" line info:", hexlify_to_str(bc[self.offset_line_info : self.offset_opcodes]))
837 ip = self.offset_opcodes
838 while ip < len(bc):
839 fmt, sz, arg = mp_opcode_decode(bc, ip)
840 if bc[ip] == Opcodes.MP_BC_LOAD_CONST_OBJ:
841 arg = "%r" % self.obj_table[arg]
842 if fmt == MP_BC_FORMAT_QSTR:
843 arg = self.qstr_table[arg].str
844 elif fmt in (MP_BC_FORMAT_VAR_UINT, MP_BC_FORMAT_OFFSET):
845 pass
Damien Georgeea3c80a2019-02-21 15:18:59 +1100846 else:
Damien Georgef2040bf2021-10-22 22:22:47 +1100847 arg = ""
848 print(
849 " %-11s %s %s" % (hexlify_to_str(bc[ip : ip + sz]), Opcodes.mapping[bc[ip]], arg)
850 )
Damien Georgeea3c80a2019-02-21 15:18:59 +1100851 ip += sz
Damien Georgef2040bf2021-10-22 22:22:47 +1100852 self.disassemble_children()
853
854 def freeze(self):
855 # generate bytecode data
856 bc = self.fun_data
857 print(
858 "// frozen bytecode for file %s, scope %s"
859 % (self.qstr_table[0].str, self.escaped_name)
860 )
861 print("static const byte fun_data_%s[%u] = {" % (self.escaped_name, len(bc)))
862
863 print(" ", end="")
864 for b in bc[: self.offset_names]:
865 print("0x%02x," % b, end="")
866 print(" // prelude")
867
868 print(" ", end="")
869 for b in bc[self.offset_names : self.offset_line_info]:
870 print("0x%02x," % b, end="")
871 print(" // names: %s" % ", ".join(self.qstr_table[i].str for i in self.names))
872
873 print(" ", end="")
874 for b in bc[self.offset_line_info : self.offset_opcodes]:
875 print("0x%02x," % b, end="")
876 print(" // code info")
877
878 ip = self.offset_opcodes
879 while ip < len(bc):
880 fmt, sz, arg = mp_opcode_decode(bc, ip)
881 opcode_name = Opcodes.mapping[bc[ip]]
882 if fmt == MP_BC_FORMAT_QSTR:
robert-hh5c467212022-02-26 07:55:53 +0100883 opcode_name += " " + repr(self.qstr_table[arg].str)
Damien Georgef2040bf2021-10-22 22:22:47 +1100884 elif fmt in (MP_BC_FORMAT_VAR_UINT, MP_BC_FORMAT_OFFSET):
885 opcode_name += " %u" % arg
886 print(
887 " %s, // %s" % (",".join("0x%02x" % b for b in bc[ip : ip + sz]), opcode_name)
888 )
889 ip += sz
890
Damien George69661f32020-02-27 15:36:53 +1100891 print("};")
Damien Georgeea3c80a2019-02-21 15:18:59 +1100892
Damien Georgef2040bf2021-10-22 22:22:47 +1100893 self.freeze_children()
894 self.freeze_raw_code()
895
896 global bc_content
897 bc_content += len(bc)
Damien Georgeea3c80a2019-02-21 15:18:59 +1100898
Damien George69661f32020-02-27 15:36:53 +1100899
Damien Georgeea3c80a2019-02-21 15:18:59 +1100900class RawCodeNative(RawCode):
Damien George69661f32020-02-27 15:36:53 +1100901 def __init__(
902 self,
Damien Georgef2040bf2021-10-22 22:22:47 +1100903 cm_escaped_name,
904 qstr_table,
905 kind,
Damien George69661f32020-02-27 15:36:53 +1100906 fun_data,
907 prelude_offset,
Damien George69661f32020-02-27 15:36:53 +1100908 qstr_links,
Damien Georgef2040bf2021-10-22 22:22:47 +1100909 scope_flags,
910 n_pos_args,
Damien George69661f32020-02-27 15:36:53 +1100911 type_sig,
912 ):
913 super(RawCodeNative, self).__init__(
Damien Georgef2040bf2021-10-22 22:22:47 +1100914 cm_escaped_name, qstr_table, fun_data, prelude_offset, kind
Damien George69661f32020-02-27 15:36:53 +1100915 )
Damien Georgef2040bf2021-10-22 22:22:47 +1100916
917 if kind in (MP_CODE_NATIVE_VIPER, MP_CODE_NATIVE_ASM):
918 self.scope_flags = scope_flags
919 self.n_pos_args = n_pos_args
920
Damien Georgeea3c80a2019-02-21 15:18:59 +1100921 self.qstr_links = qstr_links
922 self.type_sig = type_sig
Damien George69661f32020-02-27 15:36:53 +1100923 if config.native_arch in (
924 MP_NATIVE_ARCH_X86,
925 MP_NATIVE_ARCH_X64,
926 MP_NATIVE_ARCH_XTENSA,
927 MP_NATIVE_ARCH_XTENSAWIN,
928 ):
Damien Georgeea3c80a2019-02-21 15:18:59 +1100929 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",@progbits # ")))'
930 else:
931 self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
932
Damien George7f24c292019-11-28 13:11:51 +1100933 # Allow single-byte alignment by default for x86/x64.
934 # ARM needs word alignment, ARM Thumb needs halfword, due to instruction size.
935 # Xtensa needs word alignment due to the 32-bit constant table embedded in the code.
Damien George69661f32020-02-27 15:36:53 +1100936 if config.native_arch in (
937 MP_NATIVE_ARCH_ARMV6,
938 MP_NATIVE_ARCH_XTENSA,
939 MP_NATIVE_ARCH_XTENSAWIN,
940 ):
Damien George7f24c292019-11-28 13:11:51 +1100941 # ARMV6 or Xtensa -- four byte align.
Damien George69661f32020-02-27 15:36:53 +1100942 self.fun_data_attributes += " __attribute__ ((aligned (4)))"
Jim Mussared4ab51562019-08-17 00:32:04 +1000943 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
944 # ARMVxxM -- two byte align.
Damien George69661f32020-02-27 15:36:53 +1100945 self.fun_data_attributes += " __attribute__ ((aligned (2)))"
Jim Mussared4ab51562019-08-17 00:32:04 +1000946
Damien Georgef2040bf2021-10-22 22:22:47 +1100947 def disassemble(self):
948 fun_data = self.fun_data
949 print("simple_name:", self.simple_name.str)
950 print(
951 " raw data:",
952 len(fun_data),
953 hexlify_to_str(fun_data[:32]),
954 "..." if len(fun_data) > 32 else "",
955 )
956 if self.code_kind != MP_CODE_NATIVE_PY:
957 return
958 print(" prelude:", self.prelude)
959 print(" args:", [self.qstr_table[i].str for i in self.names[1:]])
960 print(" line info:", fun_data[self.offset_line_info : self.offset_opcodes])
961 ip = 0
962 while ip < self.prelude_offset:
963 sz = 16
964 print(" ", hexlify_to_str(fun_data[ip : min(ip + sz, self.prelude_offset)]))
965 ip += sz
966 self.disassemble_children()
967
Damien Georgeea3c80a2019-02-21 15:18:59 +1100968 def _asm_thumb_rewrite_mov(self, pc, val):
Damien Georgef2040bf2021-10-22 22:22:47 +1100969 print(" (%u & 0xf0) | (%s >> 12)," % (self.fun_data[pc], val), end="")
970 print(" (%u & 0xfb) | (%s >> 9 & 0x04)," % (self.fun_data[pc + 1], val), end="")
Damien George69661f32020-02-27 15:36:53 +1100971 print(" (%s & 0xff)," % (val,), end="")
Damien Georgef2040bf2021-10-22 22:22:47 +1100972 print(" (%u & 0x07) | (%s >> 4 & 0x70)," % (self.fun_data[pc + 3], val))
Damien Georgeea3c80a2019-02-21 15:18:59 +1100973
974 def _link_qstr(self, pc, kind, qst):
975 if kind == 0:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000976 # Generic 16-bit link
Damien George69661f32020-02-27 15:36:53 +1100977 print(" %s & 0xff, %s >> 8," % (qst, qst))
Damien George9d3031c2019-06-11 11:36:39 +1000978 return 2
Damien Georgeea3c80a2019-02-21 15:18:59 +1100979 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000980 # Architecture-specific link
981 is_obj = kind == 2
982 if is_obj:
Damien George69661f32020-02-27 15:36:53 +1100983 qst = "((uintptr_t)MP_OBJ_NEW_QSTR(%s))" % qst
Damien George7f24c292019-11-28 13:11:51 +1100984 if config.native_arch in (
Damien George69661f32020-02-27 15:36:53 +1100985 MP_NATIVE_ARCH_X86,
986 MP_NATIVE_ARCH_X64,
Damien George2c1a6a22021-05-25 22:16:06 +1000987 MP_NATIVE_ARCH_ARMV6,
Damien George69661f32020-02-27 15:36:53 +1100988 MP_NATIVE_ARCH_XTENSA,
989 MP_NATIVE_ARCH_XTENSAWIN,
990 ):
991 print(
992 " %s & 0xff, (%s >> 8) & 0xff, (%s >> 16) & 0xff, %s >> 24,"
993 % (qst, qst, qst, qst)
994 )
Damien George9d3031c2019-06-11 11:36:39 +1000995 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +1100996 elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
997 if is_obj:
Damien Georgefaf3d3e2019-06-04 22:13:32 +1000998 # qstr object, movw and movt
999 self._asm_thumb_rewrite_mov(pc, qst)
Damien George69661f32020-02-27 15:36:53 +11001000 self._asm_thumb_rewrite_mov(pc + 4, "(%s >> 16)" % qst)
Damien George9d3031c2019-06-11 11:36:39 +10001001 return 8
Damien Georgeea3c80a2019-02-21 15:18:59 +11001002 else:
Damien Georgefaf3d3e2019-06-04 22:13:32 +10001003 # qstr number, movw instruction
1004 self._asm_thumb_rewrite_mov(pc, qst)
Damien George9d3031c2019-06-11 11:36:39 +10001005 return 4
Damien Georgeea3c80a2019-02-21 15:18:59 +11001006 else:
1007 assert 0
1008
Damien Georgef2040bf2021-10-22 22:22:47 +11001009 def freeze(self):
1010 if self.scope_flags & ~0x0F:
Damien George69661f32020-02-27 15:36:53 +11001011 raise FreezeError("unable to freeze code with relocations")
Damien Georgefc97d6d2019-12-10 14:57:12 +11001012
Damien Georgeea3c80a2019-02-21 15:18:59 +11001013 # generate native code data
1014 print()
Damien George69661f32020-02-27 15:36:53 +11001015 print(
Damien Georgef2040bf2021-10-22 22:22:47 +11001016 "// frozen native code for file %s, scope %s"
1017 % (self.qstr_table[0].str, self.escaped_name)
1018 )
1019 print(
1020 "static const byte fun_data_%s[%u] %s = {"
1021 % (self.escaped_name, len(self.fun_data), self.fun_data_attributes)
Damien George69661f32020-02-27 15:36:53 +11001022 )
Damien Georgeea3c80a2019-02-21 15:18:59 +11001023
Damien Georgef2040bf2021-10-22 22:22:47 +11001024 i_top = len(self.fun_data)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001025 i = 0
1026 qi = 0
1027 while i < i_top:
1028 if qi < len(self.qstr_links) and i == self.qstr_links[qi][0]:
1029 # link qstr
1030 qi_off, qi_kind, qi_val = self.qstr_links[qi]
1031 qst = global_qstrs[qi_val].qstr_id
Damien George9d3031c2019-06-11 11:36:39 +10001032 i += self._link_qstr(i, qi_kind, qst)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001033 qi += 1
1034 else:
1035 # copy machine code (max 16 bytes)
1036 i16 = min(i + 16, i_top)
1037 if qi < len(self.qstr_links):
1038 i16 = min(i16, self.qstr_links[qi][0])
Damien George69661f32020-02-27 15:36:53 +11001039 print(" ", end="")
Damien Georgeea3c80a2019-02-21 15:18:59 +11001040 for ii in range(i, i16):
Damien Georgef2040bf2021-10-22 22:22:47 +11001041 print(" 0x%02x," % self.fun_data[ii], end="")
Damien Georgeea3c80a2019-02-21 15:18:59 +11001042 print()
1043 i = i16
1044
Damien George69661f32020-02-27 15:36:53 +11001045 print("};")
Damien Georgeea3c80a2019-02-21 15:18:59 +11001046
Damien Georgef2040bf2021-10-22 22:22:47 +11001047 self.freeze_children()
1048 self.freeze_raw_code(self.qstr_links, self.type_sig)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001049
Damien George69661f32020-02-27 15:36:53 +11001050
Damien Georgef2040bf2021-10-22 22:22:47 +11001051class MPYSegment:
1052 META = 0
1053 QSTR = 1
1054 OBJ = 2
1055 CODE = 3
Damien George992a6e12019-03-01 14:03:10 +11001056
Damien Georgef2040bf2021-10-22 22:22:47 +11001057 def __init__(self, kind, name, start, end):
1058 self.kind = kind
1059 self.name = name
1060 self.start = start
1061 self.end = end
Damien George992a6e12019-03-01 14:03:10 +11001062
Damien George69661f32020-02-27 15:36:53 +11001063
Damien Georgef2040bf2021-10-22 22:22:47 +11001064class MPYReader:
1065 def __init__(self, filename, fileobj):
1066 self.filename = filename
1067 self.fileobj = fileobj
1068
1069 def tell(self):
1070 return self.fileobj.tell()
1071
1072 def read_byte(self):
1073 return bytes_cons(self.fileobj.read(1))[0]
1074
1075 def read_bytes(self, n):
1076 return bytes_cons(self.fileobj.read(n))
1077
1078 def read_uint(self):
1079 i = 0
1080 while True:
1081 b = self.read_byte()
1082 i = (i << 7) | (b & 0x7F)
1083 if b & 0x80 == 0:
1084 break
1085 return i
Damien George992a6e12019-03-01 14:03:10 +11001086
Damien George69661f32020-02-27 15:36:53 +11001087
Damien Georgef2040bf2021-10-22 22:22:47 +11001088def read_qstr(reader, segments):
1089 start_pos = reader.tell()
1090 ln = reader.read_uint()
Damien George5996eeb2019-02-25 23:15:51 +11001091 if ln & 1:
Damien Georgef2040bf2021-10-22 22:22:47 +11001092 # static qstr
1093 segments.append(
1094 MPYSegment(MPYSegment.META, global_qstrs[ln >> 1].str, start_pos, start_pos)
1095 )
1096 return ln >> 1
Damien George5996eeb2019-02-25 23:15:51 +11001097 ln >>= 1
Damien Georgef2040bf2021-10-22 22:22:47 +11001098 start_pos = reader.tell()
1099 data = str_cons(reader.read_bytes(ln), "utf8")
1100 reader.read_byte() # read and discard null terminator
1101 segments.append(MPYSegment(MPYSegment.QSTR, data, start_pos, reader.tell()))
Damien George4f0931b2019-03-01 14:33:03 +11001102 global_qstrs.append(QStrType(data))
Damien George0699c6b2016-01-31 21:45:22 +00001103 return len(global_qstrs) - 1
1104
Damien George69661f32020-02-27 15:36:53 +11001105
Damien Georgef2040bf2021-10-22 22:22:47 +11001106def read_obj(reader, segments):
1107 obj_type = reader.read_bytes(1)
1108 if obj_type == b"t":
1109 return MPFunTable()
1110 elif obj_type == b"e":
Damien George0699c6b2016-01-31 21:45:22 +00001111 return Ellipsis
1112 else:
Damien Georgef2040bf2021-10-22 22:22:47 +11001113 ln = reader.read_uint()
1114 start_pos = reader.tell()
1115 buf = reader.read_bytes(ln)
1116 if obj_type in (b"s", b"b"):
1117 reader.read_byte() # read and discard null terminator
Damien George69661f32020-02-27 15:36:53 +11001118 if obj_type == b"s":
Damien Georgef2040bf2021-10-22 22:22:47 +11001119 obj = str_cons(buf, "utf8")
Damien George69661f32020-02-27 15:36:53 +11001120 elif obj_type == b"b":
Damien Georgef2040bf2021-10-22 22:22:47 +11001121 obj = buf
Damien George69661f32020-02-27 15:36:53 +11001122 elif obj_type == b"i":
Damien Georgef2040bf2021-10-22 22:22:47 +11001123 obj = int(str_cons(buf, "ascii"), 10)
Damien George69661f32020-02-27 15:36:53 +11001124 elif obj_type == b"f":
Damien Georgef2040bf2021-10-22 22:22:47 +11001125 obj = float(str_cons(buf, "ascii"))
Damien George69661f32020-02-27 15:36:53 +11001126 elif obj_type == b"c":
Damien Georgef2040bf2021-10-22 22:22:47 +11001127 obj = complex(str_cons(buf, "ascii"))
Damien George0699c6b2016-01-31 21:45:22 +00001128 else:
Damien Georgef2040bf2021-10-22 22:22:47 +11001129 raise MPYReadError(reader.filename, "corrupt .mpy file")
1130 segments.append(MPYSegment(MPYSegment.OBJ, obj, start_pos, reader.tell()))
1131 return obj
Damien George0699c6b2016-01-31 21:45:22 +00001132
Damien George69661f32020-02-27 15:36:53 +11001133
Damien Georgef2040bf2021-10-22 22:22:47 +11001134def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
1135 # Read raw code header.
1136 kind_len = reader.read_uint()
Damien Georgeea3c80a2019-02-21 15:18:59 +11001137 kind = (kind_len & 3) + MP_CODE_BYTECODE
Damien Georgef2040bf2021-10-22 22:22:47 +11001138 has_children = (kind_len >> 2) & 1
1139 fun_data_len = kind_len >> 3
1140
1141 # Read the body of the raw code.
1142 file_offset = reader.tell()
1143 fun_data = reader.read_bytes(fun_data_len)
1144 segments_len = len(segments)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001145
1146 if kind == MP_CODE_BYTECODE:
Damien Georgef2040bf2021-10-22 22:22:47 +11001147 # Create bytecode raw code.
1148 rc = RawCodeBytecode(cm_escaped_name, qstr_table, obj_table, fun_data)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001149 else:
Damien Georgef2040bf2021-10-22 22:22:47 +11001150 # Create native raw code.
Damien Georgeea3c80a2019-02-21 15:18:59 +11001151 qstr_links = []
1152 if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
Damien Georgef2040bf2021-10-22 22:22:47 +11001153 # Read qstr link table.
1154 n_qstr_link = reader.read_uint()
Damien Georgeea3c80a2019-02-21 15:18:59 +11001155 for _ in range(n_qstr_link):
Damien Georgef2040bf2021-10-22 22:22:47 +11001156 off = reader.read_uint()
1157 qst = read_qstr(reader, segments)
Damien Georgeea3c80a2019-02-21 15:18:59 +11001158 qstr_links.append((off >> 2, off & 3, qst))
1159
Damien Georgef2040bf2021-10-22 22:22:47 +11001160 native_scope_flags = 0
1161 native_n_pos_args = 0
1162 native_type_sig = 0
Damien Georgeea3c80a2019-02-21 15:18:59 +11001163 if kind == MP_CODE_NATIVE_PY:
Damien Georgef2040bf2021-10-22 22:22:47 +11001164 prelude_offset = reader.read_uint()
Damien Georgeea3c80a2019-02-21 15:18:59 +11001165 else:
Damien Georgef2040bf2021-10-22 22:22:47 +11001166 prelude_offset = 0
1167 native_scope_flags = reader.read_uint()
1168 if kind == MP_CODE_NATIVE_VIPER:
1169 # Read any additional sections for native viper.
1170 if native_scope_flags & MP_SCOPE_FLAG_VIPERRODATA:
1171 rodata_size = reader.read_uint()
1172 if native_scope_flags & MP_SCOPE_FLAG_VIPERBSS:
1173 bss_size = reader.read_uint()
1174 if native_scope_flags & MP_SCOPE_FLAG_VIPERRODATA:
1175 reader.read_bytes(rodata_size)
1176 if native_scope_flags & MP_SCOPE_FLAG_VIPERRELOC:
1177 while True:
1178 op = reader.read_byte()
1179 if op == 0xFF:
1180 break
1181 if op & 1:
1182 addr = reader.read_uint()
1183 op >>= 1
1184 if op <= 5 and op & 1:
1185 n = reader.read_uint()
1186 else:
1187 assert kind == MP_CODE_NATIVE_ASM
1188 native_n_pos_args = reader.read_uint()
1189 native_type_sig = reader.read_uint()
Damien Georgeea3c80a2019-02-21 15:18:59 +11001190
Damien Georgef2040bf2021-10-22 22:22:47 +11001191 rc = RawCodeNative(
1192 cm_escaped_name,
1193 qstr_table,
Damien George69661f32020-02-27 15:36:53 +11001194 kind,
Damien Georgef2040bf2021-10-22 22:22:47 +11001195 fun_data,
Damien George69661f32020-02-27 15:36:53 +11001196 prelude_offset,
Damien George69661f32020-02-27 15:36:53 +11001197 qstr_links,
Damien Georgef2040bf2021-10-22 22:22:47 +11001198 native_scope_flags,
1199 native_n_pos_args,
1200 native_type_sig,
Damien George69661f32020-02-27 15:36:53 +11001201 )
1202
Damien Georgef2040bf2021-10-22 22:22:47 +11001203 # Add a segment for the raw code data.
1204 segments.insert(
1205 segments_len,
1206 MPYSegment(MPYSegment.CODE, rc.simple_name.str, file_offset, file_offset + fun_data_len),
1207 )
1208
1209 # Read children, if there are any.
1210 rc.children = []
1211 if has_children:
1212 n_children = reader.read_uint()
1213 for _ in range(n_children):
1214 rc.children.append(
1215 read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments)
1216 )
1217
1218 return rc
1219
Damien George0699c6b2016-01-31 21:45:22 +00001220
1221def read_mpy(filename):
Damien Georgef2040bf2021-10-22 22:22:47 +11001222 with open(filename, "rb") as fileobj:
1223 reader = MPYReader(filename, fileobj)
1224 segments = []
1225
1226 # Read and verify the header.
1227 header = reader.read_bytes(4)
Damien George69661f32020-02-27 15:36:53 +11001228 if header[0] != ord("M"):
Damien Georgef2040bf2021-10-22 22:22:47 +11001229 raise MPYReadError(filename, "not a valid .mpy file")
Damien George6a110482017-02-17 00:19:34 +11001230 if header[1] != config.MPY_VERSION:
Damien Georgef2040bf2021-10-22 22:22:47 +11001231 raise MPYReadError(filename, "incompatible .mpy version")
Damien George5996eeb2019-02-25 23:15:51 +11001232 feature_byte = header[2]
Damien George5996eeb2019-02-25 23:15:51 +11001233 config.MICROPY_PY_BUILTINS_STR_UNICODE = (feature_byte & 2) != 0
Damien Georgefaf3d3e2019-06-04 22:13:32 +10001234 mpy_native_arch = feature_byte >> 2
1235 if mpy_native_arch != MP_NATIVE_ARCH_NONE:
1236 if config.native_arch == MP_NATIVE_ARCH_NONE:
1237 config.native_arch = mpy_native_arch
1238 elif config.native_arch != mpy_native_arch:
Damien Georgef2040bf2021-10-22 22:22:47 +11001239 raise MPYReadError(filename, "native architecture mismatch")
Damien George0699c6b2016-01-31 21:45:22 +00001240 config.mp_small_int_bits = header[3]
Damien Georgef2040bf2021-10-22 22:22:47 +11001241
1242 # Read number of qstrs, and number of objects.
1243 n_qstr = reader.read_uint()
1244 n_obj = reader.read_uint()
1245
1246 # Read qstrs and construct qstr table.
1247 qstr_table = []
1248 for i in range(n_qstr):
1249 q = read_qstr(reader, segments)
1250 qstr_table.append(global_qstrs[q])
1251
1252 # Read objects and construct object table.
1253 obj_table = []
1254 for i in range(n_obj):
1255 obj_table.append(read_obj(reader, segments))
1256
1257 # Compute the compiled-module escaped name.
1258 cm_escaped_name = qstr_table[0].str.replace("/", "_")[:-3]
1259
1260 # Read the outer raw code, which will in turn read all its children.
1261 raw_code_file_offset = reader.tell()
1262 raw_code = read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments)
1263
1264 # Create the outer-level compiled module representing the whole .mpy file.
1265 return CompiledModule(
1266 filename,
1267 segments,
1268 header,
1269 qstr_table,
1270 obj_table,
1271 raw_code,
1272 raw_code_file_offset,
1273 cm_escaped_name,
1274 )
Damien George0699c6b2016-01-31 21:45:22 +00001275
Damien George69661f32020-02-27 15:36:53 +11001276
Damien Georgef2040bf2021-10-22 22:22:47 +11001277def hexdump_mpy(compiled_modules):
1278 for cm in compiled_modules:
1279 cm.hexdump()
Damien George0699c6b2016-01-31 21:45:22 +00001280
Damien George69661f32020-02-27 15:36:53 +11001281
Damien Georgef2040bf2021-10-22 22:22:47 +11001282def disassemble_mpy(compiled_modules):
1283 for cm in compiled_modules:
1284 cm.disassemble()
1285
1286
1287def freeze_mpy(base_qstrs, compiled_modules):
Damien George0699c6b2016-01-31 21:45:22 +00001288 # add to qstrs
1289 new = {}
1290 for q in global_qstrs:
1291 # don't add duplicates
Damien George4f0931b2019-03-01 14:33:03 +11001292 if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new:
Damien George0699c6b2016-01-31 21:45:22 +00001293 continue
Artyom Skrobov18b1ba02021-05-03 14:17:36 -04001294 new[q.qstr_esc] = (len(new), q.qstr_esc, q.str, bytes_cons(q.str, "utf8"))
Damien George0699c6b2016-01-31 21:45:22 +00001295 new = sorted(new.values(), key=lambda x: x[0])
1296
1297 print('#include "py/mpconfig.h"')
1298 print('#include "py/objint.h"')
1299 print('#include "py/objstr.h"')
1300 print('#include "py/emitglue.h"')
Damien George360d9722019-10-07 11:56:24 +11001301 print('#include "py/nativeglue.h"')
Damien George0699c6b2016-01-31 21:45:22 +00001302 print()
1303
Damien George69661f32020-02-27 15:36:53 +11001304 print("#if MICROPY_LONGINT_IMPL != %u" % config.MICROPY_LONGINT_IMPL)
Damien George99b47192016-05-16 23:13:30 +01001305 print('#error "incompatible MICROPY_LONGINT_IMPL"')
Damien George69661f32020-02-27 15:36:53 +11001306 print("#endif")
Damien George99b47192016-05-16 23:13:30 +01001307 print()
1308
1309 if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ:
Damien George69661f32020-02-27 15:36:53 +11001310 print("#if MPZ_DIG_SIZE != %u" % config.MPZ_DIG_SIZE)
Damien George99b47192016-05-16 23:13:30 +01001311 print('#error "incompatible MPZ_DIG_SIZE"')
Damien George69661f32020-02-27 15:36:53 +11001312 print("#endif")
Damien George99b47192016-05-16 23:13:30 +01001313 print()
1314
Damien George69661f32020-02-27 15:36:53 +11001315 print("#if MICROPY_PY_BUILTINS_FLOAT")
1316 print("typedef struct _mp_obj_float_t {")
1317 print(" mp_obj_base_t base;")
1318 print(" mp_float_t value;")
1319 print("} mp_obj_float_t;")
1320 print("#endif")
Damien George0699c6b2016-01-31 21:45:22 +00001321 print()
1322
Damien George69661f32020-02-27 15:36:53 +11001323 print("#if MICROPY_PY_BUILTINS_COMPLEX")
1324 print("typedef struct _mp_obj_complex_t {")
1325 print(" mp_obj_base_t base;")
1326 print(" mp_float_t real;")
1327 print(" mp_float_t imag;")
1328 print("} mp_obj_complex_t;")
1329 print("#endif")
Damien Georgec51c8832016-09-03 00:19:02 +10001330 print()
1331
Dave Hylands39eef272018-12-11 14:55:26 -08001332 if len(new) > 0:
Damien George69661f32020-02-27 15:36:53 +11001333 print("enum {")
Dave Hylands39eef272018-12-11 14:55:26 -08001334 for i in range(len(new)):
1335 if i == 0:
Damien George69661f32020-02-27 15:36:53 +11001336 print(" MP_QSTR_%s = MP_QSTRnumber_of," % new[i][1])
Dave Hylands39eef272018-12-11 14:55:26 -08001337 else:
Damien George69661f32020-02-27 15:36:53 +11001338 print(" MP_QSTR_%s," % new[i][1])
1339 print("};")
Damien George0699c6b2016-01-31 21:45:22 +00001340
Rich Barlow6e5a40c2018-07-19 12:42:26 +01001341 # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len
1342 qstr_pool_alloc = min(len(new), 10)
1343
Damien Georgef2040bf2021-10-22 22:22:47 +11001344 global bc_content, const_str_content, const_int_content, const_obj_content, const_table_qstr_content, const_table_ptr_content, raw_code_count, raw_code_content
1345 qstr_content = 0
1346 bc_content = 0
1347 const_str_content = 0
1348 const_int_content = 0
1349 const_obj_content = 0
1350 const_table_qstr_content = 0
1351 const_table_ptr_content = 0
1352 raw_code_count = 0
1353 raw_code_content = 0
1354
Damien George0699c6b2016-01-31 21:45:22 +00001355 print()
Artyom Skrobov18b1ba02021-05-03 14:17:36 -04001356 print("const qstr_hash_t mp_qstr_frozen_const_hashes[] = {")
1357 qstr_size = {"metadata": 0, "data": 0}
1358 for _, _, _, qbytes in new:
1359 qhash = qstrutil.compute_hash(qbytes, config.MICROPY_QSTR_BYTES_IN_HASH)
1360 print(" %d," % qhash)
1361 print("};")
1362 print()
1363 print("const qstr_len_t mp_qstr_frozen_const_lengths[] = {")
1364 for _, _, _, qbytes in new:
1365 print(" %d," % len(qbytes))
1366 qstr_size["metadata"] += (
1367 config.MICROPY_QSTR_BYTES_IN_LEN + config.MICROPY_QSTR_BYTES_IN_HASH
1368 )
1369 qstr_size["data"] += len(qbytes)
1370 print("};")
1371 print()
Damien George69661f32020-02-27 15:36:53 +11001372 print("extern const qstr_pool_t mp_qstr_const_pool;")
1373 print("const qstr_pool_t mp_qstr_frozen_const_pool = {")
Artyom Skrobovf46a7142021-05-04 03:35:45 -04001374 print(" &mp_qstr_const_pool, // previous pool")
Damien George69661f32020-02-27 15:36:53 +11001375 print(" MP_QSTRnumber_of, // previous pool size")
1376 print(" %u, // allocated entries" % qstr_pool_alloc)
1377 print(" %u, // used entries" % len(new))
Artyom Skrobov18b1ba02021-05-03 14:17:36 -04001378 print(" (qstr_hash_t *)mp_qstr_frozen_const_hashes,")
1379 print(" (qstr_len_t *)mp_qstr_frozen_const_lengths,")
Damien George69661f32020-02-27 15:36:53 +11001380 print(" {")
Artyom Skrobov18b1ba02021-05-03 14:17:36 -04001381 for _, _, qstr, qbytes in new:
1382 print(' "%s",' % qstrutil.escape_bytes(qstr, qbytes))
Damien Georgef2040bf2021-10-22 22:22:47 +11001383 qstr_content += (
1384 config.MICROPY_QSTR_BYTES_IN_LEN + config.MICROPY_QSTR_BYTES_IN_HASH + len(qbytes) + 1
1385 )
Damien George69661f32020-02-27 15:36:53 +11001386 print(" },")
1387 print("};")
Damien George0699c6b2016-01-31 21:45:22 +00001388
Damien Georgef2040bf2021-10-22 22:22:47 +11001389 # Freeze all modules.
1390 for idx, cm in enumerate(compiled_modules):
1391 cm.freeze(idx)
Damien George0699c6b2016-01-31 21:45:22 +00001392
Damien Georgef2040bf2021-10-22 22:22:47 +11001393 # Print separator, separating individual modules from global data structures.
1394 print()
1395 print("/" * 80)
1396 print("// collection of all frozen modules")
1397
1398 # Define the string of frozen module names.
Damien George0699c6b2016-01-31 21:45:22 +00001399 print()
Jim Mussarede0bf4612021-12-11 22:40:21 +11001400 print("const char mp_frozen_names[] = {")
Damien Georgef2040bf2021-10-22 22:22:47 +11001401 print(" #ifdef MP_FROZEN_STR_NAMES")
Jim Mussarede0bf4612021-12-11 22:40:21 +11001402 # makemanifest.py might also include some frozen string content.
Damien Georgef2040bf2021-10-22 22:22:47 +11001403 print(" MP_FROZEN_STR_NAMES")
1404 print(" #endif")
1405 mp_frozen_mpy_names_content = 1
1406 for cm in compiled_modules:
1407 module_name = cm.source_file.str
1408 print(' "%s\\0"' % module_name)
1409 mp_frozen_mpy_names_content += len(cm.source_file.str) + 1
1410 print(' "\\0"')
Damien George69661f32020-02-27 15:36:53 +11001411 print("};")
1412
Damien Georgef2040bf2021-10-22 22:22:47 +11001413 # Define the array of pointers to frozen module content.
1414 print()
1415 print("const mp_frozen_module_t *const mp_frozen_mpy_content[] = {")
1416 for cm in compiled_modules:
1417 print(" &frozen_module_%s," % cm.escaped_name)
1418 print("};")
1419 mp_frozen_mpy_content_size = len(compiled_modules * 4)
1420
Damien Georgefe16e782021-01-16 02:01:26 +11001421 # If a port defines MICROPY_FROZEN_LIST_ITEM then list all modules wrapped in that macro.
Damien Georgef2040bf2021-10-22 22:22:47 +11001422 print()
Damien Georgefe16e782021-01-16 02:01:26 +11001423 print("#ifdef MICROPY_FROZEN_LIST_ITEM")
Damien Georgef2040bf2021-10-22 22:22:47 +11001424 for cm in compiled_modules:
1425 module_name = cm.source_file.str
Damien Georgefe16e782021-01-16 02:01:26 +11001426 if module_name.endswith("/__init__.py"):
1427 short_name = module_name[: -len("/__init__.py")]
1428 else:
1429 short_name = module_name[: -len(".py")]
1430 print('MICROPY_FROZEN_LIST_ITEM("%s", "%s")' % (short_name, module_name))
1431 print("#endif")
1432
Damien Georgef2040bf2021-10-22 22:22:47 +11001433 print()
1434 print("/*")
1435 print("byte sizes:")
1436 print("qstr content: %d unique, %d bytes" % (len(new), qstr_content))
1437 print("bc content: %d" % bc_content)
1438 print("const str content: %d" % const_str_content)
1439 print("const int content: %d" % const_int_content)
1440 print("const obj content: %d" % const_obj_content)
1441 print(
1442 "const table qstr content: %d entries, %d bytes"
1443 % (const_table_qstr_content, const_table_qstr_content * 4)
1444 )
1445 print(
1446 "const table ptr content: %d entries, %d bytes"
1447 % (const_table_ptr_content, const_table_ptr_content * 4)
1448 )
1449 print("raw code content: %d * 4 = %d" % (raw_code_count, raw_code_content))
1450 print("mp_frozen_mpy_names_content: %d" % mp_frozen_mpy_names_content)
1451 print("mp_frozen_mpy_content_size: %d" % mp_frozen_mpy_content_size)
1452 print(
1453 "total: %d"
1454 % (
1455 qstr_content
1456 + bc_content
1457 + const_str_content
1458 + const_int_content
1459 + const_obj_content
1460 + const_table_qstr_content * 4
1461 + const_table_ptr_content * 4
1462 + raw_code_content
1463 + mp_frozen_mpy_names_content
1464 + mp_frozen_mpy_content_size
1465 )
1466 )
1467 print("*/")
1468
Damien George0699c6b2016-01-31 21:45:22 +00001469
Damien George27879842019-10-09 14:23:15 +11001470def merge_mpy(raw_codes, output_file):
Damien Georgef2040bf2021-10-22 22:22:47 +11001471 assert len(raw_codes) <= 2 # so var-uints all fit in 1 byte
Damien George27879842019-10-09 14:23:15 +11001472 merged_mpy = bytearray()
1473
1474 if len(raw_codes) == 1:
Damien George69661f32020-02-27 15:36:53 +11001475 with open(raw_codes[0].mpy_source_file, "rb") as f:
Damien George27879842019-10-09 14:23:15 +11001476 merged_mpy.extend(f.read())
1477 else:
Damien Georgef2040bf2021-10-22 22:22:47 +11001478 main_rc = None
1479 for rc in raw_codes:
1480 if len(rc.qstr_table) > 1 or len(rc.obj_table) > 0:
1481 # Must use qstr_table and obj_table from this raw_code
1482 if main_rc is not None:
1483 raise Exception(
1484 "can't merge files when more than one has a populated qstr or obj table"
1485 )
1486 main_rc = rc
1487 if main_rc is None:
1488 main_rc = raw_codes[0]
1489
1490 header = bytearray(4)
Damien George69661f32020-02-27 15:36:53 +11001491 header[0] = ord("M")
Damien George27879842019-10-09 14:23:15 +11001492 header[1] = config.MPY_VERSION
Jim Mussaredb326edf2021-09-06 12:28:06 +10001493 header[2] = config.native_arch << 2 | config.MICROPY_PY_BUILTINS_STR_UNICODE << 1
Damien George27879842019-10-09 14:23:15 +11001494 header[3] = config.mp_small_int_bits
Damien George27879842019-10-09 14:23:15 +11001495 merged_mpy.extend(header)
1496
Damien Georgef2040bf2021-10-22 22:22:47 +11001497 # Copy n_qstr, n_obj, qstr_table, obj_table from main_rc.
1498 with open(main_rc.mpy_source_file, "rb") as f:
1499 data = f.read(main_rc.raw_code_file_offset)
1500 merged_mpy.extend(data[4:])
1501
Damien George27879842019-10-09 14:23:15 +11001502 bytecode = bytearray()
Damien Georgef2040bf2021-10-22 22:22:47 +11001503 bytecode_len = 3 + len(raw_codes) * 5 + 2
1504 bytecode.append(bytecode_len << 3 | 1 << 2) # kind, has_children and length
Damien George69661f32020-02-27 15:36:53 +11001505 bytecode.append(0b00000000) # signature prelude
Damien Georgef2040bf2021-10-22 22:22:47 +11001506 bytecode.append(0b00000010) # size prelude; n_info=1
1507 bytecode.extend(b"\x00") # simple_name: qstr index 0 (will use source filename)
Damien George27879842019-10-09 14:23:15 +11001508 for idx in range(len(raw_codes)):
Damien George69661f32020-02-27 15:36:53 +11001509 bytecode.append(0x32) # MP_BC_MAKE_FUNCTION
1510 bytecode.append(idx) # index raw code
Damien George4f2fe342020-09-04 16:12:09 +10001511 bytecode.extend(b"\x34\x00\x59") # MP_BC_CALL_FUNCTION, 0 args, MP_BC_POP_TOP
Damien George69661f32020-02-27 15:36:53 +11001512 bytecode.extend(b"\x51\x63") # MP_BC_LOAD_NONE, MP_BC_RETURN_VALUE
Damien George27879842019-10-09 14:23:15 +11001513
Damien George27879842019-10-09 14:23:15 +11001514 merged_mpy.extend(bytecode)
1515
Damien Georgef2040bf2021-10-22 22:22:47 +11001516 merged_mpy.append(len(raw_codes)) # n_children
1517
Damien George27879842019-10-09 14:23:15 +11001518 for rc in raw_codes:
Damien George69661f32020-02-27 15:36:53 +11001519 with open(rc.mpy_source_file, "rb") as f:
Damien Georgef2040bf2021-10-22 22:22:47 +11001520 f.seek(rc.raw_code_file_offset)
Damien George69661f32020-02-27 15:36:53 +11001521 data = f.read() # read rest of mpy file
Damien George27879842019-10-09 14:23:15 +11001522 merged_mpy.extend(data)
1523
1524 if output_file is None:
1525 sys.stdout.buffer.write(merged_mpy)
1526 else:
Damien George69661f32020-02-27 15:36:53 +11001527 with open(output_file, "wb") as f:
Damien George27879842019-10-09 14:23:15 +11001528 f.write(merged_mpy)
1529
Damien George69661f32020-02-27 15:36:53 +11001530
Damien George0699c6b2016-01-31 21:45:22 +00001531def main():
1532 import argparse
Damien George69661f32020-02-27 15:36:53 +11001533
1534 cmd_parser = argparse.ArgumentParser(description="A tool to work with MicroPython .mpy files.")
Damien Georgef2040bf2021-10-22 22:22:47 +11001535 cmd_parser.add_argument(
1536 "-x", "--hexdump", action="store_true", help="output an annotated hex dump of files"
1537 )
1538 cmd_parser.add_argument(
1539 "-d", "--disassemble", action="store_true", help="output disassembled contents of files"
1540 )
Damien George69661f32020-02-27 15:36:53 +11001541 cmd_parser.add_argument("-f", "--freeze", action="store_true", help="freeze files")
1542 cmd_parser.add_argument(
1543 "--merge", action="store_true", help="merge multiple .mpy files into one"
1544 )
1545 cmd_parser.add_argument("-q", "--qstr-header", help="qstr header file to freeze against")
1546 cmd_parser.add_argument(
1547 "-mlongint-impl",
1548 choices=["none", "longlong", "mpz"],
1549 default="mpz",
1550 help="long-int implementation used by target (default mpz)",
1551 )
1552 cmd_parser.add_argument(
1553 "-mmpz-dig-size",
1554 metavar="N",
1555 type=int,
1556 default=16,
1557 help="mpz digit size used by target (default 16)",
1558 )
1559 cmd_parser.add_argument("-o", "--output", default=None, help="output file")
1560 cmd_parser.add_argument("files", nargs="+", help="input .mpy files")
Damien George0699c6b2016-01-31 21:45:22 +00001561 args = cmd_parser.parse_args()
1562
1563 # set config values relevant to target machine
1564 config.MICROPY_LONGINT_IMPL = {
Damien George69661f32020-02-27 15:36:53 +11001565 "none": config.MICROPY_LONGINT_IMPL_NONE,
1566 "longlong": config.MICROPY_LONGINT_IMPL_LONGLONG,
1567 "mpz": config.MICROPY_LONGINT_IMPL_MPZ,
Damien George0699c6b2016-01-31 21:45:22 +00001568 }[args.mlongint_impl]
1569 config.MPZ_DIG_SIZE = args.mmpz_dig_size
Damien Georgefaf3d3e2019-06-04 22:13:32 +10001570 config.native_arch = MP_NATIVE_ARCH_NONE
Damien George0699c6b2016-01-31 21:45:22 +00001571
Damien Georgeb4790af2016-09-02 15:09:21 +10001572 # set config values for qstrs, and get the existing base set of qstrs
Damien George0699c6b2016-01-31 21:45:22 +00001573 if args.qstr_header:
1574 qcfgs, base_qstrs = qstrutil.parse_input_headers([args.qstr_header])
Damien George69661f32020-02-27 15:36:53 +11001575 config.MICROPY_QSTR_BYTES_IN_LEN = int(qcfgs["BYTES_IN_LEN"])
1576 config.MICROPY_QSTR_BYTES_IN_HASH = int(qcfgs["BYTES_IN_HASH"])
Damien George0699c6b2016-01-31 21:45:22 +00001577 else:
Damien Georgeb4790af2016-09-02 15:09:21 +10001578 config.MICROPY_QSTR_BYTES_IN_LEN = 1
1579 config.MICROPY_QSTR_BYTES_IN_HASH = 1
Damien Georgef2040bf2021-10-22 22:22:47 +11001580 base_qstrs = list(qstrutil.static_qstr_list)
Damien George0699c6b2016-01-31 21:45:22 +00001581
Damien Georgef2040bf2021-10-22 22:22:47 +11001582 # Load all .mpy files.
1583 try:
1584 compiled_modules = [read_mpy(file) for file in args.files]
1585 except MPYReadError as er:
1586 print(er, file=sys.stderr)
1587 sys.exit(1)
Damien George0699c6b2016-01-31 21:45:22 +00001588
Damien Georgef2040bf2021-10-22 22:22:47 +11001589 if args.hexdump:
1590 hexdump_mpy(compiled_modules)
1591
1592 if args.disassemble:
1593 if args.hexdump:
1594 print()
1595 disassemble_mpy(compiled_modules)
1596
1597 if args.freeze:
Damien George0699c6b2016-01-31 21:45:22 +00001598 try:
Damien Georgef2040bf2021-10-22 22:22:47 +11001599 freeze_mpy(base_qstrs, compiled_modules)
Damien George0699c6b2016-01-31 21:45:22 +00001600 except FreezeError as er:
1601 print(er, file=sys.stderr)
1602 sys.exit(1)
Damien Georgef2040bf2021-10-22 22:22:47 +11001603
1604 if args.merge:
1605 merge_mpy(compiled_modules, args.output)
Damien George0699c6b2016-01-31 21:45:22 +00001606
Damien George69661f32020-02-27 15:36:53 +11001607
1608if __name__ == "__main__":
Damien George0699c6b2016-01-31 21:45:22 +00001609 main()