Skip to content

Preprocessor for ULP/RTC macros #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
79db90f
add units test for the .set directive
wnienhaus Jul 22, 2021
84d734d
add support for left aligned assembler directives (e.g. .set)
wnienhaus Jul 22, 2021
ec81ecc
fix a crash bug where BSS size calculation was attempted on the value…
wnienhaus Jul 22, 2021
c184924
raise error when attempting to store values in .bss section
wnienhaus Jul 29, 2021
25d34b0
fix reference to non-existing variable
wnienhaus Jul 22, 2021
76a81ac
fix typo in comment of instruction definition
wnienhaus Jul 22, 2021
56f4530
add support for the .global directive. only symbols flagged as global…
wnienhaus Jul 22, 2021
9907b10
let SymbolTable.export() optionally export non-global symbols too
wnienhaus Jul 22, 2021
27ab850
support ULP opcodes in upper case
wnienhaus Jul 22, 2021
54b117e
add a compatibility test for the recent fixes and improvements
wnienhaus Jul 22, 2021
feb42dc
add support for evaluating expressions
wnienhaus Jul 22, 2021
87507c9
add a compatibility test for evaluating expressions
wnienhaus Jul 23, 2021
99352a3
docs: add that expressions are now supported
wnienhaus Jul 29, 2021
d76fd26
add preprocessor that can replace simple #define values in code
wnienhaus Jul 23, 2021
4dded94
allow assembler to skip comment removal to avoid removing comments twice
wnienhaus Aug 7, 2021
219f939
fix evaluation of expressions during first assembler pass
wnienhaus Jul 25, 2021
5c3eeb8
remove no-longer-needed pass dependent code from SymbolTable
wnienhaus Jul 26, 2021
3e8c0d5
add support for macros such as WRITE_RTC_REG
wnienhaus Jul 26, 2021
ac1de99
add simple include file processing
wnienhaus Jul 26, 2021
8d88fd1
add support for using a btree database (DefinesDB) to store defines f…
wnienhaus Jul 27, 2021
46f1442
add special handling for the BIT macro used in the esp-idf framework
wnienhaus Jul 27, 2021
2f6ee78
add include processor tool for populating a defines.db from include f…
wnienhaus Jul 28, 2021
69ae946
add compatibility tests using good example code off the net
wnienhaus Jul 28, 2021
4f90f76
add documentation for the preprocessor
wnienhaus Jul 29, 2021
d44384f
fix use of treg field in i_move instruction to match binutils-esp32 o…
wnienhaus Jul 28, 2021
254adf9
allow specifying the address for reg_rd and reg_wr in 32-bit words
wnienhaus Jul 28, 2021
c3bd101
support .int data type
wnienhaus Jul 29, 2021
2a0a39a
refactor: small improvements based on PR comments.
wnienhaus Aug 9, 2021
47d5e8a
Updated LICENSE file and added AUTHORS file
wnienhaus Aug 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions esp32_ulp/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@


class SymbolTable:
def __init__(self, symbols, bases):
def __init__(self, symbols, bases, globals):
self._symbols = symbols
self._bases = bases
self._globals = globals
self._pass = None

def set_pass(self, _pass):
Expand All @@ -32,7 +33,7 @@ def get_from(self):
def set_sym(self, symbol, stype, section, value):
entry = (stype, section, value)
if symbol in self._symbols and entry != self._symbols[symbol]:
raise Exception('redefining symbol %s with different value %r -> %r.' % (label, self._symbols[symbol], entry))
raise Exception('redefining symbol %s with different value %r -> %r.' % (symbol, self._symbols[symbol], entry))
self._symbols[symbol] = entry

def has_sym(self, symbol):
Expand All @@ -53,7 +54,9 @@ def dump(self):
print(symbol, entry)

def export(self):
addrs_syms = [(self.resolve_absolute(entry), symbol) for symbol, entry in self._symbols.items()]
addrs_syms = [(self.resolve_absolute(entry), symbol)
for symbol, entry in self._symbols.items()
if symbol in self._globals]
return sorted(addrs_syms)

def to_abs_addr(self, section, offset):
Expand Down Expand Up @@ -93,11 +96,15 @@ def resolve_relative(self, symbol):
from_addr = self.to_abs_addr(self._from_section, self._from_offset)
return sym_addr - from_addr

def set_global(self, symbol):
self._globals[symbol] = True
pass


class Assembler:

def __init__(self, symbols=None, bases=None):
self.symbols = SymbolTable(symbols or {}, bases or {})
def __init__(self, symbols=None, bases=None, globls=None):
self.symbols = SymbolTable(symbols or {}, bases or {}, globls or {})
opcodes.symbols = self.symbols # XXX dirty hack

def init(self, a_pass):
Expand All @@ -118,7 +125,7 @@ def parse_line(self, line):
"""
if not line:
return
has_label = line[0] not in '\t '
has_label = line[0] not in '\t .'
if has_label:
label_line = line.split(None, 1)
if len(label_line) == 2:
Expand Down Expand Up @@ -150,8 +157,10 @@ def append_section(self, value, expected_section=None):
if expected_section is not None and s is not expected_section:
raise TypeError('only allowed in %s section' % expected_section)
if s is BSS:
# just increase BSS size by value
self.offsets[s] += value
if int.from_bytes(value, 'little') != 0:
raise ValueError('attempt to store non-zero value in section .bss')
# just increase BSS size by length of value
self.offsets[s] += len(value)
else:
self.sections[s].append(value)
self.offsets[s] += len(value)
Expand Down Expand Up @@ -234,6 +243,9 @@ def d_set(self, symbol, expr):
value = int(expr) # TODO: support more than just integers
self.symbols.set_sym(symbol, ABS, None, value)

def d_global(self, symbol):
self.symbols.set_global(symbol)

def append_data(self, wordlen, args):
data = [int(arg).to_bytes(wordlen, 'little') for arg in args]
self.append_section(b''.join(data))
Expand Down
2 changes: 1 addition & 1 deletion esp32_ulp/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def make_ins(layout):
unused : 8 # Unused
low : 5 # Low bit
high : 5 # High bit
opcode : 4 # Opcode (OPCODE_WR_REG)
opcode : 4 # Opcode (OPCODE_RD_REG)
""")


Expand Down
101 changes: 92 additions & 9 deletions tests/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,54 @@
from esp32_ulp.nocomment import remove_comments

src = """\
.set const, 123
.set const_left, 976

start: wait 42
ld r0, r1, 0
st r0, r1,0
halt
end:
.data
"""

src_bss = """\
.bss

label:
.long 0
"""


src_global = """\

.global counter
counter:
.long 0

internal:
.long 0

.text
.global entry
entry:
wait 42
halt
"""


def test_parse_line():
a = Assembler()
lines = src.splitlines()
# note: line number = index + 1
assert a.parse_line(lines[0]) == None
assert a.parse_line(lines[1]) == ('start', 'wait', ('42', ))
assert a.parse_line(lines[2]) == (None, 'ld', ('r0', 'r1', '0', ))
assert a.parse_line(lines[3]) == (None, 'st', ('r0', 'r1', '0', ))
assert a.parse_line(lines[4]) == (None, 'halt', ())
assert a.parse_line(lines[5]) == ('end', None, ())
lines = iter(src.splitlines())
assert a.parse_line(next(lines)) == (None, '.set', ('const', '123', ))
assert a.parse_line(next(lines)) == (None, '.set', ('const_left', '976', ))
assert a.parse_line(next(lines)) == None
assert a.parse_line(next(lines)) == ('start', 'wait', ('42', ))
assert a.parse_line(next(lines)) == (None, 'ld', ('r0', 'r1', '0', ))
assert a.parse_line(next(lines)) == (None, 'st', ('r0', 'r1', '0', ))
assert a.parse_line(next(lines)) == (None, 'halt', ())
assert a.parse_line(next(lines)) == ('end', None, ())
assert a.parse_line(next(lines)) == (None, '.data', ()) # test left-aligned directive is not treated as label


def test_parse():
Expand All @@ -34,22 +63,69 @@ def test_parse():
def test_assemble():
a = Assembler()
a.assemble(src)
assert a.symbols.has_sym('const')
assert a.symbols.has_sym('const_left')
assert a.symbols.has_sym('start')
assert a.symbols.has_sym('end')
assert a.symbols.get_sym('const') == (ABS, None, 123)
assert a.symbols.get_sym('const_left') == (ABS, None, 976)
assert a.symbols.get_sym('start') == (REL, TEXT, 0)
assert a.symbols.get_sym('end') == (REL, TEXT, 4)
assert len(b''.join(a.sections[TEXT])) == 16 # 4 instructions * 4B
assert len(a.sections[DATA]) == 0
assert a.offsets[BSS] == 0


def test_assemble_bss():
a = Assembler()
try:
a.assemble(src_bss)
except TypeError:
raised = True
else:
raised = False
assert not raised
assert a.offsets[BSS] == 4 # 1 word * 4B


def test_assemble_bss_with_value():
lines = """\
.bss
.long 3 #non-zero value not allowed in bss section
"""

a = Assembler()
try:
a.assemble(lines)
except ValueError as e:
if str(e) != "attempt to store non-zero value in section .bss":
raise # re-raise failures we didn't expect
raised = True
else:
raised = False

assert raised


def test_assemble_global():
a = Assembler()
a.assemble(src_global)
assert a.symbols.has_sym('counter')
assert a.symbols.has_sym('internal')
assert a.symbols.has_sym('entry')

exported_symbols = a.symbols.export()
assert exported_symbols == [(0, 'counter'), (2, 'entry')] # internal not exported


def test_symbols():
st = SymbolTable({}, {})
st = SymbolTable({}, {}, {})
for entry in [
('rel_t4', REL, TEXT, 4),
('abs_t4', ABS, TEXT, 4),
('rel_d4', REL, DATA, 4),
('abs_d4', ABS, DATA, 4),
('const', ABS, None, 123),
]:
st.set_sym(*entry)
# PASS 1 ========================================================
Expand All @@ -62,11 +138,13 @@ def test_symbols():
assert st.resolve_absolute('abs_d4') == 4
assert st.resolve_absolute('rel_t4') == 4
assert st.resolve_absolute('rel_d4') == 4
assert st.resolve_absolute('const') == 123
st.set_from(TEXT, 8)
assert st.resolve_relative('abs_t4') == -4
assert st.resolve_relative('abs_d4') == -4
assert st.resolve_relative('rel_t4') == -4
assert st.resolve_relative('rel_d4') == -4
assert st.resolve_absolute('const') == 123
# PASS 2 ========================================================
st.set_bases({TEXT: 100, DATA: 200})
st.set_pass(2)
Expand All @@ -84,14 +162,19 @@ def test_symbols():
assert st.resolve_absolute('abs_d4') == 4
assert st.resolve_absolute('rel_t4') == 100 + 4
assert st.resolve_absolute('rel_d4') == 200 + 4
assert st.resolve_absolute('const') == 123
st.set_from(TEXT, 8)
assert st.resolve_relative('abs_t4') == 4 - 108
assert st.resolve_relative('abs_d4') == 4 - 108
assert st.resolve_relative('rel_t4') == 104 - 108
assert st.resolve_relative('rel_d4') == 204 - 108
assert st.resolve_absolute('const') == 123


test_parse_line()
test_parse()
test_assemble()
test_assemble_bss()
test_assemble_bss_with_value()
test_assemble_global()
test_symbols()
2 changes: 2 additions & 0 deletions tests/compat/symbols.S
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
.text

.set constant42, 42
.set notindented, 1

start: move r0, data0
move r1, data1
move r2, constant42
move r3, notindented

# count from 0 .. 42 in stage register
stage_rst
Expand Down