summaryrefslogtreecommitdiff
path: root/src/decode.rs
blob: 6b274a4c45d168c1816a9746cfb52ef8102d8c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2025 taitep
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of TRVE (https://git.taitep.se/trve.git)
// See LICENSE file in the project root for full license text.

use std::sync::atomic::Ordering;

const MASK_REGISTER: u32 = 0x1f;

#[derive(Clone, Copy)]
pub struct Instruction(pub u32);

impl std::fmt::Debug for Instruction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:08x}", self.0))
    }
}

#[allow(dead_code)]
impl Instruction {
    #[inline]
    pub fn opcode(self) -> u8 {
        (self.0 & 0x7f) as u8
    }

    /// Returns the opcode of the instruction, with the last 2 bits stripped away, as they are always 0b11 in a non-compressed instruction
    #[inline]
    pub fn opcode_noncompressed(self) -> u8 {
        debug_assert_eq!(self.0 & 0b11, 0b11, "Compressed (or invalid) opcode");
        (self.0 >> 2 & 0x1f) as u8
    }

    #[inline]
    pub fn rd(self) -> u8 {
        (self.0 >> 7 & MASK_REGISTER) as u8
    }

    #[inline]
    pub fn funct3(self) -> u8 {
        (self.0 >> 12 & 0x7) as u8
    }

    #[inline]
    pub fn rs1(self) -> u8 {
        (self.0 >> 15 & MASK_REGISTER) as u8
    }

    #[inline]
    pub fn rs2(self) -> u8 {
        (self.0 >> 20 & MASK_REGISTER) as u8
    }

    #[inline]
    pub fn funct7(self) -> u8 {
        (self.0 >> 25) as u8
    }

    #[inline]
    pub fn funct5(self) -> u8 {
        (self.0 >> 27) as u8
    }

    #[inline]
    pub fn imm_i(self) -> u64 {
        (self.0 as i32 as i64 >> 20) as u64
    }

    #[inline]
    pub fn imm_s(self) -> u64 {
        let imm_11_5 = (self.0 as i32 as i64 >> 25 << 5) as u64;
        let imm_4_0 = (self.0 >> 7 & 0x1f) as u64;
        imm_11_5 | imm_4_0
    }

    #[inline]
    pub fn imm_b(self) -> u64 {
        let imm_12 = ((self.0 & 0x8000_0000) as i32 as i64 >> (31 - 12)) as u64;
        let imm_10_5 = ((self.0 >> 25 & 0x3f) << 5) as u64;
        let imm_4_1 = ((self.0 >> 8 & 0xf) << 1) as u64;
        let imm_11 = ((self.0 >> 7 & 1) << 11) as u64;

        imm_12 | imm_10_5 | imm_4_1 | imm_11
    }

    #[inline]
    pub fn imm_u(self) -> u64 {
        (self.0 & 0xffff_f000) as i32 as i64 as u64
    }

    #[inline]
    pub fn imm_j(self) -> u64 {
        let imm_20 = ((self.0 & 0x8000_0000) as i32 as i64 >> (31 - 20)) as u64;
        let imm_10_1 = ((self.0 >> 21 & 0x3ff) << 1) as u64;
        let imm_11 = ((self.0 >> 20 & 1) << 11) as u64;
        let imm_19_12 = ((self.0 >> 12 & 0xff) << 12) as u64;

        imm_20 | imm_10_1 | imm_11 | imm_19_12
    }

    /// Technically part of immediate. Only used to determine shift type for immediate shifts afaik
    /// 32bit ones use funct7 in this way
    #[inline]
    pub fn funct6(self) -> u8 {
        (self.0 >> 26) as u8
    }

    /// Mostly/only used for the SYSTEM opcode
    #[inline]
    pub fn funct12(self) -> u16 {
        (self.0 >> 20) as u16
    }

    /// Looks at the aq/rl bits of atomic instructions and converts to an Ordering
    #[inline]
    pub fn amo_ordering(self) -> Ordering {
        let aq = self.0 >> 26 & 1 != 0;
        let rl = self.0 >> 25 & 1 != 0;

        match (aq, rl) {
            (false, false) => Ordering::Relaxed,
            (false, true) => Ordering::Release,
            (true, false) => Ordering::Acquire,
            (true, true) => Ordering::AcqRel,
        }
    }
}