diff mbox

[2/8] tcg: Avoid stores to unaligned addresses

Message ID 1396052834-26834-3-git-send-email-rth@twiddle.net
State Superseded
Headers show

Commit Message

Richard Henderson March 29, 2014, 12:27 a.m. UTC
From: Peter Maydell <peter.maydell@linaro.org>

Avoid stores to unaligned addresses in TCG code generation, by using the
usual memcpy() approach. (Using bswap.h would drag a lot of QEMU baggage
into TCG, so it's simpler just to do direct memcpy() here.)

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/tcg.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Alex Bennée April 1, 2014, 12:12 p.m. UTC | #1
Richard Henderson <rth@twiddle.net> writes:

> From: Peter Maydell <peter.maydell@linaro.org>
>
> Avoid stores to unaligned addresses in TCG code generation, by using the
> usual memcpy() approach. (Using bswap.h would drag a lot of QEMU baggage
> into TCG, so it's simpler just to do direct memcpy() here.)

I notice bswap.h has an interesting exultation to the efficiency of
inline memcpy's which I hope is justified.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>


>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  tcg/tcg.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tcg/tcg.c b/tcg/tcg.c
> index f1e0763..60f06c5 100644
> --- a/tcg/tcg.c
> +++ b/tcg/tcg.c
> @@ -125,21 +125,21 @@ static inline void tcg_out8(TCGContext *s, uint8_t v)
>  static inline void tcg_out16(TCGContext *s, uint16_t v)
>  {
>      uint8_t *p = s->code_ptr;
> -    *(uint16_t *)p = v;
> +    memcpy(p, &v, sizeof(v));
>      s->code_ptr = p + 2;
>  }
>  
>  static inline void tcg_out32(TCGContext *s, uint32_t v)
>  {
>      uint8_t *p = s->code_ptr;
> -    *(uint32_t *)p = v;
> +    memcpy(p, &v, sizeof(v));
>      s->code_ptr = p + 4;
>  }
>  
>  static inline void tcg_out64(TCGContext *s, uint64_t v)
>  {
>      uint8_t *p = s->code_ptr;
> -    *(uint64_t *)p = v;
> +    memcpy(p, &v, sizeof(v));
>      s->code_ptr = p + 8;
>  }
diff mbox

Patch

diff --git a/tcg/tcg.c b/tcg/tcg.c
index f1e0763..60f06c5 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -125,21 +125,21 @@  static inline void tcg_out8(TCGContext *s, uint8_t v)
 static inline void tcg_out16(TCGContext *s, uint16_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint16_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 2;
 }
 
 static inline void tcg_out32(TCGContext *s, uint32_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint32_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 4;
 }
 
 static inline void tcg_out64(TCGContext *s, uint64_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint64_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 8;
 }