Skip to content

Commit 2260fe0

Browse files
committed
tests/micropython/viper_ptr: Add tests for arch edge cases.
This commit adds a series of test cases to exercise the Viper code generator load/store emitting capabilities on certain boundary conditions. The new test cases check whether the emitted load/store code performs correctly when dealing with specific memory offsets, which trigger specific code generation sequences on different architectures. Right now the cases are for unsigned offsets whose bitmasks span up to 5, 8, and 12 bits (respectively Arm/Thumb, Xtensa, RV32). Signed-off-by: Alessandro Gatti <[email protected]>
1 parent e66a602 commit 2260fe0

12 files changed

+265
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Test boundary conditions for various architectures
2+
3+
GET_TEMPLATE = """
4+
@micropython.viper
5+
def get{off}(src: ptr16) -> int:
6+
return src[{off}]
7+
print(b[{off} * 2:({off} + 1) * 2])
8+
"""
9+
10+
11+
@micropython.viper
12+
def get_index(src: ptr16, i: int) -> int:
13+
return src[i]
14+
15+
16+
b = bytearray(5000)
17+
b[28:38] = b"0123456789"
18+
b[252:262] = b"ABCDEFGHIJ"
19+
b[4092:4102] = b"KLMNOPQRST"
20+
21+
for pre, idx, post in (15, 16, 17), (127, 128, 129), (2047, 2048, 2049):
22+
print(get_index(b, pre), get_index(b, idx), get_index(b, post))
23+
exec(GET_TEMPLATE.format(off=pre))
24+
exec(GET_TEMPLATE.format(off=idx))
25+
exec(GET_TEMPLATE.format(off=post))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
13106 13620 14134
2+
bytearray(b'23')
3+
bytearray(b'45')
4+
bytearray(b'67')
5+
17475 17989 18503
6+
bytearray(b'CD')
7+
bytearray(b'EF')
8+
bytearray(b'GH')
9+
20045 20559 21073
10+
bytearray(b'MN')
11+
bytearray(b'OP')
12+
bytearray(b'QR')
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Test boundary conditions for various architectures
2+
3+
SET_TEMPLATE = """
4+
@micropython.viper
5+
def set{off}(dest: ptr16):
6+
dest[{off}] = {val}
7+
set{off}(b)
8+
print(b[{off} * 2:({off} + 1) * 2])
9+
"""
10+
11+
TEST_DATA = (
12+
(15, (0x4241, 0x4443, 0x4645)),
13+
(127, (0x4847, 0x4A49, 0x4C4B)),
14+
(2047, (0x4E4D, 0x504F, 0x5251)),
15+
)
16+
17+
18+
@micropython.viper
19+
def set_index(dest: ptr16, i: int, val: int):
20+
dest[i] = val
21+
22+
23+
@micropython.viper
24+
def set_index(dest: ptr16, i: int, val: int):
25+
dest[i] = val
26+
27+
28+
b = bytearray(5000)
29+
for start, vals in TEST_DATA:
30+
for i, v in enumerate(vals):
31+
set_index(b, start + i, v)
32+
print(b[(start + i) * 2 : (start + i + 1) * 2])
33+
34+
35+
for i in range(len(b)):
36+
b[i] = 0
37+
38+
39+
for start, vals in TEST_DATA:
40+
for i, v in enumerate(vals):
41+
exec(SET_TEMPLATE.format(off=start + i, val=v + 0x0101))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
bytearray(b'AB')
2+
bytearray(b'CD')
3+
bytearray(b'EF')
4+
bytearray(b'GH')
5+
bytearray(b'IJ')
6+
bytearray(b'KL')
7+
bytearray(b'MN')
8+
bytearray(b'OP')
9+
bytearray(b'QR')
10+
bytearray(b'BC')
11+
bytearray(b'DE')
12+
bytearray(b'FG')
13+
bytearray(b'HI')
14+
bytearray(b'JK')
15+
bytearray(b'LM')
16+
bytearray(b'NO')
17+
bytearray(b'PQ')
18+
bytearray(b'RS')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Test boundary conditions for various architectures
2+
3+
GET_TEMPLATE = """
4+
@micropython.viper
5+
def get{off}(src: ptr32) -> int:
6+
return src[{off}]
7+
print(b[{off} * 4:({off} + 1) * 4])
8+
"""
9+
10+
11+
@micropython.viper
12+
def get_index(src: ptr32, i: int) -> int:
13+
return src[i]
14+
15+
16+
b = bytearray(5000)
17+
b[24:43] = b"0123456789ABCDEFGHIJ"
18+
b[248:268] = b"KLMNOPQRSTUVWXYZabcd"
19+
b[4088:4108] = b"efghijklmnopqrstuvwx"
20+
21+
for pre, idx, post in (7, 8, 9), (63, 64, 65), (1023, 1024, 1025):
22+
print(get_index(b, pre), get_index(b, idx), get_index(b, post))
23+
exec(GET_TEMPLATE.format(off=pre))
24+
exec(GET_TEMPLATE.format(off=idx))
25+
exec(GET_TEMPLATE.format(off=post))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
926299444 1111570744 1178944579
2+
bytearray(b'4567')
3+
bytearray(b'89AB')
4+
bytearray(b'CDEF')
5+
1381060687 1448432723 1515804759
6+
bytearray(b'OPQR')
7+
bytearray(b'STUV')
8+
bytearray(b'WXYZ')
9+
1818978921 1886350957 1953722993
10+
bytearray(b'ijkl')
11+
bytearray(b'mnop')
12+
bytearray(b'qrst')
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Test boundary conditions for various architectures
2+
3+
TEST_DATA = (
4+
(3, (0x04030201, 0x08070605, 0x0C0B0A09)),
5+
(63, (0x100F0E0D, 0x14131211, 0x18171615)),
6+
(1023, (0x1C1B1A19, 0x201F1E1D, 0x24232221)),
7+
)
8+
9+
SET_TEMPLATE = """
10+
@micropython.viper
11+
def set{off}(dest: ptr32):
12+
dest[{off}] = {val} & 0x3FFFFFFF
13+
set{off}(b)
14+
print(b[{off} * 4:({off} + 1) * 4])
15+
"""
16+
17+
18+
@micropython.viper
19+
def set_index(dest: ptr32, i: int, val: int):
20+
dest[i] = val
21+
22+
23+
b = bytearray(5000)
24+
for start, vals in TEST_DATA:
25+
for i, v in enumerate(vals):
26+
set_index(b, start + i, v)
27+
print(b[(start + i) * 4 : (start + i + 1) * 4])
28+
29+
for i in range(len(b)):
30+
b[i] = 0
31+
32+
33+
for start, vals in TEST_DATA:
34+
for i, v in enumerate(vals):
35+
exec(SET_TEMPLATE.format(off=start + i, val=v + 0x01010101))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
bytearray(b'\x01\x02\x03\x04')
2+
bytearray(b'\x05\x06\x07\x08')
3+
bytearray(b'\t\n\x0b\x0c')
4+
bytearray(b'\r\x0e\x0f\x10')
5+
bytearray(b'\x11\x12\x13\x14')
6+
bytearray(b'\x15\x16\x17\x18')
7+
bytearray(b'\x19\x1a\x1b\x1c')
8+
bytearray(b'\x1d\x1e\x1f ')
9+
bytearray(b'!"#$')
10+
bytearray(b'\x02\x03\x04\x05')
11+
bytearray(b'\x06\x07\x08\t')
12+
bytearray(b'\n\x0b\x0c\r')
13+
bytearray(b'\x0e\x0f\x10\x11')
14+
bytearray(b'\x12\x13\x14\x15')
15+
bytearray(b'\x16\x17\x18\x19')
16+
bytearray(b'\x1a\x1b\x1c\x1d')
17+
bytearray(b'\x1e\x1f !')
18+
bytearray(b'"#$%')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Test boundary conditions for various architectures
2+
3+
GET_TEMPLATE = """
4+
@micropython.viper
5+
def get{off}(src: ptr8) -> int:
6+
return src[{off}]
7+
print(get{off}(b))
8+
"""
9+
10+
11+
@micropython.viper
12+
def get_index(src: ptr8, i: int) -> int:
13+
return src[i]
14+
15+
16+
b = bytearray(5000)
17+
b[30:32] = b"123"
18+
b[254:256] = b"456"
19+
b[4094:4096] = b"789"
20+
21+
for pre, idx, post in (30, 31, 32), (254, 255, 256), (4094, 4095, 4096):
22+
print(get_index(b, pre), get_index(b, idx), get_index(b, post))
23+
exec(GET_TEMPLATE.format(off=pre))
24+
exec(GET_TEMPLATE.format(off=idx))
25+
exec(GET_TEMPLATE.format(off=post))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
49 50 51
2+
49
3+
50
4+
51
5+
52 53 54
6+
52
7+
53
8+
54
9+
55 56 57
10+
55
11+
56
12+
57
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Test boundary conditions for various architectures
2+
3+
TEST_DATA = ((49, 30, 3), (52, 254, 3), (55, 4094, 3))
4+
5+
SET_TEMPLATE = """
6+
@micropython.viper
7+
def set{off}(dest: ptr8):
8+
dest[{off}] = {val}
9+
set{off}(b)
10+
print(b[{off}])
11+
"""
12+
13+
14+
@micropython.viper
15+
def set_index(dest: ptr8, i: int, val: int):
16+
dest[i] = val
17+
18+
19+
b = bytearray(5000)
20+
for val, start, count in TEST_DATA:
21+
for i in range(count):
22+
set_index(b, start + i, val + i)
23+
print(b[start : start + count])
24+
25+
for i in range(len(b)):
26+
b[i] = 0
27+
28+
for val, start, count in TEST_DATA:
29+
for i in range(count):
30+
exec(SET_TEMPLATE.format(off=start + i, val=val + i + 16))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
bytearray(b'123')
2+
bytearray(b'456')
3+
bytearray(b'789')
4+
65
5+
66
6+
67
7+
68
8+
69
9+
70
10+
71
11+
72
12+
73

0 commit comments

Comments
 (0)