1 /*
2  * Keep all the ugly #ifdef for system stuff here
3  */
4 
5 #ifndef __COMPILER_H__
6 #define __COMPILER_H__
7 
8 #include <stddef.h>
9 #include <stdbool.h>
10 
11 #ifdef USE_HOSTCC
12 
13 #if defined(__BEOS__)	 || \
14     defined(__NetBSD__)  || \
15     defined(__FreeBSD__) || \
16     defined(__sun__)	 || \
17     defined(__APPLE__)
18 # include <inttypes.h>
19 #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__) || defined(__DragonFly__)
20 # include <stdint.h>
21 #endif
22 
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 #if !defined(__WIN32__) && !defined(__MINGW32__)
30 # include <sys/mman.h>
31 #endif
32 
33 /* Not all systems (like Windows) has this define, and yes
34  * we do replace/emulate mmap() on those systems ...
35  */
36 #ifndef MAP_FAILED
37 # define MAP_FAILED ((void *)-1)
38 #endif
39 
40 #include <fcntl.h>
41 #ifndef O_BINARY		/* should be define'd on __WIN32__ */
42 #define O_BINARY	0
43 #endif
44 
45 #ifdef __linux__
46 # include <endian.h>
47 # include <byteswap.h>
48 #elif defined(__MACH__) || defined(__FreeBSD__) || defined(__DragonFly__)
49 # include <machine/endian.h>
50 #endif
51 #if defined(__FreeBSD__) || defined(__DragonFly__)
52 # include <sys/endian.h> /* htole32 and friends */
53 #ifndef __BYTE_ORDER
54 # define __BYTE_ORDER BYTE_ORDER
55 # define __LITTLE_ENDIAN LITTLE_ENDIAN
56 # define __BIG_ENDIAN BIG_ENDIAN
57 #endif
58 #elif defined(__OpenBSD__)
59 # include <endian.h>
60 # define __BYTE_ORDER BYTE_ORDER
61 # define __LITTLE_ENDIAN LITTLE_ENDIAN
62 # define __BIG_ENDIAN BIG_ENDIAN
63 #endif
64 
65 #include <time.h>
66 
67 typedef uint8_t __u8;
68 typedef uint16_t __u16;
69 typedef uint32_t __u32;
70 typedef unsigned int uint;
71 typedef unsigned long ulong;
72 
73 #define uswap_16(x) \
74 	((((x) & 0xff00) >> 8) | \
75 	 (((x) & 0x00ff) << 8))
76 #define uswap_32(x) \
77 	((((x) & 0xff000000) >> 24) | \
78 	 (((x) & 0x00ff0000) >>  8) | \
79 	 (((x) & 0x0000ff00) <<  8) | \
80 	 (((x) & 0x000000ff) << 24))
81 #define _uswap_64(x, sfx) \
82 	((((x) & 0xff00000000000000##sfx) >> 56) | \
83 	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
84 	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
85 	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
86 	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
87 	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
88 	 (((x) & 0x000000000000ff00##sfx) << 40) | \
89 	 (((x) & 0x00000000000000ff##sfx) << 56))
90 #if defined(__GNUC__)
91 # define uswap_64(x) _uswap_64(x, ull)
92 #else
93 # define uswap_64(x) _uswap_64(x, )
94 #endif
95 
96 #if __BYTE_ORDER == __LITTLE_ENDIAN
97 # define cpu_to_le16(x)		(x)
98 # define cpu_to_le32(x)		(x)
99 # define cpu_to_le64(x)		(x)
100 # define le16_to_cpu(x)		(x)
101 # define le32_to_cpu(x)		(x)
102 # define le64_to_cpu(x)		(x)
103 # define cpu_to_be16(x)		uswap_16(x)
104 # define cpu_to_be32(x)		uswap_32(x)
105 # define cpu_to_be64(x)		uswap_64(x)
106 # define be16_to_cpu(x)		uswap_16(x)
107 # define be32_to_cpu(x)		uswap_32(x)
108 # define be64_to_cpu(x)		uswap_64(x)
109 #else
110 # define cpu_to_le16(x)		uswap_16(x)
111 # define cpu_to_le32(x)		uswap_32(x)
112 # define cpu_to_le64(x)		uswap_64(x)
113 # define le16_to_cpu(x)		uswap_16(x)
114 # define le32_to_cpu(x)		uswap_32(x)
115 # define le64_to_cpu(x)		uswap_64(x)
116 # define cpu_to_be16(x)		(x)
117 # define cpu_to_be32(x)		(x)
118 # define cpu_to_be64(x)		(x)
119 # define be16_to_cpu(x)		(x)
120 # define be32_to_cpu(x)		(x)
121 # define be64_to_cpu(x)		(x)
122 #endif
123 
124 #else /* !USE_HOSTCC */
125 
126 /* Type for `void *' pointers. */
127 typedef unsigned long int uintptr_t;
128 
129 #include <linux/string.h>
130 #include <linux/types.h>
131 #include <asm/byteorder.h>
132 
133 #if __SIZEOF_LONG__ == 8
134 # define __WORDSIZE	64
135 #elif __SIZEOF_LONG__ == 4
136 # define __WORDSIZE	32
137 #else
138 /*
139  * Assume 32-bit for now - only newer toolchains support this feature and
140  * this is only required for sandbox support at present.
141  */
142 #define __WORDSIZE	32
143 #endif
144 
145 #endif /* USE_HOSTCC */
146 
147 #define likely(x)	__builtin_expect(!!(x), 1)
148 #define unlikely(x)	__builtin_expect(!!(x), 0)
149 
150 #ifdef __LP64__
151 #define MEM_SUPPORT_64BIT_DATA	1
152 #else
153 #define MEM_SUPPORT_64BIT_DATA	0
154 #endif
155 
host_build(void)156 static inline bool host_build(void) {
157 #ifdef USE_HOSTCC
158 	return true;
159 #else
160 	return false;
161 #endif
162 }
163 
164 #endif
165