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
);
134 /* Compatibility with existing Makefile */
136 #define DBGLVL DEBUG_SYMBOL
142 #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
143 #define DBGLVL_EXTRA 2
144 #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
145 #define DBGLVL_PROFILE 4
146 #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
148 #define DBG_IO ( DBGLVL & DBGLVL_IO )
151 * Print debugging message if we are at a certain debug level
153 * @v level Debug level
154 * @v ... printf() argument list
156 #define DBG_IF( level, ... ) do { \
157 if ( DBG_ ## level ) { \
158 dbg_printf ( __VA_ARGS__ ); \
163 * Print a hex dump if we are at a certain debug level
165 * @v level Debug level
166 * @v dispaddr Display address
167 * @v data Data to print
168 * @v len Length of data
170 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
171 if ( DBG_ ## level ) { \
174 typeof ( dispaddr ) raw; \
177 dbg_hex_dump_da ( da.ul, data, len ); \
182 * Print a hex dump if we are at a certain debug level
184 * @v level Debug level
185 * @v data Data to print
186 * @v len Length of data
188 #define DBG_HD_IF( level, data, len ) do { \
189 DBG_HDA_IF ( level, data, data, len ); \
193 * Select colour for debug messages if we are at a certain debug level
195 * @v level Debug level
196 * @v id Message stream ID
198 #define DBG_AC_IF( level, id ) do { \
199 if ( DBG_ ## level ) { \
204 dbg_stream.raw = id; \
205 dbg_autocolourise ( dbg_stream.ul ); \
210 * Revert colour for debug messages if we are at a certain debug level
212 * @v level Debug level
214 #define DBG_DC_IF( level ) do { \
215 if ( DBG_ ## level ) { \
220 /* Autocolourising versions of the DBGxxx_IF() macros */
222 #define DBGC_IF( level, id, ... ) do { \
223 DBG_AC_IF ( level, id ); \
224 DBG_IF ( level, __VA_ARGS__ ); \
225 DBG_DC_IF ( level ); \
228 #define DBGC_HDA_IF( level, id, ... ) do { \
229 DBG_AC_IF ( level, id ); \
230 DBG_HDA_IF ( level, __VA_ARGS__ ); \
231 DBG_DC_IF ( level ); \
234 #define DBGC_HD_IF( level, id, ... ) do { \
235 DBG_AC_IF ( level, id ); \
236 DBG_HD_IF ( level, __VA_ARGS__ ); \
237 DBG_DC_IF ( level ); \
240 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
242 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
243 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
244 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
245 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
246 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
247 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
249 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
251 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
252 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
253 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
254 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
255 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
256 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
258 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
260 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
261 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
262 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
263 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
264 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
265 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
267 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
269 #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
270 #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
271 #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
272 #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
273 #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
274 #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
277 #if DEBUG_SYMBOL == 0
281 /** Select file identifier for errno.h (if used) */
282 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
284 /** Declare a data structure as packed. */
285 #define PACKED __attribute__ (( packed ))
287 /** Declare a variable or data structure as unused. */
288 #define __unused __attribute__ (( unused ))
290 /** Apply standard C calling conventions */
291 #define __cdecl __attribute__ (( cdecl , regparm(0) ))
294 * Declare a function as pure - i.e. without side effects
296 #define __pure __attribute__ (( pure ))
299 * Declare a function as const - i.e. it does not access global memory
300 * (including dereferencing pointers passed to it) at all.
301 * Must also not call any non-const functions.
303 #define __const __attribute__ (( const ))
306 * Declare a function's pointer parameters as non-null - i.e. force
307 * compiler to check pointers at compile time and enable possible
308 * optimizations based on that fact
310 #define __nonnull __attribute__ (( nonnull ))
313 * Declare a pointer returned by a function as a unique memory address
314 * as returned by malloc-type functions.
316 #define __malloc __attribute__ (( malloc ))
319 * Declare a function as used.
321 * Necessary only if the function is called only from assembler code.
323 #define __used __attribute__ (( used ))
325 /** Declare a data structure to be aligned with 16-byte alignment */
326 #define __aligned __attribute__ (( aligned ( 16 ) ))
331 * To save space in the binary when multiple-driver images are
332 * compiled, uninitialised data areas can be shared between drivers.
333 * This will typically be used to share statically-allocated receive
334 * and transmit buffers between drivers.
341 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
342 * char tx_buf[TX_BUF_SIZE];
343 * } my_static_data __shared;
348 #define __shared __asm__ ( "_shared_bss" )
351 * Optimisation barrier
353 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
355 #endif /* ASSEMBLY */
357 #endif /* COMPILER_H */