[PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx

Alex Bennée posted 4 patches 1 month, 2 weeks ago
Maintainers: Peter Maydell <peter.maydell@linaro.org>
[PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx
Posted by Alex Bennée 1 month, 2 weeks ago
We can use the registerfields API to safely set the individual fields
and avoid open coding magic numbers. The EC and IL fields are still
driven by defines but we could convert them later.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 target/arm/syndrome.h | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/target/arm/syndrome.h b/target/arm/syndrome.h
index bff61f052cc..86a2d5ea9d0 100644
--- a/target/arm/syndrome.h
+++ b/target/arm/syndrome.h
@@ -26,6 +26,7 @@
 #define TARGET_ARM_SYNDROME_H
 
 #include "qemu/bitops.h"
+#include "hw/core/registerfields.h"
 
 /* Valid Syndrome Register EC field values */
 enum arm_exception_class {
@@ -352,11 +353,24 @@ static inline uint32_t syn_breakpoint(int same_el)
         | ARM_EL_IL | 0x22;
 }
 
+FIELD(WFX_ISS, TI, 0, 2)
+FIELD(WFX_ISS, RV, 14, 1)
+FIELD(WFX_ISS, RN, 15, 5)
+FIELD(WFX_ISS, COND, 20, 4)
+FIELD(WFX_ISS, CV, 24, 1)
+
 static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
 {
-    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
-           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
-           (cv << 24) | (cond << 20) | ti;
+    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
+
+    res = FIELD_DP32(res, WFX_ISS, CV, cv);
+    res = FIELD_DP32(res, WFX_ISS, COND, cond);
+    res = FIELD_DP32(res, WFX_ISS, TI, ti);
+
+    if (!is_16bit) {
+        res |= ARM_EL_IL;
+    }
+    return res;
 }
 
 static inline uint32_t syn_illegalstate(void)
-- 
2.47.3


Re: [PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx
Posted by Peter Maydell 1 month, 2 weeks ago
On Tue, 24 Feb 2026 at 15:43, Alex Bennée <alex.bennee@linaro.org> wrote:
>
> We can use the registerfields API to safely set the individual fields
> and avoid open coding magic numbers. The EC and IL fields are still
> driven by defines but we could convert them later.
>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

> +FIELD(WFX_ISS, TI, 0, 2)
> +FIELD(WFX_ISS, RV, 14, 1)
> +FIELD(WFX_ISS, RN, 15, 5)
> +FIELD(WFX_ISS, COND, 20, 4)
> +FIELD(WFX_ISS, CV, 24, 1)
> +
>  static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
>  {
> -    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
> -           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
> -           (cv << 24) | (cond << 20) | ti;
> +    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
> +
> +    res = FIELD_DP32(res, WFX_ISS, CV, cv);
> +    res = FIELD_DP32(res, WFX_ISS, COND, cond);
> +    res = FIELD_DP32(res, WFX_ISS, TI, ti);
> +
> +    if (!is_16bit) {
> +        res |= ARM_EL_IL;
> +    }
> +    return res;
>  }

I'm not hugely enthusiastic about this mostly because it
means we would have one syn_foo function that's written totally
differently to all the rest.

-- PMM
Re: [PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx
Posted by Alex Bennée 1 month, 2 weeks ago
Peter Maydell <peter.maydell@linaro.org> writes:

> On Tue, 24 Feb 2026 at 15:43, Alex Bennée <alex.bennee@linaro.org> wrote:
>>
>> We can use the registerfields API to safely set the individual fields
>> and avoid open coding magic numbers. The EC and IL fields are still
>> driven by defines but we could convert them later.
>>
>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>
>> +FIELD(WFX_ISS, TI, 0, 2)
>> +FIELD(WFX_ISS, RV, 14, 1)
>> +FIELD(WFX_ISS, RN, 15, 5)
>> +FIELD(WFX_ISS, COND, 20, 4)
>> +FIELD(WFX_ISS, CV, 24, 1)
>> +
>>  static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
>>  {
>> -    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
>> -           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
>> -           (cv << 24) | (cond << 20) | ti;
>> +    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
>> +
>> +    res = FIELD_DP32(res, WFX_ISS, CV, cv);
>> +    res = FIELD_DP32(res, WFX_ISS, COND, cond);
>> +    res = FIELD_DP32(res, WFX_ISS, TI, ti);
>> +
>> +    if (!is_16bit) {
>> +        res |= ARM_EL_IL;
>> +    }
>> +    return res;
>>  }
>
> I'm not hugely enthusiastic about this mostly because it
> means we would have one syn_foo function that's written totally
> differently to all the rest.

I did consider running through the rest of the file updating with proper
field setting but I didn't want to bloat the series. I think it does
help with readability compared to open coded constant shifts.

>
> -- PMM

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro