Skip to content

Commit 308bcc1

Browse files
committed
arcv: apex: Validate immediate arguments during expansion.
Add validation for immediate arguments in APEX intrinsic calls that do not support the APEX_XD format. This check is performed during riscv_expand_builtin_direct, where the actual values of the current RTL expression are available. The validation ensures that the argument is a constant integer and falls within the correct range: - For APEX_XI and APEX_XC formats: signed 12-bit [-2048, 2047] - For APEX_XS format: signed 8-bit [-128, 127] Report an error if the value does not meet the previous checks. Signed-off-by: Luis Silva <[email protected]>
1 parent 246613e commit 308bcc1

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

gcc/config/riscv/riscv-builtins.cc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,73 @@ riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops,
382382
return has_target_p ? ops[0].value : const0_rtx;
383383
}
384384

385+
/* Validate the immediate argument passed to an APEX intrinsic.
386+
387+
This function checks if the argument to the intrinsic call is a constant
388+
integer and fits within the required immediate range depending on the
389+
format supported by the given SUBCODE. Only instructions that do not
390+
support APEX_XD are validated here.
391+
392+
- For APEX_XI and APEX_XC formats: the argument must be a
393+
signed 12-bit integer.
394+
- For APEX_XS format: the argument must be a signed 8-bit integer.
395+
396+
Returns false and reports an error if the argument is invalid; true
397+
otherwise. */
398+
399+
static bool
400+
arcv_apex_immediate_argument_valid_p (unsigned int subcode, tree exp)
401+
{
402+
if (!arcv_apex_format_supports_p (subcode, APEX_XD))
403+
{
404+
tree arg;
405+
/* Get the first (and only) argument passed to the intrinsic call. */
406+
if (arcv_apex_format_supports_p (subcode, APEX_XI))
407+
arg = CALL_EXPR_ARG (exp, 0);
408+
else if (arcv_apex_format_supports_p (subcode, APEX_XC)
409+
|| arcv_apex_format_supports_p (subcode, APEX_XS))
410+
arg = CALL_EXPR_ARG (exp, 1);
411+
412+
/* If the argument is NOT a constant integer. */
413+
if (!TREE_CONSTANT (arg) || TREE_CODE (arg) != INTEGER_CST)
414+
{
415+
error ("argument to %qs must be a constant integer",
416+
arcv_apex_builtins[subcode].name);
417+
return false;
418+
}
419+
420+
/* If the current subcode supports the APEX_XI or APEX_XC format, then
421+
the operand must fit within a signed 12-bit immediate. */
422+
if (arcv_apex_format_supports_p (subcode, APEX_XI)
423+
|| arcv_apex_format_supports_p (subcode, APEX_XC))
424+
{
425+
HOST_WIDE_INT val = tree_to_shwi (arg);
426+
/* Check if the value fits within a signed 12-bit immediate. */
427+
if ((val < -2048 || val > 2047))
428+
{
429+
error ("argument value %d is outside the valid range [-2048, 2047]",
430+
val);
431+
return false;
432+
}
433+
}
434+
435+
/* If the current subcode supports the APEX_XS format, then
436+
the operand must fit within a signed 8-bit immediate. */
437+
if (arcv_apex_format_supports_p (subcode, APEX_XS))
438+
{
439+
HOST_WIDE_INT val = tree_to_shwi (arg);
440+
/* Check if the value fits within a signed 8-bit immediate. */
441+
if ((val < -128 || val > 127))
442+
{
443+
error ("argument value %d is outside the valid range [-128, 127]",
444+
val);
445+
return false;
446+
}
447+
}
448+
}
449+
return true;
450+
}
451+
385452
/* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function;
386453
HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
387454
and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
@@ -405,6 +472,9 @@ riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
405472
rtx const_rtx = GEN_INT (subcode);
406473
/* Add the subcode as an additional input operand to the RTL expression. */
407474
create_input_operand (&ops[opno++], const_rtx, SImode);
475+
/* Validate the immediate argument passed to the APEX intrinsic. */
476+
if (!arcv_apex_immediate_argument_valid_p (subcode, exp))
477+
return const0_rtx;
408478
}
409479

410480
/* Map the arguments to the other operands. */

0 commit comments

Comments
 (0)