Skip to content

Commit fb52b21

Browse files
committed
iss-testing: Add Zicsr trap test
1 parent a975589 commit fb52b21

File tree

6 files changed

+144
-7
lines changed

6 files changed

+144
-7
lines changed

vadl/main/vadl/ast/BehaviorLowering.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ private ExpressionNode fetch(Expr expr) {
335335
var result = expr.accept(this);
336336
result.setSourceLocationIfNotSet(expr.location());
337337
expressionCache.put(expr, result);
338+
result.ensure(!(result.type() instanceof ConstantType),
339+
"Constant types must not exist in the VIAM");
338340
return result;
339341
}
340342

vadl/test/vadl/iss/AsmTestBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public BigInteger fillReg(String reg, int size) {
6161
);
6262
}
6363

64-
public void addLabel(String label) {
64+
public AsmTestBuilder addLabel(String label) {
6565
add("%s:", label);
66+
return this;
6667
}
6768

6869
@FormatMethod

vadl/test/vadl/iss/IssInstrTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.util.Collection;
24+
import java.util.Comparator;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.function.Function;
@@ -61,6 +62,12 @@ public Map<String, String> toMap() {
6162

6263
public abstract Tool compiler();
6364

65+
66+
protected final Stream<DynamicTest> runTest(
67+
IssTestUtils.TestCase test) throws IOException {
68+
return runTestsWith(1, (i) -> test);
69+
}
70+
6471
@SafeVarargs
6572
protected final Stream<DynamicTest> runTestsWith(
6673
Function<Integer, IssTestUtils.TestCase>... generators) throws IOException {
@@ -148,7 +155,13 @@ protected Stream<DynamicTest> runQemuInstrTests(ImageFromDockerfile image,
148155
System.out.println("Test " + e.id());
149156
System.out.println("ASM: \n" + testSpec.asmCore());
150157
System.out.println("\nRan stages: " + e.completedStages());
151-
System.out.println("Register tests: \n" + e.regTests());
158+
System.out.println("Register tests: ");
159+
e.regTests().stream()
160+
.sorted(Comparator.comparing(IssTestUtils.TestResult.RegTestResult::reg))
161+
.forEachOrdered(r -> {
162+
System.out.println(
163+
"- " + r.reg() + " exp: " + r.expected() + " act: " + r.actual());
164+
});
152165
System.out.println("Duration: " + e.duration());
153166

154167
if (!success) {

vadl/test/vadl/iss/QemuIssTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
import java.io.IOException;
2020
import java.nio.file.Path;
21+
import java.util.Arrays;
2122
import java.util.Map;
2223
import java.util.concurrent.ConcurrentHashMap;
24+
import java.util.stream.Collectors;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527
import org.testcontainers.images.builder.ImageFromDockerfile;
26-
import org.testcontainers.shaded.org.checkerframework.checker.nullness.qual.Nullable;
2728
import vadl.DockerExecutionTest;
2829
import vadl.configuration.IssConfiguration;
2930
import vadl.pass.PassOrders;
@@ -90,7 +91,7 @@ private ImageFromDockerfile generateSimulator(Map<String, ImageFromDockerfile> c
9091
}
9192

9293
// generate iss image from the output path
93-
return getIssImage(issOutputPath, configuration, "riscv64-softmmu");
94+
return getIssImage(issOutputPath, configuration, "riscv64-softmmu", "riscv32-softmmu");
9495
} catch (IOException | DuplicatedPassKeyException e) {
9596
throw new RuntimeException(e);
9697
}
@@ -102,13 +103,13 @@ private ImageFromDockerfile generateSimulator(Map<String, ImageFromDockerfile> c
102103
* This will produce a new image for the given generated iss sources.
103104
*
104105
* @param generatedIssSources the path to the generated ISS/QEMU sources.
105-
* @param referenceTarget The reference target that should also be compiled
106+
* @param referenceTargets The reference targets that should also be build
106107
* (e.g. riscv64-softmmu)
107108
* @return a new image that builds the ISS at build time.
108109
*/
109110
private ImageFromDockerfile getIssImage(Path generatedIssSources,
110111
IssConfiguration configuration,
111-
@Nullable String referenceTarget
112+
String... referenceTargets
112113
) {
113114

114115
// get redis cache for faster compilation using sccache
@@ -117,7 +118,8 @@ private ImageFromDockerfile getIssImage(Path generatedIssSources,
117118
var targetName = configuration.targetName().toLowerCase();
118119
var softmmuTarget = targetName + "-softmmu";
119120
var qemuBin = "qemu-system-" + targetName;
120-
var refTarget = referenceTarget == null ? "" : "," + referenceTarget;
121+
var refTargetString = Arrays.stream(referenceTargets).collect(Collectors.joining(","));
122+
var refTarget = refTargetString.isEmpty() ? "" : "," + refTargetString;
121123

122124
var dockerImage = new ImageFromDockerfile()
123125
.withDockerfileFromBuilder(d -> {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText : © 2025 TU Wien <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.iss.riscv;
18+
19+
import java.util.Map;
20+
import vadl.iss.IssInstrTest;
21+
22+
public abstract class AbstractIssRiscv32InstrTest extends IssInstrTest {
23+
24+
25+
@Override
26+
public Map<String, String> gdbRegMap() {
27+
return AbstractIssRiscv64InstrTest.GDB_REF_MAP;
28+
}
29+
30+
@Override
31+
public Tool simulator() {
32+
return new Tool("/qemu/build/qemu-system-rv32zicsr", "-bios");
33+
}
34+
35+
@Override
36+
public Tool reference() {
37+
return new Tool("/qemu/build/qemu-system-riscv32", "-M spike -bios");
38+
}
39+
40+
@Override
41+
public Tool compiler() {
42+
return new Tool("/scripts/compilers/riscv_compiler.py", "-march=rv32imzicsr -mabi=ilp32");
43+
}
44+
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText : © 2025 TU Wien <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.iss.riscv;
18+
19+
import java.io.IOException;
20+
import java.util.stream.Stream;
21+
import org.junit.jupiter.api.DynamicTest;
22+
import org.junit.jupiter.api.TestFactory;
23+
import vadl.iss.AsmTestBuilder;
24+
import vadl.iss.IssTestUtils;
25+
26+
public class IssRV32ZicsrInstrTest extends AbstractIssRiscv32InstrTest {
27+
private static final String VADL_SPEC = "sys/risc-v/rv32csr.vadl";
28+
private static final int TESTS_PER_INSTRUCTION = 50;
29+
30+
@Override
31+
public int getTestPerInstruction() {
32+
return TESTS_PER_INSTRUCTION;
33+
}
34+
35+
@Override
36+
public String getVadlSpec() {
37+
return VADL_SPEC;
38+
}
39+
40+
@Override
41+
public AsmTestBuilder getBuilder(String testNamePrefix, int id) {
42+
return new RV64IMTestBuilder(testNamePrefix + "_" + id);
43+
}
44+
45+
@TestFactory
46+
Stream<DynamicTest> testSimpleTrap() throws IOException {
47+
var asmCore = """
48+
la t0, handler
49+
csrw mtvec, t0
50+
51+
li a2, 1
52+
li a0, 1
53+
li a1, 1
54+
ebreak
55+
li a0, 2
56+
j exit
57+
58+
handler:
59+
csrr t1, mepc
60+
addi t1, t1, 4
61+
csrw mepc, t1
62+
addi t2, x0, 5
63+
csrr a3, mcause
64+
mret
65+
66+
li a2, 2 # should not reach this
67+
68+
exit:
69+
""";
70+
return runTest(new IssTestUtils.TestCase("Simple Trap", asmCore));
71+
}
72+
73+
74+
}

0 commit comments

Comments
 (0)