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

Improve InstructionCounter and InstructionStatistics #21

Closed
TheThirdOne opened this issue Apr 10, 2019 · 6 comments
Closed

Improve InstructionCounter and InstructionStatistics #21

TheThirdOne opened this issue Apr 10, 2019 · 6 comments

Comments

@TheThirdOne
Copy link
Owner

InstructionCounter could be updated to also have counts for B and J. This would require copying some extra UI elements, adding some state to track counts and changing this if. This would only require understanding the code in InstructionCounter.

// TODO: update this to have labels for the extra formats
try {
ProgramStatement stmt = Memory.getInstance().getStatement(a);
BasicInstruction instr = (BasicInstruction) stmt.getInstruction();
BasicInstructionFormat format = instr.getInstructionFormat();
if (format == BasicInstructionFormat.R_FORMAT)
counterR++;
else if (format == BasicInstructionFormat.I_FORMAT)
counterI++;
else if (format == BasicInstructionFormat.S_FORMAT || format == BasicInstructionFormat.B_FORMAT)
counterS++;
else if (format == BasicInstructionFormat.U_FORMAT || format == BasicInstructionFormat.J_FORMAT)
counterU++;

InstructionStatistics is was never updated from MIPS. Updating this requires more work that InstructionCounter. You will need to update getInstructionCategory(...); that will probably involve a fair amount of instanceof checks. This requires understanding code in rars.riscv.instructions.

protected int getInstructionCategory(ProgramStatement stmt) {
int opCode = stmt.getBinaryStatement() >>> (32 - 6);
int funct = stmt.getBinaryStatement() & 0x1F;
if (opCode == 0x00) {
if (funct == 0x00)
return InstructionStatistics.CATEGORY_ALU; // sll
if (0x02 <= funct && funct <= 0x07)
return InstructionStatistics.CATEGORY_ALU; // srl, sra, sllv, srlv, srav
if (funct == 0x08 || funct == 0x09)
return InstructionStatistics.CATEGORY_JUMP; // jr, jalr
if (0x10 <= funct && funct <= 0x2F)
return InstructionStatistics.CATEGORY_ALU; // mfhi, mthi, mflo, mtlo, mult, multu, div, divu, add, addu, sub, subu, and, or, xor, nor, slt, sltu
return InstructionStatistics.CATEGORY_OTHER;
}
if (opCode == 0x01) {
if (0x00 <= funct && funct <= 0x07)
return InstructionStatistics.CATEGORY_BRANCH; // bltz, bgez, bltzl, bgezl
if (0x10 <= funct && funct <= 0x13)
return InstructionStatistics.CATEGORY_BRANCH; // bltzal, bgezal, bltzall, bgczall
return InstructionStatistics.CATEGORY_OTHER;
}
if (opCode == 0x02 || opCode == 0x03)
return InstructionStatistics.CATEGORY_JUMP; // j, jal
if (0x04 <= opCode && opCode <= 0x07)
return InstructionStatistics.CATEGORY_BRANCH; // beq, bne, blez, bgtz
if (0x08 <= opCode && opCode <= 0x0F)
return InstructionStatistics.CATEGORY_ALU; // addi, addiu, slti, sltiu, andi, ori, xori, lui
if (0x14 <= opCode && opCode <= 0x17)
return InstructionStatistics.CATEGORY_BRANCH; // beql, bnel, blezl, bgtzl
if (0x20 <= opCode && opCode <= 0x26)
return InstructionStatistics.CATEGORY_MEM; // lb, lh, lwl, lw, lbu, lhu, lwr
if (0x28 <= opCode && opCode <= 0x2E)
return InstructionStatistics.CATEGORY_MEM; // sb, sh, swl, sw, swr
return InstructionStatistics.CATEGORY_OTHER;
}

If you want to try to solve either of these and have trouble, feel free to ask for advice.

@esperz2019
Copy link
Contributor

Hello,
I think the Instruction Statistics tool is not working properly.
The amount of memory instructions is always 0 or 1, while sometimes the Others will make up about 50% of the total.
I haven't looked into it yet and would like to ask if it is a bug?

@TheThirdOne
Copy link
Owner Author

@esperz2019, you are correct; Instruction Statistics is not working properly. The tool was never updated from using MIPS. It reads the RISC-V instructions as if they were MIPS instructions and therefore incorrectly categorizes them.

The snippet I linked above shows exactly how the classification of instructions according to MIPS works.

I haven't yet fixed it because its a nice self contained issue that a new contributor could solve.

@esperz2019
Copy link
Contributor

@TheThirdOne Thanks, I'd like to have a try if I have some time later.

@giancarlopernudisegura
Copy link
Contributor

I'm currently modifying the Instruction Statistics tool to properly categorize the A extension instructions as part of #75.

protected int getInstructionCategory(ProgramStatement stmt) {
int opCode = stmt.getBinaryStatement() >>> (32 - 6);
int funct = stmt.getBinaryStatement() & 0x1F;

After referring to the RISC-V Reference Datasheet, I believe the variables opCode and funct are reversed, as funct7 is in 25-31 and opcode is in 0-6. Furthermore, since opcode is 7 bits long, the bitmask to obtain it should be 0x7F, not 0x1F.

@TheThirdOne
Copy link
Owner Author

@giancarlopernudisegura, The code in InstructionStatistics is almost entirely wrong for RISC-V. I would recommend using instanceof checks rather than binary manipulation as an easier way to check classes of instructions. Classes like Arithmetic already group various types of instructions.

@TheThirdOne
Copy link
Owner Author

This is officially fixed now. Thanks to @esperz2019 and @giancarlopernudisegura.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants