-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodeGeneration.java
More file actions
140 lines (119 loc) · 4.8 KB
/
Copy pathCodeGeneration.java
File metadata and controls
140 lines (119 loc) · 4.8 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
import java.util.*;
import java.io.*;
class CodeGeneration {
static Vector<String> threeadd = new Vector<>();
static char tempVar='z';
static int regcounter=0;
static Map<String,Integer> reg = new HashMap<>();
static Vector<String> machinecode = new Vector<>();
public static void main(String []args) {
String input;int i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter expression: ");
input = sc.nextLine();
generateThreeAddressCode(input);
System.out.println();
System.out.println("Three Address Code: ");
for(i=0;i<threeadd.size();i++) System.out.println(threeadd.get(i));
generateMachineCode();
System.out.println();
System.out.println("Machine Code: ");
for(i=0;i<machinecode.size();i++) System.out.println(machinecode.get(i));
}
public static void generateThreeAddressCode(String s) {
String after="";
if(s.contains("=") && s.length()==3) {
threeadd.add(s);
return;
}
else if(s=="") return;
if(s.contains("(")) {
int start = s.indexOf("(");
int end = s.indexOf(")");
String temp = tempVar+"= "+s.substring(start+1,end);
after = s.substring(0,start)+tempVar+s.substring(end+1);
tempVar--;
threeadd.add(temp);
}
else if(s.contains("/")) {
int c = s.indexOf("/");
String temp = tempVar+"= "+s.substring(c-1,c)+"/"+s.substring(c+1,c+2);
after = s.substring(0,c-1)+tempVar+s.substring(c+2);
tempVar--;
threeadd.add(temp);
}
else if(s.contains("*")) {
int c = s.indexOf("*");
String temp = tempVar+"= "+s.substring(c-1,c)+"*"+s.substring(c+1,c+2);
after = s.substring(0,c-1)+tempVar+s.substring(c+2);
tempVar--;
threeadd.add(temp);
}
else if(s.contains("+")) {
int c = s.indexOf("+");
String temp = tempVar+"= "+s.substring(c-1,c)+"+"+s.substring(c+1,c+2);
after = s.substring(0,c-1)+tempVar+s.substring(c+2);
tempVar--;
threeadd.add(temp);
}
else if(s.contains("-")) {
int c = s.indexOf("-");
String temp = tempVar+"= "+s.substring(c-1,c)+"-"+s.substring(c+1,c+2);
after = s.substring(0,c-1)+tempVar+s.substring(c+2);
tempVar--;
threeadd.add(temp);
}
//System.out.println(after);
generateThreeAddressCode(after);
}
public static void generateMachineCode() {
int i,p=0,f=0;
for(i=0;i<threeadd.size();i++) {
String s = threeadd.get(i);
String temp="";
if(s.contains("+")) p = s.indexOf("+");
else if(s.contains("-")) p = s.indexOf("-");
else if(s.contains("*")) p = s.indexOf("*");
else if(s.contains("/")) p = s.indexOf("/");
else if(s.contains("=")) {
p = s.indexOf("=");
temp = "MOV "+reg.get(s.substring(p+1))+","+s.substring(0,1);
machinecode.add(temp);
temp="";
f=1;
continue;
}
if(reg.containsKey(s.substring(p-1,p)) == false) {
reg.put(s.substring(p-1,p),regcounter);
temp = "MOV "+s.substring(p-1,p)+","+regcounter;
machinecode.add(temp);
regcounter++;
}
//System.out.println(temp);
temp="";
if(reg.containsKey(s.substring(p+1,p+2)) == false) {
reg.put(s.substring(p+1,p+2),regcounter);
temp = "MOV "+s.substring(p+1,p+2)+","+regcounter;
machinecode.add(temp);
regcounter++;
}
//System.out.println(temp);
temp="";
if(s.contains("+")) temp = "ADD ";
else if(s.contains("-")) temp = "SUB ";
else if(s.contains("*")) temp = "MUL ";
else if(s.contains("/")) temp = "DIV ";
temp += reg.get(s.substring(p-1,p))+","+reg.get(s.substring(p+1,p+2));
int r= reg.get(s.substring(p-1,p));
reg.remove(s.substring(p-1,p));
reg.put(s.substring(0,1),r);
machinecode.add(temp);
//System.out.println(temp);
}
if(f==0) {
String temp="",s=threeadd.get(i-1);
temp = "MOV "+reg.get(s.substring(0,1))+","+s.substring(0,1);
machinecode.add(temp);
}
}
}