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: 31 additions & 19 deletions esp32_ulp/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,31 @@ def WRITE_RTC_FIELD(rtc_reg, low_bit, value):

class Preprocessor:
def __init__(self):
self._defines_db = None
self._defines = {}

def parse_define_line(self, line):
line = line.strip()
if not line.startswith("#define"):
# skip lines not containing #define
return {}
line = line[8:].strip() # remove #define
parts = line.split(None, 1)
if len(parts) != 2:
# skip defines without value
return {}
identifier, value = parts
tmp = identifier.split('(', 1)
if len(tmp) == 2:
# skip parameterised defines (macros)
return {}
value = "".join(nocomment.remove_comments(value)).strip()
return {identifier: value}

def parse_defines(self, content):
result = {}
for line in content.splitlines():
line = line.strip()
if not line.startswith("#define"):
# skip lines not containing #define
continue
line = line[8:].strip() # remove #define
parts = line.split(None, 1)
if len(parts) != 2:
# skip defines without value
continue
identifier, value = parts
tmp = identifier.split('(', 1)
if len(tmp) == 2:
# skip parameterised defines (macros)
continue
value = "".join(nocomment.remove_comments(value)).strip()
result[identifier] = value
self._defines = result
return result
self._defines.update(self.parse_define_line(line))
return self._defines

def expand_defines(self, line):
found = True
Expand All @@ -70,6 +72,16 @@ def expand_defines(self, line):

return line

def process_include_file(self, filename):
defines = self._defines

with open(filename, 'r') as f:
for line in f:
result = self.parse_defines(line)
defines.update(result)

return defines

def expand_rtc_macros(self, line):
clean_line = line.strip()
if not clean_line:
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/incl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define CONST1 42
#define MACRO(x,y) x+y
#define MULTI_LINE abc \
xyz
#define CONST2 99
2 changes: 2 additions & 0 deletions tests/fixtures/incl2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CONST2 123
#define CONST3 777
49 changes: 36 additions & 13 deletions tests/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ def replace_defines_should_return_remove_comments():
def test_parse_defines():
p = Preprocessor()

assert p.parse_defines("") == {}
assert p.parse_defines("// comment") == {}
assert p.parse_defines(" // comment") == {}
assert p.parse_defines(" /* comment */") == {}
assert p.parse_defines(" /* comment */ #define A 42") == {} # #define must be the first thing on a line
assert p.parse_defines("#define a 1") == {"a": "1"}
assert p.parse_defines(" #define a 1") == {"a": "1"}
assert p.parse_defines("#define a 1 2") == {"a": "1 2"}
assert p.parse_defines("#define f(a,b) 1") == {} # macros not supported
assert p.parse_defines("#define f(a, b) 1") == {} # macros not supported
assert p.parse_defines("#define f (a,b) 1") == {"f": "(a,b) 1"} # f is not a macro
assert p.parse_defines("#define f (a, b) 1") == {"f": "(a, b) 1"} # f is not a macro
assert p.parse_defines("#define RTC_ADDR 0x12345 // start of range") == {"RTC_ADDR": "0x12345"}
assert p.parse_define_line("") == {}
assert p.parse_define_line("// comment") == {}
assert p.parse_define_line(" // comment") == {}
assert p.parse_define_line(" /* comment */") == {}
assert p.parse_define_line(" /* comment */ #define A 42") == {} # #define must be the first thing on a line
assert p.parse_define_line("#define a 1") == {"a": "1"}
assert p.parse_define_line(" #define a 1") == {"a": "1"}
assert p.parse_define_line("#define a 1 2") == {"a": "1 2"}
assert p.parse_define_line("#define f(a,b) 1") == {} # macros not supported
assert p.parse_define_line("#define f(a, b) 1") == {} # macros not supported
assert p.parse_define_line("#define f (a,b) 1") == {"f": "(a,b) 1"} # f is not a macro
assert p.parse_define_line("#define f (a, b) 1") == {"f": "(a, b) 1"} # f is not a macro
assert p.parse_define_line("#define RTC_ADDR 0x12345 // start of range") == {"RTC_ADDR": "0x12345"}


@test
Expand Down Expand Up @@ -181,6 +181,29 @@ def test_expand_rtc_macros():
assert p.expand_rtc_macros("READ_RTC_FIELD(1, 2)") == "\treg_rd 1, 2 + 1 - 1, 2"


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

defines = p.process_include_file('fixtures/incl.h')
assert defines['CONST1'] == '42'
assert defines['CONST2'] == '99'
assert defines.get('MULTI_LINE', None) == 'abc \\' # correct. line continuations not supported
assert 'MACRO' not in defines


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

defines = p.process_include_file('fixtures/incl.h')
defines = p.process_include_file('fixtures/incl2.h')

assert defines['CONST1'] == '42', "constant from incl.h"
assert defines['CONST2'] == '123', "constant overridden by incl2.h"
assert defines['CONST3'] == '777', "constant from incl2.h"


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