forked from micropython/micropython-esp32-ulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
43 lines (32 loc) · 1.21 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# This file is part of the micropython-esp32-ulp project,
# https://github.com/micropython/micropython-esp32-ulp
#
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
# SPDX-License-Identifier: MIT
from .util import garbage_collect
from .preprocess import preprocess
from .assemble import Assembler
from .link import make_binary
garbage_collect('after import')
def src_to_binary_ext(src, cpu):
assembler = Assembler(cpu)
src = preprocess(src)
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
garbage_collect('before symbols export')
addrs_syms = assembler.symbols.export()
text, data, bss_len = assembler.fetch()
return make_binary(text, data, bss_len), addrs_syms
def src_to_binary(src, cpu):
binary, addrs_syms = src_to_binary_ext(src, cpu)
for addr, sym in addrs_syms:
print('%04d %s' % (addr, sym))
return binary
def assemble_file(filename, cpu):
with open(filename) as f:
src = f.read()
binary = src_to_binary(src, cpu)
if filename.endswith('.s') or filename.endswith('.S'):
filename = filename[:-2]
with open(filename + '.ulp', 'wb') as f:
f.write(binary)