xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-endian.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
14e98e3e1Schristos /* The common simulator framework for GDB, the GNU Debugger.
24e98e3e1Schristos 
3*88241920Schristos    Copyright 2002-2024 Free Software Foundation, Inc.
44e98e3e1Schristos 
54e98e3e1Schristos    Contributed by Andrew Cagney and Red Hat.
64e98e3e1Schristos 
74e98e3e1Schristos    This file is part of GDB.
84e98e3e1Schristos 
94e98e3e1Schristos    This program is free software; you can redistribute it and/or modify
104e98e3e1Schristos    it under the terms of the GNU General Public License as published by
114e98e3e1Schristos    the Free Software Foundation; either version 3 of the License, or
124e98e3e1Schristos    (at your option) any later version.
134e98e3e1Schristos 
144e98e3e1Schristos    This program is distributed in the hope that it will be useful,
154e98e3e1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
164e98e3e1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
174e98e3e1Schristos    GNU General Public License for more details.
184e98e3e1Schristos 
194e98e3e1Schristos    You should have received a copy of the GNU General Public License
204e98e3e1Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
214e98e3e1Schristos 
224e98e3e1Schristos 
234e98e3e1Schristos #ifndef _SIM_ENDIAN_C_
244e98e3e1Schristos #define _SIM_ENDIAN_C_
254e98e3e1Schristos 
264b169a6bSchristos /* This must come before any other includes.  */
274b169a6bSchristos #include "defs.h"
284b169a6bSchristos 
29*88241920Schristos #include "bfd.h"
30*88241920Schristos 
314e98e3e1Schristos #include "sim-basics.h"
324e98e3e1Schristos #include "sim-assert.h"
334e98e3e1Schristos 
344e98e3e1Schristos 
354e98e3e1Schristos #if !defined(_SWAP_1)
364e98e3e1Schristos #define _SWAP_1(SET,RAW) SET (RAW)
374e98e3e1Schristos #endif
384e98e3e1Schristos 
39ba340e45Schristos #if !defined(_SWAP_2) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htons)
404e98e3e1Schristos #define _SWAP_2(SET,RAW) SET htons (RAW)
414e98e3e1Schristos #endif
424e98e3e1Schristos 
434e98e3e1Schristos #ifndef	_SWAP_2
444e98e3e1Schristos #define _SWAP_2(SET,RAW) SET (((RAW) >> 8) | ((RAW) << 8))
454e98e3e1Schristos #endif
464e98e3e1Schristos 
47ba340e45Schristos #if !defined(_SWAP_4) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htonl)
484e98e3e1Schristos #define _SWAP_4(SET,RAW) SET htonl (RAW)
494e98e3e1Schristos #endif
504e98e3e1Schristos 
514e98e3e1Schristos #ifndef _SWAP_4
524e98e3e1Schristos #define	_SWAP_4(SET,RAW) SET (((RAW) << 24) | (((RAW) & 0xff00) << 8) | (((RAW) & 0xff0000) >> 8) | ((RAW) >> 24))
534e98e3e1Schristos #endif
544e98e3e1Schristos 
554e98e3e1Schristos #ifndef _SWAP_8
564e98e3e1Schristos #define _SWAP_8(SET,RAW) \
574e98e3e1Schristos   union { unsigned_8 dword; unsigned_4 words[2]; } in, out; \
584e98e3e1Schristos   in.dword = RAW; \
594e98e3e1Schristos   _SWAP_4 (out.words[0] =, in.words[1]); \
604e98e3e1Schristos   _SWAP_4 (out.words[1] =, in.words[0]); \
614e98e3e1Schristos   SET out.dword;
624e98e3e1Schristos #endif
634e98e3e1Schristos 
644e98e3e1Schristos #ifndef _SWAP_16
654e98e3e1Schristos #define _SWAP_16(SET,RAW) \
664e98e3e1Schristos   union { unsigned_16 word; unsigned_4 words[4]; } in, out; \
674e98e3e1Schristos   in.word = (RAW); \
684e98e3e1Schristos   _SWAP_4 (out.words[0] =, in.words[3]); \
694e98e3e1Schristos   _SWAP_4 (out.words[1] =, in.words[2]); \
704e98e3e1Schristos   _SWAP_4 (out.words[2] =, in.words[1]); \
714e98e3e1Schristos   _SWAP_4 (out.words[3] =, in.words[0]); \
724e98e3e1Schristos   SET out.word;
734e98e3e1Schristos #endif
744e98e3e1Schristos 
754e98e3e1Schristos 
764e98e3e1Schristos #define N 1
774e98e3e1Schristos #include "sim-n-endian.h"
784e98e3e1Schristos #undef N
794e98e3e1Schristos 
804e98e3e1Schristos #define N 2
814e98e3e1Schristos #include "sim-n-endian.h"
824e98e3e1Schristos #undef N
834e98e3e1Schristos 
844e98e3e1Schristos #define N 4
854e98e3e1Schristos #include "sim-n-endian.h"
864e98e3e1Schristos #undef N
874e98e3e1Schristos 
884e98e3e1Schristos #define N 8
894e98e3e1Schristos #include "sim-n-endian.h"
904e98e3e1Schristos #undef N
914e98e3e1Schristos 
924e98e3e1Schristos #define N 16
934e98e3e1Schristos #include "sim-n-endian.h"
944e98e3e1Schristos #undef N
954e98e3e1Schristos 
964e98e3e1Schristos 
974e98e3e1Schristos INLINE_SIM_ENDIAN\
984e98e3e1Schristos (unsigned_8)
994e98e3e1Schristos sim_endian_split_16 (unsigned_16 word, int w)
1004e98e3e1Schristos {
101ba340e45Schristos   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
1024e98e3e1Schristos     {
1034e98e3e1Schristos       return word.a[1 - w];
1044e98e3e1Schristos     }
1054e98e3e1Schristos   else
1064e98e3e1Schristos     {
1074e98e3e1Schristos       return word.a[w];
1084e98e3e1Schristos     }
1094e98e3e1Schristos }
1104e98e3e1Schristos 
1114e98e3e1Schristos 
1124e98e3e1Schristos INLINE_SIM_ENDIAN\
1134e98e3e1Schristos (unsigned_16)
1144e98e3e1Schristos sim_endian_join_16 (unsigned_8 h, unsigned_8 l)
1154e98e3e1Schristos 
1164e98e3e1Schristos {
1174e98e3e1Schristos   unsigned_16 word;
118ba340e45Schristos   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
1194e98e3e1Schristos     {
1204e98e3e1Schristos       word.a[0] = l;
1214e98e3e1Schristos       word.a[1] = h;
1224e98e3e1Schristos     }
1234e98e3e1Schristos   else
1244e98e3e1Schristos     {
1254e98e3e1Schristos       word.a[0] = h;
1264e98e3e1Schristos       word.a[1] = l;
1274e98e3e1Schristos     }
1284e98e3e1Schristos   return word;
1294e98e3e1Schristos }
1304e98e3e1Schristos 
1314e98e3e1Schristos 
1324e98e3e1Schristos 
1334e98e3e1Schristos #endif /* _SIM_ENDIAN_C_ */
134