Repository navigation
Expand file tree
/
Copy pathallocator.cpp
More file actions
187 lines (142 loc) · 3.6 KB
/
Copy pathallocator.cpp
File metadata and controls
187 lines (142 loc) · 3.6 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
/*
* 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:
* - computing the alignment of types
* - maintaining minimum offset in nested blocks
* - allocation within while, for, and if-then-else statements
*/
# 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 Type::size() const
{
unsigned count;
assert(_kind != FUNCTION && _kind != ERROR);
count = (_kind == ARRAY ? _length : 1);
if (_indirection > 0)
return count * SIZEOF_PTR;
if (_specifier == INT)
return count * SIZEOF_INT;
if (_specifier == CHAR)
return count * SIZEOF_CHAR;
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;
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;
unsigned i;
Symbols symbols;
symbols = _decls->symbols();
for (i = 0; i < symbols.size(); i ++)
if (symbols[i]->_offset == 0) {
offset -= symbols[i]->type().size();
symbols[i]->_offset = offset;
}
saved = offset;
for (i = 0; i < _stmts.size(); i ++) {
temp = saved;
_stmts[i]->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.
*/
void Function::allocate(int &offset) const
{
Parameters *params;
Symbols symbols;
params = _id->type().parameters();
symbols = _body->declarations()->symbols();
offset = PARAM_OFFSET;
for (unsigned i = 0; i < params->_types.size(); i ++) {
symbols[i]->_offset = offset;
offset += SIZEOF_ARG;
}
offset = 0;
_body->allocate(offset);
}