On 01/10/26, Pierrick Bouvier wrote:
> On 10/1/2026 8:59 AM, Anton Johansson wrote:
> > Replaces uses of target_ulong with fixed-size integers, this is a
> > prerequisite step to compiling cpu.c once for system-mode.
> > 
> > Note: the widening of target_long to int64_t in adjust_addr_body() is
> > safe as rv32 is ruled out earlier in the function via the check
> > 
> >   /* do nothing for rv32 mode */
> >   if (riscv_cpu_mxl(env) == MXL_RV32) {
> >       return addr;
> >   }
> > 
> > meaning target_long must be equal to int64_t anyway.
> > 
> > Duplicate includes between vector_helper.c and vector_internals.h are
> > removed.
> > 
> > Signed-off-by: Anton Johansson <[email protected]>
> > ---
> >  target/riscv/internals.h            | 31 +++++++++---------
> >  target/riscv/tcg/vector_internals.h |  5 +--
> >  target/riscv/tcg/vector_helper.c    | 64 
> > +++++++++++++++++--------------------
> >  3 files changed, 47 insertions(+), 53 deletions(-)
> > 
> 
> ...
> 
> >  /* Zjpm addr masking routine */
> > -static inline target_ulong adjust_addr_body(CPURISCVState *env,
> > -                                            target_ulong addr,
> > -                                            bool is_virt_addr)
> > +static inline uint64_t adjust_addr_body(CPURISCVState *env,
> > +                                        uint64_t addr,
> > +                                        bool is_virt_addr)
> >  {
> >      RISCVPmPmm pmm = PMM_FIELD_DISABLED;
> >      uint32_t pmlen = 0;
> > @@ -239,7 +238,7 @@ static inline target_ulong 
> > adjust_addr_body(CPURISCVState *env,
> >  
> >      /* sign/zero extend masked address by N-1 bit */
> >      if (signext) {
> > -        addr = (target_long)addr >> pmlen;
> > +        addr = (int64_t)addr >> pmlen;
> >      } else {
> >          addr = addr >> pmlen;
> >      }
> 
> I think this one is incorrect, since for a 32-bit addr, value will not
> get sign extended, so shift will be unsigned.
> 
> ```
> #include <stdio.h>
> #include <stdint.h>
> #include <inttypes.h>
> 
> uint64_t out64(uint64_t in)
> {
>     return (int64_t) in >> 1;
> }
> 
> uint32_t out32(uint32_t in)
> {
>     return (int32_t) in >> 1;
> }
> 
> int main()
> {
>     printf("32: %"PRIx32"\n", out32(0xffffffff));
>     printf("64: %"PRIx64"\n", out64(0xffffffff));
> }
> ```
> 
> Output is:
> 32: ffffffff
> 64: 7fffffff
> 
> We need to add a:
> if (target_long_bits() == 32) {
>   addr = (int32_t)addr >> pmlen;
> else {
>   addr = (int64_t)addr >> pmlen;
> }
> 
> Regards,
> Pierrick

No this is not necessary unless I'm missing something, see commit comment.
MXL_RV32 is ruled out earlier in the function

  static inline uint64_t adjust_addr_body(CPURISCVState *env,
                                          uint64_t addr,
                                          bool is_virt_addr)
  {
      ...
  
      /* do nothing for rv32 mode */
      if (riscv_cpu_mxl(env) == MXL_RV32) {
          return addr;
      }

      ...

      /* sign/zero extend masked address by N-1 bit */
      if (signext) {
          addr = (int64_t)addr >> pmlen;
      } else {
          addr = addr >> pmlen;
      }

      return addr;
  }

-- 
Anton Johansson
rev.ng Labs Srl.

Reply via email to