2025-10-26 21:30:38 +00:00
|
|
|
from copapy import variable
|
2025-10-30 09:55:23 +00:00
|
|
|
from copapy.backend import Write, compile_to_dag, stencil_db_from_package
|
2025-11-03 14:01:42 +00:00
|
|
|
import subprocess
|
2025-10-26 12:21:35 +00:00
|
|
|
import copapy as cp
|
2025-10-26 21:30:38 +00:00
|
|
|
from copapy._binwrite import Command
|
2025-11-03 14:01:42 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
|
# On Windows wsl and qemu-user is required:
|
|
|
|
|
# sudo apt install qemu-user
|
|
|
|
|
qemu_command = ['wsl', 'qemu-aarch64',
|
|
|
|
|
'bin/coparun-aarch64',
|
|
|
|
|
'bin/test-aarch64.copapy',
|
|
|
|
|
'bin/test-aarch64.copapy.bin']
|
|
|
|
|
else:
|
|
|
|
|
qemu_command = ['qemu-aarch64',
|
|
|
|
|
'bin/coparun-aarch64',
|
|
|
|
|
'bin/test-aarch64.copapy',
|
|
|
|
|
'bin/test-aarch64.copapy.bin']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command: list[str]) -> str:
|
|
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False)
|
|
|
|
|
assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
return result.stdout
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_compile() -> None:
|
2025-10-26 21:30:38 +00:00
|
|
|
"""Test compilation of a simple program."""
|
2025-10-26 12:21:35 +00:00
|
|
|
c1 = variable(9.0)
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
#ret = [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4]
|
2025-10-26 21:30:38 +00:00
|
|
|
ret = [c1 // 3.3 + 5]
|
|
|
|
|
#ret = [cp.sqrt(c1)]
|
|
|
|
|
#c2 = cp._math.get_42()
|
|
|
|
|
#ret = [c2]
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret]
|
|
|
|
|
|
2025-10-26 21:30:38 +00:00
|
|
|
dw, vars = compile_to_dag(out, cp.generic_sdb)
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
# run program command
|
2025-10-26 21:30:38 +00:00
|
|
|
dw.write_com(Command.RUN_PROG)
|
|
|
|
|
|
|
|
|
|
# read first 32 byte
|
|
|
|
|
dw.write_com(Command.READ_DATA)
|
|
|
|
|
dw.write_int(0)
|
|
|
|
|
dw.write_int(32)
|
2025-10-12 20:22:30 +00:00
|
|
|
|
2025-10-26 21:30:38 +00:00
|
|
|
# read variables
|
|
|
|
|
for addr, lengths, _ in vars.values():
|
|
|
|
|
dw.write_com(Command.READ_DATA)
|
|
|
|
|
dw.write_int(addr)
|
|
|
|
|
dw.write_int(lengths)
|
2025-10-12 20:22:30 +00:00
|
|
|
|
2025-10-26 21:30:38 +00:00
|
|
|
dw.write_com(Command.END_COM)
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
print('* Data to runner:')
|
2025-10-26 21:30:38 +00:00
|
|
|
dw.print()
|
2025-10-12 20:22:30 +00:00
|
|
|
|
2025-10-26 21:30:38 +00:00
|
|
|
dw.to_file('bin/test.copapy')
|
2025-10-12 20:22:30 +00:00
|
|
|
|
|
|
|
|
|
2025-10-30 09:55:23 +00:00
|
|
|
def test_compile_aarch64() -> None:
|
|
|
|
|
"""Test compilation of a simple program."""
|
|
|
|
|
c1 = variable(9.0)
|
|
|
|
|
|
|
|
|
|
#ret = [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4]
|
2025-11-03 14:01:42 +00:00
|
|
|
#ret = [cp.sin(c1), cp.sqrt(c1) + 5]
|
|
|
|
|
ret = [cp.sqrt(c1)]
|
2025-10-30 09:55:23 +00:00
|
|
|
#c2 = cp._math.get_42()
|
|
|
|
|
#ret = [c2]
|
|
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret]
|
|
|
|
|
|
|
|
|
|
sdb = stencil_db_from_package('aarch64')
|
|
|
|
|
dw, vars = compile_to_dag(out, sdb)
|
|
|
|
|
|
|
|
|
|
# run program command
|
2025-11-03 14:01:42 +00:00
|
|
|
#dw.write_com(Command.RUN_PROG)
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
# read first 32 byte
|
2025-11-03 14:01:42 +00:00
|
|
|
#dw.write_com(Command.READ_DATA)
|
|
|
|
|
#dw.write_int(0)
|
|
|
|
|
#dw.write_int(32)
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
# read variables
|
2025-11-03 14:01:42 +00:00
|
|
|
#for addr, lengths, _ in vars.values():
|
|
|
|
|
# dw.write_com(Command.READ_DATA)
|
|
|
|
|
# dw.write_int(addr)
|
|
|
|
|
# dw.write_int(lengths)
|
2025-10-30 09:55:23 +00:00
|
|
|
|
2025-11-03 14:01:42 +00:00
|
|
|
#dw.write_com(Command.END_COM)
|
|
|
|
|
dw.write_com(Command.DUMP_CODE)
|
2025-10-30 09:55:23 +00:00
|
|
|
|
|
|
|
|
print('* Data to runner:')
|
|
|
|
|
dw.print()
|
|
|
|
|
|
|
|
|
|
dw.to_file('bin/test-aarch64.copapy')
|
|
|
|
|
|
2025-11-03 14:01:42 +00:00
|
|
|
result = run_command(qemu_command)
|
|
|
|
|
print('* Output from runner:\n--')
|
|
|
|
|
print(result)
|
|
|
|
|
print('--')
|
|
|
|
|
|
|
|
|
|
assert 'DUMP_CODE' in result
|
|
|
|
|
|
2025-10-30 09:55:23 +00:00
|
|
|
|
2025-10-12 20:22:30 +00:00
|
|
|
if __name__ == "__main__":
|
2025-11-03 14:01:42 +00:00
|
|
|
#test_compile()
|
|
|
|
|
test_compile_aarch64()
|