3
3
from esp32_ulp .nocomment import remove_comments
4
4
5
5
src = """\
6
+ .set const, 123
6
7
7
8
start: wait 42
8
9
ld r0, r1, 0
14
15
15
16
def test_parse_line ():
16
17
a = Assembler ()
17
- lines = src .splitlines ()
18
- # note: line number = index + 1
19
- assert a .parse_line (lines [ 0 ] ) == None
20
- assert a .parse_line (lines [ 1 ] ) == ('start' , 'wait' , ('42' , ))
21
- assert a .parse_line (lines [ 2 ] ) == (None , 'ld' , ('r0' , 'r1' , '0' , ))
22
- assert a .parse_line (lines [ 3 ] ) == (None , 'st' , ('r0' , 'r1' , '0' , ))
23
- assert a .parse_line (lines [ 4 ] ) == (None , 'halt' , ())
24
- assert a .parse_line (lines [ 5 ] ) == ('end' , None , ())
18
+ lines = iter ( src .splitlines () )
19
+ assert a . parse_line ( next ( lines )) == ( None , '.set' , ( 'const' , '123' , ))
20
+ assert a .parse_line (next ( lines ) ) == None
21
+ assert a .parse_line (next ( lines ) ) == ('start' , 'wait' , ('42' , ))
22
+ assert a .parse_line (next ( lines ) ) == (None , 'ld' , ('r0' , 'r1' , '0' , ))
23
+ assert a .parse_line (next ( lines ) ) == (None , 'st' , ('r0' , 'r1' , '0' , ))
24
+ assert a .parse_line (next ( lines ) ) == (None , 'halt' , ())
25
+ assert a .parse_line (next ( lines ) ) == ('end' , None , ())
25
26
26
27
27
28
def test_parse ():
@@ -34,8 +35,10 @@ def test_parse():
34
35
def test_assemble ():
35
36
a = Assembler ()
36
37
a .assemble (src )
38
+ assert a .symbols .has_sym ('const' )
37
39
assert a .symbols .has_sym ('start' )
38
40
assert a .symbols .has_sym ('end' )
41
+ assert a .symbols .get_sym ('const' ) == (ABS , None , 123 )
39
42
assert a .symbols .get_sym ('start' ) == (REL , TEXT , 0 )
40
43
assert a .symbols .get_sym ('end' ) == (REL , TEXT , 4 )
41
44
assert len (b'' .join (a .sections [TEXT ])) == 16 # 4 instructions * 4B
@@ -50,6 +53,7 @@ def test_symbols():
50
53
('abs_t4' , ABS , TEXT , 4 ),
51
54
('rel_d4' , REL , DATA , 4 ),
52
55
('abs_d4' , ABS , DATA , 4 ),
56
+ ('const' , ABS , None , 123 ),
53
57
]:
54
58
st .set_sym (* entry )
55
59
# PASS 1 ========================================================
@@ -62,11 +66,13 @@ def test_symbols():
62
66
assert st .resolve_absolute ('abs_d4' ) == 4
63
67
assert st .resolve_absolute ('rel_t4' ) == 4
64
68
assert st .resolve_absolute ('rel_d4' ) == 4
69
+ assert st .resolve_absolute ('const' ) == 123
65
70
st .set_from (TEXT , 8 )
66
71
assert st .resolve_relative ('abs_t4' ) == - 4
67
72
assert st .resolve_relative ('abs_d4' ) == - 4
68
73
assert st .resolve_relative ('rel_t4' ) == - 4
69
74
assert st .resolve_relative ('rel_d4' ) == - 4
75
+ assert st .resolve_absolute ('const' ) == 123
70
76
# PASS 2 ========================================================
71
77
st .set_bases ({TEXT : 100 , DATA : 200 })
72
78
st .set_pass (2 )
@@ -84,11 +90,13 @@ def test_symbols():
84
90
assert st .resolve_absolute ('abs_d4' ) == 4
85
91
assert st .resolve_absolute ('rel_t4' ) == 100 + 4
86
92
assert st .resolve_absolute ('rel_d4' ) == 200 + 4
93
+ assert st .resolve_absolute ('const' ) == 123
87
94
st .set_from (TEXT , 8 )
88
95
assert st .resolve_relative ('abs_t4' ) == 4 - 108
89
96
assert st .resolve_relative ('abs_d4' ) == 4 - 108
90
97
assert st .resolve_relative ('rel_t4' ) == 104 - 108
91
98
assert st .resolve_relative ('rel_d4' ) == 204 - 108
99
+ assert st .resolve_absolute ('const' ) == 123
92
100
93
101
94
102
test_parse_line ()
0 commit comments