Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
incbin.h
Go to the documentation of this file.
1
11#ifndef INCBIN_HDR
12#define INCBIN_HDR
13#include <limits.h>
14#if defined(__AVX512BW__) || \
15 defined(__AVX512CD__) || \
16 defined(__AVX512DQ__) || \
17 defined(__AVX512ER__) || \
18 defined(__AVX512PF__) || \
19 defined(__AVX512VL__) || \
20 defined(__AVX512F__)
21# define INCBIN_ALIGNMENT_INDEX 6
22#elif defined(__AVX__) || \
23 defined(__AVX2__)
24# define INCBIN_ALIGNMENT_INDEX 5
25#elif defined(__SSE__) || \
26 defined(__SSE2__) || \
27 defined(__SSE3__) || \
28 defined(__SSSE3__) || \
29 defined(__SSE4_1__) || \
30 defined(__SSE4_2__) || \
31 defined(__neon__) || \
32 defined(__ARM_NEON) || \
33 defined(__ALTIVEC__)
34# define INCBIN_ALIGNMENT_INDEX 4
35#elif ULONG_MAX != 0xffffffffu
36# define INCBIN_ALIGNMENT_INDEX 3
37# else
38# define INCBIN_ALIGNMENT_INDEX 2
39#endif
40
41/* Lookup table of (1 << n) where `n' is `INCBIN_ALIGNMENT_INDEX' */
42#define INCBIN_ALIGN_SHIFT_0 1
43#define INCBIN_ALIGN_SHIFT_1 2
44#define INCBIN_ALIGN_SHIFT_2 4
45#define INCBIN_ALIGN_SHIFT_3 8
46#define INCBIN_ALIGN_SHIFT_4 16
47#define INCBIN_ALIGN_SHIFT_5 32
48#define INCBIN_ALIGN_SHIFT_6 64
49
50/* Actual alignment value */
51#define INCBIN_ALIGNMENT \
52 INCBIN_CONCATENATE( \
53 INCBIN_CONCATENATE(INCBIN_ALIGN_SHIFT, _), \
54 INCBIN_ALIGNMENT_INDEX)
55
56/* Stringize */
57#define INCBIN_STR(X) \
58 #X
59#define INCBIN_STRINGIZE(X) \
60 INCBIN_STR(X)
61/* Concatenate */
62#define INCBIN_CAT(X, Y) \
63 X ## Y
64#define INCBIN_CONCATENATE(X, Y) \
65 INCBIN_CAT(X, Y)
66/* Deferred macro expansion */
67#define INCBIN_EVAL(X) \
68 X
69#define INCBIN_INVOKE(N, ...) \
70 INCBIN_EVAL(N(__VA_ARGS__))
71/* Variable argument count for overloading by arity */
72#define INCBIN_VA_ARG_COUNTER(_1, _2, _3, N, ...) N
73#define INCBIN_VA_ARGC(...) INCBIN_VA_ARG_COUNTER(__VA_ARGS__, 3, 2, 1, 0)
74
75/* Green Hills uses a different directive for including binary data */
76#if defined(__ghs__)
77# if (__ghs_asm == 2)
78# define INCBIN_MACRO ".file"
79/* Or consider the ".myrawdata" entry in the ld file */
80# else
81# define INCBIN_MACRO "\tINCBIN"
82# endif
83#else
84# define INCBIN_MACRO ".incbin"
85#endif
86
87#ifndef _MSC_VER
88# define INCBIN_ALIGN \
89 __attribute__((aligned(INCBIN_ALIGNMENT)))
90#else
91# define INCBIN_ALIGN __declspec(align(INCBIN_ALIGNMENT))
92#endif
93
94#if defined(__arm__) || /* GNU C and RealView */ \
95 defined(__arm) || /* Diab */ \
96 defined(_ARM) /* ImageCraft */
97# define INCBIN_ARM
98#endif
99
100#ifdef __GNUC__
101/* Utilize .balign where supported */
102# define INCBIN_ALIGN_HOST ".balign " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
103# define INCBIN_ALIGN_BYTE ".balign 1\n"
104#elif defined(INCBIN_ARM)
105/*
106 * On arm assemblers, the alignment value is calculated as (1 << n) where `n' is
107 * the shift count. This is the value passed to `.align'
108 */
109# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT_INDEX) "\n"
110# define INCBIN_ALIGN_BYTE ".align 0\n"
111#else
112/* We assume other inline assembler's treat `.align' as `.balign' */
113# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
114# define INCBIN_ALIGN_BYTE ".align 1\n"
115#endif
116
117/* INCBIN_CONST is used by incbin.c generated files */
118#if defined(__cplusplus)
119# define INCBIN_EXTERNAL extern "C"
120# define INCBIN_CONST extern const
121#else
122# define INCBIN_EXTERNAL extern
123# define INCBIN_CONST const
124#endif
125
133#if !defined(INCBIN_OUTPUT_SECTION)
134# if defined(__APPLE__)
135# define INCBIN_OUTPUT_SECTION ".const_data"
136# else
137# define INCBIN_OUTPUT_SECTION ".rodata"
138# endif
139#endif
140
147#if !defined(INCBIN_OUTPUT_DATA_SECTION)
148# define INCBIN_OUTPUT_DATA_SECTION INCBIN_OUTPUT_SECTION
149#endif
150
161#if !defined(INCBIN_OUTPUT_SIZE_SECTION)
162# define INCBIN_OUTPUT_SIZE_SECTION INCBIN_OUTPUT_SECTION
163#endif
164
165#if defined(__APPLE__)
166# include "TargetConditionals.h"
167# if defined(TARGET_OS_IPHONE) && !defined(INCBIN_SILENCE_BITCODE_WARNING)
168# warning "incbin is incompatible with bitcode. Using the library will break upload to App Store if you have bitcode enabled. Add `#define INCBIN_SILENCE_BITCODE_WARNING` before including this header to silence this warning."
169# endif
170/* The directives are different for Apple branded compilers */
171# define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n"
172# define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
173# define INCBIN_INT ".long "
174# define INCBIN_MANGLE "_"
175# define INCBIN_BYTE ".byte "
176# define INCBIN_TYPE(...)
177#else
178# define INCBIN_SECTION ".section " INCBIN_OUTPUT_SECTION "\n"
179# define INCBIN_GLOBAL(NAME) ".global " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
180# if defined(__ghs__)
181# define INCBIN_INT ".word "
182# else
183# define INCBIN_INT ".int "
184# endif
185# if defined(__USER_LABEL_PREFIX__)
186# define INCBIN_MANGLE INCBIN_STRINGIZE(__USER_LABEL_PREFIX__)
187# else
188# define INCBIN_MANGLE ""
189# endif
190# if defined(INCBIN_ARM)
191/* On arm assemblers, `@' is used as a line comment token */
192# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", %object\n"
193# elif defined(__MINGW32__) || defined(__MINGW64__)
194/* Mingw doesn't support this directive either */
195# define INCBIN_TYPE(NAME)
196# else
197/* It's safe to use `@' on other architectures */
198# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", @object\n"
199# endif
200# define INCBIN_BYTE ".byte "
201#endif
202
203/* List of style types used for symbol names */
204#define INCBIN_STYLE_CAMEL 0
205#define INCBIN_STYLE_SNAKE 1
206
223#if !defined(INCBIN_PREFIX)
224# define INCBIN_PREFIX g
225#endif
226
247#if !defined(INCBIN_STYLE)
248# define INCBIN_STYLE INCBIN_STYLE_CAMEL
249#endif
250
251/* Style lookup tables */
252#define INCBIN_STYLE_0_DATA Data
253#define INCBIN_STYLE_0_END End
254#define INCBIN_STYLE_0_SIZE Size
255#define INCBIN_STYLE_1_DATA _data
256#define INCBIN_STYLE_1_END _end
257#define INCBIN_STYLE_1_SIZE _size
258
259/* Style lookup: returning identifier */
260#define INCBIN_STYLE_IDENT(TYPE) \
261 INCBIN_CONCATENATE( \
262 INCBIN_STYLE_, \
263 INCBIN_CONCATENATE( \
264 INCBIN_EVAL(INCBIN_STYLE), \
265 INCBIN_CONCATENATE(_, TYPE)))
266
267/* Style lookup: returning string literal */
268#define INCBIN_STYLE_STRING(TYPE) \
269 INCBIN_STRINGIZE( \
270 INCBIN_STYLE_IDENT(TYPE)) \
271
272/* Generate the global labels by indirectly invoking the macro with our style
273 * type and concatenating the name against them. */
274#define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
275 INCBIN_INVOKE( \
276 INCBIN_GLOBAL, \
277 INCBIN_CONCATENATE( \
278 NAME, \
279 INCBIN_INVOKE( \
280 INCBIN_STYLE_IDENT, \
281 TYPE))) \
282 INCBIN_INVOKE( \
283 INCBIN_TYPE, \
284 INCBIN_CONCATENATE( \
285 NAME, \
286 INCBIN_INVOKE( \
287 INCBIN_STYLE_IDENT, \
288 TYPE)))
289
321#define INCBIN_EXTERN(...) \
322 INCBIN_CONCATENATE(INCBIN_EXTERN_, INCBIN_VA_ARGC(__VA_ARGS__))(__VA_ARGS__)
323#define INCBIN_EXTERN_1(NAME, ...) \
324 INCBIN_EXTERN_2(unsigned char, NAME)
325#define INCBIN_EXTERN_2(TYPE, NAME) \
326 INCBIN_EXTERNAL const INCBIN_ALIGN TYPE \
327 INCBIN_CONCATENATE( \
328 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
329 INCBIN_STYLE_IDENT(DATA))[]; \
330 INCBIN_EXTERNAL const INCBIN_ALIGN TYPE *const \
331 INCBIN_CONCATENATE( \
332 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
333 INCBIN_STYLE_IDENT(END)); \
334 INCBIN_EXTERNAL const unsigned int \
335 INCBIN_CONCATENATE( \
336 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
337 INCBIN_STYLE_IDENT(SIZE))
338
359#define INCTXT_EXTERN(NAME) \
360 INCBIN_EXTERN_2(char, NAME)
361
401#ifdef _MSC_VER
402# define INCBIN(NAME, FILENAME) \
403 INCBIN_EXTERN(NAME)
404#else
405# define INCBIN(...) \
406 INCBIN_CONCATENATE(INCBIN_, INCBIN_VA_ARGC(__VA_ARGS__))(__VA_ARGS__)
407# if defined(__GNUC__)
408# define INCBIN_1(...) _Pragma("GCC error \"Single argument INCBIN not allowed\"")
409# elif defined(__clang__)
410# define INCBIN_1(...) _Pragma("clang error \"Single argument INCBIN not allowed\"")
411# else
412# define INCBIN_1(...) /* Cannot do anything here */
413# endif
414# define INCBIN_2(NAME, FILENAME) \
415 INCBIN_3(unsigned char, NAME, FILENAME)
416# define INCBIN_3(TYPE, NAME, FILENAME) INCBIN_COMMON(TYPE, NAME, FILENAME, /* No terminator for binary data */)
417# define INCBIN_COMMON(TYPE, NAME, FILENAME, TERMINATOR) \
418 __asm__(INCBIN_SECTION \
419 INCBIN_GLOBAL_LABELS(NAME, DATA) \
420 INCBIN_ALIGN_HOST \
421 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
422 INCBIN_MACRO " \"" FILENAME "\"\n" \
423 TERMINATOR \
424 INCBIN_GLOBAL_LABELS(NAME, END) \
425 INCBIN_ALIGN_BYTE \
426 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \
427 INCBIN_BYTE "1\n" \
428 INCBIN_GLOBAL_LABELS(NAME, SIZE) \
429 INCBIN_ALIGN_HOST \
430 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(SIZE) ":\n" \
431 INCBIN_INT INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) " - " \
432 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) "\n" \
433 INCBIN_ALIGN_HOST \
434 ".text\n" \
435 ); \
436 INCBIN_EXTERN(TYPE, NAME)
437#endif
438
470#if defined(_MSC_VER)
471# define INCTXT(NAME, FILENAME) \
472 INCBIN_EXTERN(NAME)
473#else
474# define INCTXT(NAME, FILENAME) \
475 INCBIN_COMMON(char, NAME, FILENAME, INCBIN_BYTE "0\n")
476#endif
477
478#endif