-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp.java
More file actions
159 lines (141 loc) · 5.86 KB
/
Exp.java
File metadata and controls
159 lines (141 loc) · 5.86 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
import java.util.ArrayList;
public class Exp {
public static final int Add = 1 ,Sub = 2, Mult = 3,
Div = 4,Mod = 5,Or = 6,
And = 7,Eq = 8, Neq = 9,
S = 10,L = 11,Se = 12,
Le =13;
public int value;
public boolean isConstValue;
public int blockNum;
public int regIndex;
public int type;
public ArrayList<Integer> arrayDimensions;
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 int getArraySize(){
return arrayDimensions.size()+1;
}
private static void ExpZext(StringBuilder str) {
Compiler.varList.regNum++;
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum)
.append(" = ")
.append("zext i1 ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum-1)
.append(" to i32\n");
}
public static Exp ExpCompute(Exp a,Exp b,int operator){
if(a == null || b == null){
System.exit(1);
}
Exp ret;
StringBuilder str = Compiler.res;
if(a.isConstValue && b.isConstValue){
switch (operator){
case Add -> ret = new Exp(a.value + b.value);
case Sub -> ret = new Exp(a.value - b.value);
case Mult -> ret = new Exp(a.value * b.value);
case Div -> ret = new Exp(a.value / b.value);
case Mod -> ret = new Exp(a.value % b.value);
case Eq -> ret = new Exp((a.value == b.value)?1:0);
case Neq -> ret = new Exp((a.value != b.value)?1:0);
case S -> ret = new Exp((a.value < b.value)?1:0);
case L -> ret = new Exp((a.value > b.value)?1:0);
case Se -> ret = new Exp((a.value <= b.value)?1:0);
case Le -> ret = new Exp((a.value >= b.value)?1:0);
default -> throw new IllegalStateException("Unexpected value: " + operator);
}
}else{
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v").append(Compiler.varList.regNum)
.append(" = ");
switch (operator){
case Add -> str.append("add i32 ").append(a).append(", ").append(b).append("\n");
case Sub -> str.append("sub i32 ").append(a).append(", ").append(b).append("\n");
case Mult -> str.append("mul i32 ").append(a).append(", ").append(b).append("\n");
case Div -> str.append("sdiv i32 ").append(a).append(", ").append(b).append("\n");
case Mod -> str.append("srem i32 ").append(a).append(", ").append(b).append("\n");
case Eq -> {
str.append("icmp eq i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
case Neq -> {
str.append("icmp ne i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
case S -> {
str.append("icmp slt i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
case L -> {
str.append("icmp sgt i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
case Se -> {
str.append("icmp sle i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
case Le -> {
str.append("icmp sge i32 ").append(a).append(", ").append(b).append("\n");
ExpZext(str);
}
default -> throw new IllegalStateException("Unexpected value: " + operator);
}
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
}
return ret;
}
public static Exp ExpTransToBool(Exp a){
if(a==null) System.exit(1);
Exp ret;
StringBuilder str = Compiler.res;
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v")
.append(Compiler.varList.regNum)
.append(" = ")
.append("icmp ne i32 ")
.append(a)
.append(", 0\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
return ret;
}
public Exp(int value) {
this.value = value;
this.isConstValue = true;
this.type = Symbol.TypeInt;
}
public Exp(int blockNum,int regIndex){
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConstValue = false;
this.type = Symbol.TypeInt;
}
public Exp(int blockNum,int regIndex,int type,ArrayList<Integer> arrayDimensions){
this.blockNum = blockNum;
this.regIndex = regIndex;
this.isConstValue = false;
this.type = type;
this.arrayDimensions = new ArrayList<>();
this.arrayDimensions.addAll(arrayDimensions);
}
@Override
public String toString() {
if(isConstValue){
return String.valueOf(value);
}else{
return ((blockNum == 0) ? "@" : "%")+"b"+blockNum+"v"+regIndex;
}
}
}