]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-endian.h
sim: overhaul & unify endian settings management
[thirdparty/binutils-gdb.git] / sim / common / sim-endian.h
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3 Copyright 2002-2021 Free Software Foundation, Inc.
4
5 Contributed by Andrew Cagney and Red Hat.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22
23 #ifndef SIM_ENDIAN_H
24 #define SIM_ENDIAN_H
25
26 #include "bfd.h"
27
28 /* C byte conversion functions */
29
30 INLINE_SIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x);
31 INLINE_SIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x);
32 INLINE_SIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x);
33 INLINE_SIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x);
34 INLINE_SIM_ENDIAN(unsigned_16) endian_h2t_16(unsigned_16 x);
35
36 INLINE_SIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x);
37 INLINE_SIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x);
38 INLINE_SIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x);
39 INLINE_SIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x);
40 INLINE_SIM_ENDIAN(unsigned_16) endian_t2h_16(unsigned_16 x);
41
42 INLINE_SIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x);
43 INLINE_SIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x);
44 INLINE_SIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x);
45 INLINE_SIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x);
46 INLINE_SIM_ENDIAN(unsigned_16) swap_16(unsigned_16 x);
47
48 INLINE_SIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x);
49 INLINE_SIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x);
50 INLINE_SIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x);
51 INLINE_SIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x);
52 INLINE_SIM_ENDIAN(unsigned_16) endian_h2be_16(unsigned_16 x);
53
54 INLINE_SIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x);
55 INLINE_SIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x);
56 INLINE_SIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x);
57 INLINE_SIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x);
58 INLINE_SIM_ENDIAN(unsigned_16) endian_be2h_16(unsigned_16 x);
59
60 INLINE_SIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x);
61 INLINE_SIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x);
62 INLINE_SIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x);
63 INLINE_SIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x);
64 INLINE_SIM_ENDIAN(unsigned_16) endian_h2le_16(unsigned_16 x);
65
66 INLINE_SIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x);
67 INLINE_SIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x);
68 INLINE_SIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x);
69 INLINE_SIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x);
70 INLINE_SIM_ENDIAN(unsigned_16) endian_le2h_16(unsigned_16 x);
71
72 INLINE_SIM_ENDIAN(void*) offset_1(unsigned_1 *x, unsigned ws, unsigned w);
73 INLINE_SIM_ENDIAN(void*) offset_2(unsigned_2 *x, unsigned ws, unsigned w);
74 INLINE_SIM_ENDIAN(void*) offset_4(unsigned_4 *x, unsigned ws, unsigned w);
75 INLINE_SIM_ENDIAN(void*) offset_8(unsigned_8 *x, unsigned ws, unsigned w);
76 INLINE_SIM_ENDIAN(void*) offset_16(unsigned_16 *x, unsigned ws, unsigned w);
77
78 INLINE_SIM_ENDIAN(unsigned_16) sim_endian_join_16 (unsigned_8 h, unsigned_8 l);
79 INLINE_SIM_ENDIAN(unsigned_8) sim_endian_split_16 (unsigned_16 word, int w);
80
81
82 /* SWAP */
83
84 #define SWAP_1 swap_1
85 #define SWAP_2 swap_2
86 #define SWAP_4 swap_4
87 #define SWAP_8 swap_8
88 #define SWAP_16 swap_16
89
90
91 /* HOST to BE */
92
93 #define H2BE_1 endian_h2be_1
94 #define H2BE_2 endian_h2be_2
95 #define H2BE_4 endian_h2be_4
96 #define H2BE_8 endian_h2be_8
97 #define H2BE_16 endian_h2be_16
98 #define BE2H_1 endian_be2h_1
99 #define BE2H_2 endian_be2h_2
100 #define BE2H_4 endian_be2h_4
101 #define BE2H_8 endian_be2h_8
102 #define BE2H_16 endian_be2h_16
103
104
105 /* HOST to LE */
106
107 #define H2LE_1 endian_h2le_1
108 #define H2LE_2 endian_h2le_2
109 #define H2LE_4 endian_h2le_4
110 #define H2LE_8 endian_h2le_8
111 #define H2LE_16 endian_h2le_16
112 #define LE2H_1 endian_le2h_1
113 #define LE2H_2 endian_le2h_2
114 #define LE2H_4 endian_le2h_4
115 #define LE2H_8 endian_le2h_8
116 #define LE2H_16 endian_le2h_16
117
118
119 /* HOST to TARGET */
120
121 #define H2T_1 endian_h2t_1
122 #define H2T_2 endian_h2t_2
123 #define H2T_4 endian_h2t_4
124 #define H2T_8 endian_h2t_8
125 #define H2T_16 endian_h2t_16
126 #define T2H_1 endian_t2h_1
127 #define T2H_2 endian_t2h_2
128 #define T2H_4 endian_t2h_4
129 #define T2H_8 endian_t2h_8
130 #define T2H_16 endian_t2h_16
131
132
133 /* CONVERT IN PLACE
134
135 These macros, given an argument of unknown size, swap its value in
136 place if a host/target conversion is required. */
137
138 #define H2T(VARIABLE) \
139 do { \
140 void *vp = &(VARIABLE); \
141 switch (sizeof (VARIABLE)) { \
142 case 1: *(unsigned_1*)vp = H2T_1(*(unsigned_1*)vp); break; \
143 case 2: *(unsigned_2*)vp = H2T_2(*(unsigned_2*)vp); break; \
144 case 4: *(unsigned_4*)vp = H2T_4(*(unsigned_4*)vp); break; \
145 case 8: *(unsigned_8*)vp = H2T_8(*(unsigned_8*)vp); break; \
146 case 16: *(unsigned_16*)vp = H2T_16(*(unsigned_16*)vp); break; \
147 } \
148 } while (0)
149
150 #define T2H(VARIABLE) \
151 do { \
152 switch (sizeof(VARIABLE)) { \
153 case 1: VARIABLE = T2H_1(VARIABLE); break; \
154 case 2: VARIABLE = T2H_2(VARIABLE); break; \
155 case 4: VARIABLE = T2H_4(VARIABLE); break; \
156 case 8: VARIABLE = T2H_8(VARIABLE); break; \
157 /*case 16: VARIABLE = T2H_16(VARIABLE); break;*/ \
158 } \
159 } while (0)
160
161 #define SWAP(VARIABLE) \
162 do { \
163 switch (sizeof(VARIABLE)) { \
164 case 1: VARIABLE = SWAP_1(VARIABLE); break; \
165 case 2: VARIABLE = SWAP_2(VARIABLE); break; \
166 case 4: VARIABLE = SWAP_4(VARIABLE); break; \
167 case 8: VARIABLE = SWAP_8(VARIABLE); break; \
168 /*case 16: VARIABLE = SWAP_16(VARIABLE); break;*/ \
169 } \
170 } while (0)
171
172 #define H2BE(VARIABLE) \
173 do { \
174 switch (sizeof(VARIABLE)) { \
175 case 1: VARIABLE = H2BE_1(VARIABLE); break; \
176 case 2: VARIABLE = H2BE_2(VARIABLE); break; \
177 case 4: VARIABLE = H2BE_4(VARIABLE); break; \
178 case 8: VARIABLE = H2BE_8(VARIABLE); break; \
179 /*case 16: VARIABLE = H2BE_16(VARIABLE); break;*/ \
180 } \
181 } while (0)
182
183 #define BE2H(VARIABLE) \
184 do { \
185 switch (sizeof(VARIABLE)) { \
186 case 1: VARIABLE = BE2H_1(VARIABLE); break; \
187 case 2: VARIABLE = BE2H_2(VARIABLE); break; \
188 case 4: VARIABLE = BE2H_4(VARIABLE); break; \
189 case 8: VARIABLE = BE2H_8(VARIABLE); break; \
190 /*case 16: VARIABLE = BE2H_16(VARIABLE); break;*/ \
191 } \
192 } while (0)
193
194 #define H2LE(VARIABLE) \
195 do { \
196 switch (sizeof(VARIABLE)) { \
197 case 1: VARIABLE = H2LE_1(VARIABLE); break; \
198 case 2: VARIABLE = H2LE_2(VARIABLE); break; \
199 case 4: VARIABLE = H2LE_4(VARIABLE); break; \
200 case 8: VARIABLE = H2LE_8(VARIABLE); break; \
201 /*case 16: VARIABLE = H2LE_16(VARIABLE); break;*/ \
202 } \
203 } while (0)
204
205 #define LE2H(VARIABLE) \
206 do { \
207 switch (sizeof(VARIABLE)) { \
208 case 1: VARIABLE = LE2H_1(VARIABLE); break; \
209 case 2: VARIABLE = LE2H_2(VARIABLE); break; \
210 case 4: VARIABLE = LE2H_4(VARIABLE); break; \
211 case 8: VARIABLE = LE2H_8(VARIABLE); break; \
212 /*case 16: VARIABLE = LE2H_16(VARIABLE); break;*/ \
213 } \
214 } while (0)
215
216
217
218 /* TARGET WORD:
219
220 Byte swap a quantity the size of the targets word */
221
222 #if (WITH_TARGET_WORD_BITSIZE == 64)
223 #define H2T_word H2T_8
224 #define T2H_word T2H_8
225 #define H2BE_word H2BE_8
226 #define BE2H_word BE2H_8
227 #define H2LE_word H2LE_8
228 #define LE2H_word LE2H_8
229 #define SWAP_word SWAP_8
230 #endif
231 #if (WITH_TARGET_WORD_BITSIZE == 32)
232 #define H2T_word H2T_4
233 #define T2H_word T2H_4
234 #define H2BE_word H2BE_4
235 #define BE2H_word BE2H_4
236 #define H2LE_word H2LE_4
237 #define LE2H_word LE2H_4
238 #define SWAP_word SWAP_4
239 #endif
240
241
242
243 /* TARGET CELL:
244
245 Byte swap a quantity the size of the targets IEEE 1275 memory cell */
246
247 #define H2T_cell H2T_4
248 #define T2H_cell T2H_4
249 #define H2BE_cell H2BE_4
250 #define BE2H_cell BE2H_4
251 #define H2LE_cell H2LE_4
252 #define LE2H_cell LE2H_4
253 #define SWAP_cell SWAP_4
254
255
256
257 /* HOST Offsets:
258
259 Address of high/low sub-word within a host word quantity.
260
261 Address of sub-word N within a host word quantity. NOTE: Numbering
262 is BIG endian always. */
263
264 #define AH1_2(X) (unsigned_1*)offset_2((X), 1, 0)
265 #define AL1_2(X) (unsigned_1*)offset_2((X), 1, 1)
266
267 #define AH2_4(X) (unsigned_2*)offset_4((X), 2, 0)
268 #define AL2_4(X) (unsigned_2*)offset_4((X), 2, 1)
269
270 #define AH4_8(X) (unsigned_4*)offset_8((X), 4, 0)
271 #define AL4_8(X) (unsigned_4*)offset_8((X), 4, 1)
272
273 #define AH8_16(X) (unsigned_8*)offset_16((X), 8, 0)
274 #define AL8_16(X) (unsigned_8*)offset_16((X), 8, 1)
275
276 #if (WITH_TARGET_WORD_BITSIZE == 64)
277 #define AH_word(X) AH4_8(X)
278 #define AL_word(X) AL4_8(X)
279 #endif
280 #if (WITH_TARGET_WORD_BITSIZE == 32)
281 #define AH_word(X) AH2_4(X)
282 #define AL_word(X) AL2_4(X)
283 #endif
284
285
286 #define A1_2(X,N) (unsigned_1*)offset_2((X), 1, (N))
287
288 #define A1_4(X,N) (unsigned_1*)offset_4((X), 1, (N))
289 #define A2_4(X,N) (unsigned_2*)offset_4((X), 2, (N))
290
291 #define A1_8(X,N) (unsigned_1*)offset_8((X), 1, (N))
292 #define A2_8(X,N) (unsigned_2*)offset_8((X), 2, (N))
293 #define A4_8(X,N) (unsigned_4*)offset_8((X), 4, (N))
294
295 #define A1_16(X,N) (unsigned_1*)offset_16((X), 1, (N))
296 #define A2_16(X,N) (unsigned_2*)offset_16((X), 2, (N))
297 #define A4_16(X,N) (unsigned_4*)offset_16((X), 4, (N))
298 #define A8_16(X,N) (unsigned_8*)offset_16((X), 8, (N))
299
300
301
302
303 /* HOST Components:
304
305 Value of sub-word within a host word quantity */
306
307 #define VH1_2(X) ((unsigned_1)((unsigned_2)(X) >> 8))
308 #define VL1_2(X) (unsigned_1)(X)
309
310 #define VH2_4(X) ((unsigned_2)((unsigned_4)(X) >> 16))
311 #define VL2_4(X) ((unsigned_2)(X))
312
313 #define VH4_8(X) ((unsigned_4)((unsigned_8)(X) >> 32))
314 #define VL4_8(X) ((unsigned_4)(X))
315
316 #define VH8_16(X) (sim_endian_split_16 ((X), 0))
317 #define VL8_16(X) (sim_endian_split_16 ((X), 1))
318
319 #if (WITH_TARGET_WORD_BITSIZE == 64)
320 #define VH_word(X) VH4_8(X)
321 #define VL_word(X) VL4_8(X)
322 #endif
323 #if (WITH_TARGET_WORD_BITSIZE == 32)
324 #define VH_word(X) VH2_4(X)
325 #define VL_word(X) VL2_4(X)
326 #endif
327
328
329 #define V1_2(X,N) ((unsigned_1)((unsigned_2)(X) >> ( 8 * (1 - (N)))))
330
331 #define V1_4(X,N) ((unsigned_1)((unsigned_4)(X) >> ( 8 * (3 - (N)))))
332 #define V2_4(X,N) ((unsigned_2)((unsigned_4)(X) >> (16 * (1 - (N)))))
333
334 #define V1_8(X,N) ((unsigned_1)((unsigned_8)(X) >> ( 8 * (7 - (N)))))
335 #define V2_8(X,N) ((unsigned_2)((unsigned_8)(X) >> (16 * (3 - (N)))))
336 #define V4_8(X,N) ((unsigned_4)((unsigned_8)(X) >> (32 * (1 - (N)))))
337
338 #define V1_16(X,N) (*A1_16 (&(X),N))
339 #define V2_16(X,N) (*A2_16 (&(X),N))
340 #define V4_16(X,N) (*A4_16 (&(X),N))
341 #define V8_16(X,N) (*A8_16 (&(X),N))
342
343
344 /* Reverse - insert sub-word into word quantity */
345
346 #define V2_H1(X) ((unsigned_2)(unsigned_1)(X) << 8)
347 #define V2_L1(X) ((unsigned_2)(unsigned_1)(X))
348
349 #define V4_H2(X) ((unsigned_4)(unsigned_2)(X) << 16)
350 #define V4_L2(X) ((unsigned_4)(unsigned_2)(X))
351
352 #define V8_H4(X) ((unsigned_8)(unsigned_4)(X) << 32)
353 #define V8_L4(X) ((unsigned_8)(unsigned_4)(X))
354
355 #define V16_H8(X) ((unsigned_16)(unsigned_8)(X) << 64)
356 #define V16_L8(X) ((unsigned_16)(unsigned_8)(X))
357
358
359 #define V2_1(X,N) ((unsigned_2)(unsigned_1)(X) << ( 8 * (1 - (N))))
360
361 #define V4_1(X,N) ((unsigned_4)(unsigned_1)(X) << ( 8 * (3 - (N))))
362 #define V4_2(X,N) ((unsigned_4)(unsigned_2)(X) << (16 * (1 - (N))))
363
364 #define V8_1(X,N) ((unsigned_8)(unsigned_1)(X) << ( 8 * (7 - (N))))
365 #define V8_2(X,N) ((unsigned_8)(unsigned_2)(X) << (16 * (3 - (N))))
366 #define V8_4(X,N) ((unsigned_8)(unsigned_4)(X) << (32 * (1 - (N))))
367
368 #define V16_1(X,N) ((unsigned_16)(unsigned_1)(X) << ( 8 * (15 - (N))))
369 #define V16_2(X,N) ((unsigned_16)(unsigned_2)(X) << (16 * (7 - (N))))
370 #define V16_4(X,N) ((unsigned_16)(unsigned_4)(X) << (32 * (3 - (N))))
371 #define V16_8(X,N) ((unsigned_16)(unsigned_8)(X) << (64 * (1 - (N))))
372
373
374 /* Reverse - insert N sub-words into single word quantity */
375
376 #define U2_1(I0,I1) (V2_1(I0,0) | V2_1(I1,1))
377 #define U4_1(I0,I1,I2,I3) (V4_1(I0,0) | V4_1(I1,1) | V4_1(I2,2) | V4_1(I3,3))
378 #define U8_1(I0,I1,I2,I3,I4,I5,I6,I7) \
379 (V8_1(I0,0) | V8_1(I1,1) | V8_1(I2,2) | V8_1(I3,3) \
380 | V8_1(I4,4) | V8_1(I5,5) | V8_1(I6,6) | V8_1(I7,7))
381 #define U16_1(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11,I12,I13,I14,I15) \
382 (V16_1(I0,0) | V16_1(I1,1) | V16_1(I2,2) | V16_1(I3,3) \
383 | V16_1(I4,4) | V16_1(I5,5) | V16_1(I6,6) | V16_1(I7,7) \
384 | V16_1(I8,8) | V16_1(I9,9) | V16_1(I10,10) | V16_1(I11,11) \
385 | V16_1(I12,12) | V16_1(I13,13) | V16_1(I14,14) | V16_1(I15,15))
386
387 #define U4_2(I0,I1) (V4_2(I0,0) | V4_2(I1,1))
388 #define U8_2(I0,I1,I2,I3) (V8_2(I0,0) | V8_2(I1,1) | V8_2(I2,2) | V8_2(I3,3))
389 #define U16_2(I0,I1,I2,I3,I4,I5,I6,I7) \
390 (V16_2(I0,0) | V16_2(I1,1) | V16_2(I2,2) | V16_2(I3,3) \
391 | V16_2(I4,4) | V16_2(I5,5) | V16_2(I6,6) | V16_2(I7,7) )
392
393 #define U8_4(I0,I1) (V8_4(I0,0) | V8_4(I1,1))
394 #define U16_4(I0,I1,I2,I3) (V16_4(I0,0) | V16_4(I1,1) | V16_4(I2,2) | V16_4(I3,3))
395
396 #define U16_8(I0,I1) (sim_endian_join_16 (I0, I1))
397
398
399 #if (WITH_TARGET_WORD_BITSIZE == 64)
400 #define Vword_H(X) V8_H4(X)
401 #define Vword_L(X) V8_L4(X)
402 #endif
403 #if (WITH_TARGET_WORD_BITSIZE == 32)
404 #define Vword_H(X) V4_H2(X)
405 #define Vword_L(X) V4_L2(X)
406 #endif
407
408
409
410
411 #if H_REVEALS_MODULE_P (SIM_ENDIAN_INLINE)
412 #include "sim-endian.c"
413 #endif
414
415 #endif /* SIM_ENDIAN_H */