Nuitka
The Python compiler
Loading...
Searching...
No Matches
incbin.h
Go to the documentation of this file.
1
35#ifndef INCBIN_HDR
36#define INCBIN_HDR
37#include <limits.h>
38#if defined(__AVX512BW__) || \
39 defined(__AVX512CD__) || \
40 defined(__AVX512DQ__) || \
41 defined(__AVX512ER__) || \
42 defined(__AVX512PF__) || \
43 defined(__AVX512VL__) || \
44 defined(__AVX512F__)
45# define INCBIN_ALIGNMENT_INDEX 6
46#elif defined(__AVX__) || \
47 defined(__AVX2__)
48# define INCBIN_ALIGNMENT_INDEX 5
49#elif defined(__SSE__) || \
50 defined(__SSE2__) || \
51 defined(__SSE3__) || \
52 defined(__SSSE3__) || \
53 defined(__SSE4_1__) || \
54 defined(__SSE4_2__) || \
55 defined(__neon__)
56# define INCBIN_ALIGNMENT_INDEX 4
57#elif ULONG_MAX != 0xffffffffu
58# define INCBIN_ALIGNMENT_INDEX 3
59# else
60# define INCBIN_ALIGNMENT_INDEX 2
61#endif
62
63/* Lookup table of (1 << n) where `n' is `INCBIN_ALIGNMENT_INDEX' */
64#define INCBIN_ALIGN_SHIFT_0 1
65#define INCBIN_ALIGN_SHIFT_1 2
66#define INCBIN_ALIGN_SHIFT_2 4
67#define INCBIN_ALIGN_SHIFT_3 8
68#define INCBIN_ALIGN_SHIFT_4 16
69#define INCBIN_ALIGN_SHIFT_5 32
70#define INCBIN_ALIGN_SHIFT_6 64
71
72/* Actual alignment value */
73#define INCBIN_ALIGNMENT \
74 INCBIN_CONCATENATE( \
75 INCBIN_CONCATENATE(INCBIN_ALIGN_SHIFT, _), \
76 INCBIN_ALIGNMENT_INDEX)
77
78/* Stringize */
79#define INCBIN_STR(X) \
80 #X
81#define INCBIN_STRINGIZE(X) \
82 INCBIN_STR(X)
83/* Concatenate */
84#define INCBIN_CAT(X, Y) \
85 X ## Y
86#define INCBIN_CONCATENATE(X, Y) \
87 INCBIN_CAT(X, Y)
88/* Deferred macro expansion */
89#define INCBIN_EVAL(X) \
90 X
91#define INCBIN_INVOKE(N, ...) \
92 INCBIN_EVAL(N(__VA_ARGS__))
93
94/* Green Hills uses a different directive for including binary data */
95#if defined(__ghs__)
96# if (__ghs_asm == 2)
97# define INCBIN_MACRO ".file"
98/* Or consider the ".myrawdata" entry in the ld file */
99# else
100# define INCBIN_MACRO "\tINCBIN"
101# endif
102#else
103# define INCBIN_MACRO ".incbin"
104#endif
105
106#ifndef _MSC_VER
107# define INCBIN_ALIGN \
108 __attribute__((aligned(INCBIN_ALIGNMENT)))
109#else
110# define INCBIN_ALIGN __declspec(align(INCBIN_ALIGNMENT))
111#endif
112
113#if defined(__arm__) || /* GNU C and RealView */ \
114 defined(__arm) || /* Diab */ \
115 defined(_ARM) /* ImageCraft */
116# define INCBIN_ARM
117#endif
118
119#ifdef __GNUC__
120/* Utilize .balign where supported */
121# define INCBIN_ALIGN_HOST ".balign " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
122# define INCBIN_ALIGN_BYTE ".balign 1\n"
123#elif defined(INCBIN_ARM)
124/*
125 * On arm assemblers, the alignment value is calculated as (1 << n) where `n' is
126 * the shift count. This is the value passed to `.align'
127 */
128# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT_INDEX) "\n"
129# define INCBIN_ALIGN_BYTE ".align 0\n"
130#else
131/* We assume other inline assembler's treat `.align' as `.balign' */
132# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
133# define INCBIN_ALIGN_BYTE ".align 1\n"
134#endif
135
136/* INCBIN_CONST is used by incbin.c generated files */
137#if defined(__cplusplus)
138# define INCBIN_EXTERNAL extern "C"
139# define INCBIN_CONST extern const
140#else
141# define INCBIN_EXTERNAL extern
142# define INCBIN_CONST const
143#endif
144
159#if !defined(INCBIN_OUTPUT_SECTION)
160# if defined(__APPLE__)
161# define INCBIN_OUTPUT_SECTION ".const_data"
162# else
163# define INCBIN_OUTPUT_SECTION ".rodata"
164# endif
165#endif
166
167#if defined(__APPLE__)
168/* The directives are different for Apple branded compilers */
169# define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n"
170# ifndef INCBIN_LOCAL
171# define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
172# else
173# define INCBIN_GLOBAL(NAME)
174# endif
175# define INCBIN_INT ".long "
176# define INCBIN_MANGLE "_"
177# define INCBIN_BYTE ".byte "
178# define INCBIN_TYPE(...)
179#else
180# define INCBIN_SECTION ".section " INCBIN_OUTPUT_SECTION "\n"
181# ifndef INCBIN_LOCAL
182# define INCBIN_GLOBAL(NAME) ".global " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
183# else
184# define INCBIN_GLOBAL(NAME)
185# endif
186# if defined(__ghs__)
187# define INCBIN_INT ".word "
188# else
189# define INCBIN_INT ".int "
190# endif
191# if defined(__USER_LABEL_PREFIX__)
192# define INCBIN_MANGLE INCBIN_STRINGIZE(__USER_LABEL_PREFIX__)
193# else
194# define INCBIN_MANGLE ""
195# endif
196# if defined(INCBIN_ARM)
197/* On arm assemblers, `@' is used as a line comment token */
198# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", %object\n"
199# elif defined(__MINGW32__) || defined(__MINGW64__)
200/* Mingw doesn't support this directive either */
201# define INCBIN_TYPE(NAME)
202# else
203/* It's safe to use `@' on other architectures */
204# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", @object\n"
205# endif
206# define INCBIN_BYTE ".byte "
207#endif
208
209/* List of style types used for symbol names */
210#define INCBIN_STYLE_CAMEL 0
211#define INCBIN_STYLE_SNAKE 1
212
239#if !defined(INCBIN_PREFIX)
240# define INCBIN_PREFIX g
241#endif
242
273#if !defined(INCBIN_STYLE)
274# define INCBIN_STYLE INCBIN_STYLE_CAMEL
275#endif
276
277/* Style lookup tables */
278#define INCBIN_STYLE_0_DATA Data
279#define INCBIN_STYLE_0_END End
280#define INCBIN_STYLE_0_SIZE Size
281#define INCBIN_STYLE_1_DATA _data
282#define INCBIN_STYLE_1_END _end
283#define INCBIN_STYLE_1_SIZE _size
284
285/* Style lookup: returning identifier */
286#define INCBIN_STYLE_IDENT(TYPE) \
287 INCBIN_CONCATENATE( \
288 INCBIN_STYLE_, \
289 INCBIN_CONCATENATE( \
290 INCBIN_EVAL(INCBIN_STYLE), \
291 INCBIN_CONCATENATE(_, TYPE)))
292
293/* Style lookup: returning string literal */
294#define INCBIN_STYLE_STRING(TYPE) \
295 INCBIN_STRINGIZE( \
296 INCBIN_STYLE_IDENT(TYPE)) \
297
298/* Generate the global labels by indirectly invoking the macro with our style
299 * type and concatenating the name against them. */
300#define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
301 INCBIN_INVOKE( \
302 INCBIN_GLOBAL, \
303 INCBIN_CONCATENATE( \
304 NAME, \
305 INCBIN_INVOKE( \
306 INCBIN_STYLE_IDENT, \
307 TYPE))) \
308 INCBIN_INVOKE( \
309 INCBIN_TYPE, \
310 INCBIN_CONCATENATE( \
311 NAME, \
312 INCBIN_INVOKE( \
313 INCBIN_STYLE_IDENT, \
314 TYPE)))
315
336#define INCBIN_EXTERN(NAME) \
337 INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char \
338 INCBIN_CONCATENATE( \
339 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
340 INCBIN_STYLE_IDENT(DATA))[]; \
341 INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char *const \
342 INCBIN_CONCATENATE( \
343 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
344 INCBIN_STYLE_IDENT(END)); \
345 INCBIN_EXTERNAL const unsigned int \
346 INCBIN_CONCATENATE( \
347 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
348 INCBIN_STYLE_IDENT(SIZE))
349
377#if defined(_MSC_VER)
378#define INCBIN(NAME, FILENAME) \
379 INCBIN_EXTERN(NAME)
380#else
381#define INCBIN(NAME, FILENAME) \
382 __asm__(INCBIN_SECTION \
383 INCBIN_GLOBAL_LABELS(NAME, DATA) \
384 INCBIN_ALIGN_HOST \
385 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
386 INCBIN_MACRO " \"" FILENAME "\"\n" \
387 INCBIN_GLOBAL_LABELS(NAME, END) \
388 INCBIN_ALIGN_BYTE \
389 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \
390 INCBIN_BYTE "1\n" \
391 INCBIN_GLOBAL_LABELS(NAME, SIZE) \
392 INCBIN_ALIGN_HOST \
393 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(SIZE) ":\n" \
394 INCBIN_INT INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) " - " \
395 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) "\n" \
396 INCBIN_ALIGN_HOST \
397 ".text\n" \
398 ); \
399 INCBIN_EXTERN(NAME)
400
401#endif
402#endif