tmpvar.C 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* T E M P O R A R Y V A R I A B L E S */
  2. /* Code for the allocation and de-allocation of temporary variables,
  3. allowing re-use.
  4. The routines use "ProcScope" instead of "CurrentScope", because
  5. "CurrentScope" also reflects WITH statements, and these scopes do not
  6. have local variables.
  7. */
  8. #include "debug.h"
  9. #include <alloc.h>
  10. #include <em_arith.h>
  11. #include <em_label.h>
  12. #include <em_reg.h>
  13. #include "def.h"
  14. #include "main.h"
  15. #include "scope.h"
  16. #include "type.h"
  17. struct tmpvar {
  18. struct tmpvar *next;
  19. arith t_offset; /* offset from LocalBase */
  20. };
  21. /* ALLOCDEF "tmpvar" 10 */
  22. static struct tmpvar *TmpInts, /* for integer temporaries */
  23. *TmpPtrs; /* for pointer temporaries */
  24. static struct scope *ProcScope; /* scope of procedure in which the
  25. temporaries are allocated
  26. */
  27. TmpOpen(sc)
  28. struct scope *sc;
  29. {
  30. /* Initialize for temporaries in scope "sc".
  31. */
  32. ProcScope = sc;
  33. }
  34. arith
  35. TmpSpace(sz, al)
  36. arith sz;
  37. {
  38. register struct scope *sc = ProcScope;
  39. sc->sc_off = - WA(align(sz - sc->sc_off, al));
  40. return sc->sc_off;
  41. }
  42. STATIC arith
  43. NewTmp(plist, sz, al, regtype, priority)
  44. struct tmpvar **plist;
  45. arith sz;
  46. {
  47. register arith offset;
  48. register struct tmpvar *tmp;
  49. if( !*plist ) {
  50. offset = TmpSpace(sz, al);
  51. if( !options['n'] ) C_ms_reg(offset, sz, regtype, priority);
  52. }
  53. else {
  54. tmp = *plist;
  55. offset = tmp->t_offset;
  56. *plist = tmp->next;
  57. free_tmpvar(tmp);
  58. }
  59. return offset;
  60. }
  61. arith
  62. NewInt(reg_prior)
  63. {
  64. return NewTmp(&TmpInts, int_size, int_align, reg_any, reg_prior);
  65. }
  66. arith
  67. NewPtr(reg_prior)
  68. {
  69. return NewTmp(&TmpPtrs, pointer_size, pointer_align, reg_pointer, reg_prior);
  70. }
  71. STATIC
  72. FreeTmp(plist, off)
  73. struct tmpvar **plist;
  74. arith off;
  75. {
  76. register struct tmpvar *tmp = new_tmpvar();
  77. tmp->next = *plist;
  78. tmp->t_offset = off;
  79. *plist = tmp;
  80. }
  81. FreeInt(off)
  82. arith off;
  83. {
  84. FreeTmp(&TmpInts, off);
  85. }
  86. FreePtr(off)
  87. arith off;
  88. {
  89. FreeTmp(&TmpPtrs, off);
  90. }
  91. TmpClose()
  92. {
  93. register struct tmpvar *tmp, *tmp1;
  94. tmp = TmpInts;
  95. while( tmp ) {
  96. tmp1 = tmp;
  97. tmp = tmp->next;
  98. free_tmpvar(tmp1);
  99. }
  100. tmp = TmpPtrs;
  101. while( tmp ) {
  102. tmp1 = tmp;
  103. tmp = tmp->next;
  104. free_tmpvar(tmp1);
  105. }
  106. TmpInts = TmpPtrs = 0;
  107. }