Skip to content

Commit 8d7981d

Browse files
committed
add support for left aligned assembler directives (e.g. .set)
Much open-source code out there has .global, .set, etc directives starting in the first column of a line. This change allows assembling such code. Incidentally this also fixes a bug, where directives without parameters, such as .text, .data, etc were silently accepted when left-aligned but in those cases treated as labels instead of section headers.
1 parent 5a9b7e9 commit 8d7981d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

esp32_ulp/assemble.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def parse_line(self, line):
118118
"""
119119
if not line:
120120
return
121-
has_label = line[0] not in '\t '
121+
has_label = line[0] not in '\t .'
122122
if has_label:
123123
label_line = line.split(None, 1)
124124
if len(label_line) == 2:

tests/assemble.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
src = """\
66
.set const, 123
7+
.set const_left, 976
78
89
start: wait 42
910
ld r0, r1, 0
1011
st r0, r1,0
1112
halt
1213
end:
14+
.data
1315
"""
1416

1517

@@ -18,12 +20,14 @@ def test_parse_line():
1820
lines = src.splitlines()
1921
# note: line number = index + 1
2022
assert a.parse_line(lines[0]) == (None, '.set', ('const', '123', ))
21-
assert a.parse_line(lines[1]) == None
22-
assert a.parse_line(lines[2]) == ('start', 'wait', ('42', ))
23-
assert a.parse_line(lines[3]) == (None, 'ld', ('r0', 'r1', '0', ))
24-
assert a.parse_line(lines[4]) == (None, 'st', ('r0', 'r1', '0', ))
25-
assert a.parse_line(lines[5]) == (None, 'halt', ())
26-
assert a.parse_line(lines[6]) == ('end', None, ())
23+
assert a.parse_line(lines[1]) == (None, '.set', ('const_left', '976', ))
24+
assert a.parse_line(lines[2]) == None
25+
assert a.parse_line(lines[3]) == ('start', 'wait', ('42', ))
26+
assert a.parse_line(lines[4]) == (None, 'ld', ('r0', 'r1', '0', ))
27+
assert a.parse_line(lines[5]) == (None, 'st', ('r0', 'r1', '0', ))
28+
assert a.parse_line(lines[6]) == (None, 'halt', ())
29+
assert a.parse_line(lines[7]) == ('end', None, ())
30+
assert a.parse_line(lines[8]) == (None, '.data', ()) # test left-aligned directive is not treated as label
2731

2832

2933
def test_parse():
@@ -37,9 +41,11 @@ def test_assemble():
3741
a = Assembler()
3842
a.assemble(src)
3943
assert a.symbols.has_sym('const')
44+
assert a.symbols.has_sym('const_left')
4045
assert a.symbols.has_sym('start')
4146
assert a.symbols.has_sym('end')
4247
assert a.symbols.get_sym('const') == (ABS, None, 123)
48+
assert a.symbols.get_sym('const_left') == (ABS, None, 976)
4349
assert a.symbols.get_sym('start') == (REL, TEXT, 0)
4450
assert a.symbols.get_sym('end') == (REL, TEXT, 4)
4551
assert len(b''.join(a.sections[TEXT])) == 16 # 4 instructions * 4B

tests/compat/symbols.S

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
.text
22

33
.set constant42, 42
4+
.set notindented, 1
45

56
start: move r0, data0
67
move r1, data1
78
move r2, constant42
9+
move r3, notindented
810

911
# count from 0 .. 42 in stage register
1012
stage_rst

0 commit comments

Comments
 (0)