title: How to explain the code used to implement va_arg and va_start in tcc's stdarg.h?
body: <pre><code>struct __va_list_struct
  {
      unsigned int gp_offset;
      unsigned int fp_offset;
      union {
          unsigned int overflow_offset;
          char* overflow_arg_area;
      };
      char* reg_save_area;
  };
  
  typedef struct __va_list_struct* va_list;
  
  /* we use __builtin_(malloc|free) to avoid #define malloc tcc_malloc */
  /* XXX: this lacks the support of aggregated types. */
  #define va_start( ap, last )                                                  \
      ( ap = (va_list)__builtin_malloc ( sizeof ( struct __va_list_struct ) ),  \
        *ap = *(struct __va_list_struct*)( (char*)__builtin_frame_address ( 0 ) \
                                           - 16 ),                              \
        ap-&gt;overflow_arg_area                                                   \
        = ( (char*)__builtin_frame_address ( 0 ) + ap-&gt;overflow_offset ),       \
        ap-&gt;reg_save_area = (char*)__builtin_frame_address ( 0 ) - 176 - 16 )
  #define va_arg( ap, type )                                                     \
      ( *(type*)( __builtin_types_compatible_p ( type, long double )             \
                      ? ( ap-&gt;overflow_arg_area += 16,                           \
                          ap-&gt;overflow_arg_area - 16 )                           \
                      : __builtin_types_compatible_p ( type, double )            \
                            ? ( ap-&gt;fp_offset &lt; 128 + 48                         \
                                    ? ( ap-&gt;fp_offset += 16,                     \
                                        ap-&gt;reg_save_area + ap-&gt;fp_offset - 16 ) \
                                    : ( ap-&gt;overflow_arg_area += 8,              \
                                        ap-&gt;overflow_arg_area - 8 ) )            \
                            : ( ap-&gt;gp_offset &lt; 48                               \
                                    ? ( ap-&gt;gp_offset += 8,                      \
                                        ap-&gt;reg_save_area + ap-&gt;gp_offset - 8 )  \
                                    : ( ap-&gt;overflow_arg_area += 8,              \
                                        ap-&gt;overflow_arg_area - 8 ) ) ) )
  </code></pre>
  
  <p>The definition of first structure especially confuses me.</p>
  
  <p>The next is macro — <code>va_start()</code>.</p>
  
  <p>The final is macro — <code>va_arg()</code>.</p>
  
  <p>And all of above is used in x86_64 platform ...</p>
  
  <p>Can you explain what this code does?</p>