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

ckb-debugger support decode instruction #94

Merged
merged 5 commits into from
Jul 19, 2023

Conversation

joii2020
Copy link
Contributor

No description provided.

Comment on lines 59 to 73
OP_SUB => Rtype(self.instruction).to_string(),
OP_SUBW => Rtype(self.instruction).to_string(),
OP_ADD => Rtype(self.instruction).to_string(),
OP_ADDW => Rtype(self.instruction).to_string(),
OP_XOR => Rtype(self.instruction).to_string(),
OP_OR => Rtype(self.instruction).to_string(),
OP_AND => Rtype(self.instruction).to_string(),
OP_SLL => Rtype(self.instruction).to_string(),
OP_SLLW => Rtype(self.instruction).to_string(),
OP_SRL => Rtype(self.instruction).to_string(),
OP_SRLW => Rtype(self.instruction).to_string(),
OP_SRA => Rtype(self.instruction).to_string(),
OP_SRAW => Rtype(self.instruction).to_string(),
OP_SLT => Rtype(self.instruction).to_string(),
OP_SLTU => Rtype(self.instruction).to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

These match arms can be merged.

ISA::Imc => "Base Instruction",
ISA::A => "A extension",
ISA::B => "B extension",
ISA::Mop => "MOP extension",
Copy link
Collaborator

Choose a reason for hiding this comment

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

RISC-V doesn't have MOP extension.

Copy link
Collaborator

@mohanson mohanson left a comment

Choose a reason for hiding this comment

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

You can use TaggedInstruction to get this job done

if let Some(i) = instructions::i::factory::<u64>(instruction, VERSION2) {
          let tagged_instruction_option = instructions::tagged::TaggedInstruction::try_from(i);
          if let Ok(tagged_instruction) = tagged_instruction_option {
              println!("{}", tagged_instruction)
          } else {
              println!("unknown instruction");
          }
}

result:

addi a7,93(zero) # if instruction is 0x05d00893

}

pub fn decode_instruction(data: &str) -> Result<(), Box<dyn std::error::Error>> {
let has_0x = data.find("0x");
Copy link
Collaborator

Choose a reason for hiding this comment

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

data.start_with("0x")

}
}

fn get_isa(ins_opcode: InstructionOpcode) -> ISA {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ckb-vm does not guarantee that the instructions in an instruction set are ordered together. For example branch instructions in IMC are not grouped with other IMC instructions, See: https://github.com/nervosnetwork/ckb-vm/blob/develop/definitions/src/instructions.rs#L270-L287

@mohanson
Copy link
Collaborator

mohanson commented Jul 19, 2023

I think there is an easier way to do it, just do things like below

if let Some(i) = instructions::i::factory::<u64>(instruction, VERSION2) {
    let tagged_instruction = instructions::tagged::TaggedInstruction::try_from(i).unwrap();
    println!("{} (ISA_I)", tagged_instruction)
}

if let Some(i) = instructions::m::factory::<u64>(instruction, VERSION2) {
    let tagged_instruction = instructions::tagged::TaggedInstruction::try_from(i).unwrap();
    println!("{} (ISA_M)", tagged_instruction)
}

if let Some(i) = instructions::a::factory::<u64>(instruction, VERSION2) {
    let tagged_instruction = instructions::tagged::TaggedInstruction::try_from(i).unwrap();
    println!("{} (ISA_A)", tagged_instruction)
}

if let Some(i) = instructions::b::factory::<u64>(instruction, VERSION2) {
    let tagged_instruction = instructions::tagged::TaggedInstruction::try_from(i).unwrap();
    println!("{} (ISA_B)", tagged_instruction)
}

if let Some(i) = instructions::rvc::factory::<u64>(instruction, VERSION2) {
    let tagged_instruction = instructions::tagged::TaggedInstruction::try_from(i).unwrap();
    println!("{} (ISA_RVC)", tagged_instruction)
}

@XuJiandong XuJiandong merged commit b7ca5cc into nervosnetwork:develop Jul 19, 2023
1 check passed
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