tmpvar.C 2.3 KB

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