-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate_utils.cpp
324 lines (296 loc) · 11.1 KB
/
translate_utils.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "translate_utils.h"
#include "layeredmap.h"
namespace IR {
AbstractVarLocation* AbstractFrame::addVariable(const std::string &name,
int size, bool cant_be_register)
{
AbstractVarLocation *impl = createVariable(name, size, cant_be_register);
variables.push_back(impl);
return impl;
}
AbstractVarLocation* AbstractFrame::addParameter(const std::string &name,
int size, bool cant_be_register)
{
AbstractVarLocation *impl = createParameter(name, size);
parameters.push_back(impl);
if (impl->isRegister() && cant_be_register) {
AbstractVarLocation *storage = addVariable(name + "::.store." + name,
size, true);
parameter_store_prologue.push_back(ParameterMovement());
parameter_store_prologue.back().parameter = impl;
parameter_store_prologue.back().where_store = storage;
return storage;
}
return impl;
}
void AbstractFrame::addParentFpParamVariable(bool cant_be_register)
{
parent_fp_parameter = createVariable(".parent_fp",
framemanager->getPointerSize(), false);
if (parent_fp_parameter->isRegister() && cant_be_register) {
parent_fp_memory_storage = addVariable(name + "::.store." + name,
framemanager->getPointerSize(), true);
parameter_store_prologue.push_back(ParameterMovement());
parameter_store_prologue.back().parameter = parent_fp_parameter;
parameter_store_prologue.back().where_store = parent_fp_memory_storage;
parent_fp_parameter->prespillRegister(parent_fp_memory_storage);
}
}
AbstractFrame::~AbstractFrame()
{
for (std::list<AbstractVarLocation *>::iterator var = variables.begin();
var != variables.end(); var++)
delete *var;
}
}
namespace Semantic {
struct VarAccessDefInfo {
ObjectId object_id;
ObjectId owner_func_id;
bool is_function;
/**
* If it actually describes a function not a variable
*/
bool func_needs_parent_fp;
bool func_exports_parent_fp_to_children;
VarAccessDefInfo(ObjectId _id, ObjectId _owner_func_id, bool _is_func) :
object_id(_id), owner_func_id(_owner_func_id), is_function(_is_func),
func_needs_parent_fp(false), func_exports_parent_fp_to_children(false) {}
};
class VariablesAccessInfoPrivate {
public:
std::list<VarAccessDefInfo> variables, functions;
LayeredMap variable_names;
IdMap var_info;
VariablesAccessInfoPrivate() {}
};
VariablesAccessInfo::VariablesAccessInfo()
{
impl = new VariablesAccessInfoPrivate;
func_stack.push_back(INVALID_OBJECT_ID); // program body "function"
}
VariablesAccessInfo::~VariablesAccessInfo()
{
delete impl;
}
void VariablesAccessInfo::handleCall(Syntax::Tree function_exp,
ObjectId current_function_id)
{
return;
if (function_exp->type != Syntax::IDENTIFIER)
return;
VarAccessDefInfo *callee_info = (VarAccessDefInfo *)impl->variable_names.
lookup(((Syntax::Identifier *)function_exp)->name);
if (callee_info == NULL)
return;
int callee_parent = callee_info->owner_func_id;
int id = current_function_id;
while ((id >= 0) && (id != callee_parent)) {
VarAccessDefInfo *func_info =
(VarAccessDefInfo *)impl->var_info.lookup(current_function_id);
assert(func_info != NULL);
func_info->func_needs_parent_fp = true;
}
}
void VariablesAccessInfo::processDeclaration(Syntax::Tree declaration,
ObjectId current_function_id)
{
switch (declaration->type) {
case Syntax::TYPEDECLARATION:
break;
case Syntax::VARDECLARATION:
impl->variables.push_back(VarAccessDefInfo(
((Syntax::VariableDeclaration *)declaration)->id,
current_function_id, false));
impl->variable_names.add(((Syntax::VariableDeclaration *)declaration)->name->name,
&(impl->variables.back()));
break;
case Syntax::FUNCTION: {
Syntax::Function *func_declaration = (Syntax::Function *)declaration;
impl->functions.push_back(VarAccessDefInfo(
func_declaration->id,
current_function_id, true));
impl->functions.back().func_needs_parent_fp = true;
if (current_function_id >= 0) {
VarAccessDefInfo *our_info =
(VarAccessDefInfo *)impl->var_info.lookup(current_function_id);
assert(our_info != NULL);
our_info->func_exports_parent_fp_to_children = true;
}
impl->variable_names.add(func_declaration->name->name,
&(impl->functions.back()));
impl->var_info.add(func_declaration->id, &(impl->functions.back()));
if (func_declaration->body != NULL) {
impl->variable_names.newLayer();
for (std::list<Syntax::Tree>::iterator param = func_declaration->parameters->expressions.begin();
param != func_declaration->parameters->expressions.end();
param++) {
assert((*param)->type == Syntax::PARAMETERDECLARATION);
impl->variables.push_back(VarAccessDefInfo(
((Syntax::ParameterDeclaration *) *param)->id,
func_declaration->id, false));
impl->variable_names.add(((Syntax::VariableDeclaration *) *param)->name->name,
&(impl->variables.back()));
}
func_stack.push_back(func_declaration->id);
processExpression(func_declaration->body, func_declaration->id);
func_stack.pop_back();
impl->variable_names.removeLastLayer();
}
break;
}
default:
Error::fatalError("Not a declaration inside scope declarations?", declaration->linenumber);
}
}
void VariablesAccessInfo::processExpression(Syntax::Tree expression,
ObjectId current_function_id)
{
switch (expression->type) {
case Syntax::IDENTIFIER: {
VarAccessDefInfo *variable = (VarAccessDefInfo *)impl->variable_names.
lookup(((Syntax::Identifier *)expression)->name);
if ((variable != NULL) && (! variable->is_function) &&
(variable->owner_func_id != current_function_id)) {
impl->var_info.add(variable->object_id, variable);
std::list<int>::reverse_iterator func_in_stack =
func_stack.rbegin();
while ((func_in_stack != func_stack.rend()) &&
(*func_in_stack != variable->owner_func_id)) {
VarAccessDefInfo *func_info =
(VarAccessDefInfo *)impl->var_info.lookup(*func_in_stack);
assert(func_info != NULL);
func_info->func_needs_parent_fp = true;
if (*func_in_stack != current_function_id)
func_info->func_exports_parent_fp_to_children = true;
func_in_stack++;
}
}
break;
}
case Syntax::INTVALUE:
case Syntax::STRINGVALUE:
case Syntax::NIL:
case Syntax::BREAK:
break;
case Syntax::BINARYOP:
processExpression(((Syntax::BinaryOp *)expression)->left, current_function_id);
processExpression(((Syntax::BinaryOp *)expression)->right, current_function_id);
break;
case Syntax::SEQUENCE: {
for (std::list<Syntax::Tree>::iterator child =
((Syntax::Sequence *)expression)->content->expressions.begin();
child != ((Syntax::Sequence *)expression)->content->expressions.end();
child++)
processExpression(*child, current_function_id);
break;
}
case Syntax::ARRAYINDEXING:
processExpression(((Syntax::ArrayIndexing *)expression)->array, current_function_id);
processExpression(((Syntax::ArrayIndexing *)expression)->index, current_function_id);
break;
case Syntax::ARRAYINSTANTIATION:
processExpression(((Syntax::ArrayInstantiation *)expression)->arraydef->index, current_function_id);
processExpression(((Syntax::ArrayInstantiation *)expression)->value, current_function_id);
break;
case Syntax::IF:
processExpression(((Syntax::If *)expression)->condition, current_function_id);
processExpression(((Syntax::If *)expression)->action, current_function_id);
break;
case Syntax::IFELSE:
processExpression(((Syntax::IfElse *)expression)->condition, current_function_id);
processExpression(((Syntax::IfElse *)expression)->action, current_function_id);
processExpression(((Syntax::IfElse *)expression)->elseaction, current_function_id);
break;
case Syntax::WHILE:
processExpression(((Syntax::While *)expression)->condition, current_function_id);
processExpression(((Syntax::While *)expression)->action, current_function_id);
break;
case Syntax::FOR:
impl->variables.push_back(VarAccessDefInfo(
((Syntax::For *)expression)->variable_id, current_function_id, false));
impl->variable_names.add(((Syntax::For *)expression)->variable->name,
&(impl->variables.back()));
processExpression(((Syntax::For *)expression)->start, current_function_id);
processExpression(((Syntax::For *)expression)->stop, current_function_id);
processExpression(((Syntax::For *)expression)->action, current_function_id);
break;
case Syntax::SCOPE: {
impl->variable_names.newLayer();
for (std::list<Syntax::Tree>::iterator declaration =
((Syntax::Scope *)expression)->declarations->expressions.begin();
declaration != ((Syntax::Scope *)expression)->declarations->expressions.end();
declaration++)
processDeclaration(*declaration, current_function_id);
for (std::list<Syntax::Tree>::iterator body_expression =
((Syntax::Scope *)expression)->action->expressions.begin();
body_expression != ((Syntax::Scope *)expression)->action->expressions.end();
body_expression++)
processExpression(*body_expression, current_function_id);
impl->variable_names.removeLastLayer();
break;
}
case Syntax::RECORDFIELD:
processExpression(((Syntax::RecordField *)expression)->record, current_function_id);
break;
case Syntax::FUNCTIONCALL:
processExpression(((Syntax::FunctionCall *)expression)->function, current_function_id);
handleCall(((Syntax::FunctionCall *)expression)->function, current_function_id);
for (std::list<Syntax::Tree>::iterator child =
((Syntax::FunctionCall *)expression)->arguments->expressions.begin();
child != ((Syntax::FunctionCall *)expression)->arguments->expressions.end();
child++)
processExpression(*child, current_function_id);
break;
case Syntax::RECORDINSTANTIATION: {
for (std::list<Syntax::Tree>::iterator field_def =
((Syntax::RecordInstantiation *)expression)->fieldvalues->expressions.begin();
field_def != ((Syntax::RecordInstantiation *)expression)->fieldvalues->expressions.end();
field_def++) {
assert((*field_def)->type == Syntax::BINARYOP);
Syntax::BinaryOp *fieldvalue = (Syntax::BinaryOp *) *field_def;
assert(fieldvalue->operation == SYM_ASSIGN);
processExpression(fieldvalue->right, current_function_id);
}
break;
}
default:
Error::fatalError("Unexpected syntax element :-/", expression->linenumber);
}
}
bool VariablesAccessInfo::isAccessedByAddress(Syntax::Tree definition)
{
int id;
switch (definition->type) {
case Syntax::VARDECLARATION:
id = ((Syntax::VariableDeclaration *)definition)->id;
break;
case Syntax::PARAMETERDECLARATION:
id = ((Syntax::ParameterDeclaration *)definition)->id;
break;
case Syntax::FOR:
id = ((Syntax::For *)definition)->variable_id;
break;
default:
Error::fatalError("Variable access checked for non-variable declaration",
definition->linenumber);
}
return impl->var_info.lookup(id) != NULL;
}
bool VariablesAccessInfo::functionNeedsParentFp(Syntax::Function* definition)
{
VarAccessDefInfo *info = (VarAccessDefInfo *)impl->var_info.lookup(definition->id);
if (info == NULL)
return false;
else
return info->func_needs_parent_fp;
}
bool VariablesAccessInfo::isFunctionParentFpAccessedByChildren(Syntax::Function* definition)
{
VarAccessDefInfo *info = (VarAccessDefInfo *)impl->var_info.lookup(definition->id);
if (info == NULL)
return false;
else
return info->func_exports_parent_fp_to_children;
}
}