lib/std/arch/rv64/atomics.rad 5.1 KiB raw
1
//! RV64 atomic instruction fields and canonical assembly names.
2
3
use std::mem;
4
use std::lang::gen;
5
6
/// Atomic word-width field value.
7
constant WIDTH_WORD: u32 = super::encode::F3_WORD;
8
/// Atomic doubleword-width field value.
9
constant WIDTH_DWORD: u32 = super::encode::F3_DWORD;
10
/// Atomic acquire ordering bit.
11
constant ORDER_ACQUIRE: u32 = 2;
12
/// Atomic release ordering bit.
13
constant ORDER_RELEASE: u32 = 1;
14
/// Atomic acquire and release ordering bits.
15
constant ORDER_BOTH: u32 = ORDER_ACQUIRE | ORDER_RELEASE;
16
/// Atomic load-reserved operation field value.
17
constant OP_LR: u32 = 2;
18
/// Atomic store-conditional operation field value.
19
constant OP_SC: u32 = 3;
20
21
/// Atomic operation, width, and ordering fields.
22
export record Format: Copy {
23
    /// Five-bit operation code from the A extension.
24
    operation: u32,
25
    /// Memory width encoding: 2 for a word, 3 for a doubleword.
26
    width: u32,
27
    /// Ordering bits: 2 for acquire, 1 for release, 3 for both.
28
    order: u32,
29
}
30
31
/// Decoded atomic instruction operands.
32
export record Instruction: Copy {
33
    /// Operation and ordering fields.
34
    format: Format,
35
    /// Destination register.
36
    rd: gen::Reg,
37
    /// Address register.
38
    rs1: gen::Reg,
39
    /// Source value; zero for load-reserved.
40
    rs2: gen::Reg,
41
}
42
43
/// An architectural operation and its mnemonic stem.
44
record Operation: Copy {
45
    /// Mnemonic without width or ordering suffixes.
46
    name: *[u8],
47
    /// Five-bit instruction field.
48
    code: u32,
49
}
50
51
/// Operations shared by the 32-bit and 64-bit forms.
52
constant OPERATIONS: [Operation; 11] = [
53
    { name: "amoadd",  code: 0 }, { name: "amoswap", code: 1 },
54
    { name: "lr",      code: OP_LR }, { name: "sc", code: OP_SC },
55
    { name: "amoxor",  code: 4 }, { name: "amoor", code: 8 },
56
    { name: "amoand",  code: 12 }, { name: "amomin", code: 16 },
57
    { name: "amomax",  code: 20 }, { name: "amominu", code: 24 },
58
    { name: "amomaxu", code: 28 },
59
];
60
61
/// Look up an operation's canonical mnemonic stem.
62
export fn name(code: u32) -> ?*[u8] {
63
    for operation in &OPERATIONS[..] {
64
        if operation.code == code {
65
            return operation.name;
66
        }
67
    }
68
    return nil;
69
}
70
71
/// Parse an atomic mnemonic with mandatory width and optional ordering suffix.
72
export fn parse(text: *[u8]) -> ?Format {
73
    for operation in &OPERATIONS[..] {
74
        let n = operation.name.len;
75
        if text.len < n + 2 or not mem::eq(&text[.. n], operation.name) or text[n] <> '.' {
76
            continue;
77
        }
78
        let mut width: u32 = WIDTH_WORD;
79
        if text[n + 1] == 'd' {
80
            set width = WIDTH_DWORD;
81
        } else if text[n + 1] <> 'w' {
82
            return nil;
83
        }
84
        let suffix = &text[n + 2 ..];
85
        let mut order: u32 = 0;
86
        if mem::eq(suffix, ".aq") {
87
            set order = ORDER_ACQUIRE;
88
        } else if mem::eq(suffix, ".rl") {
89
            set order = ORDER_RELEASE;
90
        } else if mem::eq(suffix, ".aqrl") {
91
            set order = ORDER_BOTH;
92
        } else if suffix.len <> 0 {
93
            return nil;
94
        }
95
        return Format { operation: operation.code, width, order };
96
    }
97
    return nil;
98
}
99
100
/// Encode a validated atomic operation and register operands.
101
export fn encode(instruction: Instruction) -> u32 {
102
    let format = instruction.format;
103
    assert name(format.operation) <> nil
104
        and (format.width == WIDTH_WORD
105
            or format.width == WIDTH_DWORD)
106
        and format.order <= ORDER_BOTH;
107
    assert format.operation <> OP_LR or instruction.rs2 == super::ZERO;
108
    return super::encode::OP_AMO
109
        | (*instruction.rd as u32 << 7)
110
        | (format.width << 12)
111
        | (*instruction.rs1 as u32 << 15)
112
        | (*instruction.rs2 as u32 << 20)
113
        | (format.order << 25)
114
        | (format.operation << 27);
115
}
116
117
/// Encode `lr.d.aqrl rd, (rs1)`, which loads the doubleword at `rs1` and reserves it.
118
export fn loadReserved(rd: gen::Reg, rs1: gen::Reg) -> u32 {
119
    return encode(Instruction {
120
        format: Format { operation: OP_LR, width: WIDTH_DWORD, order: ORDER_BOTH },
121
        rd,
122
        rs1,
123
        rs2: super::ZERO,
124
    });
125
}
126
127
/// Encode `sc.d.aqrl rd, rs2, (rs1)`, which stores `rs2` at `rs1` if the reservation holds.
128
/// `rd` receives zero on success.
129
export fn storeConditional(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 {
130
    return encode(Instruction {
131
        format: Format { operation: OP_SC, width: WIDTH_DWORD, order: ORDER_BOTH },
132
        rd,
133
        rs1,
134
        rs2,
135
    });
136
}
137
138
/// Decode a supported atomic word and reject reserved width and LR fields.
139
export fn decode(word: u32) -> ?Instruction {
140
    let operation = word >> 27;
141
    let width = (word >> 12) & 7;
142
    let rs2 = super::reg(((word >> 20) & 31) as u8);
143
    if (word & 127) <> super::encode::OP_AMO
144
        or name(operation) == nil
145
        or (width <> WIDTH_WORD and width <> WIDTH_DWORD)
146
    {
147
        return nil;
148
    }
149
    if operation == OP_LR and rs2 <> super::ZERO {
150
        return nil;
151
    }
152
    return Instruction {
153
        format: Format { operation, width, order: (word >> 25) & ORDER_BOTH },
154
        rd: super::reg(((word >> 7) & 31) as u8),
155
        rs1: super::reg(((word >> 15) & 31) as u8),
156
        rs2,
157
    };
158
}