title: How does this sequence of nested C Preprocessor Macro expansions work?
body: <p>I'm looking over the <a href="http://ipxe.org/" rel="nofollow">iPXE</a> source code to try to understand what happens early on at this stage of boot up, but there is some major nested macro expansion going on that I'm unfamiliar with and having trouble following.</p>
  
  <p>In one of the source files (<code>core/init.c</code>), there is:</p>
  
  <pre><code>void initialise ( void ) {
      struct init_fn *init_fn;
  
      /* Call registered initialisation functions */
      for_each_table_entry ( init_fn, INIT_FNS )
              init_fn-&gt;initialise ();
  }
  </code></pre>
  
  <p>So I'm walking through the expansion of <code>for_each_table_entry</code>.</p>
  
  <p>Here are some of the relevant macro definitions:</p>
  
  <pre><code>#define for_each_table_entry( pointer, table )                          \
      for ( pointer = table_start ( table ) ;                         \
            pointer &lt; table_end ( table ) ;                           \
            pointer++ )
  
  #define table_start( table ) __table_entries ( table, 00 )
  
  #define table_end( table ) __table_entries ( table, 99 )
  
  #define __table_entries( table, idx ) ( {                               \
      static __table_type ( table ) __table_entries[0]                \
              __table_entry ( table, idx )                            \
              __attribute__ (( unused ));                             \
      __table_entries; } )
  
  #define __table_type( table ) __table_extract_type table
  #define __table_extract_type( type, name ) type
  
  #define __table_entry( table, idx )                                     \
      __attribute__ (( __section__ ( __table_section ( table, idx ) ),\
                       __aligned__ ( __table_alignment ( table ) ) ))
  
  #define __table_section( table, idx ) \
      ".tbl." __table_name ( table ) "." __table_str ( idx )
  #define __table_str( x ) #x
  
  #define __table_alignment( table ) __alignof__ ( __table_type ( table ) )
  
  #define __table_name( table ) __table_extract_name table
  #define __table_extract_name( type, name ) name
  </code></pre>
  
  <p>Phew, I think that may be it. A ridiculous amount of nested expansion in my opinion.</p>
  
  <p>So if I try to manually expand to see what is going on, I get:</p>
  
  <pre><code># 1
  for_each_table_entry ( init_fn, INIT_FNS )
  
  # 2
  for ( init_fn = table_start ( INIT_FNS ) ;                               \
        init_fn &lt; table_end ( INIT_FNS ) ;                                 \
        init_fn++ )
  
  # 3
  for ( init_fn = __table_entries ( INIT_FNS, 00 ) ;                       \
        init_fn &lt; __table_entries ( INIT_FNS, 00 ) ;                       \
        init_fn++ )
  
  # 4
  for ( init_fn = ( {                                                      \
      static __table_type ( INIT_FNS ) __table_entries[0]                  \
              __table_entry ( INIT_FNS, 00 )                               \
              __attribute__ (( unused ));                                  \
      __table_entries; } ) ;                                               \
        init_fn &lt; ( {                                                      \
      static __table_type ( INIT_FNS ) __table_entries[0]                  \
              __table_entry ( INIT_FNS, 99 )                               \
              __attribute__ (( unused ));                                  \
      __table_entries; } ) ;                                               \
        init_fn++ )
  
  # 5
  for ( init_fn = ( {                                                           \
      static  __table_extract_type INIT_FNS __table_entries[0]                  \
              __attribute__ (( __section__ ( __table_section ( INIT_FNS, 00 ) ),\
                   __aligned__ ( __table_alignment ( INIT_FNS ) ) ))            \
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn &lt; ( {                                                           \
      static __table_extract_type INIT_FNS __table_entries[0]               
              __attribute__ (( __section__ ( __table_section ( INIT_FNS, 99 ) ),\
                   __aligned__ ( __table_alignment ( INIT_FNS ) ) ))
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn++ )
  
  # 6
  for ( init_fn = ( {                                                           \
      static  __table_extract_type INIT_FNS __table_entries[0]                  \
              __attribute__ (( __section__ ( ".tbl." __table_name ( INIT_FNS ) "." __table_str ( 00 ) ),\
                   __aligned__ ( __alignof__ ( __table_type ( INIT_FNS ) ) ) ))            \
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn &lt; ( {                                                           \
      static __table_extract_type INIT_FNS __table_entries[0]               
              __attribute__ (( __section__ ( ".tbl." __table_name ( INIT_FNS ) "." __table_str ( 99 ),\
                   __aligned__ ( __alignof__ ( __table_type ( INIT_FNS ) ) ) ))
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn++ )
  
  # 7
  for ( init_fn = ( {                                                           \
      static  __table_extract_type INIT_FNS __table_entries[0]                  \
              __attribute__ (( __section__ ( ".tbl." __table_extract_name INIT_FNS "." #00 ),\
                   __aligned__ ( __alignof__ ( __table_extract_type INIT_FNS ) ) ))            \
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn &lt; ( {                                                           \
      static __table_extract_type INIT_FNS __table_entries[0]               
              __attribute__ (( __section__ ( ".tbl." __table_extract_name INIT_FNS "." #99,\
                   __aligned__ ( __alignof__ ( __table_extract_type INIT_FNS ) ) ))
              __attribute__ (( unused ));                                       \
      __table_entries; } ) ;                                                    \
        init_fn++ )
  </code></pre>
  
  <p>Holy hell batman, I think that's it.  I mean, it looks like there are more macros in there, so I may have messed up, but I don't see any <em>object type</em> macro definitions for</p>
  
  <ul>
  <li><code>__table_extract_type</code></li>
  <li><code>__table_extract_name</code></li>
  <li><code>__table_entries</code></li>
  <li><code>__attribute__</code></li>
  <li><code>__aligned__</code>
  
  <ul>
  <li>There is a def. for <code>__aligned__attribute__</code>, maybe I messed that up?</li>
  </ul></li>
  <li><code>__section__</code></li>
  <li><code>__alignof__</code></li>
  </ul>
  
  <p>So was my expansion correct? And if so, how is this even valid code? Is this low level C/ASM stuff? I know the basics of assembly, but haven't used it in several years so I'm not sure what's going on here.</p>