Skip to content

Commit 75521b9

Browse files
committed
arcv: apex: Avoid registering instructions with duplicate opcode and insn format.
Add a check to detect when a new APEX intrinsic specifies an opcode value that overlaps with an already registered intrinsic, considering only the lower 4 bits of the insn format (XD, XS, XI, XC). Emit an error in such cases to prevent opcode conflicts at runtime. Signed-off-by: Luis Silva <[email protected]>
1 parent ec752b8 commit 75521b9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

gcc/config/riscv/riscv-builtins.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ struct arcv_apex_builtin_description {
247247
/* The name of the built-in instruction. */
248248
const char *insn_name;
249249

250+
/* The opcode of the built-in instruction. */
251+
unsigned int opcode;
252+
250253
/* Specifies how the function should be expanded. */
251254
enum riscv_builtin_type builtin_type;
252255

@@ -805,6 +808,24 @@ arcv_apex_validate_insn_format (const char* fn_name, unsigned int insn_format,
805808
/* If the instruction format is APEX_NONE, report an error. */
806809
gcc_assert (insn_format != APEX_NONE);
807810

811+
/* Check for opcode duplication:
812+
Emit an error if a previously registered APEX instruction has both:
813+
- At least one matching insn format bit (among XD, XS, XI, XC).
814+
- The same opcode value.
815+
816+
This prevents defining two intrinsics with overlapping formats
817+
that would conflict in opcode decoding. */
818+
for (int i = 0; i < arcv_apex_builtin_index; i++)
819+
{
820+
if ((arcv_apex_builtins[i].insn_formats & 0xF) & insn_format
821+
&& arcv_apex_builtins[i].opcode == opcode)
822+
{
823+
error ("pragma intrinsic: this specification defines an "
824+
"opcode that duplicates a previous one", fn_name, opcode);
825+
return;
826+
}
827+
}
828+
808829
bool has_dest = (insn_format & APEX_DEST) >> 5;
809830
unsigned int num_arguments = ((insn_format & APEX_SRC0) >> 6)
810831
+ ((insn_format & APEX_SRC1) >> 7);
@@ -971,7 +992,8 @@ arcv_apex_init_builtin (tree fndecl, const char *fn_name,
971992

972993
/* Store APEX insn information. */
973994
arcv_apex_builtins[arcv_apex_builtin_index]
974-
= { icode, fn_name, insn_name, builtin_type, insn_formats, insn_suffix };
995+
= { icode, fn_name, insn_name, opcode,
996+
builtin_type, insn_formats, insn_suffix };
975997

976998
/* Modify the prototype type as built-in. */
977999
fndecl->function_decl.built_in_class = BUILT_IN_MD;

0 commit comments

Comments
 (0)