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 )
149 * Print debugging message if we are at a certain debug level
151 * @v level Debug level
152 * @v ... printf() argument list
154 #define DBG_IF( level, ... ) do { \
155 if ( DBG_ ## level ) { \
156 dbg_printf ( __VA_ARGS__ ); \
161 * Print a hex dump if we are at a certain debug level
163 * @v level Debug level
164 * @v dispaddr Display address
165 * @v data Data to print
166 * @v len Length of data
168 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
169 if ( DBG_ ## level ) { \
172 typeof ( dispaddr ) raw; \
175 dbg_hex_dump_da ( da.ul, data, len ); \
180 * Print a hex dump if we are at a certain debug level
182 * @v level Debug level
183 * @v data Data to print
184 * @v len Length of data
186 #define DBG_HD_IF( level, data, len ) do { \
187 DBG_HDA_IF ( level, data, data, len ); \
191 * Select colour for debug messages if we are at a certain debug level
193 * @v level Debug level
194 * @v id Message stream ID
196 #define DBG_AC_IF( level, id ) do { \
197 if ( DBG_ ## level ) { \
202 dbg_stream.raw = id; \
203 dbg_autocolourise ( dbg_stream.ul ); \
208 * Revert colour for debug messages if we are at a certain debug level
210 * @v level Debug level
212 #define DBG_DC_IF( level ) do { \
213 if ( DBG_ ## level ) { \
218 /* Autocolourising versions of the DBGxxx_IF() macros */
220 #define DBGC_IF( level, id, ... ) do { \
221 DBG_AC_IF ( level, id ); \
222 DBG_IF ( level, __VA_ARGS__ ); \
223 DBG_DC_IF ( level ); \
226 #define DBGC_HDA_IF( level, id, ... ) do { \
227 DBG_AC_IF ( level, id ); \
228 DBG_HDA_IF ( level, __VA_ARGS__ ); \
229 DBG_DC_IF ( level ); \
232 #define DBGC_HD_IF( level, id, ... ) do { \
233 DBG_AC_IF ( level, id ); \
234 DBG_HD_IF ( level, __VA_ARGS__ ); \
235 DBG_DC_IF ( level ); \
238 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
240 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
241 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
242 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
243 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
244 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
245 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
247 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
249 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
250 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
251 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
252 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
253 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
254 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
256 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
258 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
259 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
260 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
261 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
262 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
263 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
266 #if DEBUG_SYMBOL == 0
270 /** Select file identifier for errno.h (if used) */
271 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
273 /** Declare a data structure as packed. */
274 #define PACKED __attribute__ (( packed ))
276 /** Declare a variable or data structure as unused. */
277 #define __unused __attribute__ (( unused ))
279 /** Apply standard C calling conventions */
280 #define __cdecl __attribute__ (( cdecl , regparm(0) ))
283 * Declare a function as used.
285 * Necessary only if the function is called only from assembler code.
287 #define __used __attribute__ (( used ))
289 /** Declare a data structure to be aligned with 16-byte alignment */
290 #define __aligned __attribute__ (( aligned ( 16 ) ))
295 * To save space in the binary when multiple-driver images are
296 * compiled, uninitialised data areas can be shared between drivers.
297 * This will typically be used to share statically-allocated receive
298 * and transmit buffers between drivers.
305 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
306 * char tx_buf[TX_BUF_SIZE];
307 * } my_static_data __shared;
312 #define __shared __asm__ ( "_shared_bss" )
315 * Optimisation barrier
317 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
319 #endif /* ASSEMBLY */
321 #endif /* COMPILER_H */