m88k.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 m88k. Parameters are saved on the stack as
  16. something like (stack grows down to low memory; low at bottom of
  17. picture):
  18. | :
  19. | arg8 <-- va_list.__va_stk
  20. +---
  21. | :
  22. +---
  23. | arg7
  24. | :
  25. | iarg0 <-- va_list.__va_reg
  26. +---
  27. | :
  28. | va_list { __va_arg, __va_stk, __va_reg }
  29. | :
  30. +---
  31. Here, `va_list.__va_arg' is the number of word-size arguments
  32. that have already been skipped. Doubles must be double-arligned.
  33. What this means for us is that the user's routine needs to be
  34. called with an arg list where some of the words in the `__va_stk'
  35. part of the parameter list have to be promoted to registers.
  36. BUG: doubleword register arguments must be double-aligned. If
  37. something is passed as an even # arg and used as an odd # arg or
  38. vice-versa, the code in the called routine (in the new thread) that
  39. decides how to adjust the index will get it wrong, because it will
  40. be expect it to be, say, doubleword aligned and it will really be
  41. singleword aligned.
  42. I'm not sure you can solve this without knowing the types of all
  43. the arguments. All in all, we never promised varargs would work
  44. reliably. */
  45. #define QT_VADJ(sp) (((char *)sp) - QT_VSTKBASE)
  46. /* Always allocate at least enough space for 8 args; waste some space
  47. at the base of the stack to ensure the startup routine doesn't read
  48. off the end of the stack. */
  49. #define QT_VARGS_MD0(sp, vabytes) \
  50. ((qt_t *)(((char *)(sp)) - 8*4 - QT_STKROUNDUP(vabytes)))
  51. extern void qt_vstart(void);
  52. #define QT_VARGS_MD1(sp) (QT_SPUT (sp, QT_1, qt_vstart))
  53. struct qt_t *
  54. qt_vargs (struct qt_t *qsp, int nbytes, void *vargs,
  55. void *pt, qt_function_t *startup,
  56. qt_function_t *vuserf, qt_function_t *cleanup)
  57. {
  58. va_list ap;
  59. int i;
  60. int n; /* Number of words into original arg list. */
  61. qt_word_t *sp;
  62. int *reg; /* Where to read passed-in-reg args. */
  63. int *stk; /* Where to read passed-on-stk args. */
  64. ap = *(va_list *)vargs;
  65. qsp = QT_VARGS_MD0 (qsp, nbytes);
  66. sp = (qt_word_t *)qsp;
  67. reg = (ap.__va_arg < 8)
  68. ? &ap.__va_reg[ap.__va_arg]
  69. : 0;
  70. stk = &ap.__va_stk[8];
  71. n = ap.__va_arg;
  72. for (i=0; i<nbytes/sizeof(qt_word_t) && n<8; ++i,++n) {
  73. sp[i] = *reg++;
  74. }
  75. for (; i<nbytes/sizeof(qt_word_t); ++i) {
  76. sp[i] = *stk++;
  77. }
  78. #ifdef QT_NDEF
  79. for (i=0; i<nbytes/sizeof(qt_word_t); ++i) {
  80. sp[i] = (n < 8)
  81. ? *reg++
  82. : *stk++;
  83. ++n;
  84. }
  85. #endif
  86. QT_VARGS_MD1 (QT_VADJ(sp));
  87. QT_SPUT (QT_VADJ(sp), QT_VARGT_INDEX, pt);
  88. QT_SPUT (QT_VADJ(sp), QT_VSTARTUP_INDEX, startup);
  89. QT_SPUT (QT_VADJ(sp), QT_VUSERF_INDEX, vuserf);
  90. QT_SPUT (QT_VADJ(sp), QT_VCLEANUP_INDEX, cleanup);
  91. return ((qt_t *)QT_VADJ(sp));
  92. }