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#elif defined(INCBIN_ARM)
123/*
124 * On arm assemblers, the alignment value is calculated as (1 << n) where `n' is
125 * the shift count. This is the value passed to `.align'
126 */
127# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT_INDEX) "\n"
128#else
129/* We assume other inline assembler's treat `.align' as `.balign' */
130# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
131#endif
132
133/* INCBIN_CONST is used by incbin.c generated files */
134#if defined(__cplusplus)
135# define INCBIN_EXTERNAL extern "C"
136# define INCBIN_CONST extern const
137#else
138# define INCBIN_EXTERNAL extern
139# define INCBIN_CONST const
140#endif
141
156#if !defined(INCBIN_OUTPUT_SECTION)
157# if defined(__APPLE__)
158# define INCBIN_OUTPUT_SECTION ".const_data"
159# else
160# define INCBIN_OUTPUT_SECTION ".rodata"
161# endif
162#endif
163
164#if defined(__APPLE__)
165/* The directives are different for Apple branded compilers */
166# define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n"
167# ifndef INCBIN_LOCAL
168# define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
169# else
170# define INCBIN_GLOBAL(NAME)
171# endif
172# define INCBIN_MANGLE "_"
173# define INCBIN_BYTE ".byte "
174# define INCBIN_TYPE(...)
175#else
176# define INCBIN_SECTION ".section " INCBIN_OUTPUT_SECTION "\n"
177# ifndef INCBIN_LOCAL
178# define INCBIN_GLOBAL(NAME) ".global " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
179# else
180# define INCBIN_GLOBAL(NAME)
181# endif
182# if defined(__USER_LABEL_PREFIX__)
183# define INCBIN_MANGLE INCBIN_STRINGIZE(__USER_LABEL_PREFIX__)
184# else
185# define INCBIN_MANGLE ""
186# endif
187# if defined(INCBIN_ARM)
188/* On arm assemblers, `@' is used as a line comment token */
189# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", %object\n"
190# elif defined(__MINGW32__) || defined(__MINGW64__)
191/* Mingw doesn't support this directive either */
192# define INCBIN_TYPE(NAME)
193# else
194/* It's safe to use `@' on other architectures */
195# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", @object\n"
196# endif
197# define INCBIN_BYTE ".byte "
198#endif
199
200/* List of style types used for symbol names */
201#define INCBIN_STYLE_CAMEL 0
202#define INCBIN_STYLE_SNAKE 1
203
226#if !defined(INCBIN_PREFIX)
227# define INCBIN_PREFIX g
228#endif
229
256#if !defined(INCBIN_STYLE)
257# define INCBIN_STYLE INCBIN_STYLE_CAMEL
258#endif
259
260/* Style lookup tables */
261#define INCBIN_STYLE_0_DATA Data
262#define INCBIN_STYLE_1_DATA _data
263
264/* Style lookup: returning identifier */
265#define INCBIN_STYLE_IDENT(TYPE) \
266 INCBIN_CONCATENATE( \
267 INCBIN_STYLE_, \
268 INCBIN_CONCATENATE( \
269 INCBIN_EVAL(INCBIN_STYLE), \
270 INCBIN_CONCATENATE(_, TYPE)))
271
272/* Style lookup: returning string literal */
273#define INCBIN_STYLE_STRING(TYPE) \
274 INCBIN_STRINGIZE( \
275 INCBIN_STYLE_IDENT(TYPE)) \
276
277/* Generate the global labels by indirectly invoking the macro with our style
278 * type and concatenating the name against them. */
279#define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
280 INCBIN_INVOKE( \
281 INCBIN_GLOBAL, \
282 INCBIN_CONCATENATE( \
283 NAME, \
284 INCBIN_INVOKE( \
285 INCBIN_STYLE_IDENT, \
286 TYPE))) \
287 INCBIN_INVOKE( \
288 INCBIN_TYPE, \
289 INCBIN_CONCATENATE( \
290 NAME, \
291 INCBIN_INVOKE( \
292 INCBIN_STYLE_IDENT, \
293 TYPE)))
294
313#define INCBIN_EXTERN(NAME) \
314 INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char \
315 INCBIN_CONCATENATE( \
316 INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
317 INCBIN_STYLE_IDENT(DATA))[]
318
344#if defined(_MSC_VER)
345#define INCBIN(NAME, FILENAME) \
346 INCBIN_EXTERN(NAME)
347#else
348#define INCBIN(NAME, FILENAME) \
349 __asm__(INCBIN_SECTION \
350 INCBIN_GLOBAL_LABELS(NAME, DATA) \
351 INCBIN_ALIGN_HOST \
352 INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
353 INCBIN_MACRO " \"" FILENAME "\"\n" \
354 INCBIN_ALIGN_HOST \
355 ".text\n" \
356 ); \
357 INCBIN_EXTERN(NAME)
358
359#endif
360#endif