Skip to content

Commit

Permalink
fix exceptions on arm64 (thanks soxfox42)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeti0904 committed Sep 2, 2024
1 parent fdc431a commit ee70c08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
4 changes: 1 addition & 3 deletions examples/exceptions.cal
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ include "cores/select.cal"
include "std/io.cal"

func __arm64_exception begin
&_cal_exception Exception.msg + Array.length + @ printdec new_line
&_cal_exception Exception.msg + Array.memberSize + @ printdec new_line
&_cal_exception Exception.msg + Array.elements + @ printdec new_line
&_cal_exception Exception.msg + "Unhandled exception: %s" printf new_line
1 exit
end

Expand Down
25 changes: 11 additions & 14 deletions source/backends/arm64.d
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BackendARM64 : CompilerBackend {
version (linux) {
defaultOS = "linux";
}
version (OSX) {
else version (OSX) {
defaultOS = "osx";
}
else {
Expand Down Expand Up @@ -501,7 +501,7 @@ class BackendARM64 : CompilerBackend {
}

if (crash) {
output ~= format("ldr x9, =__global_%s\n", Sanitise("_cal_exception"));
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x9, [x9]\n";
output ~= "cmp x9, #0\n";
output ~= format("bne __func__%s\n", Sanitise("__arm64_exception"));
Expand Down Expand Up @@ -579,7 +579,7 @@ class BackendARM64 : CompilerBackend {

if (node.inline) {
if (node.errors) {
output ~= format("ldr x9, =__global_%s\n", Sanitise("_cal_exception"));
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x10, #0\n";
output ~= "str x10, [x9]\n";
}
Expand Down Expand Up @@ -607,7 +607,7 @@ class BackendARM64 : CompilerBackend {
output ~= "str lr, [x20, #-8]!\n";

if (node.errors) {
output ~= format("ldr x9, =__global_%s\n", Sanitise("_cal_exception"));
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x10, #0\n";
output ~= "str x10, [x9]\n";
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ class BackendARM64 : CompilerBackend {

++ blockCounter;

output ~= format("ldr x9, =__global_%s\n", Sanitise("_cal_exception"));
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x9, [x9]\n";
output ~= "cmp x9, #0\n";
output ~= format("beq __catch_%d_end\n", blockCounter);
Expand Down Expand Up @@ -1340,22 +1340,19 @@ class BackendARM64 : CompilerBackend {
}

// set exception error
output ~= format("ldr x9, =__global_%s\n", Sanitise("_cal_exception"));
output ~= "mov x10, 0xFFFFFFFFFFFFFFFF\n";
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "mov x10, #-1\n";
output ~= "str x10, [x9]\n";

// copy exception message
output ~= "sub x19, x19, #8\n";
output ~= "mov x10, x19\n";
output ~= "ldr x10, [x19, #-8]!\n";
output ~= "add x11, x9, #8\n";
output ~= "mov x12, #3\n";
// copy x10 to x11, x12 times
output ~= "1:\n";
output ~= "ldr x13, [x10]\n";
output ~= "str x13, [x11]\n";
output ~= "add x10, x10, #8\n";
output ~= "add x11, x11, #8\n";
output ~= "sub x12, x12, #1\n";
output ~= "ldr x13, [x10], #8\n";
output ~= "str x13, [x11], #8\n";
output ~= "subs x12, x12, #1\n";
output ~= "bne 1b\n";

CompileReturn(node);
Expand Down

0 comments on commit ee70c08

Please sign in to comment.