-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.cpp
More file actions
212 lines (165 loc) · 4.56 KB
/
Copy pathallocator.cpp
File metadata and controls
212 lines (165 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* File: allocator.cpp
*
* Description: This file contains the member function definitions for
* functions dealing with storage allocation. The actual
* classes are declared elsewhere, mainly in Tree.h.
*
* Extra functionality:
* - maintaining minimum offset in nested blocks
* - allocation within statements
* - computing alignment (necessary on some systems)
*/
# include <cassert>
# include <iostream>
# include "checker.h"
# include "machine.h"
# include "tokens.h"
# include "Tree.h"
using namespace std;
/*
* Function: Type::size
*
* Description: Return the size of a type in bytes.
*/
unsigned long Type::size() const
{
unsigned long count;
assert(_kind != FUNCTION && _kind != ERROR);
count = (_kind == ARRAY ? _length : 1);
if (_indirection > 0)
return count * SIZEOF_PTR;
if (_specifier == CHAR)
return count * SIZEOF_CHAR;
if (_specifier == INT)
return count * SIZEOF_INT;
if (_specifier == LONG)
return count * SIZEOF_LONG;
return 0;
}
/*
* Function: Type::alignment
*
* Description: Return the alignment of a type in bytes.
*/
unsigned Type::alignment() const
{
assert(_kind != FUNCTION && _kind != ERROR);
if (_indirection > 0)
return ALIGNOF_PTR;
if (_specifier == CHAR)
return ALIGNOF_CHAR;
if (_specifier == INT)
return ALIGNOF_INT;
if (_specifier == LONG)
return ALIGNOF_LONG;
return 0;
}
/*
* Function: Block::allocate
*
* Description: Allocate storage for this block. We assign decreasing
* offsets for all symbols declared within this block, and
* then for all symbols declared within any nested block.
* Only symbols that have not already been allocated an offset
* will be assigned one, since the parameters are already
* assigned special offsets.
*/
void Block::allocate(int &offset) const
{
int temp, saved;
const Symbols &symbols = _decls->symbols();
for (auto symbol : symbols)
if (symbol->offset == 0) {
offset -= symbol->type().size();
symbol->offset = offset;
}
saved = offset;
for (auto stmt : _stmts) {
temp = saved;
stmt->allocate(temp);
offset = min(offset, temp);
}
}
/*
* Function: While::allocate
*
* Description: Allocate storage for this while statement, which
* essentially means allocating storage for variables declared
* as part of its statement.
*/
void While::allocate(int &offset) const
{
_stmt->allocate(offset);
}
/*
* Function: For::allocate
*
* Description: Allocate storage for this for statement, which
* essentially means allocating storage for variables declared
* as part of its statement.
*/
void For::allocate(int &offset) const
{
_stmt->allocate(offset);
}
/*
* Function: If::allocate
*
* Description: Allocate storage for this if-then or if-then-else
* statement, which essentially means allocating storage for
* variables declared as part of its statements.
*/
void If::allocate(int &offset) const
{
int saved, temp;
saved = offset;
_thenStmt->allocate(offset);
if (_elseStmt != nullptr) {
temp = saved;
_elseStmt->allocate(temp);
offset = min(offset, temp);
}
}
/*
* Function: Function::allocate
*
* Description: Allocate storage for this function and return the number of
* bytes required. The parameters are allocated offsets as
* well, starting with the given offset. This function is
* designed to work with both 32-bit and 64-bit Intel
* architectures, with or without callee-saved registers.
*
* 32-bit Intel/Linux:
* PARAM_ALIGNMENT = 4 (each parameter is a multiple of 4 bytes)
* NUM_PARAM_REGS = 0 (all parameters are on the stack)
*
* 64-bit Intel/Linux:
* PARAM_ALIGNMENT = 8 (each parameter is a multiple of 8 bytes)
* NUM_PARAM_REGS = 6 (first six parameters are in registers)
*
* The initial value of the offset should be offset of the
* first parameter on the stack. Normally, this value is the
* size of two registers (the instruction pointer and the base
* pointer), but would be larger if additional callee-saved
* registers were used.
*/
void Function::allocate(int &offset) const
{
Types types = _id->type().parameters()->types;
const Symbols &symbols = _body->declarations()->symbols();
for (unsigned i = NUM_PARAM_REGS; i < types.size(); i ++) {
symbols[i]->offset = offset;
offset += types[i].size();
while (offset % PARAM_ALIGNMENT != 0)
offset ++;
}
offset = 0;
for (unsigned i = 0; i < NUM_PARAM_REGS; i ++)
if (i < types.size()) {
offset -= types[i].size();
symbols[i]->offset = offset;
} else
break;
_body->allocate(offset);
}