blob: 6b0d3bf5374245af589b223b53cb1dc3ceb0e004 [file] [log] [blame]
Mike Frysinger37566092009-07-02 19:23:25 -04001/*
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>
Sebastian Reicheldb43c0b2020-12-15 00:41:53 +01009#include <stdbool.h>
Mike Frysinger37566092009-07-02 19:23:25 -040010
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>
Jonathan Gray3715a542016-09-03 08:26:55 +100019#elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__)
Mike Frysinger37566092009-07-02 19:23:25 -040020# 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
Mike Frysinger37566092009-07-02 19:23:25 -040029#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>
Jeroen Hofstee5bce5dc2011-07-19 10:41:48 +000048#elif defined(__MACH__) || defined(__FreeBSD__)
Mike Frysinger37566092009-07-02 19:23:25 -040049# include <machine/endian.h>
Mike Frysinger37566092009-07-02 19:23:25 -040050#endif
Jeroen Hofsteeed8271d2014-10-07 22:42:27 +020051#ifdef __FreeBSD__
52# include <sys/endian.h> /* htole32 and friends */
Justin Hibbitsf29aa232018-01-29 22:23:36 -060053# define __BYTE_ORDER BYTE_ORDER
54# define __LITTLE_ENDIAN LITTLE_ENDIAN
55# define __BIG_ENDIAN BIG_ENDIAN
Jonathan Gray3715a542016-09-03 08:26:55 +100056#elif defined(__OpenBSD__)
57# include <endian.h>
Jonathan Grayfd184b92016-11-26 15:18:00 +110058# define __BYTE_ORDER BYTE_ORDER
59# define __LITTLE_ENDIAN LITTLE_ENDIAN
60# define __BIG_ENDIAN BIG_ENDIAN
Jeroen Hofsteeed8271d2014-10-07 22:42:27 +020061#endif
62
Jeroen Hofstee9d16e932014-06-25 23:02:21 +020063#include <time.h>
Mike Frysinger37566092009-07-02 19:23:25 -040064
65typedef uint8_t __u8;
66typedef uint16_t __u16;
67typedef uint32_t __u32;
Mike Frysingerb050c722010-04-20 05:49:30 -040068typedef unsigned int uint;
Sergei Trofimovichd11fa9c2019-12-30 15:53:41 +000069typedef unsigned long ulong;
Mike Frysinger37566092009-07-02 19:23:25 -040070
Simon Glass5a4f10d2021-09-25 07:03:13 -060071/* Define these on the host so we can build some target code */
72typedef __u32 u32;
73
Mike Frysinger37566092009-07-02 19:23:25 -040074#define uswap_16(x) \
75 ((((x) & 0xff00) >> 8) | \
76 (((x) & 0x00ff) << 8))
77#define uswap_32(x) \
78 ((((x) & 0xff000000) >> 24) | \
79 (((x) & 0x00ff0000) >> 8) | \
80 (((x) & 0x0000ff00) << 8) | \
81 (((x) & 0x000000ff) << 24))
82#define _uswap_64(x, sfx) \
83 ((((x) & 0xff00000000000000##sfx) >> 56) | \
84 (((x) & 0x00ff000000000000##sfx) >> 40) | \
85 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
86 (((x) & 0x000000ff00000000##sfx) >> 8) | \
87 (((x) & 0x00000000ff000000##sfx) << 8) | \
88 (((x) & 0x0000000000ff0000##sfx) << 24) | \
89 (((x) & 0x000000000000ff00##sfx) << 40) | \
90 (((x) & 0x00000000000000ff##sfx) << 56))
91#if defined(__GNUC__)
92# define uswap_64(x) _uswap_64(x, ull)
93#else
94# define uswap_64(x) _uswap_64(x, )
95#endif
96
Jonathan Grayfd184b92016-11-26 15:18:00 +110097#if __BYTE_ORDER == __LITTLE_ENDIAN
Mike Frysinger37566092009-07-02 19:23:25 -040098# define cpu_to_le16(x) (x)
99# define cpu_to_le32(x) (x)
100# define cpu_to_le64(x) (x)
101# define le16_to_cpu(x) (x)
102# define le32_to_cpu(x) (x)
103# define le64_to_cpu(x) (x)
104# define cpu_to_be16(x) uswap_16(x)
105# define cpu_to_be32(x) uswap_32(x)
106# define cpu_to_be64(x) uswap_64(x)
107# define be16_to_cpu(x) uswap_16(x)
108# define be32_to_cpu(x) uswap_32(x)
109# define be64_to_cpu(x) uswap_64(x)
110#else
111# define cpu_to_le16(x) uswap_16(x)
112# define cpu_to_le32(x) uswap_32(x)
113# define cpu_to_le64(x) uswap_64(x)
114# define le16_to_cpu(x) uswap_16(x)
115# define le32_to_cpu(x) uswap_32(x)
116# define le64_to_cpu(x) uswap_64(x)
117# define cpu_to_be16(x) (x)
118# define cpu_to_be32(x) (x)
119# define cpu_to_be64(x) (x)
120# define be16_to_cpu(x) (x)
121# define be32_to_cpu(x) (x)
122# define be64_to_cpu(x) (x)
123#endif
124
125#else /* !USE_HOSTCC */
126
York Sun2e680f92015-12-16 14:12:24 +0800127/* Type for `void *' pointers. */
128typedef unsigned long int uintptr_t;
Gabe Black0d296cc2014-10-15 04:38:30 -0600129
Mike Frysinger37566092009-07-02 19:23:25 -0400130#include <linux/string.h>
131#include <linux/types.h>
132#include <asm/byteorder.h>
133
Simon Glassa7551a32011-09-23 06:22:06 +0000134#if __SIZEOF_LONG__ == 8
135# define __WORDSIZE 64
136#elif __SIZEOF_LONG__ == 4
137# define __WORDSIZE 32
138#else
139/*
140 * Assume 32-bit for now - only newer toolchains support this feature and
141 * this is only required for sandbox support at present.
142 */
143#define __WORDSIZE 32
144#endif
145
Simon Glassc9502f42011-11-04 06:42:35 +0000146#endif /* USE_HOSTCC */
Mike Frysinger37566092009-07-02 19:23:25 -0400147
Matthias Kaehlckeb63815e2009-12-22 23:05:45 +0100148#define likely(x) __builtin_expect(!!(x), 1)
149#define unlikely(x) __builtin_expect(!!(x), 0)
150
Simon Glass4d979bf2019-12-28 10:45:10 -0700151#ifdef __LP64__
Simon Glass3428faf2020-06-02 19:26:44 -0600152#define MEM_SUPPORT_64BIT_DATA 1
153#else
154#define MEM_SUPPORT_64BIT_DATA 0
Simon Glass4d979bf2019-12-28 10:45:10 -0700155#endif
156
Simon Glassc45b7922021-09-25 07:03:08 -0600157/**
158 * host_build() - check if we are building for the host
159 *
160 * @return true if building for the host, false if for a target
161 */
Sebastian Reicheldb43c0b2020-12-15 00:41:53 +0100162static inline bool host_build(void) {
163#ifdef USE_HOSTCC
164 return true;
165#else
166 return false;
167#endif
168}
169
Mike Frysinger37566092009-07-02 19:23:25 -0400170#endif