Skip to content

Print RISCV compressed instructions with "c." prefix #2868

@OBarronCS

Description

@OBarronCS

Work environment

Questions Answers
Capstone module affected RISCV
Version/git commit 6.0.0 alpha 7

With the release of CapstoneV6 Alpha 7, the default display of RISCV compressed instructions changed. Previously, they were prefixed with a c., such as c.li, or c.j. By default, this no longer occurs.

The "+noalias" options brings back the original behavior, but it as has the downside of applying to all instructions, not just compressed instructions. It would be nice to have a version of the "+noalias" option that only applies to compressed instructions.

Current behavior

[ubuntu:~]$ cstool -d riscv64 "9142"
 0  91 42        li	t0, 4
	ID: 519 (c_lhu)
	Is alias: 0 ((null)) with REAL operand set
	op_count: 2
		operands[0].type: REG = t0
		operands[0].access: WRITE
		operands[1].type: IMM = 0x4
		operands[1].access: READ

	Groups: HasStdExtCOrZca 

As an aside, the "Is alias" is zero, but doing "riscv64+noalias" prints c.li, like below. If it's not an alias, should noalias apply?

Requested behavior

Add a "+nocompressedalias", or something like that, flag so that when programmatically disassembling, the c. prefix is shown for compressed instructions, as was the default prior to Capstone6 Alpha 7, but other aliases are kept intact.

cstool -d riscv64+nocompressedalias "9142"
 0  91 42        c.li	t0, 4
	ID: 519 (c_lhu)
	op_count: 2
		operands[0].type: REG = t0
		operands[0].access: WRITE
		operands[1].type: IMM = 0x4
		operands[1].access: READ

	Groups: HasStdExtCOrZca 

The check for CS_OPT_SYNTAX_NO_ALIAS_TEXT currently occurs here:

MCInst_setIsAlias(MI, false);
// print the exact instruction text and done
if (MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT) {
printInstruction(MI, MI->address, O);
} else {
/* the instruction might be an alias, including in the case of a compressed instruction */
MCInst Uncompressed;
MCInst_Init(&Uncompressed, MI->csh->arch);
MCInst *McInstr = MI;
if (uncompressInst(&Uncompressed, MI)) {
McInstr = &Uncompressed;
Uncompressed.address = MI->address;
Uncompressed.MRI = MI->MRI;
Uncompressed.csh = MI->csh;
Uncompressed.flat_insn = MI->flat_insn;
}
if (printAliasInstr(McInstr, MI->address, O)) {
MCInst_setIsAlias(MI, true);
if (!map_use_alias_details(MI) && detail_is_set(MI)) {
// disable actual printing
SStream_Close(O);
memset(MI->flat_insn->detail->riscv.operands, 0,
sizeof(MI->flat_insn->detail->riscv
.operands));
MI->flat_insn->detail->riscv.op_count = 0;
// re-disassemble again in order to obtain the full details
// including the whole operands array
printInstruction(MI, MI->address, O);
// re-open the stream to restore the usual state
SStream_Open(O);
}
} else
printInstruction(McInstr, MI->address, O);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions