Skip to content

Commit e40432d

Browse files
committed
add special handling for the BIT macro used in the esp-idf framework
The functions the preprocessor supports (WRITE_RTC_*/READ_RTC_*) do not need the value returned by the BIT macro. Instead they use the bit number specified to the BIT macro, i.e. for BIT(x) they need x. So this change handles BIT by simply replacing it with an empty string, and BIT(x) results in (x) in the preprocessor output.
1 parent 9fdc838 commit e40432d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

esp32_ulp/preprocess.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ def expand_defines(self, line):
8181
lu = self._defines.get(t, t)
8282
if lu == t and self._defines_db:
8383
lu = self._defines_db.get(t, t)
84+
if lu == t and t == 'BIT':
85+
# Special hack: BIT(..) translates to a 32-bit mask where only the specified bit is set.
86+
# But the reg_wr and reg_rd opcodes expect actual bit numbers for argument 2 and 3.
87+
# While the real READ_RTC_*/WRITE_RTC_* macros take in the output of BIT(x), they
88+
# ultimately convert these back (via helper macros) to the bit number (x). And since this
89+
# preprocessor does not (aim to) implement "proper" macro-processing, we can simply
90+
# short-circuit this round-trip via macros and replace "BIT" with nothing so that
91+
# "BIT(x)" gets mapped to "(x)".
92+
continue
8493
if lu != t:
8594
found = True
8695
line += lu

tests/preprocess.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ def test_expand_rtc_macros():
195195
assert p.expand_rtc_macros("READ_RTC_FIELD(1, 2)") == "\treg_rd 1, 2 + 1 - 1, 2"
196196

197197

198+
@test
199+
def preprocess_should_replace_BIT_with_empty_string_unless_defined():
200+
# by default replace BIT with empty string (see description for why in the code)
201+
src = " move r1, 0x123 << BIT(24)"
202+
assert "move r1, 0x123 << (24)" in Preprocessor().preprocess(src)
203+
204+
# but if BIT is defined, use that
205+
src = """\
206+
#define BIT 12
207+
208+
move r1, BIT"""
209+
210+
assert "move r1, 12" in Preprocessor().preprocess(src)
211+
212+
198213
@test
199214
def test_process_include_file():
200215
p = Preprocessor()

0 commit comments

Comments
 (0)