-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate_utils.h
175 lines (147 loc) · 4.71 KB
/
translate_utils.h
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
#ifndef _TRANSLATE_UTILS_H
#define _TRANSLATE_UTILS_H
#include "idmap.h"
#include "syntaxtree.h"
#include "intermediate.h"
#include "types.h"
#include <vector>
#include <list>
namespace IR {
class AbstractFrame;
class AbstractFrameManager;
class AbstractVarLocation {
public:
AbstractFrame *owner_frame;
AbstractVarLocation(AbstractFrame *_frame) : owner_frame(_frame) {}
virtual IR::Expression *createCode(AbstractFrame *currentFrame) = 0;
virtual bool isRegister() = 0;
virtual void prespillRegister(AbstractVarLocation *location) = 0;
};
class DummyVarLocation: public AbstractVarLocation {
public:
DummyVarLocation(AbstractFrame *frame) : AbstractVarLocation(frame) {}
virtual IR::Expression *createCode(AbstractFrame *currentFrame) {return NULL;}
virtual bool isRegister() {return true;}
virtual void prespillRegister(AbstractVarLocation *) {}
};
class AbstractFrame {
private:
virtual AbstractVarLocation *createVariable(const std::string &name,
int size, bool cant_be_register) = 0;
virtual AbstractVarLocation *createParameter(const std::string &name,
int size) = 0;
public:
struct ParameterMovement {
AbstractVarLocation *parameter;
AbstractVarLocation *where_store;
};
protected:
AbstractFrame *parent;
int id;
std::string name;
AbstractVarLocation *parent_fp_parameter;
AbstractVarLocation *parent_fp_memory_storage;
AbstractFrameManager *framemanager;
std::list<AbstractVarLocation *>variables;
std::list<AbstractVarLocation *>parameters;
std::list<ParameterMovement> parameter_store_prologue;
public:
AbstractFrame(AbstractFrameManager *_framemanager,
const std::string &_name, int _id, AbstractFrame *_parent) :
framemanager(_framemanager), name(_name), id(_id), parent(_parent),
parent_fp_parameter(NULL), parent_fp_memory_storage(NULL)
{}
AbstractVarLocation *addVariable(const std::string &name,
int size, bool cant_be_register);
AbstractVarLocation *addParameter(const std::string &name,
int size, bool cant_be_register);
const std::list<AbstractVarLocation *> &getParameters()
{return parameters;}
const std::list<ParameterMovement> &getMovementPrologue()
{return parameter_store_prologue;}
void addParentFpParamVariable(bool cant_be_register);
virtual IR::VirtualRegister *getFramePointer() = 0;
const std::string &getName() {return name;}
int getId() {return id;}
AbstractFrame *getParent() {return parent;}
AbstractVarLocation *getParentFpForUs()
{
return parent_fp_parameter;
}
AbstractVarLocation *getParentFpForChildren()
{
return parent_fp_memory_storage;
}
~AbstractFrame();
};
class DummyFrame: public AbstractFrame {
private:
virtual AbstractVarLocation *createVariable(const std::string &name,
int size, bool cant_be_register)
{
return new DummyVarLocation(this);
}
virtual AbstractVarLocation *createParameter(const std::string &name,
int size)
{
return new DummyVarLocation(this);
}
public:
DummyFrame(AbstractFrameManager *_framemanager, const std::string &name,
int id, DummyFrame *parent) :
AbstractFrame(_framemanager, name, id, parent) {}
virtual IR::VirtualRegister *getFramePointer() {return NULL;}
};
class AbstractFrameManager {
protected:
IREnvironment *IR_env;
public:
AbstractFrameManager(IREnvironment *env) : IR_env(env) {}
virtual AbstractFrame *rootFrame() = 0;
virtual AbstractFrame *newFrame(AbstractFrame *parent, const std::string &name) = 0;
virtual int getVarSize(Semantic::Type *type) = 0;
virtual int getPointerSize() = 0;
virtual void updateRecordSize(int &size, Semantic::Type *newFieldType) = 0;
};
class DummyFrameManager: public AbstractFrameManager {
public:
DummyFrameManager(IREnvironment *env) : AbstractFrameManager(env) {}
virtual DummyFrame *rootFrame()
{
return new DummyFrame(this, ".root", 0, NULL);
}
virtual DummyFrame *newFrame(AbstractFrame *parent, const std::string &name)
{
return new DummyFrame(this, name, 0, (DummyFrame *)parent);
}
virtual int getVarSize(Semantic::Type *type)
{
return 0;
}
virtual int getPointerSize(Semantic::Type *type)
{
return 0;
}
virtual void updateRecordSize(int &size, Semantic::Type *newFieldType)
{
}
};
}
namespace Semantic {
class VariablesAccessInfoPrivate;
class VariablesAccessInfo {
private:
VariablesAccessInfoPrivate *impl;
std::list<int> func_stack;
void handleCall(Syntax::Tree function_exp, ObjectId current_function_id);
public:
VariablesAccessInfo();
~VariablesAccessInfo();
void processExpression(Syntax::Tree expression, ObjectId current_function_id);
void processDeclaration(Syntax::Tree declaration, ObjectId current_function_id);
bool isAccessedByAddress(Syntax::Tree definition);
bool functionNeedsParentFp(Syntax::Function *definition);
bool isFunctionParentFpAccessedByChildren(Syntax::Function *definition);
};
}
#endif