-
Notifications
You must be signed in to change notification settings - Fork 1
/
GestionDeMemoria.java
56 lines (44 loc) · 1.48 KB
/
GestionDeMemoria.java
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
package generacionDeCodigo;
import ast.DefEstructura;
import ast.DefFuncion;
import ast.DefVariable;
import visitor.DefaultVisitor;
/**
* Clase encargada de asignar direcciones a las variables
*
* @author José Antonio García García
*/
public class GestionDeMemoria extends DefaultVisitor {
private int contadorDireccionesGlobales = 0;
private int contadorDireccionesLocales = 0;
public Object visit(DefVariable node, Object param) {
super.visit(node, param);
if (node.getAmbito().equals("Global")) {
node.setDireccion(contadorDireccionesGlobales);
contadorDireccionesGlobales += node.getTipo().getSize();
} else if (node.getAmbito().equals("Local")) {
contadorDireccionesLocales += -node.getTipo().getSize();
node.setDireccion(contadorDireccionesLocales);
}
return null;
}
public Object visit(DefFuncion node, Object param) {
super.visit(node, param);
contadorDireccionesLocales = 0;
int direccionesParam = 4;
for (int i = node.getParametros().size() - 1; i >= 0; i--) {
node.getParametros().get(i).setDireccion(direccionesParam);
direccionesParam += node.getParametros().get(i).getTipo().getSize();
}
return null;
}
public Object visit(DefEstructura node, Object param) {
super.visit(node, param);
int sumaDireccionesCampos = 0;
for (int i = 0; i < node.getCampos().size(); i++) {
node.getCampos().get(i).setDireccion(sumaDireccionesCampos);
sumaDireccionesCampos += node.getCampos().get(i).getTipo().getSize();
}
return null;
}
}