Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibDisassembly: RISC-V disassembly 4-2/4-5: Disassemble extensions FD (floating-point), A (atomic) and Zicsr (CSRs) #25836

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kleinesfilmroellchen
Copy link
Collaborator

This second PR in the collection deals with all the remaining non-priviledged extensions we need right now. I encourage anyone with three years of free time to tackle V :3

LibDisassembly: Add RISC-V Zicsr extension decoding

Note that CSR pretty-printing is not yet implemented, since we aim to establish shared CSR utilities in AK in the near future.

LibDisassembly: Add RISC-V FD extension decoding

Again, no point in splitting these two since the orthagonal instruction set makes supporting both in common abstractions convenient.

LibDisassembly: Add RISC-V A extension decoding

Note that CSR pretty-printing is not yet implemented, since we aim to
establish shared CSR utilities in AK in the near future.
Again, no point in splitting these two since the orthagonal instruction
set makes supporting both in common abstractions convenient.
@github-actions github-actions bot added the 👀 pr-needs-review PR needs review from a maintainer or community member label Mar 25, 2025
Copy link
Contributor

@Hendiadyoin1 Hendiadyoin1 left a comment

Choose a reason for hiding this comment

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

My ketchup, you already have the mustard
(Also not sure why I reviewed the commits in reverse)

auto is_sc = (raw_parts.funct7 & (1 << 2)) > 0;
return adopt_own(*new (nothrow) LoadReserveStoreConditional(is_sc ? LoadReserveStoreConditional::Operation::StoreConditional : LoadReserveStoreConditional::Operation::LoadReserve, is_acquire, is_release, width, raw_parts.rs1, raw_parts.rs2, raw_parts.rd));
}
case 0b00001:
Copy link
Contributor

Choose a reason for hiding this comment

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

I am once again asking you to make this the enum values you are eventually/essentially assigning, so this becomes a cast-or-rejection pattern
(Seems like this did not make the cut for the the arithmetic instructions in G?)


auto operation = FloatArithmeticInstruction::Operation::Add;
switch (raw_parts.funct7 & ~0b11) {
case 0b0000000:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as in A

ErrorOr<void> format(FormatBuilder& builder, Disassembly::RISCV64::CSRInstruction::Operation op)
{
auto op_name = ""sv;
switch (op) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe sprinkle some using/using enums in the formatters
The namespace lookup chains assume alarming proportions

auto is_immediate = (raw_parts.funct3 & 0b100) > 0;
auto operation = CSRInstruction::Operation::ReadWrite;

switch (raw_parts.funct3 & 0b11) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we add

case 0b00:
    VERIFY_NOT_REACHED();

here (just to be safe)? SYSTEM instructions with funct3 == 0 shouldn't be CSR instructions afaict.

@@ -172,6 +172,26 @@ String InstructionFetchFence::mnemonic() const
return "fence.i"_string;
}

String CSRImmediateInstruction::to_string(DisplayStyle display_style, u32, Optional<SymbolProvider const&>) const
{
return MUST(String::formatted("{:10} {}, {}, {:d}", mnemonic(), format_register(destination_register(), display_style), immediate(), csr()));
Copy link
Member

Choose a reason for hiding this comment

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

CSR immediate instructions are written as csrrwi rd, csr, uimm.
Also, CSR addresses are typically formatted as hex.


String CSRRegisterInstruction::to_string(DisplayStyle display_style, u32, Optional<SymbolProvider const&>) const
{
return MUST(String::formatted("{:10} {}, {}, {:d}", mnemonic(), format_register(destination_register(), display_style), format_register(source_register(), display_style), csr()));
Copy link
Member

Choose a reason for hiding this comment

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

CSR register instructions are written as csrrw rd, csr, rs.
Also, CSR addresses are typically formatted as hex.

@@ -147,6 +271,16 @@ String MemoryLoad::mnemonic() const
return MUST(String::formatted("l{}", m_width));
}

String FloatMemoryLoad::to_string(DisplayStyle display_style, u32, Optional<SymbolProvider const&>) const
{
return MUST(String::formatted("fl{:8} {}, {:#03x}({})", memory_width(), format_register(destination_register(), display_style), immediate(), format_register(base(), display_style)));
Copy link
Member

Choose a reason for hiding this comment

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

You use mnemonic() in the format string everywhere except here and in FloatMemoryStore::to_string.

RoundingMode m_rounding_mode;
};

class FloatRTypeInstruction : public FloatComputationInstruction {
Copy link
Member

Choose a reason for hiding this comment

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

Why do you not derive from this class in some of the R-Type F/D instruction implementations?

Copy link
Member

@spholz spholz Mar 29, 2025

Choose a reason for hiding this comment

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

Tbh I think the inheritance hierarchy is a bit overengineered in general.
(not a blocker though)

@@ -352,4 +352,25 @@ String Fence::mnemonic() const
VERIFY_NOT_REACHED();
}

String AtomicMemoryOperation::to_string(DisplayStyle display_style, u32, Optional<SymbolProvider const&>) const
{
return MUST(String::formatted("{:10} {}, {}, {}", mnemonic(), format_register(destination_register(), display_style), format_register(source_register_1(), display_style), format_register(source_register_2(), display_style)));
Copy link
Member

Choose a reason for hiding this comment

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

amo{} rd, rs2, (rs1) is the correct syntax, not amo{} rd, rs1, rs2.


String LoadReserveStoreConditional::to_string(DisplayStyle display_style, u32, Optional<SymbolProvider const&>) const
{
return MUST(String::formatted("{:10} {}, {} ({})", mnemonic(), format_register(destination_register(), display_style), format_register(source_register_2(), display_style), format_register(source_register_1(), display_style)));
Copy link
Member

Choose a reason for hiding this comment

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

lr only has one source operand: rs1 as the address.


String AtomicMemoryOperation::mnemonic() const
{
return MUST(String::formatted("amo{}.{}{}{}", m_operation, MemoryAccessMode { width(), Signedness::Signed }, is_acquire() ? ".aq"sv : ""sv, is_release() ? ".rl"sv : ""sv));
Copy link
Member

Choose a reason for hiding this comment

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

.aqrl is the correct syntax for both acquire and release.

String LoadReserveStoreConditional::mnemonic() const
{
auto operation = m_operation == Operation::LoadReserve ? "lr"sv : "sc"sv;
return MUST(String::formatted("{}.{}{}{}", operation, MemoryAccessMode { width(), Signedness::Signed }, is_acquire() ? ".aq"sv : ""sv, is_release() ? ".rl"sv : ""sv));
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Comment on lines +37 to +42
class LoadReserveStoreConditional : public AtomicOperation {
public:
enum class Operation : bool {
LoadReserve,
StoreConditional,
};
Copy link
Member

Choose a reason for hiding this comment

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

s/LoadReserve/LoadReserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 pr-needs-review PR needs review from a maintainer or community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants