lib/std/arch/rv64/tests/atomics.rad 4.9 KiB raw
1
//! RV64 A-extension and fence encoding checks.
2
3
use std::testing;
4
use std::arch::rv64;
5
use std::mem::heap;
6
use std::lang::strings;
7
use rv64::atomics;
8
use rv64::encode;
9
use rv64::decode;
10
use rv64::asm;
11
12
/// Assembler workspace for individual atomic instruction fixtures.
13
static MEMORY: [u8; 65536] = [0; 65536];
14
/// Interned fixture names.
15
unsafe static TABLE: [*[u8]; strings::TABLE_SIZE] = undefined;
16
/// Output storage for one assembled instruction.
17
static WORDS: [u32; 1] = [0; 1];
18
19
/// Assemble `source` with fresh storage.
20
unsafe fn run(source: *[u8]) -> asm::Program throws (asm::Error) {
21
    let mut pool = strings::pool(&mut TABLE[..]);
22
    let arena = heap::over(&mut MEMORY[..]);
23
    let data: *mut [u8] = &mut [];
24
    return try asm::assemble(
25
        asm::scanner::SourceKind::String, source, &mut WORDS[..], data, arena, &mut pool, 0
26
    );
27
}
28
29
/// Exact unordered word encodings for a0, a1, and a2 operands.
30
@test fn encodings() throws (testing::TestError) {
31
    let operations: [u32; 11] = [0, 1, 2, 3, 4, 8, 12, 16, 20, 24, 28];
32
    let words: [u32; 11] = [
33
        0x00c5a52f, 0x08c5a52f, 0x1005a52f, 0x18c5a52f, 0x20c5a52f,
34
        0x40c5a52f, 0x60c5a52f, 0x80c5a52f, 0xa0c5a52f, 0xc0c5a52f, 0xe0c5a52f,
35
    ];
36
    for operation, i in &operations[..] {
37
        for wide in 0 .. 2 {
38
            for order in 0 .. 4 {
39
                let item = atomics::Instruction {
40
                    format: atomics::Format { operation, width: 2 + wide, order },
41
                    rd: rv64::A0,
42
                    rs1: rv64::A1,
43
                    rs2: rv64::ZERO if operation == 2 else rv64::A2,
44
                };
45
                let word = words[i] | (wide << 12) | (order << 25);
46
                try testing::expect(atomics::encode(item) == word);
47
                let decoded = atomics::decode(word) else {
48
                    throw testing::TestError::Failed;
49
                };
50
                try testing::expect(decoded == item);
51
                let case decode::Instr::Atomic(instruction) = decode::decode(word) else {
52
                    throw testing::TestError::Failed;
53
                };
54
                try testing::expect(instruction == item);
55
            }
56
        }
57
    }
58
}
59
60
/// Reject reserved opcodes, widths, LR sources, and malformed suffixes.
61
@test fn invalid() throws (testing::TestError) {
62
    for word in &[0x2805a52f, 0x00c5852f, 0x10c5a52f, 0x00c5a513] {
63
        try testing::expect(atomics::decode(word) == nil);
64
    }
65
    for name in &["lr", "lr.q", "lr.w.aq.rl", "sc.d.rl.aq", "amoswap.d.bad", "amoadd.wextra"] {
66
        try testing::expect(atomics::parse(name) == nil);
67
    }
68
    let format = atomics::parse("amoswap.d.aqrl") else {
69
        throw testing::TestError::Failed;
70
    };
71
    try testing::expect(format.operation == 1 and format.width == 3 and format.order == 3);
72
}
73
74
/// Assemble one instruction and return its exact word.
75
unsafe fn assemble(source: *[u8]) -> u32 throws (testing::TestError) {
76
    let result = try run(source) catch {
77
        throw testing::TestError::Failed;
78
    };
79
    try testing::expect(result.text.len == 1);
80
    return result.text[0];
81
}
82
83
/// Check instruction suffix scanning and assembler operand order.
84
@test unsafe fn assembly() throws (testing::TestError) {
85
    try testing::expect(try assemble("amoadd.d.aqrl %a0 %a2 (%a1);") == 0x06c5b52f);
86
    try testing::expect(try assemble("amoswap.w.aq %a0 %a2 0(%a1);") == 0x0cc5a52f);
87
    try testing::expect(try assemble("lr.d.aq %a0 (%a1);") == 0x1405b52f);
88
    try testing::expect(try assemble("sc.w.rl %a0 %a2 (%a1);") == 0x1ac5a52f);
89
    try testing::expect(try assemble("fence.i;") == 0x0000100f);
90
    try testing::expect(try assemble("fence iorw iorw;") == 0x0ff0000f);
91
    try testing::expect(try assemble("fence r rw;") == 0x0230000f);
92
    try testing::expect(try assemble("fence rw w;") == 0x0310000f);
93
    try testing::expect(try assemble("fence 0 0;") == 0x0000000f);
94
}
95
96
/// Decode fence classes and local instruction synchronization.
97
@test fn fences() throws (testing::TestError) {
98
    try testing::expect(encode::fenceI() == 0x0000100f);
99
    try testing::expect(decode::decode(encode::fenceI()) == decode::Instr::FenceI);
100
    let case decode::Instr::Fence { predecessor, successor } = decode::decode(encode::fenceOrder(
101
        15, 15
102
    )) else {
103
        throw testing::TestError::Failed;
104
    };
105
    try testing::expect(predecessor == 15 and successor == 15);
106
    let case decode::Instr::Unknown { .. } = decode::decode(0x0000200f) else {
107
        throw testing::TestError::Failed;
108
    };
109
}
110
111
/// Reject invalid atomic operands and fence access classes.
112
@test unsafe fn invalidAssembly() throws (testing::TestError) {
113
    for source in &[
114
        "lr.w %a0 %a2 (%a1);", "sc.d %a0 %a2 8(%a1);",
115
        "amoadd.d.aq.aq %a0 %a2 (%a1);", "fence rr rw;", "fence rx rw;",
116
        "fence 1 rw;", "fence.i %a0;",
117
    ] {
118
        let mut rejected = false;
119
        try run(source) catch {
120
            set rejected = true;
121
        };
122
        try testing::expect(rejected);
123
    }
124
}