Skip to content

Commit

Permalink
optimise freeing the stack frame
Browse files Browse the repository at this point in the history
  • Loading branch information
yeti0904 committed Jun 8, 2024
1 parent ce74b8c commit 3aa906d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
15 changes: 13 additions & 2 deletions source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ class BackendLinux86 : CompilerBackend {
foreach (ref var ; variables) {
scopeSize += var.Size();
}
output ~= format("add rsp, %d\n", scopeSize);
if (scopeSize == 1) {
output ~= "inc rsp\n";
}
else if (scopeSize > 0) {
output ~= format("add rsp, %d\n", scopeSize);
}

output ~= "ret\n";
//output ~= format("__func_end__%s:\n", node.name.Sanitise());
Expand Down Expand Up @@ -652,7 +657,13 @@ class BackendLinux86 : CompilerBackend {
foreach (ref var ; variables) {
scopeSize += var.Size();
}
output ~= format("add rsp, %d\n", scopeSize);
if (scopeSize == 1) {
output ~= "inc rsp\n";
}
else if (scopeSize > 0) {
output ~= format("add rsp, %d\n", scopeSize);
}

output ~= "ret\n";
}

Expand Down
15 changes: 12 additions & 3 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ class BackendRM86 : CompilerBackend {
foreach (ref var ; variables) {
scopeSize += var.Size();
}
output ~= format("add sp, %d\n", scopeSize);
if (scopeSize == 1) {
output ~= "inc sp\n";
}
else {
output ~= format("add sp, %d\n", scopeSize);
}

output ~= "ret\n";
//output ~= format("__func_end__%s:\n", node.name.Sanitise());
Expand Down Expand Up @@ -565,8 +570,12 @@ class BackendRM86 : CompilerBackend {
foreach (ref var ; variables) {
scopeSize += var.Size();
}
output ~= format("add sp, %d\n", scopeSize);
output ~= "ret\n";
if (scopeSize == 1) {
output ~= "inc sp\n";
}
else {
output ~= format("add sp, %d\n", scopeSize);
}
}

override void CompileConst(ConstNode node) {
Expand Down
14 changes: 13 additions & 1 deletion source/backends/uxn.d
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,19 @@ class BackendUXN : CompilerBackend {
foreach (ref var ; variables) {
scopeSize += var.Size();
}
output ~= format(".vsp LDZ2 #%.4x ADD2 .vsp STZ2\n", scopeSize);
//output ~= format(".vsp LDZ2 #%.4x ADD2 .vsp STZ2\n", scopeSize);
if (scopeSize > 0) {
output ~= ".vsp LDZ2 ";

switch (scopeSize) {
case 1: output ~= "INC2 "; break;
case 2: output ~= "INC2 INC2 "; break;
case 3: output ~= "INC2 INC2 INC2 "; break;
default: output ~= format("#%.4x ADD2 ", scopeSize); break;
}

output ~= ".vsp STZ2\n";
}

output ~= "JMP2r\n";
variables = [];
Expand Down

0 comments on commit 3aa906d

Please sign in to comment.