GenASM: Get number of local variables from the "localVariables" attribute of FunctionDefinition.

This commit is contained in:
2023-03-22 23:40:02 +01:00
parent c5c01041e4
commit 06609ae899

View File

@@ -64,7 +64,6 @@ public class GenASM implements Visitor<Void> {
private String mainName;
Map<String, Integer> env = new HashMap<>();
Map<String, StructDefinition> structs;
Set<String> vars;
String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" };
private int lCount = 0; // Invariant: lCount is used
@@ -545,11 +544,6 @@ public class GenASM implements Visitor<Void> {
asm.mov("q", "%rsp", "%rbp");
asm.label(lblStart);
// Get the number of local variables.
// TODO: Do this during context analysis and save the result as an attribute of FunctionDefinition.
vars = new TreeSet<>();
new GetVars(vars, new HashMap<>()).visit(e);
// Create a new environment
env = new HashMap<>();
@@ -606,19 +600,19 @@ public class GenASM implements Visitor<Void> {
}
// Reserve memory on the stack for the local variables.
if (!vars.isEmpty()) {
if (e.localVariables.length > 0) {
// Each variable is at most 8 bytes in size.
asm.sub("q", "$" + (8 * vars.size()), "%rsp");
asm.sub("q", "$" + (8 * e.localVariables.length), "%rsp");
// Save the offsets (they are relative to rbp)
for (String lok_var : vars) {
for (var localVariable : e.localVariables) {
offset -= 8;
this.env.put(lok_var, offset);
env.put(localVariable.name, offset);
}
}
// Check the stack alignment and correct if necessary
if ((registerParameters.size() + vars.size()) % 2 != 0) {
if ((registerParameters.size() + e.localVariables.length) % 2 != 0) {
// Since each variable is 8 bytes and the stack is 16 byte aligned
// we need to add 8 bytes to the stack to make it aligned
// if there is an odd number of parameters and local variables.