From ee70c08285e3cd104932e8c318e2f4eb4a87f96d Mon Sep 17 00:00:00 2001 From: MESYETI Date: Mon, 2 Sep 2024 10:50:43 +0100 Subject: [PATCH] fix exceptions on arm64 (thanks soxfox42) --- examples/exceptions.cal | 4 +--- source/backends/arm64.d | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/examples/exceptions.cal b/examples/exceptions.cal index c5213fd..5266eef 100644 --- a/examples/exceptions.cal +++ b/examples/exceptions.cal @@ -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 diff --git a/source/backends/arm64.d b/source/backends/arm64.d index 7a36004..6fd6e0f 100644 --- a/source/backends/arm64.d +++ b/source/backends/arm64.d @@ -97,7 +97,7 @@ class BackendARM64 : CompilerBackend { version (linux) { defaultOS = "linux"; } - version (OSX) { + else version (OSX) { defaultOS = "osx"; } else { @@ -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")); @@ -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"; } @@ -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"; } @@ -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); @@ -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);