On Wed, Jul 6, 2011 at 11:35 PM, Øyvind Harboe <oyvind.har...@zylin.com>wrote:

> On Wed, Jul 6, 2011 at 11:28 PM, Mahr, Stefan <stefan.m...@sphairon.com>
> wrote:
> >>>> "mips32_pracc_read_mem" casts uint32 to void, so we need to cast it
> >>>> back to uint32. I found no suitable macro in actual sources.
> >>>
> >>> Hmm.... then I think we ought to define one to get this put to
> >>> bed once and for all...
> >>
> >> static inline uint32_t uint32_read_unaligned(const void *data)
> >> {
> >> uint32_t t;
> >>  // Let's trust the compiler to do something very clever here.
> >> memcpy(&t, data, sizeof(t));
> >> return t;
> >> }
> >
> > Do we really need a memcpy?
>
> Trust the compiler:
>
> oyvind@fierce:~$ cat test.c
> #include <string.h>
> #include <stdint.h>
>
> uint32_t uint32_read_unaligned(const void *data)
> {
> uint32_t t;
>  // Let's trust the compiler to do something very clever here.
> memcpy(&t, data, sizeof(t));
>  return t;
> }
> oyvind@fierce:~$ gcc -O3 -S test.c
>

And with the default optimization level (O2, isn't it)?


> > Could we ever run into an alignment
> > issue when simply cast void *back* to uint32 ? If not, I would prefer
> > the simplifed solution.
>
> The below does not work in all cases. It will break on architectures that
> do not support unaligned access.
>

No, casting a pointer-to-any-type to a pointer-to-void and back will never
cause alignment issues. The question is who makes the guarantee that the
function is only ever called with uint32-aligned generic pointers. If it
just happens to be so that all *current* callers pass a uint32 pointer cast
to void pointer, it would be a terribly bad idea to suddenly assume that. If
it is guaranteed by design/contract, why is a void pointer used at all? Just
use a pointer to uint32 then.

I have no experience with the MIPS code or arch at all, but if it was up to
me I'd prefer if a *_read_mem function didn't interpret the data and thus it
should deal with void pointers. It would by design be totally free from all
endian issues and conversions. Moreover, a function accepting a
pointer-to-void should be prepared to handle any kind of alignment thrown at
it (unless explicitly stated). That excludes direct casts to uint32_t*.


> That's why GCC is complaining. Sometimes...
>

It should never complain when casting a generic pointer to another pointer,
right? You shouldn't even need an explicit cast IIRC.

/Andreas
_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to