axp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * QuickThreads -- Threads-building toolkit.
  3. * Copyright (c) 1993 by David Keppel
  4. *
  5. * Permission to use, copy, modify and distribute this software and
  6. * its documentation for any purpose and without fee is hereby
  7. * granted, provided that the above copyright notice and this notice
  8. * appear in all copies. This software is provided as a
  9. * proof-of-concept and for demonstration purposes; there is no
  10. * representation about the suitability of this software for any
  11. * purpose.
  12. */
  13. #include <stdarg.h>
  14. #include "qt.h"
  15. /* Varargs is harder on the AXP. Parameters are saved on the stack as
  16. something like (stack grows down to low memory; low at bottom of
  17. picture):
  18. | :
  19. | arg6
  20. +---
  21. | iarg5
  22. | :
  23. | iarg3 <-- va_list._a0 + va_list._offset
  24. | :
  25. | iarg0 <-- va_list._a0
  26. +---
  27. | farg5
  28. | :
  29. | farg0
  30. +---
  31. When some of the arguments have known type, there is no need to
  32. save all of them in the struct. So, for example, if the routine is
  33. called
  34. zork (int a0, float a1, int a2, ...)
  35. {
  36. va_list ap;
  37. va_start (ap, a2);
  38. qt_vargs (... &ap ...);
  39. }
  40. then offset is set to 3 * 8 (8 === sizeof machine word) = 24.
  41. What this means for us is that the user's routine needs to be
  42. called with an arg list where some of the words in the `any type'
  43. parameter list have to be split and moved up in to the int/fp
  44. region.
  45. Ways in which this can fail:
  46. - The user might not know the size of the pushed arguments anyway.
  47. - Structures have funny promotion rules.
  48. - Probably lots of other things.
  49. All in all, we never promised varargs would work reliably. */
  50. #define QT_VADJ(sp) (((char *)sp) - QT_VSTKBASE)
  51. #define QT_VARGS_MD0(sp, vabytes) \
  52. ((qt_t *)(((char *)(sp)) - 6*2*8 - QT_STKROUNDUP(vabytes)))
  53. extern void qt_vstart(void);
  54. #define QT_VARGS_MD1(sp) (QT_SPUT (sp, QT_R26, qt_vstart))
  55. /* Different machines use different implementations for varargs.
  56. Unfortunately, the code below ``looks in to'' the varargs
  57. structure, `va_list', and thus depends on the conventions.
  58. The following #defines try to deal with it but don't catch
  59. everything. */
  60. #ifdef __GNUC__
  61. #define _a0 __base
  62. #define _offset __offset
  63. #else
  64. #ifdef __OSF1__
  65. #define _a0 a0
  66. #define _offset offset
  67. #endif
  68. #endif /* def __GNUC__ */
  69. struct qt_t *
  70. qt_vargs (struct qt_t *qsp, int nbytes, struct va_list *vargs,
  71. void *pt, qt_function_t *startup,
  72. qt_function_t *vuserf, qt_function_t *cleanup)
  73. {
  74. va_list ap;
  75. int i;
  76. int max; /* Maximum *words* of args to copy. */
  77. int tmove; /* *Words* of args moved typed->typed. */
  78. qt_word_t *sp;
  79. ap = *(va_list *)vargs;
  80. qsp = QT_VARGS_MD0 (qsp, nbytes);
  81. sp = (qt_word_t *)qsp;
  82. tmove = 6 - ap._offset/sizeof(qt_word_t);
  83. /* Copy from one typed area to the other. */
  84. for (i=0; i<tmove; ++i) {
  85. /* Integer args: */
  86. sp[i+6] = ((qt_word_t *)(ap._a0 + ap._offset))[i];
  87. /* Fp args: */
  88. sp[i] = ((qt_word_t *)(ap._a0 + ap._offset))[i-6];
  89. }
  90. max = nbytes/sizeof(qt_word_t);
  91. /* Copy from the untyped area to the typed area. Split each arg.
  92. in to integer and floating-point save areas. */
  93. for (; i<6 && i<max; ++i) {
  94. sp[i] = sp[i+6] = ((qt_word_t *)(ap._a0 + ap._offset))[i];
  95. }
  96. /* Copy from the untyped area to the other untyped area. */
  97. for (; i<max; ++i) {
  98. sp[i+6] = ((qt_word_t *)(ap._a0 + ap._offset))[i];
  99. }
  100. QT_VARGS_MD1 (QT_VADJ(sp));
  101. QT_SPUT (QT_VADJ(sp), QT_VARGT_INDEX, pt);
  102. QT_SPUT (QT_VADJ(sp), QT_VSTARTUP_INDEX, startup);
  103. QT_SPUT (QT_VADJ(sp), QT_VUSERF_INDEX, vuserf);
  104. QT_SPUT (QT_VADJ(sp), QT_VCLEANUP_INDEX, cleanup);
  105. return ((qt_t *)QT_VADJ(sp));
  106. }