Skip to content

Commit 3e8c0d5

Browse files
committed
add support for macros such as WRITE_RTC_REG
This is a simplified implementation, rather than adding "proper" support for macros because we don't need more. The macros WRITE_RTC_REG, READ_RTC_REG, WRITE_RTC_FIELD and READ_RTC_FIELD are simply expanded in a predefined way. If they are also defined as macros in the source code, those macros in the source will be ignored.
1 parent 5c3eeb8 commit 3e8c0d5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

esp32_ulp/preprocess.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
from .util import split_tokens
33

44

5+
class RTC_Macros:
6+
@staticmethod
7+
def READ_RTC_REG(rtc_reg, low_bit, bit_width):
8+
return '\treg_rd ' + ', '.join((
9+
rtc_reg,
10+
'%s + %s - 1' % (low_bit, bit_width),
11+
low_bit
12+
))
13+
14+
@staticmethod
15+
def WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value):
16+
args = (
17+
rtc_reg,
18+
'%s + %s - 1' % (low_bit, bit_width),
19+
low_bit,
20+
value
21+
)
22+
return '\treg_wr ' + ', '.join(args)
23+
24+
@staticmethod
25+
def READ_RTC_FIELD(rtc_reg, low_bit):
26+
return RTC_Macros.READ_RTC_REG(rtc_reg, low_bit, 1)
27+
28+
@staticmethod
29+
def WRITE_RTC_FIELD(rtc_reg, low_bit, value):
30+
return RTC_Macros.WRITE_RTC_REG(rtc_reg, low_bit, 1, value + ' & 1')
31+
32+
533
class Preprocessor:
634
def __init__(self):
735
self._defines = {}
@@ -42,12 +70,34 @@ def expand_defines(self, line):
4270

4371
return line
4472

73+
def expand_rtc_macros(self, line):
74+
clean_line = line.strip()
75+
if not clean_line:
76+
return line
77+
78+
macro = clean_line.split('(', 1)
79+
if len(macro) != 2:
80+
return line
81+
82+
macro_name, macro_args = macro
83+
84+
macro_fn = getattr(RTC_Macros, macro_name, None)
85+
if macro_fn is None:
86+
return line
87+
88+
macro_args, _ = macro_args.rsplit(')', 1) # trim away right bracket. safe as comments already stripped
89+
macro_args = macro_args.split(',') # not safe when args contain ',' but we should not have those
90+
macro_args = [x.strip() for x in macro_args]
91+
92+
return macro_fn(*macro_args)
93+
4594
def preprocess(self, content):
4695
self.parse_defines(content)
4796
lines = nocomment.remove_comments(content)
4897
result = []
4998
for line in lines:
5099
line = self.expand_defines(line)
100+
line = self.expand_rtc_macros(line)
51101
result.append(line)
52102
result = "\n".join(result)
53103
return result

tests/preprocess.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ def preprocess_should_replace_defines_used_in_defines():
169169
assert "move r1, (0x1234 << 4)" in p.preprocess(lines)
170170

171171

172+
@test
173+
def test_expand_rtc_macros():
174+
p = Preprocessor()
175+
176+
assert p.expand_rtc_macros("") == ""
177+
assert p.expand_rtc_macros("abc") == "abc"
178+
assert p.expand_rtc_macros("WRITE_RTC_REG(1, 2, 3, 4)") == "\treg_wr 1, 2 + 3 - 1, 2, 4"
179+
assert p.expand_rtc_macros("READ_RTC_REG(1, 2, 3)") == "\treg_rd 1, 2 + 3 - 1, 2"
180+
assert p.expand_rtc_macros("WRITE_RTC_FIELD(1, 2, 3)") == "\treg_wr 1, 2 + 1 - 1, 2, 3 & 1"
181+
assert p.expand_rtc_macros("READ_RTC_FIELD(1, 2)") == "\treg_rd 1, 2 + 1 - 1, 2"
182+
183+
172184
if __name__ == '__main__':
173185
# run all methods marked with @test
174186
for t in tests:

0 commit comments

Comments
 (0)