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
2 changes: 1 addition & 1 deletion esp32_ulp/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def d_align(self, align=4, fill=None):
self.fill(self.section, amount, fill)

def d_set(self, symbol, expr):
value = int(expr) # TODO: support more than just integers
value = int(opcodes.eval_arg(expr)) # TODO: support more than just integers
self.symbols.set_sym(symbol, ABS, None, value)

def d_global(self, symbol):
Expand Down
23 changes: 21 additions & 2 deletions esp32_ulp/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from uctypes import struct, addressof, LITTLE_ENDIAN, UINT32, BFUINT32, BF_POS, BF_LEN

from .soc import *
from .util import split_tokens, validate_expression

# XXX dirty hack: use a global for the symbol table
symbols = None
Expand Down Expand Up @@ -267,6 +268,20 @@ def make_ins(layout):
ARG = namedtuple('ARG', ('type', 'value', 'raw'))


def eval_arg(arg):
parts = []
for token in split_tokens(arg):
if symbols.has_sym(token):
_, _, sym_value = symbols.get_sym(token)
parts.append(str(sym_value))
else:
parts.append(token)
parts = "".join(parts)
if not validate_expression(parts):
raise ValueError('Unsupported expression: %s' % parts)
return eval(parts)


def arg_qualify(arg):
"""
look at arg and qualify its type:
Expand All @@ -289,8 +304,12 @@ def arg_qualify(arg):
return ARG(IMM, int(arg), arg)
except ValueError:
pass
entry = symbols.get_sym(arg)
return ARG(SYM, entry, arg)
try:
entry = symbols.get_sym(arg)
return ARG(SYM, entry, arg)
except KeyError:
pass
return ARG(IMM, int(eval_arg(arg)), arg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    try:
        entry = symbols.get_sym(arg)
    except KeyError:
        return ARG(IMM, int(eval_arg(arg)), arg)
    else:
        return ARG(SYM, entry, arg)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seen my previous comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now.



def get_reg(arg):
Expand Down
58 changes: 58 additions & 0 deletions esp32_ulp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,68 @@

import gc

NORMAL, WHITESPACE = 0, 1


def garbage_collect(msg, verbose=DEBUG):
free_before = gc.mem_free()
gc.collect()
free_after = gc.mem_free()
if verbose:
print("%s: %d --gc--> %d bytes free" % (msg, free_before, free_after))


def split_tokens(line):
buf = ""
tokens = []
state = NORMAL
for c in line:
if ('a' <= c <= 'z') or ('A' <= c <= 'Z') or ('0' <= c <= '9') or c == '_':
if state != NORMAL:
if len(buf) > 0:
tokens.append(buf)
buf = ""
state = NORMAL
buf += c
elif c == ' ' or c == '\t':
if state != WHITESPACE:
if len(buf) > 0:
tokens.append(buf)
buf = ""
state = WHITESPACE
buf += c
else:
if len(buf) > 0:
tokens.append(buf)
buf = ""
tokens.append(c)

if len(buf) > 0:
tokens.append(buf)

return tokens


def validate_expression(param):
for token in split_tokens(param):
state = 0
for c in token:
if c not in ' \t+-*/%()<>&|~x0123456789abcdef':
return False

# the following allows hex digits a-f after 0x but not otherwise
if state == 0:
if c in 'abcdef':
return False
if c == '0':
state = 1
continue

if state == 1:
state = 2 if c == 'x' else 0
continue

if state == 2:
if c not in '0123456789abcdef':
state = 0
return True
2 changes: 1 addition & 1 deletion tests/00_unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

set -e

for file in opcodes assemble link ; do
for file in opcodes assemble link util; do
echo testing $file...
micropython $file.py
done
23 changes: 23 additions & 0 deletions tests/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ def test_assemble_uppercase_opcode():
assert not raised


def test_assemble_evalulate_expressions():
src_w_expr = """\
.set shft, 2
.set loops, (1 << shft)

entry:
move r0, 1+1
move r1, loops
move r2, (shft + 10) * 2
move r3, entry << 2
"""
a = Assembler()
a.assemble(src_w_expr)

assert a.symbols.has_sym('shft')
assert a.symbols.has_sym('loops')
assert a.symbols.has_sym('entry')
assert a.symbols.get_sym('shft') == (ABS, None, 2)
assert a.symbols.get_sym('loops') == (ABS, None, 4)
assert a.symbols.get_sym('entry') == (REL, TEXT, 0)


def test_symbols():
st = SymbolTable({}, {}, {})
for entry in [
Expand Down Expand Up @@ -195,4 +217,5 @@ def test_symbols():
test_assemble_bss_with_value()
test_assemble_global()
test_assemble_uppercase_opcode()
test_assemble_evalulate_expressions()
test_symbols()
54 changes: 53 additions & 1 deletion tests/opcodes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from uctypes import UINT32, BFUINT32, BF_POS, BF_LEN
from esp32_ulp.opcodes import make_ins, make_ins_struct_def
from esp32_ulp.opcodes import get_reg, get_imm, get_cond, arg_qualify, ARG, REG, IMM, COND
from esp32_ulp.opcodes import get_reg, get_imm, get_cond, arg_qualify, eval_arg, ARG, REG, IMM, SYM, COND
from esp32_ulp.assemble import SymbolTable, ABS, REL, TEXT
import esp32_ulp.opcodes as opcodes

OPCODE_DELAY = 4
LAYOUT_DELAY = """
Expand Down Expand Up @@ -43,6 +45,19 @@ def test_arg_qualify():
assert arg_qualify('Eq') == ARG(COND, 'eq', 'Eq')
assert arg_qualify('EQ') == ARG(COND, 'eq', 'EQ')

# for the next tests, ensure the opcodes module has a SymbolTable
opcodes.symbols = SymbolTable({}, {}, {})
opcodes.symbols.set_sym('const', ABS, None, 42) # constant as defined by .set
opcodes.symbols.set_sym('entry', REL, TEXT, 4) # label pointing to code

assert arg_qualify('1+1') == ARG(IMM, 2, '1+1')
assert arg_qualify('const >> 1') == ARG(IMM, 21, 'const >> 1')
assert arg_qualify('entry') == ARG(SYM, (REL, TEXT, 4), 'entry') # symbols should not (yet) be evaluated
assert arg_qualify('entry + const') == ARG(IMM, 46, 'entry + const')

# clean up
opcodes.symbols = None


def test_get_reg():
assert get_reg('r0') == 0
Expand All @@ -57,9 +72,46 @@ def test_get_cond():
assert get_cond('Eq') == 'eq'


def test_eval_arg():
opcodes.symbols = SymbolTable({}, {}, {})
opcodes.symbols.set_sym('const', ABS, None, 42) # constant
opcodes.symbols.set_sym('raise', ABS, None, 99) # constant using a python keyword as name (is allowed)

assert eval_arg('1+1') == 2
assert eval_arg('1+const') == 43
assert eval_arg('raise*2/3') == 66
assert eval_arg('raise-const') == 57
assert eval_arg('(raise-const)*2') == 114
assert eval_arg('const % 5') == 2
assert eval_arg('const + 0x19af') == 0x19af + 42
assert eval_arg('const & ~2') == 40
assert eval_arg('const << 3') == 336
assert eval_arg('const >> 1') == 21
assert eval_arg('(const|4)&0xf') == 0xe

assert_raises(ValueError, eval_arg, 'evil()')
assert_raises(ValueError, eval_arg, 'def cafe()')
assert_raises(ValueError, eval_arg, '1 ^ 2')
assert_raises(ValueError, eval_arg, '!100')

# clean up
opcodes.symbols = None


def assert_raises(exception, func, *args):
try:
func(*args)
except exception:
raised = True
else:
raised = False
assert raised


test_make_ins_struct_def()
test_make_ins()
test_arg_qualify()
test_get_reg()
test_get_imm()
test_get_cond()
test_eval_arg()
62 changes: 62 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from esp32_ulp.util import split_tokens, validate_expression

tests = []


def test(param):
"""
the @test decorator
"""
tests.append(param)


@test
def test_split_tokens():
assert split_tokens("") == []
assert split_tokens("t") == ['t']
assert split_tokens("test") == ['test']
assert split_tokens("t t") == ['t', ' ', 't']
assert split_tokens("t,t") == ['t', ',', 't']
assert split_tokens("test(arg)") == ['test', '(', 'arg', ')']
assert split_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')']
assert split_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')']
assert split_tokens(" test( arg, arg2)") == [' ', 'test', '(', ' ', 'arg', ',', ' ', 'arg2', ')']
assert split_tokens(" test( arg ) ") == [' ', 'test', '(', ' ', 'arg', ' ', ')', ' ']
assert split_tokens("\t test \t ") == ['\t ', 'test', " \t "]
assert split_tokens("test\nrow2") == ['test', "\n", "row2"]

# split_token does not support comments. should generally only be used after comments are already stripped
assert split_tokens("test(arg /*comment*/)") == ['test', '(', 'arg', ' ', '/', '*', 'comment', '*', '/', ')']
assert split_tokens("#test") == ['#', 'test']


@test
def test_validate_expression():
assert validate_expression('') is True
assert validate_expression('1') is True
assert validate_expression('1+1') is True
assert validate_expression('(1+1)') is True
assert validate_expression('(1+1)*2') is True
assert validate_expression('(1 + 1)') is True
assert validate_expression('10 % 2') is True
assert validate_expression('0x100 << 2') is True
assert validate_expression('0x100 & ~2') is True
assert validate_expression('0xabcdef') is True
assert validate_expression('0x123def') is True
assert validate_expression('2*3+4/5&6|7') is True
assert validate_expression('(((((1+1) * 2') is True # valid characters, even if expression is not valid

assert validate_expression(':') is False
assert validate_expression('_') is False
assert validate_expression('=') is False
assert validate_expression('.') is False
assert validate_expression('!') is False
assert validate_expression('123 ^ 4') is False # operator not supported for now
assert validate_expression('evil()') is False
assert validate_expression('def cafe()') is False # valid hex digits, but potentially dangerous code


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