Как работает эта последовательность вложенных расширений макроса препроцессора C?

Я просматриваю исходный код iPXE, чтобы попытаться понять, что происходит на ранней стадии на этом этапе загрузки, но происходит некоторое значительное расширение вложенных макросов, с которым я не знаком и которому трудно следовать.

В одном из исходных файлов (core/init.c), есть:

void initialise ( void ) {
    struct init_fn *init_fn;

    /* Call registered initialisation functions */
    for_each_table_entry ( init_fn, INIT_FNS )
            init_fn->initialise ();
}

Так что я иду через расширение for_each_table_entry,

Вот некоторые из соответствующих определений макросов:

#define for_each_table_entry( pointer, table )                          \
    for ( pointer = table_start ( table ) ;                         \
          pointer < 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

Фу, я думаю, что это может быть. Смешное количество вложенных расширений на мой взгляд.

Поэтому, если я попытаюсь развернуть вручную, чтобы увидеть, что происходит, я получу:

# 1
for_each_table_entry ( init_fn, INIT_FNS )

# 2
for ( init_fn = table_start ( INIT_FNS ) ;                               \
      init_fn < table_end ( INIT_FNS ) ;                                 \
      init_fn++ )

# 3
for ( init_fn = __table_entries ( INIT_FNS, 00 ) ;                       \
      init_fn < __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 < ( {                                                      \
    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 < ( {                                                           \
    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 < ( {                                                           \
    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 < ( {                                                           \
    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++ )

Святой ад бэтмен, я думаю это все. Я имею в виду, похоже, там больше макросов, так что, возможно, я все испортил, но я не вижу каких-либо определений макросов типа объекта для

  • __table_extract_type
  • __table_extract_name
  • __table_entries
  • __attribute__
  • __aligned__
    • Есть деф. за __aligned__attribute__Может, я все испортил?
  • __section__
  • __alignof__

Так правильно ли было мое расширение? И если да, то как это вообще допустимый код? Это низкий уровень C/ASM? Я знаю основы сборки, но не пользовался ею несколько лет, поэтому не уверен, что здесь происходит.

0 ответов

Другие вопросы по тегам