Skip to content

Commit 4bf1e53

Browse files
Add test harness for K functions (#61)
Adds a target `riscv-semantics.func-test` and a corresponding integration test. --------- Co-authored-by: devops <[email protected]>
1 parent 16b2d11 commit 4bf1e53

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ poetry-install:
2323
# Semantics
2424

2525
kdist-build: poetry-install
26-
$(POETRY) run kdist -v build riscv-semantics.llvm riscv-semantics.kllvm riscv-semantics.kllvm-runtime
26+
$(POETRY) run kdist -v build riscv-semantics.*
2727

2828
kdist-clean: poetry-install
2929
$(POETRY) run kdist clean

package/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.47
1+
0.1.48

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "kriscv"
7-
version = "0.1.47"
7+
version = "0.1.48"
88
description = "K tooling for the RISC-V architecture"
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

src/kriscv/kdist/plugin.py

+9
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,17 @@ def context(self) -> dict[str, str]:
7878
'llvm': KompileTarget(
7979
lambda src_dir: {
8080
'main_file': src_dir / 'riscv-semantics/riscv.md',
81+
'include_dirs': [src_dir],
8182
'syntax_module': 'RISCV',
83+
'md_selector': 'k',
84+
'warnings_to_errors': True,
85+
},
86+
),
87+
'func-test': KompileTarget(
88+
lambda src_dir: {
89+
'main_file': src_dir / 'riscv-semantics/func-test.md',
8290
'include_dirs': [src_dir],
91+
'syntax_module': 'FUNC-TEST',
8392
'md_selector': 'k',
8493
'warnings_to_errors': True,
8594
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```k
2+
requires "riscv-disassemble.md"
3+
4+
module FUNC-TEST
5+
imports RISCV-DISASSEMBLE
6+
endmodule
7+
```
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
from pyk.kdist import kdist
7+
from pyk.kore.prelude import INT, SORT_K_ITEM, generated_counter, generated_top, inj, int_dv, k, kseq
8+
from pyk.kore.syntax import App, SortApp
9+
from pyk.ktool.krun import llvm_interpret
10+
11+
if TYPE_CHECKING:
12+
from pathlib import Path
13+
from typing import Final
14+
15+
from pyk.kore.syntax import Pattern
16+
17+
18+
@pytest.fixture(scope='module')
19+
def definition_dir() -> Path:
20+
return kdist.get('riscv-semantics.func-test')
21+
22+
23+
def sw(rs2: int, imm: int, rs1: int) -> App:
24+
return App(
25+
'LblRegImmRegInstr',
26+
(),
27+
(
28+
App('LblSW'),
29+
reg(rs2),
30+
int_dv(imm),
31+
reg(rs1),
32+
),
33+
)
34+
35+
36+
def reg(x: int) -> App:
37+
return inj(INT, SortApp('SortRegister'), int_dv(x))
38+
39+
40+
DISASSEMBLE_TEST_DATA: Final = (
41+
('sw s0,296(sp)', b'\x12\x81\x24\x23', SortApp('SortRegImmRegInstr'), sw(8, 296, 2)),
42+
('sw s1,292(sp)', b'\x12\x91\x22\x23', SortApp('SortRegImmRegInstr'), sw(9, 292, 2)),
43+
)
44+
45+
46+
@pytest.mark.parametrize(
47+
'test_id,code,sort,pattern',
48+
DISASSEMBLE_TEST_DATA,
49+
ids=[test_id for test_id, *_ in DISASSEMBLE_TEST_DATA],
50+
)
51+
def test_disassemble(definition_dir: Path, test_id: str, code: bytes, sort: SortApp, pattern: Pattern) -> None:
52+
# Given
53+
n = int.from_bytes(code, byteorder='big')
54+
init = config(inj(SortApp('SortInstruction'), SORT_K_ITEM, disassemble(n)))
55+
expected = config(inj(sort, SORT_K_ITEM, pattern))
56+
57+
# When
58+
actual = llvm_interpret(definition_dir, init)
59+
60+
# Then
61+
assert actual == expected
62+
63+
64+
def config(kitem: Pattern) -> App:
65+
return generated_top(
66+
(
67+
k(kseq((kitem,))),
68+
generated_counter(int_dv(0)),
69+
),
70+
)
71+
72+
73+
def disassemble(n: int) -> App:
74+
return App('Lbldisassemble', (), (int_dv(n),))

0 commit comments

Comments
 (0)