Pristine Ack-5.5
[Ack-5.5.git] / lang / m2 / comp / tmpvar.C
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
8 /* T E M P O R A R Y   V A R I A B L E S */
9
10 /* $Id: tmpvar.C,v 1.14 1994/06/24 12:42:58 ceriel Exp $ */
11
12 /*      Code for the allocation and de-allocation of temporary variables,
13         allowing re-use.
14         The routines use "ProcScope" instead of "CurrentScope", because
15         "CurrentScope" also reflects WITH statements, and these scopes do not
16         have local variabes.
17 */
18
19 #include        "debug.h"
20
21 #include        <em_arith.h>
22 #include        <em_label.h>
23 #include        <em_code.h>
24 #include        <em_reg.h>
25 #include        <alloc.h>
26 #include        <assert.h>
27
28 #include        "LLlex.h"
29 #include        "def.h"
30 #include        "type.h"
31 #include        "scope.h"
32 #include        "main.h"
33
34 struct tmpvar {
35         struct tmpvar   *t_next;
36         arith           t_offset;       /* offset from LocalBase */
37 };
38
39 /* STATICALLOCDEF "tmpvar" 10 */
40
41 static struct tmpvar    *TmpInts,       /* for integer temporaries */
42                         *TmpPtrs;       /* for pointer temporaries */
43 static t_scope  *ProcScope;     /* scope of procedure in which the
44                                            temporaries are allocated
45                                         */
46
47 TmpOpen(sc) t_scope *sc;
48 {
49         /*      Initialize for temporaries in scope "sc".
50         */
51         ProcScope = sc;
52 }
53
54 arith
55 TmpSpace(sz, al)
56         arith sz;
57 {
58         register t_scope *sc = ProcScope;
59
60         sc->sc_off = - WA(align(sz - sc->sc_off, al));
61         return sc->sc_off;
62 }
63
64 STATIC arith
65 NewTmp(plist, sz, al, regtype)
66         register struct tmpvar **plist;
67         arith sz;
68 {
69         register arith offset;
70         register struct tmpvar *tmp;
71
72         if (!*plist) {
73                 offset = TmpSpace(sz, al);
74                 if (! options['n']) C_ms_reg(offset, sz, regtype, 0);
75         }
76         else {
77                 tmp = *plist;
78                 offset = tmp->t_offset;
79                 *plist = tmp->t_next;
80                 free_tmpvar(tmp);
81         }
82         return offset;
83 }
84
85 arith
86 NewInt()
87 {
88         return NewTmp(&TmpInts, int_size, int_align, reg_any);
89 }
90
91 arith
92 NewPtr()
93 {
94         return NewTmp(&TmpPtrs, pointer_size, pointer_align, reg_pointer);
95 }
96
97 STATIC
98 FreeTmp(plist, off)
99         struct tmpvar **plist;
100         arith off;
101 {
102         register struct tmpvar *tmp = new_tmpvar();
103
104         tmp->t_next = *plist;
105         tmp->t_offset = off;
106         *plist = tmp;
107 }
108
109 FreeInt(off)
110         arith off;
111 {
112         FreeTmp(&TmpInts, off);
113 }
114
115 FreePtr(off)
116         arith off;
117 {
118         FreeTmp(&TmpPtrs, off);
119 }
120
121 TmpClose()
122 {
123         register struct tmpvar *tmp, *tmp1;
124
125         tmp = TmpInts;
126         while (tmp) {
127                 tmp1 = tmp;
128                 tmp = tmp->t_next;
129                 free_tmpvar(tmp1);
130         }
131         tmp = TmpPtrs;
132         while (tmp) {
133                 tmp1 = tmp;
134                 tmp = tmp->t_next;
135                 free_tmpvar(tmp1);
136         }
137         TmpInts = TmpPtrs = 0;
138 }