Fix whitespace snafu in tc-riscv.c
[binutils-gdb.git] / sim / common / sim-endian.h
blob589169ea11cf217354a0e6452436c86817d8dceb
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2023 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #ifndef SIM_ENDIAN_H
24 #define SIM_ENDIAN_H
26 /* C byte conversion functions */
28 INLINE_SIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x);
29 INLINE_SIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x);
30 INLINE_SIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x);
31 INLINE_SIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x);
32 INLINE_SIM_ENDIAN(unsigned_16) endian_h2t_16(unsigned_16 x);
34 INLINE_SIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x);
35 INLINE_SIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x);
36 INLINE_SIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x);
37 INLINE_SIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x);
38 INLINE_SIM_ENDIAN(unsigned_16) endian_t2h_16(unsigned_16 x);
40 INLINE_SIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x);
41 INLINE_SIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x);
42 INLINE_SIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x);
43 INLINE_SIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x);
44 INLINE_SIM_ENDIAN(unsigned_16) swap_16(unsigned_16 x);
46 INLINE_SIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x);
47 INLINE_SIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x);
48 INLINE_SIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x);
49 INLINE_SIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x);
50 INLINE_SIM_ENDIAN(unsigned_16) endian_h2be_16(unsigned_16 x);
52 INLINE_SIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x);
53 INLINE_SIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x);
54 INLINE_SIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x);
55 INLINE_SIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x);
56 INLINE_SIM_ENDIAN(unsigned_16) endian_be2h_16(unsigned_16 x);
58 INLINE_SIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x);
59 INLINE_SIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x);
60 INLINE_SIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x);
61 INLINE_SIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x);
62 INLINE_SIM_ENDIAN(unsigned_16) endian_h2le_16(unsigned_16 x);
64 INLINE_SIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x);
65 INLINE_SIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x);
66 INLINE_SIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x);
67 INLINE_SIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x);
68 INLINE_SIM_ENDIAN(unsigned_16) endian_le2h_16(unsigned_16 x);
70 INLINE_SIM_ENDIAN(void*) offset_1(unsigned_1 *x, unsigned ws, unsigned w);
71 INLINE_SIM_ENDIAN(void*) offset_2(unsigned_2 *x, unsigned ws, unsigned w);
72 INLINE_SIM_ENDIAN(void*) offset_4(unsigned_4 *x, unsigned ws, unsigned w);
73 INLINE_SIM_ENDIAN(void*) offset_8(unsigned_8 *x, unsigned ws, unsigned w);
74 INLINE_SIM_ENDIAN(void*) offset_16(unsigned_16 *x, unsigned ws, unsigned w);
76 INLINE_SIM_ENDIAN(unsigned_16) sim_endian_join_16 (unsigned_8 h, unsigned_8 l);
77 INLINE_SIM_ENDIAN(unsigned_8) sim_endian_split_16 (unsigned_16 word, int w);
80 /* SWAP */
82 #define SWAP_1 swap_1
83 #define SWAP_2 swap_2
84 #define SWAP_4 swap_4
85 #define SWAP_8 swap_8
86 #define SWAP_16 swap_16
89 /* HOST to BE */
91 #define H2BE_1 endian_h2be_1
92 #define H2BE_2 endian_h2be_2
93 #define H2BE_4 endian_h2be_4
94 #define H2BE_8 endian_h2be_8
95 #define H2BE_16 endian_h2be_16
96 #define BE2H_1 endian_be2h_1
97 #define BE2H_2 endian_be2h_2
98 #define BE2H_4 endian_be2h_4
99 #define BE2H_8 endian_be2h_8
100 #define BE2H_16 endian_be2h_16
103 /* HOST to LE */
105 #define H2LE_1 endian_h2le_1
106 #define H2LE_2 endian_h2le_2
107 #define H2LE_4 endian_h2le_4
108 #define H2LE_8 endian_h2le_8
109 #define H2LE_16 endian_h2le_16
110 #define LE2H_1 endian_le2h_1
111 #define LE2H_2 endian_le2h_2
112 #define LE2H_4 endian_le2h_4
113 #define LE2H_8 endian_le2h_8
114 #define LE2H_16 endian_le2h_16
117 /* HOST to TARGET */
119 #define H2T_1 endian_h2t_1
120 #define H2T_2 endian_h2t_2
121 #define H2T_4 endian_h2t_4
122 #define H2T_8 endian_h2t_8
123 #define H2T_16 endian_h2t_16
124 #define T2H_1 endian_t2h_1
125 #define T2H_2 endian_t2h_2
126 #define T2H_4 endian_t2h_4
127 #define T2H_8 endian_t2h_8
128 #define T2H_16 endian_t2h_16
131 /* CONVERT IN PLACE
133 These macros, given an argument of unknown size, swap its value in
134 place if a host/target conversion is required. */
136 #define H2T(VARIABLE) \
137 do { \
138 void *vp = &(VARIABLE); \
139 switch (sizeof (VARIABLE)) { \
140 case 1: *(unsigned_1*)vp = H2T_1(*(unsigned_1*)vp); break; \
141 case 2: *(unsigned_2*)vp = H2T_2(*(unsigned_2*)vp); break; \
142 case 4: *(unsigned_4*)vp = H2T_4(*(unsigned_4*)vp); break; \
143 case 8: *(unsigned_8*)vp = H2T_8(*(unsigned_8*)vp); break; \
144 case 16: *(unsigned_16*)vp = H2T_16(*(unsigned_16*)vp); break; \
146 } while (0)
148 #define T2H(VARIABLE) \
149 do { \
150 switch (sizeof(VARIABLE)) { \
151 case 1: VARIABLE = T2H_1(VARIABLE); break; \
152 case 2: VARIABLE = T2H_2(VARIABLE); break; \
153 case 4: VARIABLE = T2H_4(VARIABLE); break; \
154 case 8: VARIABLE = T2H_8(VARIABLE); break; \
155 /*case 16: VARIABLE = T2H_16(VARIABLE); break;*/ \
157 } while (0)
159 #define SWAP(VARIABLE) \
160 do { \
161 switch (sizeof(VARIABLE)) { \
162 case 1: VARIABLE = SWAP_1(VARIABLE); break; \
163 case 2: VARIABLE = SWAP_2(VARIABLE); break; \
164 case 4: VARIABLE = SWAP_4(VARIABLE); break; \
165 case 8: VARIABLE = SWAP_8(VARIABLE); break; \
166 /*case 16: VARIABLE = SWAP_16(VARIABLE); break;*/ \
168 } while (0)
170 #define H2BE(VARIABLE) \
171 do { \
172 switch (sizeof(VARIABLE)) { \
173 case 1: VARIABLE = H2BE_1(VARIABLE); break; \
174 case 2: VARIABLE = H2BE_2(VARIABLE); break; \
175 case 4: VARIABLE = H2BE_4(VARIABLE); break; \
176 case 8: VARIABLE = H2BE_8(VARIABLE); break; \
177 /*case 16: VARIABLE = H2BE_16(VARIABLE); break;*/ \
179 } while (0)
181 #define BE2H(VARIABLE) \
182 do { \
183 switch (sizeof(VARIABLE)) { \
184 case 1: VARIABLE = BE2H_1(VARIABLE); break; \
185 case 2: VARIABLE = BE2H_2(VARIABLE); break; \
186 case 4: VARIABLE = BE2H_4(VARIABLE); break; \
187 case 8: VARIABLE = BE2H_8(VARIABLE); break; \
188 /*case 16: VARIABLE = BE2H_16(VARIABLE); break;*/ \
190 } while (0)
192 #define H2LE(VARIABLE) \
193 do { \
194 switch (sizeof(VARIABLE)) { \
195 case 1: VARIABLE = H2LE_1(VARIABLE); break; \
196 case 2: VARIABLE = H2LE_2(VARIABLE); break; \
197 case 4: VARIABLE = H2LE_4(VARIABLE); break; \
198 case 8: VARIABLE = H2LE_8(VARIABLE); break; \
199 /*case 16: VARIABLE = H2LE_16(VARIABLE); break;*/ \
201 } while (0)
203 #define LE2H(VARIABLE) \
204 do { \
205 switch (sizeof(VARIABLE)) { \
206 case 1: VARIABLE = LE2H_1(VARIABLE); break; \
207 case 2: VARIABLE = LE2H_2(VARIABLE); break; \
208 case 4: VARIABLE = LE2H_4(VARIABLE); break; \
209 case 8: VARIABLE = LE2H_8(VARIABLE); break; \
210 /*case 16: VARIABLE = LE2H_16(VARIABLE); break;*/ \
212 } while (0)
216 /* TARGET WORD:
218 Byte swap a quantity the size of the targets word */
220 #if (WITH_TARGET_WORD_BITSIZE == 64)
221 #define H2T_word H2T_8
222 #define T2H_word T2H_8
223 #define H2BE_word H2BE_8
224 #define BE2H_word BE2H_8
225 #define H2LE_word H2LE_8
226 #define LE2H_word LE2H_8
227 #define SWAP_word SWAP_8
228 #endif
229 #if (WITH_TARGET_WORD_BITSIZE == 32)
230 #define H2T_word H2T_4
231 #define T2H_word T2H_4
232 #define H2BE_word H2BE_4
233 #define BE2H_word BE2H_4
234 #define H2LE_word H2LE_4
235 #define LE2H_word LE2H_4
236 #define SWAP_word SWAP_4
237 #endif
241 /* TARGET CELL:
243 Byte swap a quantity the size of the targets IEEE 1275 memory cell */
245 #define H2T_cell H2T_4
246 #define T2H_cell T2H_4
247 #define H2BE_cell H2BE_4
248 #define BE2H_cell BE2H_4
249 #define H2LE_cell H2LE_4
250 #define LE2H_cell LE2H_4
251 #define SWAP_cell SWAP_4
255 /* HOST Offsets:
257 Address of high/low sub-word within a host word quantity.
259 Address of sub-word N within a host word quantity. NOTE: Numbering
260 is BIG endian always. */
262 #define AH1_2(X) (unsigned_1*)offset_2((X), 1, 0)
263 #define AL1_2(X) (unsigned_1*)offset_2((X), 1, 1)
265 #define AH2_4(X) (unsigned_2*)offset_4((X), 2, 0)
266 #define AL2_4(X) (unsigned_2*)offset_4((X), 2, 1)
268 #define AH4_8(X) (unsigned_4*)offset_8((X), 4, 0)
269 #define AL4_8(X) (unsigned_4*)offset_8((X), 4, 1)
271 #define AH8_16(X) (unsigned_8*)offset_16((X), 8, 0)
272 #define AL8_16(X) (unsigned_8*)offset_16((X), 8, 1)
274 #if (WITH_TARGET_WORD_BITSIZE == 64)
275 #define AH_word(X) AH4_8(X)
276 #define AL_word(X) AL4_8(X)
277 #endif
278 #if (WITH_TARGET_WORD_BITSIZE == 32)
279 #define AH_word(X) AH2_4(X)
280 #define AL_word(X) AL2_4(X)
281 #endif
284 #define A1_2(X,N) (unsigned_1*)offset_2((X), 1, (N))
286 #define A1_4(X,N) (unsigned_1*)offset_4((X), 1, (N))
287 #define A2_4(X,N) (unsigned_2*)offset_4((X), 2, (N))
289 #define A1_8(X,N) (unsigned_1*)offset_8((X), 1, (N))
290 #define A2_8(X,N) (unsigned_2*)offset_8((X), 2, (N))
291 #define A4_8(X,N) (unsigned_4*)offset_8((X), 4, (N))
293 #define A1_16(X,N) (unsigned_1*)offset_16((X), 1, (N))
294 #define A2_16(X,N) (unsigned_2*)offset_16((X), 2, (N))
295 #define A4_16(X,N) (unsigned_4*)offset_16((X), 4, (N))
296 #define A8_16(X,N) (unsigned_8*)offset_16((X), 8, (N))
301 /* HOST Components:
303 Value of sub-word within a host word quantity */
305 #define VH1_2(X) ((unsigned_1)((unsigned_2)(X) >> 8))
306 #define VL1_2(X) (unsigned_1)(X)
308 #define VH2_4(X) ((unsigned_2)((unsigned_4)(X) >> 16))
309 #define VL2_4(X) ((unsigned_2)(X))
311 #define VH4_8(X) ((unsigned_4)((unsigned_8)(X) >> 32))
312 #define VL4_8(X) ((unsigned_4)(X))
314 #define VH8_16(X) (sim_endian_split_16 ((X), 0))
315 #define VL8_16(X) (sim_endian_split_16 ((X), 1))
317 #if (WITH_TARGET_WORD_BITSIZE == 64)
318 #define VH_word(X) VH4_8(X)
319 #define VL_word(X) VL4_8(X)
320 #endif
321 #if (WITH_TARGET_WORD_BITSIZE == 32)
322 #define VH_word(X) VH2_4(X)
323 #define VL_word(X) VL2_4(X)
324 #endif
327 #define V1_2(X,N) ((unsigned_1)((unsigned_2)(X) >> ( 8 * (1 - (N)))))
329 #define V1_4(X,N) ((unsigned_1)((unsigned_4)(X) >> ( 8 * (3 - (N)))))
330 #define V2_4(X,N) ((unsigned_2)((unsigned_4)(X) >> (16 * (1 - (N)))))
332 #define V1_8(X,N) ((unsigned_1)((unsigned_8)(X) >> ( 8 * (7 - (N)))))
333 #define V2_8(X,N) ((unsigned_2)((unsigned_8)(X) >> (16 * (3 - (N)))))
334 #define V4_8(X,N) ((unsigned_4)((unsigned_8)(X) >> (32 * (1 - (N)))))
336 #define V1_16(X,N) (*A1_16 (&(X),N))
337 #define V2_16(X,N) (*A2_16 (&(X),N))
338 #define V4_16(X,N) (*A4_16 (&(X),N))
339 #define V8_16(X,N) (*A8_16 (&(X),N))
342 /* Reverse - insert sub-word into word quantity */
344 #define V2_H1(X) ((unsigned_2)(unsigned_1)(X) << 8)
345 #define V2_L1(X) ((unsigned_2)(unsigned_1)(X))
347 #define V4_H2(X) ((unsigned_4)(unsigned_2)(X) << 16)
348 #define V4_L2(X) ((unsigned_4)(unsigned_2)(X))
350 #define V8_H4(X) ((unsigned_8)(unsigned_4)(X) << 32)
351 #define V8_L4(X) ((unsigned_8)(unsigned_4)(X))
353 #define V16_H8(X) ((unsigned_16)(unsigned_8)(X) << 64)
354 #define V16_L8(X) ((unsigned_16)(unsigned_8)(X))
357 #define V2_1(X,N) ((unsigned_2)(unsigned_1)(X) << ( 8 * (1 - (N))))
359 #define V4_1(X,N) ((unsigned_4)(unsigned_1)(X) << ( 8 * (3 - (N))))
360 #define V4_2(X,N) ((unsigned_4)(unsigned_2)(X) << (16 * (1 - (N))))
362 #define V8_1(X,N) ((unsigned_8)(unsigned_1)(X) << ( 8 * (7 - (N))))
363 #define V8_2(X,N) ((unsigned_8)(unsigned_2)(X) << (16 * (3 - (N))))
364 #define V8_4(X,N) ((unsigned_8)(unsigned_4)(X) << (32 * (1 - (N))))
366 #define V16_1(X,N) ((unsigned_16)(unsigned_1)(X) << ( 8 * (15 - (N))))
367 #define V16_2(X,N) ((unsigned_16)(unsigned_2)(X) << (16 * (7 - (N))))
368 #define V16_4(X,N) ((unsigned_16)(unsigned_4)(X) << (32 * (3 - (N))))
369 #define V16_8(X,N) ((unsigned_16)(unsigned_8)(X) << (64 * (1 - (N))))
372 /* Reverse - insert N sub-words into single word quantity */
374 #define U2_1(I0,I1) (V2_1(I0,0) | V2_1(I1,1))
375 #define U4_1(I0,I1,I2,I3) (V4_1(I0,0) | V4_1(I1,1) | V4_1(I2,2) | V4_1(I3,3))
376 #define U8_1(I0,I1,I2,I3,I4,I5,I6,I7) \
377 (V8_1(I0,0) | V8_1(I1,1) | V8_1(I2,2) | V8_1(I3,3) \
378 | V8_1(I4,4) | V8_1(I5,5) | V8_1(I6,6) | V8_1(I7,7))
379 #define U16_1(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11,I12,I13,I14,I15) \
380 (V16_1(I0,0) | V16_1(I1,1) | V16_1(I2,2) | V16_1(I3,3) \
381 | V16_1(I4,4) | V16_1(I5,5) | V16_1(I6,6) | V16_1(I7,7) \
382 | V16_1(I8,8) | V16_1(I9,9) | V16_1(I10,10) | V16_1(I11,11) \
383 | V16_1(I12,12) | V16_1(I13,13) | V16_1(I14,14) | V16_1(I15,15))
385 #define U4_2(I0,I1) (V4_2(I0,0) | V4_2(I1,1))
386 #define U8_2(I0,I1,I2,I3) (V8_2(I0,0) | V8_2(I1,1) | V8_2(I2,2) | V8_2(I3,3))
387 #define U16_2(I0,I1,I2,I3,I4,I5,I6,I7) \
388 (V16_2(I0,0) | V16_2(I1,1) | V16_2(I2,2) | V16_2(I3,3) \
389 | V16_2(I4,4) | V16_2(I5,5) | V16_2(I6,6) | V16_2(I7,7) )
391 #define U8_4(I0,I1) (V8_4(I0,0) | V8_4(I1,1))
392 #define U16_4(I0,I1,I2,I3) (V16_4(I0,0) | V16_4(I1,1) | V16_4(I2,2) | V16_4(I3,3))
394 #define U16_8(I0,I1) (sim_endian_join_16 (I0, I1))
397 #if (WITH_TARGET_WORD_BITSIZE == 64)
398 #define Vword_H(X) V8_H4(X)
399 #define Vword_L(X) V8_L4(X)
400 #endif
401 #if (WITH_TARGET_WORD_BITSIZE == 32)
402 #define Vword_H(X) V4_H2(X)
403 #define Vword_L(X) V4_L2(X)
404 #endif
409 #if H_REVEALS_MODULE_P (SIM_ENDIAN_INLINE)
410 #include "sim-endian.c"
411 #endif
413 #endif /* SIM_ENDIAN_H */