-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptim_Ocode.java
More file actions
96 lines (88 loc) · 2.87 KB
/
Optim_Ocode.java
File metadata and controls
96 lines (88 loc) · 2.87 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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Optim_Ocode {
private ArrayList<String> oldcodes;
private ArrayList<String> optim;
public Optim_Ocode(ArrayList<String> oldcodes) {
this.oldcodes = oldcodes;
this.optim = new ArrayList<>();
Optim();
}
public void print_target() throws IOException {
BufferedWriter bout = new BufferedWriter(new FileWriter("mips.txt"));
for (String i : oldcodes) {
bout.write(i);
bout.newLine();
}
bout.close();
}
private void Optim() {
FixLi_Move();
}
private void FixLi_Move() {
for(int i=0;i<oldcodes.size()-1;i++) {
String listr = oldcodes.get(i);
String movstr = oldcodes.get(i+1);
if (listr.length() > 2 && movstr.length() > 4) {
if((listr.substring(0,2)).equals("li")
&& (movstr.substring(0,4)).equals("move")) {
String lireg = getlireg(listr);
String movreg = getfinal(movstr);
if (lireg.equals(movreg)) {
//System.out.println("ok");
String numstr = getfinal(listr);
String newstr = gen_Li_Move(movstr,numstr);
optim.add(newstr);
i++;
} else {
optim.add(listr);
}
} else {
optim.add(listr);
}
} else {
optim.add(listr);
}
}
optim.add(oldcodes.get(oldcodes.size()-1));
this.oldcodes = this.optim;
this.optim = new ArrayList<>();
}
private String getlireg(String listr) {
int idx = 0;
StringBuilder ans = new StringBuilder();
while (listr.charAt(idx)!='$') {
idx++;
}
while (listr.charAt(idx)!=',') {
ans.append(listr.charAt(idx));
idx++;
}
//System.out.println(ans.toString());
return ans.toString();
}
private String getfinal(String movstr) {
int idx=0;
while (movstr.charAt(idx)!=',') {
idx++;
}
//System.out.println(movstr.substring(idx+1));
return movstr.substring(idx+1);
}
private String gen_Li_Move(String movstr,String numstr) {
int idx=0;
String ans = "li";
while (movstr.charAt(idx)!=' ') {
idx++;
}
int start = idx;
while (movstr.charAt(idx)!=',') {
idx++;
}
ans = ans + movstr.substring(start,idx)+","+numstr;
//System.out.println(ans);
return ans;
}
}