Skip to content

Commit

Permalink
properly fixed exceptions on x86_64
Browse files Browse the repository at this point in the history
  • Loading branch information
yeti0904 committed Nov 14, 2024
1 parent a747bd6 commit fd68c0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
16 changes: 8 additions & 8 deletions source/backends/arm64.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ private struct Word {
Node[] inlineNodes;
bool error;
Type[] params;
size_t numReturns;


// for C words
Type ret;
bool isVoid;
Expand Down Expand Up @@ -583,8 +582,7 @@ class BackendARM64 : CompilerBackend {
}

words[node.name] = Word(
WordType.Callisto, true, node.nodes, node.errors, params,
node.returnTypes.length
WordType.Callisto, true, node.nodes, node.errors, params
);
}
else {
Expand All @@ -593,7 +591,7 @@ class BackendARM64 : CompilerBackend {

words[node.name] = Word(
node.raw? WordType.Raw : WordType.Callisto , false, [], node.errors,
params, node.returnTypes.length
params,
);

string symbol =
Expand Down Expand Up @@ -1234,8 +1232,10 @@ class BackendARM64 : CompilerBackend {
Error(node.error, "Non-callisto functions can't throw");
}

if (word.params.length > 0) {
output ~= format("sub x21, x19, #%d\n", word.params.length * 8);
size_t paramSize = word.params.length * 8;

if (paramSize != 0) {
output ~= format("sub x21, x19, #%d\n", paramSize);
output ~= "str x21, [x20, #-8]!\n";
}
else {
Expand All @@ -1251,7 +1251,7 @@ class BackendARM64 : CompilerBackend {
output ~= format("bl __func__%s\n", node.func.Sanitise());
}

output ~= "ldr x21, [x20], #8\n";
output ~= "ldr x19, [x20], #8\n";

++ blockCounter;

Expand Down
31 changes: 17 additions & 14 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ private struct Word {
Node[] inlineNodes;
bool error;
Type[] params;
size_t numReturns;

int StackEffect() => (-(cast(int) params.length) + (cast(int) numReturns));
}

private struct RM86Opts {
Expand Down Expand Up @@ -282,20 +279,26 @@ class BackendRM86 : CompilerBackend {
}
else {
if (word.raw) {
int stackEffect = word.StackEffect() * 8;

if (stackEffect != 0) {
output ~= format("lea ax, [si + %d]\n", stackEffect);
output ~= "push ax\n";
}
else {
output ~= "push si\n";
}

output ~= format("call %s\n", node.name);
}
else {
if (words[thisFunc].error) {
size_t paramSize = word.params.length * 2;

if (paramSize != 0) {
output ~= format("lea ax, [si - %d]\n", paramSize);
output ~= "push ax\n";
}
else {
output ~= "push si\n";
}
}

output ~= format("call __func__%s\n", node.name.Sanitise());

if (words[thisFunc].error) {
output ~= "pop r15\n";
}
}
}

Expand Down Expand Up @@ -1022,7 +1025,7 @@ class BackendRM86 : CompilerBackend {
output ~= format("call __func__%s\n", node.func.Sanitise());
}

output ~= "pop di\n";
output ~= "pop si\n";

output ~= format("mov ax, [__global_%s]\n", Sanitise("_cal_exception"));
output ~= "cmp ax, 0\n";
Expand Down
33 changes: 23 additions & 10 deletions source/backends/x86_64.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ private struct Word {
Node[] inlineNodes;
bool error;
Type[] params;
size_t numReturns;

// for C words
Type ret;
bool isVoid;
string symbolName;

int StackEffect() => (-(cast(int) params.length) + (cast(int) numReturns));
}

class BackendX86_64 : CompilerBackend {
Expand Down Expand Up @@ -525,7 +522,23 @@ class BackendX86_64 : CompilerBackend {
}
}
else {
if (words[thisFunc].error) {
size_t paramSize = word.params.length * 8;

if (paramSize != 0) {
output ~= format("lea r14, [r15 - %d]\n", paramSize);
output ~= "push r14\n";
}
else {
output ~= "push r15\n";
}
}

output ~= format("call __func__%s\n", node.name.Sanitise());

if (words[thisFunc].error) {
output ~= "pop r15\n";
}
}
}

Expand Down Expand Up @@ -622,7 +635,7 @@ class BackendX86_64 : CompilerBackend {
}

words[node.name] = Word(
WordType.Callisto, true, node.nodes, node.errors, params, node.returnTypes.length
WordType.Callisto, true, node.nodes, node.errors, params
);
}
else {
Expand All @@ -631,7 +644,7 @@ class BackendX86_64 : CompilerBackend {

words[node.name] = Word(
node.raw? WordType.Raw : WordType.Callisto , false, [], node.errors,
params, node.returnTypes.length
params
);

string symbol =
Expand Down Expand Up @@ -1290,10 +1303,10 @@ class BackendX86_64 : CompilerBackend {
Error(node.error, "Non-callisto functions can't throw");
}

int stackEffect = word.StackEffect() * 8;
size_t paramSize = word.params.length * 8;

if (stackEffect != 0) {
output ~= format("lea r14, [r15 + %d]\n", stackEffect);
if (paramSize != 0) {
output ~= format("lea r14, [r15 - %d]\n", paramSize);
output ~= "push r14\n";
}
else {
Expand All @@ -1309,7 +1322,7 @@ class BackendX86_64 : CompilerBackend {
output ~= format("call __func__%s\n", node.func.Sanitise());
}

output ~= "pop r15\n";
output ~= "pop r14\n";

++ blockCounter;

Expand All @@ -1318,7 +1331,7 @@ class BackendX86_64 : CompilerBackend {
output ~= format("je __catch_%d_end\n", blockCounter);

// function errored, assume that all it did was consume parameters
output ~= format("lea r15, [r15 - %d]\n", stackEffect);
output ~= "mov r15, r14\n";

// create scope
auto oldVars = variables.dup;
Expand Down

0 comments on commit fd68c0c

Please sign in to comment.