OpenOCD
types.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2004, 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  ***************************************************************************/
10 
11 #ifndef OPENOCD_HELPER_TYPES_H
12 #define OPENOCD_HELPER_TYPES_H
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include <stddef.h>
19 #include <assert.h>
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #ifdef HAVE_STDINT_H
24 #include <stdint.h>
25 #endif
26 #ifdef HAVE_INTTYPES_H
27 #include <inttypes.h>
28 #endif
29 
30 #ifdef HAVE_STDBOOL_H
31 #include <stdbool.h>
32 #else /* HAVE_STDBOOL_H */
33 #define __bool_true_false_are_defined 1
34 
35 #ifndef HAVE__BOOL
36 #ifndef __cplusplus
37 
38 #define false 0
39 #define true 1
40 
41 #endif /* __cplusplus */
42 #endif /* HAVE__BOOL */
43 #endif /* HAVE_STDBOOL_H */
44 
46 #define stringify(s) __stringify(s)
47 #define __stringify(s) #s
48 
49 
57 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
58 
59 
68 #define container_of(ptr, type, member) ({ \
69  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
70  (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
71 
72 
79 #define DIV_ROUND_UP(m, n) (((m) + (n) - 1) / (n))
80 
81 
82 /* DANGER!!!! here be dragons!
83  *
84  * Leave these fn's as byte accesses because it is safe
85  * across architectures. Clever usage of 32 bit access
86  * will create problems on some hosts.
87  *
88  * Note that the "buf" pointer in memory is probably unaligned.
89  *
90  * Were these functions to be re-written to take a 32 bit wide or 16 bit wide
91  * memory access shortcut, then on some CPU's, i.e. ARM7, the 2 lsbytes of the address are
92  * ignored for 32 bit access, whereas on other CPU's a 32 bit wide unaligned memory access
93  * will cause an exception, and lastly on x86, an unaligned "greater than bytewide"
94  * memory access works as if aligned. So what follows below will work for all
95  * platforms and gives the compiler leeway to do its own platform specific optimizations.
96  *
97  * Again, note that the "buf" pointer in memory is probably unaligned.
98  */
99 
100 static inline uint64_t le_to_h_u64(const uint8_t *buf)
101 {
102  return (uint64_t)((uint64_t)buf[0] |
103  (uint64_t)buf[1] << 8 |
104  (uint64_t)buf[2] << 16 |
105  (uint64_t)buf[3] << 24 |
106  (uint64_t)buf[4] << 32 |
107  (uint64_t)buf[5] << 40 |
108  (uint64_t)buf[6] << 48 |
109  (uint64_t)buf[7] << 56);
110 }
111 
112 static inline uint32_t le_to_h_u32(const uint8_t *buf)
113 {
114  return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24);
115 }
116 
117 static inline uint32_t le_to_h_u24(const uint8_t *buf)
118 {
119  return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16);
120 }
121 
122 static inline uint16_t le_to_h_u16(const uint8_t *buf)
123 {
124  return (uint16_t)((uint16_t)buf[0] | (uint16_t)buf[1] << 8);
125 }
126 
127 static inline uint64_t be_to_h_u64(const uint8_t *buf)
128 {
129  return (uint64_t)((uint64_t)buf[7] |
130  (uint64_t)buf[6] << 8 |
131  (uint64_t)buf[5] << 16 |
132  (uint64_t)buf[4] << 24 |
133  (uint64_t)buf[3] << 32 |
134  (uint64_t)buf[2] << 40 |
135  (uint64_t)buf[1] << 48 |
136  (uint64_t)buf[0] << 56);
137 }
138 
139 static inline uint32_t be_to_h_u32(const uint8_t *buf)
140 {
141  return (uint32_t)((uint32_t)buf[3] | (uint32_t)buf[2] << 8 | (uint32_t)buf[1] << 16 | (uint32_t)buf[0] << 24);
142 }
143 
144 static inline uint32_t be_to_h_u24(const uint8_t *buf)
145 {
146  return (uint32_t)((uint32_t)buf[2] | (uint32_t)buf[1] << 8 | (uint32_t)buf[0] << 16);
147 }
148 
149 static inline uint16_t be_to_h_u16(const uint8_t *buf)
150 {
151  return (uint16_t)((uint16_t)buf[1] | (uint16_t)buf[0] << 8);
152 }
153 
154 static inline void h_u64_to_le(uint8_t *buf, uint64_t val)
155 {
156  buf[7] = (uint8_t) (val >> 56);
157  buf[6] = (uint8_t) (val >> 48);
158  buf[5] = (uint8_t) (val >> 40);
159  buf[4] = (uint8_t) (val >> 32);
160  buf[3] = (uint8_t) (val >> 24);
161  buf[2] = (uint8_t) (val >> 16);
162  buf[1] = (uint8_t) (val >> 8);
163  buf[0] = (uint8_t) (val >> 0);
164 }
165 
166 static inline void h_u64_to_be(uint8_t *buf, uint64_t val)
167 {
168  buf[0] = (uint8_t) (val >> 56);
169  buf[1] = (uint8_t) (val >> 48);
170  buf[2] = (uint8_t) (val >> 40);
171  buf[3] = (uint8_t) (val >> 32);
172  buf[4] = (uint8_t) (val >> 24);
173  buf[5] = (uint8_t) (val >> 16);
174  buf[6] = (uint8_t) (val >> 8);
175  buf[7] = (uint8_t) (val >> 0);
176 }
177 
178 static inline void h_u32_to_le(uint8_t *buf, uint32_t val)
179 {
180  buf[3] = (val >> 24) & 0xff;
181  buf[2] = (val >> 16) & 0xff;
182  buf[1] = (val >> 8) & 0xff;
183  buf[0] = (val >> 0) & 0xff;
184 }
185 
186 static inline void h_u32_to_be(uint8_t *buf, uint32_t val)
187 {
188  buf[0] = (val >> 24) & 0xff;
189  buf[1] = (val >> 16) & 0xff;
190  buf[2] = (val >> 8) & 0xff;
191  buf[3] = (val >> 0) & 0xff;
192 }
193 
194 static inline void h_u24_to_le(uint8_t *buf, unsigned int val)
195 {
196  buf[2] = (val >> 16) & 0xff;
197  buf[1] = (val >> 8) & 0xff;
198  buf[0] = (val >> 0) & 0xff;
199 }
200 
201 static inline void h_u24_to_be(uint8_t *buf, unsigned int val)
202 {
203  buf[0] = (val >> 16) & 0xff;
204  buf[1] = (val >> 8) & 0xff;
205  buf[2] = (val >> 0) & 0xff;
206 }
207 
208 static inline void h_u16_to_le(uint8_t *buf, uint16_t val)
209 {
210  buf[1] = (val >> 8) & 0xff;
211  buf[0] = (val >> 0) & 0xff;
212 }
213 
214 static inline void h_u16_to_be(uint8_t *buf, uint16_t val)
215 {
216  buf[0] = (val >> 8) & 0xff;
217  buf[1] = (val >> 0) & 0xff;
218 }
219 
229 static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
230 {
231  assert(len % 2 == 0);
232  assert(dst == src || dst + len <= src || src + len <= dst);
233 
234  for (size_t n = 0; n < len; n += 2) {
235  uint16_t x = be_to_h_u16(src + n);
236  h_u16_to_le(dst + n, x);
237  }
238 }
239 
249 static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
250 {
251  assert(len % 4 == 0);
252  assert(dst == src || dst + len <= src || src + len <= dst);
253 
254  for (size_t n = 0; n < len; n += 4) {
255  uint32_t x = be_to_h_u32(src + n);
256  h_u32_to_le(dst + n, x);
257  }
258 }
259 
265 static inline int parity_u32(uint32_t x)
266 {
267 #ifdef __GNUC__
268  return __builtin_parityl(x);
269 #else
270  x ^= x >> 16;
271  x ^= x >> 8;
272  x ^= x >> 4;
273  x ^= x >> 2;
274  x ^= x >> 1;
275  return x & 1;
276 #endif
277 }
278 
279 #if defined(__ECOS)
280 
281 /* eCos plain lacks these definition... A series of upstream patches
282  * could probably repair it, but it seems like too much work to be
283  * worth it.
284  */
285 
286 #if !defined(_STDINT_H)
287 #define PRId32 "d"
288 #define PRIi32 "i"
289 #define PRIo32 "o"
290 #define PRIu32 "u"
291 #define PRIx32 "x"
292 #define PRIX32 "X"
293 #define SCNx32 "x"
294 #define PRId8 PRId32
295 #define SCNx64 "llx"
296 #define PRId64 "lld"
297 #define PRIi64 "lli"
298 #define PRIo64 "llo"
299 #define PRIu64 "llu"
300 #define PRIx64 "llx"
301 #define PRIX64 "llX"
302 
303 typedef CYG_ADDRWORD intptr_t;
304 typedef int64_t intmax_t;
305 typedef uint64_t uintmax_t;
306 #define INT8_MAX 0x7f
307 #define INT8_MIN (-INT8_MAX - 1)
308 # define UINT8_MAX (255)
309 #define INT16_MAX 0x7fff
310 #define INT16_MIN (-INT16_MAX - 1)
311 # define UINT16_MAX (65535)
312 #define INT32_MAX 0x7fffffffL
313 #define INT32_MIN (-INT32_MAX - 1L)
314 # define UINT32_MAX (4294967295U)
315 #define INT64_MAX 0x7fffffffffffffffLL
316 #define INT64_MIN (-INT64_MAX - 1LL)
317 #define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
318 #endif
319 
320  #ifndef LLONG_MAX
321  #define ULLONG_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
322  #define LLONG_MAX INT64_C(0x7FFFFFFFFFFFFFFF)
323  #define LLONG_MIN ULLONG_MAX
324  #endif
325 
326 
327 #define ULLONG_MAX 18446744073709551615
328 
329 /* C99, eCos is C90 compliant (with bits of C99) */
330 #define isblank(c) ((c) == ' ' || (c) == '\t')
331 
332 
333 #endif
334 
335 typedef uint64_t target_addr_t;
336 #define TARGET_ADDR_MAX UINT64_MAX
337 #define TARGET_PRIdADDR PRId64
338 #define TARGET_PRIuADDR PRIu64
339 #define TARGET_PRIoADDR PRIo64
340 #define TARGET_PRIxADDR PRIx64
341 #define TARGET_PRIXADDR PRIX64
342 #define TARGET_ADDR_FMT "0x%8.8" TARGET_PRIxADDR
343 
344 #endif /* OPENOCD_HELPER_TYPES_H */
static void h_u32_to_be(uint8_t *buf, uint32_t val)
Definition: types.h:186
static uint64_t le_to_h_u64(const uint8_t *buf)
Definition: types.h:100
static uint32_t be_to_h_u24(const uint8_t *buf)
Definition: types.h:144
static void h_u16_to_be(uint8_t *buf, uint16_t val)
Definition: types.h:214
static void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
Byte-swap buffer 16-bit.
Definition: types.h:229
static uint64_t be_to_h_u64(const uint8_t *buf)
Definition: types.h:127
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
uint64_t target_addr_t
Definition: types.h:335
static void h_u24_to_le(uint8_t *buf, unsigned int val)
Definition: types.h:194
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
static void h_u16_to_le(uint8_t *buf, uint16_t val)
Definition: types.h:208
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
static void h_u64_to_be(uint8_t *buf, uint64_t val)
Definition: types.h:166
static void h_u64_to_le(uint8_t *buf, uint64_t val)
Definition: types.h:154
static void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
Byte-swap buffer 32-bit.
Definition: types.h:249