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 1 commit
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
50 changes: 50 additions & 0 deletions esp32_ulp/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
from .util import split_tokens


class RTC_Macros:
@staticmethod
def READ_RTC_REG(rtc_reg, low_bit, bit_width):
return '\treg_rd ' + ', '.join((
rtc_reg,
'%s + %s - 1' % (low_bit, bit_width),
low_bit
))

@staticmethod
def WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value):
args = (
rtc_reg,
'%s + %s - 1' % (low_bit, bit_width),
low_bit,
value
)
return '\treg_wr ' + ', '.join(args)

@staticmethod
def READ_RTC_FIELD(rtc_reg, low_bit):
return RTC_Macros.READ_RTC_REG(rtc_reg, low_bit, 1)

@staticmethod
def WRITE_RTC_FIELD(rtc_reg, low_bit, value):
return RTC_Macros.WRITE_RTC_REG(rtc_reg, low_bit, 1, value + ' & 1')


class Preprocessor:
def __init__(self):
self._defines = {}
Expand Down Expand Up @@ -42,12 +70,34 @@ def expand_defines(self, line):

return line

def expand_rtc_macros(self, line):
clean_line = line.strip()
if not clean_line:
return line

macro = clean_line.split('(', 1)
if len(macro) != 2:
return line

macro_name, macro_args = macro

macro_fn = getattr(RTC_Macros, macro_name, None)
if macro_fn is None:
return line

macro_args, _ = macro_args.rsplit(')', 1) # trim away right bracket. safe as comments already stripped
macro_args = macro_args.split(',') # not safe when args contain ',' but we should not have those
macro_args = [x.strip() for x in macro_args]

return macro_fn(*macro_args)

def preprocess(self, content):
self.parse_defines(content)
lines = nocomment.remove_comments(content)
result = []
for line in lines:
line = self.expand_defines(line)
line = self.expand_rtc_macros(line)
result.append(line)
result = "\n".join(result)
return result
Expand Down
12 changes: 12 additions & 0 deletions tests/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ def preprocess_should_replace_defines_used_in_defines():
assert "move r1, (0x1234 << 4)" in p.preprocess(lines)


@test
def test_expand_rtc_macros():
p = Preprocessor()

assert p.expand_rtc_macros("") == ""
assert p.expand_rtc_macros("abc") == "abc"
assert p.expand_rtc_macros("WRITE_RTC_REG(1, 2, 3, 4)") == "\treg_wr 1, 2 + 3 - 1, 2, 4"
assert p.expand_rtc_macros("READ_RTC_REG(1, 2, 3)") == "\treg_rd 1, 2 + 3 - 1, 2"
assert p.expand_rtc_macros("WRITE_RTC_FIELD(1, 2, 3)") == "\treg_wr 1, 2 + 1 - 1, 2, 3 & 1"
assert p.expand_rtc_macros("READ_RTC_FIELD(1, 2)") == "\treg_rd 1, 2 + 1 - 1, 2"


if __name__ == '__main__':
# run all methods marked with @test
for t in tests:
Expand Down