-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.java
More file actions
102 lines (90 loc) · 3.54 KB
/
Function.java
File metadata and controls
102 lines (90 loc) · 3.54 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
import java.util.ArrayList;
public class Function extends Symbol{
ArrayList<Variable> argList = new ArrayList<>();
public Function(String name, int FuncType) {
super(name, FuncType);
}
public void addArg(Variable variable){
argList.add(variable);
}
public Variable getArg(int index){
return argList.get(index);
}
public int getArgSize(){
return argList.size();
}
public boolean checkArgList(ArrayList<Exp> list){
if(list == null && argList.isEmpty())
return true;
if(list != null && list.size() == argList.size()){
for (int i = 0; i < list.size(); i++) {
Exp exp = list.get(i);
Variable param = argList.get(i);
if(exp.type != param.BType) {
Compiler.res.append("\n类型不同\n");
return false;
}
else if(exp.type == Symbol.TypePointer){
if(exp.getArraySize()!=param.getArraySize()) {
Compiler.res.append("\n"+exp.getArraySize()).append(param.getArraySize()).append("数组维数不同\n");
return false;
}
else{
for (int j = 0; j < exp.arrayDimensions.size(); j++) {
if(exp.arrayDimensions.get(i)!=param.getArrayDimension(i)) {
Compiler.res.append("\n数组某一维不匹配\n");
return false;
}
}
}
}
}
return true;
}
return false;
}
public Exp callFunction(ArrayList<Exp> list) {
StringBuilder str = Compiler.res;
Exp ret = null;
if(BType == Symbol.TypeInt ){
str.append(" ")
.append((Compiler.varList.blockNum == 0) ? "@" : ("%b"+Compiler.varList.blockNum))
.append("v")
.append(Compiler.varList.regNum)
.append(" = call i32 @")
.append(this.name)
.append("(");
if(list !=null){
for (int i = 0; i < list.size(); i++) {
Exp exp = list.get(i);
if(exp.type == Symbol.TypeInt)
str.append("i32 ").append(list.get(i));
if(exp.type== Symbol.TypePointer)
str.append(exp.getArrayAllocaInfo(exp.arrayDimensions.size())).append("* ").append(exp);
if(i<list.size()-1)
str.append(", ");
}
}
str.append(")\n");
ret = new Exp(Compiler.varList.blockNum,Compiler.varList.regNum++);
}else {
str.append(" ")
.append("call void @")
.append(this.name)
.append("(");
if(list !=null){
for (int i = 0; i < list.size(); i++) {
Exp exp = list.get(i);
if(exp.type == Symbol.TypeInt)
str.append("i32 ").append(list.get(i));
if(exp.type== Symbol.TypePointer)
str.append(exp.getArrayAllocaInfo(exp.arrayDimensions.size())).append("* ").append(exp);
if(i<list.size()-1)
str.append(", ");
}
}
str.append(")\n");
}
return ret;
}
}