You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

372 lines
10 KiB

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