-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable.java
More file actions
268 lines (218 loc) · 9.49 KB
/
Variable.java
File metadata and controls
268 lines (218 loc) · 9.49 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
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
import java.util.ArrayList;
public class Variable extends Symbol{
public int blockNum;
public int regIndex;
public int value;
public boolean isConst ;
public boolean isDefined;
public boolean isParam;
public ArrayList<Integer> arrayDimensions;
public void setRegIndex(int regIndex) {
this.regIndex = regIndex;
}
public Variable(int BType,ArrayList<Integer> arrayDimensions){
super(BType);
this.isParam = true;
this.arrayDimensions = new ArrayList<>();
this.arrayDimensions.addAll(arrayDimensions);
}
public Variable(String name, int BType, int blockNum, int regIndex, ArrayList<Integer> arrayDimensions, boolean isConst, boolean isParam) {
super(name, BType);
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConst = isConst;
this.isDefined = true;
this.isParam = isParam;
this.arrayDimensions = new ArrayList<>();
this.arrayDimensions.addAll(arrayDimensions);
}
public Variable(String name, int BType,int blockNum, int regIndex, ArrayList<Integer> arrayDimensions,boolean isConst) {
super(name, BType);
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConst = isConst;
this.isDefined = true;
this.isParam = false;
this.arrayDimensions = new ArrayList<>();
this.arrayDimensions.addAll(arrayDimensions);
allocaArray();
}
public Variable(String name, int BType,int blockNum, int regIndex) {
super(name, BType);
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConst = false;
this.isDefined = blockNum == 0;
this.isParam = false;
allocaVariable();
}
public Variable(String name, int BType,int blockNum, int regIndex,boolean isParam) {
super(name, BType);
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConst = false;
this.isDefined = true;
this.isParam = isParam;
}
public Variable(String name, int BType,int value ){
super(name, BType);
this.value = value;
this.isConst = true;
this.isParam = false;
}
public Variable(int BType){
super(BType);
}
public void allocaArray() {
StringBuilder str = Compiler.res;
if(blockNum==0){
str.append(this).append(" = dso_local ").append((isConst)?"constant ":"global ");
StringBuilder arrayInfo= getArrayAllocaInfo(arrayDimensions.size());
str.append(arrayInfo).append(" ");
}else{
str.append(" ").append(this).append(" = alloca ");
StringBuilder arrayInfo= getArrayAllocaInfo(arrayDimensions.size());
str.append(arrayInfo).append("\n")
.append(" store ").append(arrayInfo).append(" zeroinitializer, ")
.append(arrayInfo).append("* ").append(this).append("\n");
}
}
public Exp getArrayElementPtr(ArrayList<Exp> indexList,boolean passAsParam){
Exp ret = null;
StringBuilder str = Compiler.res;
if(indexList.size() > 0 ){
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum)
.append(" = getelementptr ")
.append(getArrayAllocaInfo(arrayDimensions.size())).append(", ")
.append(getArrayAllocaInfo(arrayDimensions.size())).append("* ")
.append(this);
if(!isParam)
str.append(", i32 0");
str.append(", ");
for (int i = 0; i < indexList.size(); i++) {
str.append("i32 ").append(indexList.get(i));
if(i<indexList.size()-1)
str.append(", ");
}
str.append("\n");
}
if(passAsParam){
if(indexList.size()!=getArraySize()){
if(indexList.size()==0){
if(isParam){
ret = new Exp(blockNum,regIndex,Symbol.TypePointer,arrayDimensions);
}else{
ArrayList<Integer> tempArray = new ArrayList<>();
for(int i=1;i<arrayDimensions.size();i++){
tempArray.add(arrayDimensions.get(i));
}
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum)
.append(" = getelementptr ")
.append(getArrayAllocaInfo(arrayDimensions.size())).append(", ")
.append(getArrayAllocaInfo(arrayDimensions.size())).append("* ")
.append(this).append(", i32 0, i32 0\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++,Symbol.TypePointer,tempArray);
}
}else{
ArrayList<Integer> tempArray = new ArrayList<>();
for(int i=(isParam)?indexList.size():indexList.size()+1;i<arrayDimensions.size();i++){
tempArray.add(arrayDimensions.get(i));
}
Exp exp = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum)
.append(" = getelementptr ")
.append(getArrayAllocaInfo(tempArray.size()+1)).append(", ")
.append(getArrayAllocaInfo(tempArray.size()+1)).append("* ")
.append(exp).append(", i32 0, i32 0\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++,Symbol.TypePointer,tempArray);
}
}else ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
}else ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
return ret;
}
public int getArrayDimension(int index){
return arrayDimensions.get(index);
}
public int getArraySize(){
return (isParam)?arrayDimensions.size()+1:arrayDimensions.size();
}
public StringBuilder getArrayAllocaInfo(int dimension){
StringBuilder arrayInfo= new StringBuilder("i32");
int length = arrayDimensions.size();
for (int i = length-1 ; i >=length-dimension; i--) {
arrayInfo.insert(0,"["+arrayDimensions.get(i)+" x ");
arrayInfo.append("]");
}
return arrayInfo;
}
public Exp loadArrayElementVariable(ArrayList<Exp> indexList){
Exp arrayElementPtr = getArrayElementPtr(indexList,false);
//res.append(" store i32 ").append(exp).append(", i32* ").append(arrayElementPtr);
Exp ret = null;
StringBuilder str = Compiler.res;
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v")
.append(Compiler.varList.regNum)
.append(" = load i32, i32* ")
.append(arrayElementPtr)
.append("\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
return ret;
}
public void allocaVariable(){
StringBuilder str = Compiler.res;
if(blockNum == 0){
str.append(this).append(" = dso_local global i32 ");
}else{
if(arrayDimensions!=null){
str.append(" ").append(this).append(" = alloca ").append(getArrayAllocaInfo(arrayDimensions.size()))
.append("*\n");
}else{
str.append(" ").append(this).append(" = alloca i32\n");
}
}
}
public void storeVariable(Exp initVal){
StringBuilder str = Compiler.res;
if(arrayDimensions!=null){
StringBuilder arrayAllocaInfo = getArrayAllocaInfo(arrayDimensions.size());
str.append(" ")
.append("store ").append(arrayAllocaInfo).append("* ").append(initVal)
.append(", ").append(arrayAllocaInfo).append("* * ").append(this).append("\n");
}else{
str.append(" ")
.append("store i32 ")
.append(initVal)
.append(", i32* ")
.append(this)
.append("\n");
this.isDefined = true;
}
}
public Exp loadVariable(){
Exp ret = null;
StringBuilder str = Compiler.res;
if(isDefined){
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v")
.append(Compiler.varList.regNum)
.append(" = load i32, i32* ")
.append(this)
.append("\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
}else System.exit(1);
return ret;
}
@Override
public String toString() {
return ((blockNum == 0) ? "@" : ("%b"+blockNum))+"v"+regIndex;
}
}