5 * Doxygen can't cope with some of the more esoteric areas of C, so we
6 * make its life simpler.
10 #define __attribute__(x)
15 * Global compiler definitions.
17 * This file is implicitly included by every @c .c file in Etherboot.
18 * It defines global macros such as DBG().
20 * We arrange for each object to export the symbol @c obj_OBJECT
21 * (where @c OBJECT is the object name, e.g. @c rtl8139) as a global
22 * symbol, so that the linker can drag in selected object files from
23 * the library using <tt> -u obj_OBJECT </tt>.
27 /* Not quite sure why cpp requires two levels of macro call in order
28 * to actually expand OBJECT...
31 #define _H1( x, y ) x ## y
33 #define _H2( x, y ) _H1 ( x, y )
34 #define PREFIX_OBJECT(prefix) _H2 ( prefix, OBJECT )
35 #define OBJECT_SYMBOL PREFIX_OBJECT(obj_)
39 #define _XSTR(s) _STR(s)
40 #define OBJECT_SYMBOL_STR _XSTR ( OBJECT_SYMBOL )
49 __asm__ ( ".globl\t" OBJECT_SYMBOL_STR );
50 __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
53 * Drag in an object by object name.
55 * Macro to allow objects to explicitly drag in other objects by
56 * object name. Used by config.c.
59 #define REQUIRE_OBJECT(object) \
60 __asm__ ( ".equ\tneed_" #object ", obj_" #object );
64 * Print a debugging message.
66 * The debug level is set at build time by specifying the @c DEBUG=
67 * parameter on the @c make command line. For example, to enable
68 * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
69 * for the @c rtl8139 card, you could use the command line
73 * make bin/rtl8139.dsk DEBUG=pci
77 * This will enable the debugging statements (DBG()) in pci.c. If
78 * debugging is not enabled, DBG() statements will be ignored.
80 * You can enable debugging in several objects simultaneously by
81 * separating them with commas, as in
85 * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
89 * You can increase the debugging level for an object by specifying it
90 * with @c :N, where @c N is the level, as in
94 * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
98 * which would enable debugging for the PCI, buffer-handling and
99 * heap-allocation code, with the buffer-handling code at level 2.
104 * If debug_OBJECT is set to a true value, the macro DBG(...) will
105 * expand to printf(...) when compiling OBJECT, and the symbol
106 * DEBUG_LEVEL will be inserted into the object file.
109 #define DEBUG_SYMBOL PREFIX_OBJECT(debug_)
112 #define DEBUG_SYMBOL_STR _XSTR ( DEBUG_SYMBOL )
113 __asm__ ( ".equ\tDBGLVL, " DEBUG_SYMBOL_STR );
116 /** printf() for debugging
118 * This function exists so that the DBG() macros can expand to
119 * printf() calls without dragging the printf() prototype into scope.
121 * As far as the compiler is concerned, dbg_printf() and printf() are
122 * completely unrelated calls; it's only at the assembly stage that
123 * references to the dbg_printf symbol are collapsed into references
124 * to the printf symbol.
126 extern int __attribute__ (( format ( printf, 1, 2 ) ))
127 dbg_printf ( const char *fmt, ... ) asm ( "printf" );
129 extern void dbg_autocolourise ( unsigned long id );
130 extern void dbg_decolourise ( void );
131 extern void dbg_hex_dump_da ( unsigned long dispaddr,
132 const void *data, unsigned long len );
135 #define DBGLVL_MAX DEBUG_SYMBOL
140 /* Allow for selective disabling of enabled debug levels */
143 #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
144 #define DBG_DISABLE( level ) do { \
145 __debug_disable |= ( (level) & DBGLVL_MAX ); \
147 #define DBG_ENABLE( level ) do { \
148 __debug_disable &= ~( (level) & DBGLVL_MAX ); \
152 #define DBG_DISABLE( level ) do { } while ( 0 )
153 #define DBG_ENABLE( level ) do { } while ( 0 )
157 #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
158 #define DBGLVL_EXTRA 2
159 #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
160 #define DBGLVL_PROFILE 4
161 #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
163 #define DBG_IO ( DBGLVL & DBGLVL_IO )
166 * Print debugging message if we are at a certain debug level
168 * @v level Debug level
169 * @v ... printf() argument list
171 #define DBG_IF( level, ... ) do { \
172 if ( DBG_ ## level ) { \
173 dbg_printf ( __VA_ARGS__ ); \
178 * Print a hex dump if we are at a certain debug level
180 * @v level Debug level
181 * @v dispaddr Display address
182 * @v data Data to print
183 * @v len Length of data
185 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
186 if ( DBG_ ## level ) { \
189 typeof ( dispaddr ) raw; \
192 dbg_hex_dump_da ( da.ul, data, len ); \
197 * Print a hex dump if we are at a certain debug level
199 * @v level Debug level
200 * @v data Data to print
201 * @v len Length of data
203 #define DBG_HD_IF( level, data, len ) do { \
204 DBG_HDA_IF ( level, data, data, len ); \
208 * Select colour for debug messages if we are at a certain debug level
210 * @v level Debug level
211 * @v id Message stream ID
213 #define DBG_AC_IF( level, id ) do { \
214 if ( DBG_ ## level ) { \
219 dbg_stream.raw = id; \
220 dbg_autocolourise ( dbg_stream.ul ); \
225 * Revert colour for debug messages if we are at a certain debug level
227 * @v level Debug level
229 #define DBG_DC_IF( level ) do { \
230 if ( DBG_ ## level ) { \
235 /* Autocolourising versions of the DBGxxx_IF() macros */
237 #define DBGC_IF( level, id, ... ) do { \
238 DBG_AC_IF ( level, id ); \
239 DBG_IF ( level, __VA_ARGS__ ); \
240 DBG_DC_IF ( level ); \
243 #define DBGC_HDA_IF( level, id, ... ) do { \
244 DBG_AC_IF ( level, id ); \
245 DBG_HDA_IF ( level, __VA_ARGS__ ); \
246 DBG_DC_IF ( level ); \
249 #define DBGC_HD_IF( level, id, ... ) do { \
250 DBG_AC_IF ( level, id ); \
251 DBG_HD_IF ( level, __VA_ARGS__ ); \
252 DBG_DC_IF ( level ); \
255 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
257 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
258 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
259 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
260 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
261 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
262 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
264 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
266 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
267 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
268 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
269 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
270 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
271 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
273 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
275 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
276 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
277 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
278 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
279 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
280 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
282 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
284 #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
285 #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
286 #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
287 #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
288 #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
289 #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
292 #if DEBUG_SYMBOL == 0
296 /** Select file identifier for errno.h (if used) */
297 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
299 /** Declare a data structure as packed. */
300 #define PACKED __attribute__ (( packed ))
302 /** Declare a variable or data structure as unused. */
303 #define __unused __attribute__ (( unused ))
305 /** Apply standard C calling conventions */
306 #define __cdecl __attribute__ (( cdecl , regparm(0) ))
309 * Declare a function as pure - i.e. without side effects
311 #define __pure __attribute__ (( pure ))
314 * Declare a function as const - i.e. it does not access global memory
315 * (including dereferencing pointers passed to it) at all.
316 * Must also not call any non-const functions.
318 #define __const __attribute__ (( const ))
321 * Declare a function's pointer parameters as non-null - i.e. force
322 * compiler to check pointers at compile time and enable possible
323 * optimizations based on that fact
325 #define __nonnull __attribute__ (( nonnull ))
328 * Declare a pointer returned by a function as a unique memory address
329 * as returned by malloc-type functions.
331 #define __malloc __attribute__ (( malloc ))
334 * Declare a function as used.
336 * Necessary only if the function is called only from assembler code.
338 #define __used __attribute__ (( used ))
340 /** Declare a data structure to be aligned with 16-byte alignment */
341 #define __aligned __attribute__ (( aligned ( 16 ) ))
343 /** Declare a function to be always inline */
344 #define __always_inline __attribute__ (( always_inline ))
349 * To save space in the binary when multiple-driver images are
350 * compiled, uninitialised data areas can be shared between drivers.
351 * This will typically be used to share statically-allocated receive
352 * and transmit buffers between drivers.
359 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
360 * char tx_buf[TX_BUF_SIZE];
361 * } my_static_data __shared;
366 #define __shared __asm__ ( "_shared_bss" ) __aligned
369 * Optimisation barrier
371 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
373 #endif /* ASSEMBLY */
375 #endif /* COMPILER_H */