Skip to content

refactor: use bitflags-based Extensions for RISC-V decoder#40

Merged
ChenMiaoi merged 2 commits intohust-open-atom-club:mainfrom
manchangfengxu:feat/extension
Nov 27, 2025
Merged

refactor: use bitflags-based Extensions for RISC-V decoder#40
ChenMiaoi merged 2 commits intohust-open-atom-club:mainfrom
manchangfengxu:feat/extension

Conversation

@manchangfengxu
Copy link
Contributor

  • Replace the old u32-based extension_masks with a bitflags::bitflags Extensions type in riscv::extensions
  • Update RiscVDecoder, RiscVHandler, and all RISC-V extension is_enabled implementations to use Extensions::contains()
  • Keep the existing RiscVExtensions type in riscv::arch for CLI-level configuration unchange

Comment on lines +38 to +46
pub fn rv32gc() -> Self {
Self::new(
Xlen::X32,
extension_masks::I
| extension_masks::M
| extension_masks::A
| extension_masks::F
| extension_masks::C
| extension_masks::XTHEADCONDMOV,
Extensions::I
| Extensions::M
| Extensions::A
| Extensions::F
| Extensions::C
| Extensions::XTHEADCONDMOV,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard rv32gc instruction set does not include XThead. For the instruction sets defined by the manufacturer, we need to handle them in a separate function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue10 mentions that the final goal is to distinguish at the CLI level, but I'm not quite clear on how to refactor it yet. I need to discuss it with ChenMiao.

 +thead           Enables T-Head instruction sets. (only: RISC-V)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue10 mentions that the final goal is to distinguish at the CLI level, but I'm not quite clear on how to refactor it yet. I need to discuss it with ChenMiao.

 +thead           Enables T-Head instruction sets. (only: RISC-V)

For this, in fact, I think the T-Head instruction is a single path and should come from RISC-V? The T-Head is a complete RISC-V instruction set​ — it should be at the same level as the standard instruction set, rather than being an extension of RISC-V. That’s exactly why I haven’t started working on it yet.

- Replace the old u32-based extension_masks with a bitflags::bitflags Extensions type in riscv::extensions
- Update RiscVDecoder, RiscVHandler, and all RISC-V extension is_enabled implementations to use Extensions::contains()
- Keep the existing RiscVExtensions type in riscv::arch for CLI-level configuration unchange

Signed-off-by: manchangfengxu <manchangfengxu@openatom.club>
Copy link
Collaborator

@ChenMiaoi ChenMiaoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

/// Bitflags representing enabled RISC-V extensions.
#[derive(Clone, Copy)]
pub struct Extensions: u32 {
const G = Self::I.bits() | Self::M.bits() | Self::A.bits() | Self::F.bits() | Self::D.bits();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move the line Extensions::G below the single extension?

const F = 1 << 3;
const D = 1 << 4;
const C = 1 << 5;
const XTHEADCONDMOV = 1 << 6;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XTHEADCONDMOV is too long. Its meaning is T-head conditional move extension. The manufacturer name can be removed. Just call it CMOV to keep the name similar to x86. Then add annotations for explanation.
As a custom extension, this flag should not be placed together with the standard instruction set. Because there is a possibility of conflict among different custom extenstions. So you should define your own Extensions separately for T-Head.

pub(crate) standard: StandardExtensions,
pub(crate) thead: THeadExtensions,
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In extensions module, This struct is called Extensions. So StandardExtensions, this Extensions is redundant. Just call it Standard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will also change THeadExtensions to THead

Comment on lines +41 to +46
/// Enables all available T-Head custom extensions on this configuration.
pub fn set_thead(mut self) -> Self {
self.thead |= THeadExtensions::all();
self
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function just crate a Extansions witch THead instruction set. It did not perform any operation on thead. So rename it to thead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Comment on lines +111 to +116
// RVA - Atomic Instructions
// RVC - Compressed Instructions
// RVD - Double-Precision Floating-Point
// RVF - Single-Precision Floating-Point
// RV32I/RV64I - Base Integer Instruction Set
// RVM - Multiply and Divide Instructions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not remove these comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will delete it

Comment on lines +118 to +119
// XuanTie custom extension modules
// XTheadCondMov - Conditional Move Instructions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is missing context, so remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗


/// XTheadCondMov Conditional Move Extension
pub struct XTheadCondMovExtension {
pub struct CondMovExtension {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's not very good? Do I need to change it back?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to maintain consistency throughout the entire code style. In this module, Extension is redundant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extensions.standard.contains(Standard::A) This is more clear than before.
extensions.thead.contains(THead::CondMovExtension) This code contains two extensions, which are obviously redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CMOV constant is in THead(bitflags).

bitflags! {
    /// Bitflags representing enabled T-Head custom extensions.
    pub struct THead: u32 {
        /// Conditional move extension (XTheadCondMov).
        const CMOV = 1;
    }
}

CondMovExtension is used to implement pub trait InstructionExtension: Sync
and other instruction sets under standard are currently in this form

impl InstructionExtension for RvaExtension{...}

@manchangfengxu
Copy link
Contributor Author

    pub fn rv32gc() -> Self {
        Self {
            standard: Standard::G | Standard::C,
            thead: THead::empty(),
        }
    }

Should it be changed to

    pub fn rv32gc() -> Self {
        Self {
            standard: Standard::all(),
            thead: THead::empty(),
        }
    }

Box::new(RvdExtension::new()),
Box::new(RvcExtension::new()),
Box::new(XTheadCondMovExtension::new()),
Box::new(CondMovExtension::new()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Extension here is also redundant. The Extension needs to be removed. use standard::Rva,Thead::CMov to improve readability.

- Move standard and T-Head extensions into standard/ and thead/ modules
- Update decoder, handler and tests to use the new Extensions API

Signed-off-by: manchangfengxu <manchangfengxu@openatom.club>
@ChenMiaoi ChenMiaoi merged commit 61fb16a into hust-open-atom-club:main Nov 27, 2025
8 checks passed
@manchangfengxu manchangfengxu deleted the feat/extension branch November 27, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants