-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGen.java
More file actions
116 lines (83 loc) · 2.58 KB
/
CodeGen.java
File metadata and controls
116 lines (83 loc) · 2.58 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
import java.util.ArrayList;
import java.util.Map;
import semantic.*;
public class CodeGen {
// private static ArrayList<Simbolo> registradores = new ArrayList<Simbolo>();
private static ArrayList<Symbol> funcoes = new ArrayList<Symbol>();
public static String getNextLabel(Symbol s) {
funcoes.add(s);
return "L" + funcoes.indexOf(s);
}
public static String Go() {
String result = "";
/*
for (Simbolo s : TabelaSimbolos.getInstance().getSimbolos()) {
result += getNextLabel(s) + "(" + s.getLexema() +"):\n" + GenContext(s);
if (s.getLexema().equalsIgnoreCase("main")){
result = result + "halt;\n";
}
else {
result = result + "BR *(0)SP;\n";
}
}
return result;
}*/
Map<String,Symbol> symbols = SymbolTable.getInstance().getSymbols();
for (String lexeme : symbols.keySet()) {
Symbol s = symbols.get(lexeme);
//result += getNextLabel(s) + "(" + s.getLexeme() +"):\n" + genContext(s);
if (s.getLexeme().equalsIgnoreCase("main")){
result = result + "halt;\n";
}
else {
result = result + "BR *(0)SP;\n";
}
}
return result;
}
/*public static String genContext(Symbol s) {
String result = "";
ArrayList<Symbol> registradores = new ArrayList<Symbol>();
Function funcao = (Function) s;
for (Symbol a : funcao.getSymbol()) {
if (a instanceof Variavel) {
result += GenAttrInitializer(a, registradores);
} else if (a instanceof CallFunction) {
result += GenCallFun(a, registradores);
} else if (a instanceof Attr) {
result += GenAttr(a, registradores);
}
}
return result;
}
*/
private static String GenAttr(Symbol s, ArrayList<Symbol> registradores) {
String result = "";
Variable v = (Variable) s;
result += "ST " + v.getLexeme() + ", #" + v.getValue() + "\n";
return result;
}
private static String getLabel(Symbol s) {
for (int i = 0; i < funcoes.size(); i++) {
if (funcoes.get(i).getLexeme().equals(s.getLexeme())) {
return "L" + i;
}
}
return null;
}
private static String GenCallFun(Symbol s, ArrayList<Symbol> registradores) {
registradores.add(s);
String result = "";
result += "ADD SP, SP, #" + s.getLexeme() + "size;\n";
result += "ST *SP, #here+16" + ";\n";
result += "BR " + getLabel(s) + ";\n";
result += "SUB SP, SP, #" + s.getLexeme() + "size;\n";
return result;
}
public static String GenAttrInitializer(Symbol s, ArrayList<Symbol> registradores) {
registradores.add(s);
String result = "";
result += "ST R" + registradores.indexOf(s) + ", " + s.getLexeme() + ";\n";
return result;
}
}