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