1 /***************************************************************************
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21 
22 #ifndef OPENOCD_HELPER_TYPES_H
23 #define OPENOCD_HELPER_TYPES_H
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <stddef.h>
30 #include <assert.h>
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>
36 #endif
37 #ifdef HAVE_INTTYPES_H
38 #include <inttypes.h>
39 #endif
40 
41 #ifdef HAVE_STDBOOL_H
42 #include <stdbool.h>
43 #else	/* HAVE_STDBOOL_H */
44 #define __bool_true_false_are_defined 1
45 
46 #ifndef HAVE__BOOL
47 #ifndef __cplusplus
48 
49 #define false	0
50 #define true		1
51 
52 typedef int _Bool;
53 #else
54 typedef bool _Bool;
55 #endif	/* __cplusplus */
56 #endif	/* HAVE__BOOL */
57 
58 #define bool _Bool
59 
60 #endif	/* HAVE_STDBOOL_H */
61 
62 /// turns a macro argument into a string constant
63 #define stringify(s) __stringify(s)
64 #define __stringify(s) #s
65 
66 
67 /**
68  * Compute the number of elements of a variable length array.
69  * <code>
70  * const char *strs[] = { "a", "b", "c" };
71  * unsigned num_strs = ARRAY_SIZE(strs);
72  * </code>
73  */
74 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
75 
76 
77 /**
78  * Cast a member of a structure out to the containing structure.
79  * @param ptr The pointer to the member.
80  * @param type The type of the container struct this is embedded in.
81  * @param member The name of the member within the struct.
82  *
83  * This is a mechanism which is used throughout the Linux kernel.
84  */
85 #define container_of(ptr, type, member) ({			\
86 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
87 	(type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
88 
89 
90 /**
91  * Rounds @c m up to the nearest multiple of @c n using division.
92  * @param m The value to round up to @c n.
93  * @param n Round @c m up to a multiple of this number.
94  * @returns The rounded integer value.
95  */
96 #define DIV_ROUND_UP(m, n)	(((m) + (n) - 1) / (n))
97 
98 
99 /* DANGER!!!! here be dragons!
100  *
101  * Leave these fn's as byte accesses because it is safe
102  * across architectures. Clever usage of 32 bit access
103  * will create problems on some hosts.
104  *
105  * Note that the "buf" pointer in memory is probably unaligned.
106  *
107  * Were these functions to be re-written to take a 32 bit wide or 16 bit wide
108  * memory access shortcut, then on some CPU's, i.e. ARM7, the 2 lsbytes of the address are
109  * ignored for 32 bit access, whereas on other CPU's a 32 bit wide unaligned memory access
110  * will cause an exception, and lastly on x86, an unaligned "greater than bytewide"
111  * memory access works as if aligned.  So what follows below will work for all
112  * platforms and gives the compiler leeway to do its own platform specific optimizations.
113  *
114  * Again, note that the "buf" pointer in memory is probably unaligned.
115  */
116 
le_to_h_u64(const uint8_t * buf)117 static inline uint64_t le_to_h_u64(const uint8_t *buf)
118 {
119 	return (uint64_t)((uint64_t)buf[0] |
120 			  (uint64_t)buf[1] << 8 |
121 			  (uint64_t)buf[2] << 16 |
122 			  (uint64_t)buf[3] << 24 |
123 			  (uint64_t)buf[4] << 32 |
124 			  (uint64_t)buf[5] << 40 |
125 			  (uint64_t)buf[6] << 48 |
126 			  (uint64_t)buf[7] << 56);
127 }
128 
le_to_h_u32(const uint8_t * buf)129 static inline uint32_t le_to_h_u32(const uint8_t *buf)
130 {
131 	return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24);
132 }
133 
le_to_h_u24(const uint8_t * buf)134 static inline uint32_t le_to_h_u24(const uint8_t *buf)
135 {
136 	return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16);
137 }
138 
le_to_h_u16(const uint8_t * buf)139 static inline uint16_t le_to_h_u16(const uint8_t *buf)
140 {
141 	return (uint16_t)((uint16_t)buf[0] | (uint16_t)buf[1] << 8);
142 }
143 
be_to_h_u64(const uint8_t * buf)144 static inline uint64_t be_to_h_u64(const uint8_t *buf)
145 {
146 	return (uint64_t)((uint64_t)buf[7] |
147 			  (uint64_t)buf[6] << 8 |
148 			  (uint64_t)buf[5] << 16 |
149 			  (uint64_t)buf[4] << 24 |
150 			  (uint64_t)buf[3] << 32 |
151 			  (uint64_t)buf[2] << 40 |
152 			  (uint64_t)buf[1] << 48 |
153 			  (uint64_t)buf[0] << 56);
154 }
155 
be_to_h_u32(const uint8_t * buf)156 static inline uint32_t be_to_h_u32(const uint8_t *buf)
157 {
158 	return (uint32_t)((uint32_t)buf[3] | (uint32_t)buf[2] << 8 | (uint32_t)buf[1] << 16 | (uint32_t)buf[0] << 24);
159 }
160 
be_to_h_u24(const uint8_t * buf)161 static inline uint32_t be_to_h_u24(const uint8_t *buf)
162 {
163 	return (uint32_t)((uint32_t)buf[2] | (uint32_t)buf[1] << 8 | (uint32_t)buf[0] << 16);
164 }
165 
be_to_h_u16(const uint8_t * buf)166 static inline uint16_t be_to_h_u16(const uint8_t *buf)
167 {
168 	return (uint16_t)((uint16_t)buf[1] | (uint16_t)buf[0] << 8);
169 }
170 
h_u64_to_le(uint8_t * buf,int64_t val)171 static inline void h_u64_to_le(uint8_t *buf, int64_t val)
172 {
173 	buf[7] = (uint8_t) (val >> 56);
174 	buf[6] = (uint8_t) (val >> 48);
175 	buf[5] = (uint8_t) (val >> 40);
176 	buf[4] = (uint8_t) (val >> 32);
177 	buf[3] = (uint8_t) (val >> 24);
178 	buf[2] = (uint8_t) (val >> 16);
179 	buf[1] = (uint8_t) (val >> 8);
180 	buf[0] = (uint8_t) (val >> 0);
181 }
182 
h_u64_to_be(uint8_t * buf,int64_t val)183 static inline void h_u64_to_be(uint8_t *buf, int64_t val)
184 {
185 	buf[0] = (uint8_t) (val >> 56);
186 	buf[1] = (uint8_t) (val >> 48);
187 	buf[2] = (uint8_t) (val >> 40);
188 	buf[3] = (uint8_t) (val >> 32);
189 	buf[4] = (uint8_t) (val >> 24);
190 	buf[5] = (uint8_t) (val >> 16);
191 	buf[6] = (uint8_t) (val >> 8);
192 	buf[7] = (uint8_t) (val >> 0);
193 }
194 
h_u32_to_le(uint8_t * buf,int val)195 static inline void h_u32_to_le(uint8_t *buf, int val)
196 {
197 	buf[3] = (uint8_t) (val >> 24);
198 	buf[2] = (uint8_t) (val >> 16);
199 	buf[1] = (uint8_t) (val >> 8);
200 	buf[0] = (uint8_t) (val >> 0);
201 }
202 
h_u32_to_be(uint8_t * buf,int val)203 static inline void h_u32_to_be(uint8_t *buf, int val)
204 {
205 	buf[0] = (uint8_t) (val >> 24);
206 	buf[1] = (uint8_t) (val >> 16);
207 	buf[2] = (uint8_t) (val >> 8);
208 	buf[3] = (uint8_t) (val >> 0);
209 }
210 
h_u24_to_le(uint8_t * buf,int val)211 static inline void h_u24_to_le(uint8_t *buf, int val)
212 {
213 	buf[2] = (uint8_t) (val >> 16);
214 	buf[1] = (uint8_t) (val >> 8);
215 	buf[0] = (uint8_t) (val >> 0);
216 }
217 
h_u24_to_be(uint8_t * buf,int val)218 static inline void h_u24_to_be(uint8_t *buf, int val)
219 {
220 	buf[0] = (uint8_t) (val >> 16);
221 	buf[1] = (uint8_t) (val >> 8);
222 	buf[2] = (uint8_t) (val >> 0);
223 }
224 
h_u16_to_le(uint8_t * buf,int val)225 static inline void h_u16_to_le(uint8_t *buf, int val)
226 {
227 	buf[1] = (uint8_t) (val >> 8);
228 	buf[0] = (uint8_t) (val >> 0);
229 }
230 
h_u16_to_be(uint8_t * buf,int val)231 static inline void h_u16_to_be(uint8_t *buf, int val)
232 {
233 	buf[0] = (uint8_t) (val >> 8);
234 	buf[1] = (uint8_t) (val >> 0);
235 }
236 
237 /**
238  * Byte-swap buffer 16-bit.
239  *
240  * Len must be even, dst and src must be either the same or non-overlapping.
241  *
242  * @param dst Destination buffer.
243  * @param src Source buffer.
244  * @param len Length of source (and destination) buffer, in bytes.
245  */
buf_bswap16(uint8_t * dst,const uint8_t * src,size_t len)246 static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
247 {
248 	assert(len % 2 == 0);
249 	assert(dst == src || dst + len <= src || src + len <= dst);
250 
251 	for (size_t n = 0; n < len; n += 2) {
252 		uint16_t x = be_to_h_u16(src + n);
253 		h_u16_to_le(dst + n, x);
254 	}
255 }
256 
257 /**
258  * Byte-swap buffer 32-bit.
259  *
260  * Len must be divisible by four, dst and src must be either the same or non-overlapping.
261  *
262  * @param dst Destination buffer.
263  * @param src Source buffer.
264  * @param len Length of source (and destination) buffer, in bytes.
265  */
buf_bswap32(uint8_t * dst,const uint8_t * src,size_t len)266 static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
267 {
268 	assert(len % 4 == 0);
269 	assert(dst == src || dst + len <= src || src + len <= dst);
270 
271 	for (size_t n = 0; n < len; n += 4) {
272 		uint32_t x = be_to_h_u32(src + n);
273 		h_u32_to_le(dst + n, x);
274 	}
275 }
276 
277 /**
278  * Calculate the (even) parity of a 32-bit datum.
279  * @param x The datum.
280  * @return 1 if the number of set bits in x is odd, 0 if it is even.
281  */
parity_u32(uint32_t x)282 static inline int parity_u32(uint32_t x)
283 {
284 #ifdef __GNUC__
285 	return __builtin_parityl(x);
286 #else
287 	x ^= x >> 16;
288 	x ^= x >> 8;
289 	x ^= x >> 4;
290 	x ^= x >> 2;
291 	x ^= x >> 1;
292 	return x & 1;
293 #endif
294 }
295 
296 #if defined(__ECOS)
297 
298 /* eCos plain lacks these definition... A series of upstream patches
299  * could probably repair it, but it seems like too much work to be
300  * worth it.
301  */
302 
303 #if !defined(_STDINT_H)
304 #define PRId32 "d"
305 #define PRIi32 "i"
306 #define PRIo32 "o"
307 #define PRIu32 "u"
308 #define PRIx32 "x"
309 #define PRIX32 "X"
310 #define SCNx32 "x"
311 #define PRId8 PRId32
312 #define SCNx64 "llx"
313 #define PRId64 "lld"
314 #define PRIi64 "lli"
315 #define PRIo64 "llo"
316 #define PRIu64 "llu"
317 #define PRIx64 "llx"
318 #define PRIX64 "llX"
319 
320 typedef CYG_ADDRWORD intptr_t;
321 typedef int64_t intmax_t;
322 typedef uint64_t uintmax_t;
323 #define INT8_MAX 0x7f
324 #define INT8_MIN (-INT8_MAX - 1)
325 # define UINT8_MAX		(255)
326 #define INT16_MAX 0x7fff
327 #define INT16_MIN (-INT16_MAX - 1)
328 # define UINT16_MAX		(65535)
329 #define INT32_MAX 0x7fffffffL
330 #define INT32_MIN (-INT32_MAX - 1L)
331 # define UINT32_MAX		(4294967295U)
332 #define INT64_MAX 0x7fffffffffffffffLL
333 #define INT64_MIN (-INT64_MAX - 1LL)
334 #define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
335 #endif
336 
337 	#ifndef LLONG_MAX
338 	#define ULLONG_MAX	UINT64_C(0xFFFFFFFFFFFFFFFF)
339 	#define LLONG_MAX	INT64_C(0x7FFFFFFFFFFFFFFF)
340 	#define LLONG_MIN	ULLONG_MAX
341 	#endif
342 
343 
344 #define ULLONG_MAX 18446744073709551615
345 
346 /* C99, eCos is C90 compliant (with bits of C99) */
347 #define isblank(c) ((c) == ' ' || (c) == '\t')
348 
349 
350 #endif
351 
352 typedef uint64_t target_addr_t;
353 #define TARGET_ADDR_MAX UINT64_MAX
354 #define TARGET_PRIdADDR PRId64
355 #define TARGET_PRIuADDR PRIu64
356 #define TARGET_PRIoADDR PRIo64
357 #define TARGET_PRIxADDR PRIx64
358 #define TARGET_PRIXADDR PRIX64
359 #define TARGET_ADDR_FMT "0x%8.8" TARGET_PRIxADDR
360 
361 #endif /* OPENOCD_HELPER_TYPES_H */
362