home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / varargs.h < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  171 lines

  1. /***
  2. *varargs.h - XENIX style macros for variable argument functions
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file defines XENIX style macros for accessing arguments of a
  8. *       function which takes a variable number of arguments.
  9. *       [System V]
  10. *
  11. *       [Public]
  12. *
  13. ****/
  14.  
  15. #if _MSC_VER > 1000
  16. #pragma once
  17. #endif  /* _MSC_VER > 1000 */
  18.  
  19. #ifndef _INC_VARARGS
  20. #define _INC_VARARGS
  21.  
  22. #if !defined (_WIN32) && !defined (_MAC)
  23. #error ERROR: Only Mac or Win32 targets supported!
  24. #endif  /* !defined (_WIN32) && !defined (_MAC) */
  25.  
  26. #ifndef _CRTBLD
  27. /* This version of the header files is NOT for user programs.
  28.  * It is intended for use when building the C runtimes ONLY.
  29.  * The version intended for public use will not have this message.
  30.  */
  31. #error ERROR: Use of C runtime library internal header file.
  32. #endif  /* _CRTBLD */
  33.  
  34. #ifdef _MSC_VER
  35. /*
  36.  * Currently, all MS C compilers for Win32 platforms default to 8 byte
  37.  * alignment.
  38.  */
  39. #pragma pack(push,8)
  40. #endif  /* _MSC_VER */
  41.  
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif  /* __cplusplus */
  45.  
  46. #if __STDC__
  47. #error varargs.h incompatible with ANSI (use stdarg.h)
  48. #endif  /* __STDC__ */
  49.  
  50.  
  51. #ifndef _VA_LIST_DEFINED
  52.  
  53. #ifdef _M_ALPHA
  54. typedef struct {
  55.     char *a0;           /* pointer to first homed integer argument */
  56.     int offset;         /* byte offset of next parameter */
  57. } va_list;
  58. #else  /* _M_ALPHA */
  59. typedef char *va_list;
  60. #endif  /* _M_ALPHA */
  61.  
  62. #define _VA_LIST_DEFINED
  63. #endif  /* _VA_LIST_DEFINED */
  64.  
  65.  
  66. #if defined (_M_IX86)
  67.  
  68. /*
  69.  * define a macro to compute the size of a type, variable or expression,
  70.  * rounded up to the nearest multiple of sizeof(int). This number is its
  71.  * size as function argument (Intel architecture). Note that the macro
  72.  * depends on sizeof(int) being a power of 2!
  73.  */
  74. #define _INTSIZEOF(n)    ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  75.  
  76. #define va_dcl va_list va_alist;
  77. #define va_start(ap) ap = (va_list)&va_alist
  78. #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
  79. #define va_end(ap) ap = (va_list)0
  80.  
  81.  
  82. #elif defined (_M_MRX000)
  83.  
  84.  
  85. #define va_dcl int va_alist;
  86. #define va_start(list) list = (char *) &va_alist
  87. #define va_end(list)
  88. #define va_arg(list, mode) ((mode *)(list =\
  89.  (char *) ((((int)list + (__builtin_alignof(mode)<=4?3:7)) &\
  90.  (__builtin_alignof(mode)<=4?-4:-8))+sizeof(mode))))[-1]
  91. /*  +++++++++++++++++++++++++++++++++++++++++++
  92.     Because of parameter passing conventions in C:
  93.     use mode=int for char, and short types
  94.     use mode=double for float types
  95.     use a pointer for array types
  96.     +++++++++++++++++++++++++++++++++++++++++++ */
  97.  
  98.  
  99. #elif defined (_M_ALPHA)
  100.  
  101. /*
  102.  * The Alpha compiler supports two builtin functions that are used to
  103.  * implement stdarg/varargs.  The __builtin_va_start function is used
  104.  * by va_start to initialize the data structure that locates the next
  105.  * argument.  The __builtin_isfloat function is used by va_arg to pick
  106.  * which part of the home area a given register argument is stored in.
  107.  * The home area is where up to six integer and/or six floating point
  108.  * register arguments are stored down (so they can also be referenced
  109.  * by a pointer like any arguments passed on the stack).
  110.  */
  111. extern void * __builtin_va_start(va_list, ...);
  112.  
  113. #define va_dcl long va_alist;
  114. #define va_start(list) __builtin_va_start(list, va_alist, 0)
  115. #define va_end(list)
  116. #define va_arg(list, mode) \
  117.     ( *(        ((list).offset += ((int)sizeof(mode) + 7) & -8) , \
  118.         (mode *)((list).a0 + (list).offset - \
  119.                     ((__builtin_isfloat(mode) && (list).offset <= (6 * 8)) ? \
  120.                         (6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) \
  121.                 ) \
  122.        ) \
  123.     )
  124.  
  125.  
  126. #elif defined (_M_PPC)
  127.  
  128. /*
  129.  * define a macro to compute the size of a type, variable or expression,
  130.  * rounded up to the nearest multiple of sizeof(int). This number is its
  131.  * size as function argument (PPC architecture). Note that the macro
  132.  * depends on sizeof(int) being a power of 2!
  133.  */
  134. /* this is for LITTLE-ENDIAN PowerPC */
  135.  
  136. /* bytes that a type occupies in the argument list */
  137. #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  138. /* return 'ap' adjusted for type 't' in arglist */
  139. #define _ALIGNIT(ap,t) \
  140.         ((((int)(ap))+(sizeof(t)<8?3:7)) & (sizeof(t)<8?~3:~7))
  141.  
  142. #define va_dcl va_list va_alist;
  143. #define va_start(ap) ap = (va_list)&va_alist
  144. #define va_arg(ap,t)    ( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) )
  145. #define va_end(ap) ap = (va_list)0
  146.  
  147. #else  /* defined (_M_PPC) */
  148.  
  149. /* A guess at the proper definitions for other platforms */
  150.  
  151. #define _INTSIZEOF(n)    ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  152.  
  153. #define va_dcl va_list va_alist;
  154. #define va_start(ap) ap = (va_list)&va_alist
  155. #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
  156. #define va_end(ap) ap = (va_list)0
  157.  
  158.  
  159. #endif  /* defined (_M_PPC) */
  160.  
  161.  
  162. #ifdef __cplusplus
  163. }
  164. #endif  /* __cplusplus */
  165.  
  166. #ifdef _MSC_VER
  167. #pragma pack(pop)
  168. #endif  /* _MSC_VER */
  169.  
  170. #endif  /* _INC_VARARGS */
  171.