Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ project('cimba', ['c'],
]
)

# We also need the portable Netwide Assembler for assembly code
add_languages('nasm', required : true, native : true)
# We also need the portable Netwide Assembler for the x86-64 assembly code
if host_machine.cpu_family() == 'x86_64'
add_languages('nasm', required : true, native : true)
endif

# Try to find CUDA, but don't fail if it's missing
add_languages('cuda', required : false, native : true)
Expand All @@ -33,11 +35,18 @@ cc = meson.get_compiler('c')
# Flags common to all build types
common_flags = [
'-Wno-pedantic',
'-D_POSIX_C_SOURCE=200809L',
'-fno-semantic-interposition',
'-ffp-contract=off',
]

if host_machine.system() == 'darwin'
# Keep Darwin extensions such as pthread_get_stackaddr_np() and
# arc4random_buf() visible
common_flags += '-D_DARWIN_C_SOURCE'
else
common_flags += '-D_POSIX_C_SOURCE=200809L'
endif

# Flags per build type
# 'debug' : full asserts and logging, no optimization
# 'release' : standard optimized build, release asserts only
Expand Down
8 changes: 8 additions & 0 deletions src/cimba.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <inttypes.h>
#include <pthread.h>
#include <setjmp.h>
#include <stdint.h>

#include "cimba.h"
Expand Down Expand Up @@ -75,9 +76,16 @@ static uint64_t leaking_trials;
static CMB_THREAD_LOCAL cimba_trial_cleanup_func *trial_cleanup_func = NULL;
static CMB_THREAD_LOCAL void *trial_cleanup_arg = NULL;

#if CMI_ARCH == CMI_ARM64
/* Clang does not implement __builtin_setjmp on AArch64, use the libc version */
static CMB_THREAD_LOCAL jmp_buf recovery_buf;
#define recovery_set() setjmp(recovery_buf)
#define recovery_jump() longjmp(recovery_buf, 1)
#else
static CMB_THREAD_LOCAL intptr_t recovery_buf[5];
#define recovery_set() __builtin_setjmp((void **)recovery_buf)
#define recovery_jump() __builtin_longjmp((void **)recovery_buf, 1)
#endif

extern void cmi_coroutine_recovery_prepare(void *stack_marker);
extern void cmi_coroutine_recovery_finalize(void *stack_marker);
Expand Down
15 changes: 11 additions & 4 deletions src/cmi_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,41 @@

/*
* Supported toolchains:
* - Architecture: x86-64 / AMD64
* - Operating system: Linux, or Windows via MinGW-w64
* - Architecture: x86-64 / AMD64, or arm64 (Apple Silicon)
* - Operating system: Linux, Windows via MinGW-w64, or macOS
* - Compiler: GCC or Clang, C11 or later
* (the library itself is built as C23)
*
* In particular, MSVC is not supported.
*/

/*
* Identify the processor architecture. So far, only AMD64/x86-64 is supported.
* Identify the processor architecture. AMD64/x86-64 everywhere, and arm64 on
* macOS (Apple Silicon).
*/
#define CMI_AMD64 1
#define CMI_ARM64 2
#if (defined (__amd64__) || defined (__amd64) || defined (__x86_64__) || \
defined (__x86_64) || defined (_M_X64) || defined (_M_AMD64))
#define CMI_ARCH CMI_AMD64
#elif defined (__aarch64__) || defined (__arm64__)
#define CMI_ARCH CMI_ARM64
#else
#error "Platform architecture not yet supported."
#endif

/*
* Identify the operating system. So far, only Linux and Windows are supported.
* Identify the operating system. Linux, Windows, and macOS are supported.
*/
#define CMI_LINUX 1
#define CMI_WINDOWS 2
#define CMI_MACOS 3
#if defined (__linux__) || defined (__linux) || defined (linux)
#define CMI_OS CMI_LINUX
#elif defined (_WIN64) || defined (_WIN32) || defined (__WIN32__)
#define CMI_OS CMI_WINDOWS
#elif defined (__APPLE__) && defined (__MACH__)
#define CMI_OS CMI_MACOS
#else
#error "Platform operating system not yet supported."
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ if host_machine.system() == 'windows'
elif lto_feature.enabled() or (lto_feature.auto() and get_option('buildtype').startswith('release'))
cimba_lto = ['b_lto=true']
if get_option('default_library') != 'shared'
cimba_lto_args = ['-ffat-lto-objects']
# Apple Clang does not accept -ffat-lto-objects
cimba_lto_args = cc.get_supported_arguments('-ffat-lto-objects')
endif
endif

Expand Down
19 changes: 19 additions & 0 deletions src/port/aarch64/macos/cmb_random_hwseed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Random seed generation for macOS.
*
* Copyright (c) FBarrca 2026.
* Licensed under the Apache License, Version 2.0.
*/

#include <stdlib.h>

#include "cmb_assert.h"
#include "cmb_random.h"

uint64_t cmb_random_hwseed(void)
{
uint64_t seed = 0u;
arc4random_buf(&seed, sizeof(seed));
cmb_assert_debug(seed != 0u);
return seed;
}
63 changes: 63 additions & 0 deletions src/port/aarch64/macos/cmi_coroutine_context.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Context switch and coroutine trampoline for arm64 macOS.
*
* Copyright (c) FBarrca 2026.
* Licensed under the Apache License, Version 2.0.
*/

.text
.p2align 2

.globl _cmi_coroutine_context_switch
_cmi_coroutine_context_switch:
sub sp, sp, #176
stp x19, x20, [sp, #0]
stp x21, x22, [sp, #16]
stp x23, x24, [sp, #32]
stp x25, x26, [sp, #48]
stp x27, x28, [sp, #64]
stp x29, x30, [sp, #80]
stp d8, d9, [sp, #96]
stp d10, d11, [sp, #112]
stp d12, d13, [sp, #128]
stp d14, d15, [sp, #144]
mrs x9, fpcr
mrs x10, fpsr
stp x9, x10, [sp, #160]

mov x9, sp
str x9, [x0]
ldr x9, [x1]
mov sp, x9

ldp x9, x10, [sp, #160]
msr fpcr, x9
msr fpsr, x10
ldp d14, d15, [sp, #144]
ldp d12, d13, [sp, #128]
ldp d10, d11, [sp, #112]
ldp d8, d9, [sp, #96]
ldp x29, x30, [sp, #80]
ldp x27, x28, [sp, #64]
ldp x25, x26, [sp, #48]
ldp x23, x24, [sp, #32]
ldp x21, x22, [sp, #16]
ldp x19, x20, [sp, #0]
add sp, sp, #176

mov x0, x2
ret

.p2align 2
.globl _cmi_coroutine_trampoline
_cmi_coroutine_trampoline:
stp x29, x30, [sp, #-16]!
mov x29, sp
mov x0, x20
mov x1, x21
blr x19
mov x16, x22
ldp x29, x30, [sp], #16
br x16

.subsections_via_symbols
Loading