compiler.h revision a734c64bff58bda2fa48c2795453e092167b0ff7
#ifndef COMPILER_H
#define COMPILER_H
/*
* Doxygen can't cope with some of the more esoteric areas of C, so we
* make its life simpler.
*
*/
#ifdef DOXYGEN
#define __attribute__(x)
#endif
/** @file
*
* Global compiler definitions.
*
* This file is implicitly included by every @c .c file in Etherboot.
* It defines global macros such as DBG().
*
* We arrange for each object to export the symbol @c obj_OBJECT
* (where @c OBJECT is the object name, e.g. @c rtl8139) as a global
* symbol, so that the linker can drag in selected object files from
* the library using <tt> -u obj_OBJECT </tt>.
*
*/
/* Force visibility of all symbols to "hidden", i.e. inform gcc that
* all symbol references resolve strictly within our final binary.
*
* This is a stronger claim than specifying "-fvisibility=hidden",
* since it also affects symbols marked with "extern".
*/
#ifndef ASSEMBLY
#if __GNUC__ >= 4
#endif
#endif /* ASSEMBLY */
/** Concatenate non-expanded arguments */
#define _C1( x, y ) x ## y
/** Concatenate expanded arguments */
/** Stringify non-expanded argument */
#define _S1( x ) #x
/** Stringify expanded argument */
/**
* @defgroup symmacros Macros to provide or require explicit symbols
* @{
*/
/** Provide a symbol within this object file */
#ifdef ASSEMBLY
#define PROVIDE_SYMBOL( _sym ) \
#else /* ASSEMBLY */
#define PROVIDE_SYMBOL( _sym ) \
char _sym[0]
#endif /* ASSEMBLY */
/** Require a symbol within this object file
*
* The symbol is referenced by a relocation in a discarded section, so
* if it is not available at link time the link will fail.
*/
#ifdef ASSEMBLY
#define REQUIRE_SYMBOL( _sym ) \
.extern _sym ; \
.long _sym ; \
#else /* ASSEMBLY */
#define REQUIRE_SYMBOL( _sym ) \
extern char _sym; \
= &_sym
#endif
/** Request that a symbol be available at runtime
*
* The requested symbol is entered as undefined into the symbol table
* for this object, so the linker will pull in other object files as
* necessary to satisfy the reference. However, the undefined symbol
* is not referenced in any relocations, so the link can still succeed
* if no file contains it.
*
* A symbol passed to this macro may not be referenced anywhere
* else in the file. If you want to do that, see IMPORT_SYMBOL().
*/
#ifdef ASSEMBLY
#define REQUEST_SYMBOL( _sym ) \
#else /* ASSEMBLY */
#define REQUEST_SYMBOL( _sym ) \
#endif /* ASSEMBLY */
/** Set up a symbol to be usable in another file by IMPORT_SYMBOL()
*
* The symbol must already be marked as global.
*/
/** Make a symbol usable to this file if available at link time
*
* If no file passed to the linker contains the symbol, it will have
* @c NULL value to future uses. Keep in mind that the symbol value is
* really the @e address of a variable or function; see the code
* snippet below.
*
* In C using IMPORT_SYMBOL, you must specify the declaration as the
* second argument, for instance
*
* @code
* IMPORT_SYMBOL ( my_func, int my_func ( int arg ) );
* IMPORT_SYMBOL ( my_var, int my_var );
*
* void use_imports ( void ) {
* if ( my_func && &my_var )
* my_var = my_func ( my_var );
* }
* @endcode
*
* GCC considers a weak declaration to override a strong one no matter
* which comes first, so it is safe to include a header file declaring
* the imported symbol normally, but providing the declaration to
* IMPORT_SYMBOL is still required.
*
* If no EXPORT_SYMBOL declaration exists for the imported symbol in
* another file, the behavior will be most likely be identical to that
* for an unavailable symbol.
*/
#ifdef ASSEMBLY
#define IMPORT_SYMBOL( _sym ) \
#else /* ASSEMBLY */
#endif
/** @} */
/**
* @defgroup objmacros Macros to provide or require explicit objects
* @{
*/
/** Always provide the symbol for the current object (defined by -DOBJECT) */
/** Pull in an object-specific configuration file if available */
/** Explicitly require another object */
/** Pull in another object if it exists */
/** @} */
/** Select file identifier for errno.h (if used) */
#ifndef ASSEMBLY
/** Declare a function as weak (use *before* the definition)
*
* Due to a bug in at least GCC 4.4.4 and earlier, weak symbols may be
* inlined if they have hidden visibility (see above for why hidden
* visibility is used). This results in the non-weak symbol never
* being used, so explicitly mark the function as noinline to prevent
* inlining.
*/
/** Prevent a function from being optimized away without inlining
*
* Calls to functions with void return type that contain no code in their body
* may be removed by gcc's optimizer even when inlining is inhibited. Placing
* this macro in the body of the function prevents that from occurring.
*/
#define __keepme asm("");
#endif
/** @defgroup dbg Debugging infrastructure
* @{
*/
/** @def DBG
*
* Print a debugging message.
*
* The debug level is set at build time by specifying the @c DEBUG=
* parameter on the @c make command line. For example, to enable
* debugging for the PCI bus functions (in pci.c) in a @c .dsk image
* for the @c rtl8139 card, you could use the command line
*
* @code
*
* make bin/rtl8139.dsk DEBUG=pci
*
* @endcode
*
* This will enable the debugging statements (DBG()) in pci.c. If
* debugging is not enabled, DBG() statements will be ignored.
*
* You can enable debugging in several objects simultaneously by
* separating them with commas, as in
*
* @code
*
* make bin/rtl8139.dsk DEBUG=pci,buffer,heap
*
* @endcode
*
* You can increase the debugging level for an object by specifying it
* with @c :N, where @c N is the level, as in
*
* @code
*
* make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
*
* @endcode
*
* which would enable debugging for the PCI, buffer-handling and
* heap-allocation code, with the buffer-handling code at level 2.
*
*/
/*
* If debug_OBJECT is set to a true value, the macro DBG(...) will
* expand to printf(...) when compiling OBJECT, and the symbol
* DEBUG_LEVEL will be inserted into the object file.
*
*/
#if DEBUG_SYMBOL == 0
#define NDEBUG
#endif
#ifndef ASSEMBLY
/** printf() for debugging */
dbg_printf ( const char *fmt, ... );
extern void dbg_autocolourise ( unsigned long id );
extern void dbg_decolourise ( void );
extern void dbg_hex_dump_da ( unsigned long dispaddr,
extern void dbg_md5_da ( unsigned long dispaddr,
extern void dbg_pause ( void );
extern void dbg_more ( void );
#if DEBUG_SYMBOL
#define DBGLVL_MAX DEBUG_SYMBOL
#else
#define DBGLVL_MAX 0
#endif
/* Allow for selective disabling of enabled debug levels */
#if DBGLVL_MAX
int __debug_disable;
#define DBG_DISABLE( level ) do { \
__debug_disable |= (level); \
} while ( 0 )
#define DBG_ENABLE( level ) do { \
__debug_disable &= ~(level); \
} while ( 0 )
#else
#define DBGLVL 0
#define DBG_DISABLE( level ) do { } while ( 0 )
#define DBG_ENABLE( level ) do { } while ( 0 )
#endif
#define DBGLVL_LOG 1
#define DBGLVL_EXTRA 2
#define DBGLVL_PROFILE 4
#define DBGLVL_IO 8
/**
* Print debugging message if we are at a certain debug level
*
* @v level Debug level
* @v ... printf() argument list
*/
dbg_printf ( __VA_ARGS__ ); \
} \
} while ( 0 )
/**
* Print a hex dump if we are at a certain debug level
*
* @v level Debug level
* @v dispaddr Display address
* @v data Data to print
* @v len Length of data
*/
union { \
unsigned long ul; \
} da; \
} \
} while ( 0 )
/**
* Print a hex dump if we are at a certain debug level
*
* @v level Debug level
* @v data Data to print
* @v len Length of data
*/
} while ( 0 )
/**
* Print an MD5 checksum if we are at a certain debug level
*
* @v level Debug level
* @v dispaddr Display address
* @v data Data to print
* @v len Length of data
*/
union { \
unsigned long ul; \
} da; \
} \
} while ( 0 )
/**
* Print an MD5 checksum if we are at a certain debug level
*
* @v level Debug level
* @v data Data to print
* @v len Length of data
*/
} while ( 0 )
/**
* Prompt for key press if we are at a certain debug level
*
* @v level Debug level
*/
#define DBG_PAUSE_IF( level ) do { \
dbg_pause(); \
} \
} while ( 0 )
/**
* Prompt for more output data if we are at a certain debug level
*
* @v level Debug level
*/
#define DBG_MORE_IF( level ) do { \
dbg_more(); \
} \
} while ( 0 )
/**
* Select colour for debug messages if we are at a certain debug level
*
* @v level Debug level
* @v id Message stream ID
*/
union { \
unsigned long ul; \
} dbg_stream; \
dbg_stream.ul = 0; \
} \
} while ( 0 )
/**
* Revert colour for debug messages if we are at a certain debug level
*
* @v level Debug level
*/
dbg_decolourise(); \
} \
} while ( 0 )
/* Autocolourising versions of the DBGxxx_IF() macros */
} while ( 0 )
} while ( 0 )
} while ( 0 )
} while ( 0 )
} while ( 0 )
DBG_PAUSE_IF ( level ); \
} while ( 0 )
DBG_MORE_IF ( level ); \
} while ( 0 )
/* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
/* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
/* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
/* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
#endif /* ASSEMBLY */
/** @} */
/** @defgroup attrs Miscellaneous attributes
* @{
*/
#ifndef ASSEMBLY
/** Declare a variable or data structure as unused. */
/**
* Declare a function as pure - i.e. without side effects
*/
/**
* Declare a function as const - i.e. it does not access global memory
* (including dereferencing pointers passed to it) at all.
* Must also not call any non-const functions.
*/
#define __const __attribute__ (( const ))
/**
* Declare a function's pointer parameters as non-null - i.e. force
* compiler to check pointers at compile time and enable possible
* optimizations based on that fact
*/
/**
* Declare a pointer returned by a function as a unique memory address
* as returned by malloc-type functions.
*/
/**
* Declare a function as used.
*
* Necessary only if the function is called only from assembler code.
*/
/** Declare a data structure to be aligned with 16-byte alignment */
/** Declare a function to be always inline */
/* Force all inline functions to not be instrumented
*
* This is required to cope with what seems to be a long-standing gcc
* bug, in which -finstrument-functions will cause instances of
* inlined functions to be reported as further calls to the
* *containing* function. This makes instrumentation very difficult
* to use.
*
* Work around this problem by adding the no_instrument_function
* attribute to all inlined functions.
*/
#define inline inline __attribute__ (( no_instrument_function ))
/**
* Shared data.
*
* To save space in the binary when multiple-driver images are
* compiled, uninitialised data areas can be shared between drivers.
* This will typically be used to share statically-allocated receive
* and transmit buffers between drivers.
*
* Use as e.g.
*
* @code
*
* struct {
* char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
* char tx_buf[TX_BUF_SIZE];
* } my_static_data __shared;
*
* @endcode
*
*/
#endif /* ASSEMBLY */
/** @} */
/**
* Optimisation barrier
*/
#ifndef ASSEMBLY
#endif /* ASSEMBLY */
/**
* @defgroup licences Licence declarations
*
* For reasons that are partly historical, various different files
* within the iPXE codebase have differing licences.
*
* @{
*/
/** Declare a file as being in the public domain
*
* This licence declaration is applicable when a file states itself to
* be in the public domain.
*/
#define FILE_LICENCE_PUBLIC_DOMAIN \
/** Declare a file as being under version 2 (or later) of the GNU GPL
*
* This licence declaration is applicable when a file states itself to
* be licensed under the GNU GPL; "either version 2 of the License, or
* (at your option) any later version".
*/
#define FILE_LICENCE_GPL2_OR_LATER \
/** Declare a file as being under version 2 of the GNU GPL
*
* This licence declaration is applicable when a file states itself to
* be licensed under version 2 of the GPL, and does not include the
* "or, at your option, any later version" clause.
*/
#define FILE_LICENCE_GPL2_ONLY \
/** Declare a file as being under any version of the GNU GPL
*
* This licence declaration is applicable when a file states itself to
* be licensed under the GPL, but does not specify a version.
*
* According to section 9 of the GPLv2, "If the Program does not
* specify a version number of this License, you may choose any
* version ever published by the Free Software Foundation".
*/
#define FILE_LICENCE_GPL_ANY \
/** Declare a file as being under the three-clause BSD licence
*
* This licence declaration is applicable when a file states itself to
* be licensed under terms allowing redistribution in source and
* binary forms (with or without modification) provided that:
*
* redistributions of source code retain the copyright notice,
* list of conditions and any attached disclaimers
*
* redistributions in binary form reproduce the copyright notice,
* list of conditions and any attached disclaimers in the
* distribution
*
* the name of the author is not used to endorse or promote
* products derived from the software without specific prior
* written permission
*
* It is not necessary for the file to explicitly state that it is
* under a "BSD" licence; only that the licensing terms be
* functionally equivalent to the standard three-clause BSD licence.
*/
#define FILE_LICENCE_BSD3 \
/** Declare a file as being under the two-clause BSD licence
*
* This licence declaration is applicable when a file states itself to
* be licensed under terms allowing redistribution in source and
* binary forms (with or without modification) provided that:
*
* redistributions of source code retain the copyright notice,
* list of conditions and any attached disclaimers
*
* redistributions in binary form reproduce the copyright notice,
* list of conditions and any attached disclaimers in the
* distribution
*
* It is not necessary for the file to explicitly state that it is
* under a "BSD" licence; only that the licensing terms be
* functionally equivalent to the standard two-clause BSD licence.
*/
#define FILE_LICENCE_BSD2 \
/** Declare a file as being under the one-clause MIT-style licence
*
* This licence declaration is applicable when a file states itself to
* be licensed under terms allowing redistribution for any purpose
* with or without fee, provided that the copyright notice and
* permission notice appear in all copies.
*/
#define FILE_LICENCE_MIT \
/** Declare a particular licence as applying to a file */
/** @} */
/* This file itself is under GPLv2-or-later */
#include <bits/compiler.h>
#endif /* COMPILER_H */