GenASM: Get number of local variables from the "localVariables" attribute of FunctionDefinition.
This commit is contained in:
@@ -64,7 +64,6 @@ public class GenASM implements Visitor<Void> {
|
|||||||
private String mainName;
|
private String mainName;
|
||||||
Map<String, Integer> env = new HashMap<>();
|
Map<String, Integer> env = new HashMap<>();
|
||||||
Map<String, StructDefinition> structs;
|
Map<String, StructDefinition> structs;
|
||||||
Set<String> vars;
|
|
||||||
String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
|
String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
|
||||||
String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" };
|
String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" };
|
||||||
private int lCount = 0; // Invariant: lCount is used
|
private int lCount = 0; // Invariant: lCount is used
|
||||||
@@ -545,11 +544,6 @@ public class GenASM implements Visitor<Void> {
|
|||||||
asm.mov("q", "%rsp", "%rbp");
|
asm.mov("q", "%rsp", "%rbp");
|
||||||
asm.label(lblStart);
|
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
|
// Create a new environment
|
||||||
env = new HashMap<>();
|
env = new HashMap<>();
|
||||||
|
|
||||||
@@ -606,19 +600,19 @@ public class GenASM implements Visitor<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reserve memory on the stack for the local variables.
|
// 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.
|
// 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)
|
// Save the offsets (they are relative to rbp)
|
||||||
for (String lok_var : vars) {
|
for (var localVariable : e.localVariables) {
|
||||||
offset -= 8;
|
offset -= 8;
|
||||||
this.env.put(lok_var, offset);
|
env.put(localVariable.name, offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the stack alignment and correct if necessary
|
// 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
|
// 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
|
// we need to add 8 bytes to the stack to make it aligned
|
||||||
// if there is an odd number of parameters and local variables.
|
// if there is an odd number of parameters and local variables.
|
||||||
@@ -644,7 +638,7 @@ public class GenASM implements Visitor<Void> {
|
|||||||
asm.push("q", "%rax");
|
asm.push("q", "%rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
// push args into local var locations, last arg is ontop of the stack
|
// push args into local var locations, last arg is on top of the stack
|
||||||
for (int i = e.arguments.length - 1; i >= 0; i--) {
|
for (int i = e.arguments.length - 1; i >= 0; i--) {
|
||||||
asm.pop("q", this.env.get(this.currentFunctionParams[i].name) + "(%rbp)");
|
asm.pop("q", this.env.get(this.currentFunctionParams[i].name) + "(%rbp)");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user