Skip to content

Commit 1e8d394

Browse files
committed
introduce a +noaliascompressed option to selectively enable none-aliasing for compressed instrcution only but leave aliasing for the rest of instructions
1 parent b56a28e commit 1e8d394

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

arch/RISCV/RISCVInstPrinter.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -374,39 +374,51 @@ void RISCV_LLVM_printInstruction(MCInst *MI, SStream *O,
374374
MI->MRI = (MCRegisterInfo *)info;
375375

376376
MCInst_setIsAlias(MI, false);
377+
378+
/* check for a non-compressed instruction */
379+
MCInst Uncompressed;
380+
MCInst_Init(&Uncompressed, MI->csh->arch);
381+
382+
MCInst *McInstr = MI;
383+
bool is_uncompressed = false;
384+
// side-effectful check for compressed instructions that creates the equivalent uncompressed instruction in case of true
385+
// (LLVM doesn't generate an API for doing a pure check)
386+
if (uncompressInst(&Uncompressed, MI)) {
387+
McInstr = &Uncompressed;
388+
Uncompressed.address = MI->address;
389+
Uncompressed.MRI = MI->MRI;
390+
Uncompressed.csh = MI->csh;
391+
Uncompressed.flat_insn = MI->flat_insn;
392+
is_uncompressed = true;
393+
}
394+
377395
// print the exact instruction text and done
378-
if (MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT) {
396+
bool print_exact_text =
397+
(MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT) ||
398+
(is_uncompressed &&
399+
MI->csh->syntax & CS_OPT_SYNTAX_NO_ALIAS_TEXT_COMPRESSED);
400+
if (print_exact_text) {
379401
printInstruction(MI, MI->address, O);
380402
} else {
381-
/* the instruction might be an alias, including in the case of a compressed instruction */
382-
MCInst Uncompressed;
383-
MCInst_Init(&Uncompressed, MI->csh->arch);
384-
385-
MCInst *McInstr = MI;
386-
if (uncompressInst(&Uncompressed, MI)) {
387-
McInstr = &Uncompressed;
388-
Uncompressed.address = MI->address;
389-
Uncompressed.MRI = MI->MRI;
390-
Uncompressed.csh = MI->csh;
391-
Uncompressed.flat_insn = MI->flat_insn;
392-
}
393-
403+
// side-effectful check for alias instructions that prints to the SStream if true
394404
if (printAliasInstr(McInstr, MI->address, O)) {
395405
MCInst_setIsAlias(MI, true);
406+
// do we still want the exact details even if the text is alias ?
396407
if (!map_use_alias_details(MI) && detail_is_set(MI)) {
397408
// disable actual printing
398409
SStream_Close(O);
410+
// discard the alias operands
399411
memset(MI->flat_insn->detail->riscv.operands, 0,
400412
sizeof(MI->flat_insn->detail->riscv
401413
.operands));
402414
MI->flat_insn->detail->riscv.op_count = 0;
403-
// re-disassemble again in order to obtain the full details
415+
// re-disassemble again with no printing in order to obtain the full details
404416
// including the whole operands array
405417
printInstruction(MI, MI->address, O);
406418
// re-open the stream to restore the usual state
407419
SStream_Open(O);
408420
}
409-
} else
421+
} else // the instruction is not an alias
410422
printInstruction(McInstr, MI->address, O);
411423
}
412424
RISCV_add_groups(MI);

cstool/cstool.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ static struct {
6969
{ CS_ARCH_RISCV, CS_ARCH_MAX },
7070
CS_OPT_SYNTAX_NO_ALIAS_TEXT,
7171
0 },
72+
{ "+noaliascompressed",
73+
"Does not print the text alias of compressed RISC-V instructions, "
74+
"but still prints the text alias of non-compressed RISC-V instructions "
75+
"if +noalias is not given",
76+
{ CS_ARCH_RISCV, CS_ARCH_MAX },
77+
CS_OPT_SYNTAX_NO_ALIAS_TEXT_COMPRESSED,
78+
0 },
7279
// cs_mode only
7380
{ "+nofloat",
7481
"Disables floating point support",

include/capstone/capstone.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ typedef enum cs_opt_value {
362362
CS_OPT_SYNTAX_NO_ALIAS_TEXT =
363363
1
364364
<< 10, ///< Does not print an instruction's alias test if the instruction is an alias
365+
CS_OPT_SYNTAX_NO_ALIAS_TEXT_COMPRESSED =
366+
1
367+
<< 12, ///< Does not print an instruction's alias test if the instruction is an alias
365368
CS_OPT_DETAIL_REAL =
366369
1
367370
<< 1, ///< If enabled, always sets the real instruction detail. Even if the instruction is an alias.

0 commit comments

Comments
 (0)