You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef COMPILER_H
  2. #define COMPILER_H
  3. /*
  4. * Doxygen can't cope with some of the more esoteric areas of C, so we
  5. * make its life simpler.
  6. *
  7. */
  8. #ifdef DOXYGEN
  9. #define __attribute__(x)
  10. #endif
  11. /** @file
  12. *
  13. * Global compiler definitions.
  14. *
  15. * This file is implicitly included by every @c .c file in Etherboot.
  16. * It defines global macros such as DBG().
  17. *
  18. * We arrange for each object to export the symbol @c obj_OBJECT
  19. * (where @c OBJECT is the object name, e.g. @c rtl8139) as a global
  20. * symbol, so that the linker can drag in selected object files from
  21. * the library using <tt> -u obj_OBJECT </tt>.
  22. *
  23. */
  24. /* Not quite sure why cpp requires two levels of macro call in order
  25. * to actually expand OBJECT...
  26. */
  27. #undef _H1
  28. #define _H1( x, y ) x ## y
  29. #undef _H2
  30. #define _H2( x, y ) _H1 ( x, y )
  31. #define PREFIX_OBJECT(prefix) _H2 ( prefix, OBJECT )
  32. #define OBJECT_SYMBOL PREFIX_OBJECT(obj_)
  33. #undef _STR
  34. #define _STR(s) #s
  35. #undef _XSTR
  36. #define _XSTR(s) _STR(s)
  37. #define OBJECT_SYMBOL_STR _XSTR ( OBJECT_SYMBOL )
  38. #ifdef ASSEMBLY
  39. .globl OBJECT_SYMBOL
  40. .equ OBJECT_SYMBOL, 0
  41. #else /* ASSEMBLY */
  42. __asm__ ( ".globl\t" OBJECT_SYMBOL_STR );
  43. __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
  44. /**
  45. * Drag in an object by object name.
  46. *
  47. * Macro to allow objects to explicitly drag in other objects by
  48. * object name. Used by config.c.
  49. *
  50. */
  51. #define REQUIRE_OBJECT(object) \
  52. __asm__ ( ".equ\tneed_" #object ", obj_" #object );
  53. /** @def DBG
  54. *
  55. * Print a debugging message.
  56. *
  57. * The debug level is set at build time by specifying the @c DEBUG=
  58. * parameter on the @c make command line. For example, to enable
  59. * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
  60. * for the @c rtl8139 card, you could use the command line
  61. *
  62. * @code
  63. *
  64. * make bin/rtl8139.dsk DEBUG=pci
  65. *
  66. * @endcode
  67. *
  68. * This will enable the debugging statements (DBG()) in pci.c. If
  69. * debugging is not enabled, DBG() statements will be ignored.
  70. *
  71. * You can enable debugging in several objects simultaneously by
  72. * separating them with commas, as in
  73. *
  74. * @code
  75. *
  76. * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
  77. *
  78. * @endcode
  79. *
  80. * You can increase the debugging level for an object by specifying it
  81. * with @c :N, where @c N is the level, as in
  82. *
  83. * @code
  84. *
  85. * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
  86. *
  87. * @endcode
  88. *
  89. * which would enable debugging for the PCI, buffer-handling and
  90. * heap-allocation code, with the buffer-handling code at level 2.
  91. *
  92. */
  93. /*
  94. * If debug_OBJECT is set to a true value, the macro DBG(...) will
  95. * expand to printf(...) when compiling OBJECT, and the symbol
  96. * DEBUG_LEVEL will be inserted into the object file.
  97. *
  98. */
  99. #define DEBUG_SYMBOL PREFIX_OBJECT(debug_)
  100. #if DEBUG_SYMBOL
  101. #include "console.h"
  102. #define DEBUG_SYMBOL_STR _XSTR ( DEBUG_SYMBOL )
  103. __asm__ ( ".equ\tDBGLVL, " DEBUG_SYMBOL_STR );
  104. #endif
  105. /** printf() for debugging
  106. *
  107. * This function exists so that the DBG() macros can expand to
  108. * printf() calls without dragging the printf() prototype into scope.
  109. *
  110. * As far as the compiler is concerned, dbg_printf() and printf() are
  111. * completely unrelated calls; it's only at the assembly stage that
  112. * references to the dbg_printf symbol are collapsed into references
  113. * to the printf symbol.
  114. */
  115. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  116. dbg_printf ( const char *fmt, ... ) asm ( "printf" );
  117. extern void __attribute__ (( format ( printf, 2, 3 ) ))
  118. dbg_printf_autocolour ( void *id, const char *fmt, ... );
  119. /* Compatibility with existing Makefile */
  120. #if DEBUG_SYMBOL >= 1
  121. #if DEBUG_SYMBOL >= 2
  122. #define DBGLVL 3
  123. #else
  124. #define DBGLVL 1
  125. #endif
  126. #else
  127. #define DBGLVL 0
  128. #endif
  129. #define DBGLVL_LOG 1
  130. #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
  131. #define DBGLVL_EXTRA 2
  132. #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
  133. #define DBG_IF( level, ... ) do { \
  134. if ( DBG_ ## level ) { \
  135. dbg_printf ( __VA_ARGS__ ); \
  136. } \
  137. } while ( 0 )
  138. #define DBGC_IF( level, ... ) do { \
  139. if ( DBG_ ## level ) { \
  140. dbg_printf_autocolour ( __VA_ARGS__ ); \
  141. } \
  142. } while ( 0 )
  143. #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
  144. #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
  145. #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
  146. #if DEBUG_SYMBOL == 0
  147. #define NDEBUG
  148. #endif
  149. /** Declare a data structure as packed. */
  150. #define PACKED __attribute__ (( packed ))
  151. /** Declare a variable or data structure as unused. */
  152. #define __unused __attribute__ (( unused ))
  153. /**
  154. * Declare a function as used.
  155. *
  156. * Necessary only if the function is called only from assembler code.
  157. */
  158. #define __used __attribute__ (( used ))
  159. /** Declare a data structure to be aligned with 16-byte alignment */
  160. #define __aligned __attribute__ (( aligned ( 16 ) ))
  161. /**
  162. * Shared data.
  163. *
  164. * To save space in the binary when multiple-driver images are
  165. * compiled, uninitialised data areas can be shared between drivers.
  166. * This will typically be used to share statically-allocated receive
  167. * and transmit buffers between drivers.
  168. *
  169. * Use as e.g.
  170. *
  171. * @code
  172. *
  173. * struct {
  174. * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
  175. * char tx_buf[TX_BUF_SIZE];
  176. * } my_static_data __shared;
  177. *
  178. * @endcode
  179. *
  180. */
  181. #define __shared __asm__ ( "_shared_bss" )
  182. #endif /* ASSEMBLY */
  183. #endif /* COMPILER_H */