Skip to content

Commit ee3aedb

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 6a04e70 commit ee3aedb

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
@@ -250,6 +250,9 @@ struct arcv_apex_builtin_description {
250250
/* The name of the built-in instruction. */
251251
const char *insn_name;
252252

253+
/* The opcode of the built-in instruction. */
254+
unsigned int opcode;
255+
253256
/* Specifies how the function should be expanded. */
254257
enum riscv_builtin_type builtin_type;
255258

@@ -823,6 +826,24 @@ arcv_apex_validate_insn_format (const char* fn_name, unsigned int insn_format,
823826
/* If the instruction format is APEX_NONE, report an error. */
824827
gcc_assert (insn_format != APEX_NONE);
825828

829+
/* Check for opcode duplication:
830+
Emit an error if a previously registered APEX instruction has both:
831+
- At least one matching insn format bit (among XD, XS, XI, XC).
832+
- The same opcode value.
833+
834+
This prevents defining two intrinsics with overlapping formats
835+
that would conflict in opcode decoding. */
836+
for (int i = 0; i < arcv_apex_builtin_index; i++)
837+
{
838+
if ((arcv_apex_builtins[i].insn_formats & 0xF) & insn_format
839+
&& arcv_apex_builtins[i].opcode == opcode)
840+
{
841+
error ("pragma intrinsic: this specification defines an "
842+
"opcode that duplicates a previous one", fn_name, opcode);
843+
return;
844+
}
845+
}
846+
826847
bool has_dest = (insn_format & APEX_DEST) >> 5;
827848
unsigned int num_arguments = ((insn_format & APEX_SRC0) >> 6)
828849
+ ((insn_format & APEX_SRC1) >> 7);
@@ -989,7 +1010,8 @@ arcv_apex_init_builtin (tree fndecl, const char *fn_name,
9891010

9901011
/* Store APEX insn information. */
9911012
arcv_apex_builtins[arcv_apex_builtin_index]
992-
= { icode, fn_name, insn_name, builtin_type, insn_formats, insn_suffix };
1013+
= { icode, fn_name, insn_name, opcode,
1014+
builtin_type, insn_formats, insn_suffix };
9931015

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

0 commit comments

Comments
 (0)