diff --git a/.clang-format b/.clang-format index c1042ad5a..c04e977fc 100644 --- a/.clang-format +++ b/.clang-format @@ -75,7 +75,8 @@ IncludeCategories: Priority: 1 IncludeIsMainRegex: '(Test)?$' IndentCaseLabels: true -IndentPPDirectives: None +IndentPPDirectives: AfterHash +PPIndentWidth: -1 IndentWidth: 4 IndentWrappedFunctionNames: false InsertBraces: true diff --git a/.github/workflows/check-formatting.yaml b/.github/workflows/check-formatting.yaml index 79a67f96b..b1b08b7cd 100644 --- a/.github/workflows/check-formatting.yaml +++ b/.github/workflows/check-formatting.yaml @@ -36,11 +36,8 @@ jobs: - name: "Install run-clang-format" run: | - wget -q -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - - echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-16 main" | sudo tee -a /etc/apt/sources.list - echo "deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-16 main" | sudo tee -a /etc/apt/sources.list sudo apt-get update - sudo apt-get install -y clang-format-16 + sudo apt-get install -y clang-format-20 curl -sSfL https://raw.githubusercontent.com/Sarcasm/run-clang-format/master/run-clang-format.py -o run-clang-format chmod +x run-clang-format diff --git a/C_CODING_STYLE.md b/C_CODING_STYLE.md index e437c9069..642982280 100644 --- a/C_CODING_STYLE.md +++ b/C_CODING_STYLE.md @@ -107,6 +107,38 @@ void f(bool reverse) { } ``` +### Preprocessor Indentation + +Indent nested preprocessor directives after the hash mark [AVMCCS-F022]: +- The hash `#` stays in column 1, directives are indented with 4 spaces after the hash +- Each nesting level adds 4 spaces after the `#` +- **Exception**: Include guards do not count for indentation. The guard directives themselves +(`#ifndef`, `#define`, `#endif`) and all content between them remain at column 1 + +Good: + +```c +#ifdef USE_POSITIVE_NUMBERS +# define A 1 +# define B 2 +#else +# define A -1 +# define B -2 +#endif +``` + +Bad: + +```c +#ifdef USE_POSITIVE_NUMBERS +#define A 1 +#define B 2 +#else +#define A -1 +#define B -2 +#endif +``` + ## Formatting ### General Rules @@ -2428,6 +2460,7 @@ This convention system creates self-documenting code where the naming pattern im |------|-------------|---------| | Braces | K&R variant (new line for functions/types) | `void f(void)\n{` | | Indentation | 4 spaces, no tabs | ` if (x) {` | +| Preprocessor indent | After hash, except include guards | `#ifdef X\n# define Y 1\n#endif` | | Pointer `*` | With variable name | `char *name` not `char* name` | | Line length | < 100 columns | Use intermediate variables | | Hex numbers | Uppercase letters | `0xDEADBEEF` not `0xdeadbeef` | @@ -2739,7 +2772,9 @@ This style guide can be largely enforced using clang-format. Create a `.clang-fo BasedOnStyle: LLVM -# Indentation (rules: AVMCCS-F003) +# Indentation (rules: AVMCCS-F003, AVMCCS-F022) +IndentPPDirectives: AfterHash +PPIndentWidth: -1 IndentWidth: 4 TabWidth: 4 UseTab: Never @@ -3423,6 +3458,7 @@ This section provides a complete index of all rules defined in this style guide, | AVMCCS-F019 | Use uppercase letters for hexadecimal numbers | Spacing | | AVMCCS-F020 | Use spaces around binary operators | Spacing | | AVMCCS-F021 | Always place initializer braces on the same line as declaration | Spacing | +| AVMCCS-F022 | Indent nested preprocessor directives after the hash mark | Style Rules | | **Naming** | | | | AVMCCS-N001 | Preserve word boundaries when converting between casing styles | Word Boundary Preservation | | AVMCCS-N002 | Keep acronyms capitalized in PascalCase; treat as single word when converting | Word Boundary Preservation | @@ -3519,8 +3555,16 @@ This section provides a complete index of all rules defined in this style guide, | AVMCCS-S005 | Group includes by origin with alphabetical ordering | Include Ordering | | AVMCCS-S006 | Header files match module names using underscores | Header File Naming | +## Changes + +### Version 1.1 + +**Preprocessor Indentation** (AVMCCS-F022): +- Nested preprocessor directives now indent after the hash mark (exception: include guards remain +unindented for clarity) + --- -*Document Version*: 1.0 +*Document Version*: 1.1 *Style Guide Name*: AtomVM C Coding Style Guide (AVMCCS Guide) -*Last Updated*: July 2025 +*Last Updated*: November 2025 diff --git a/src/libAtomVM/atom_table.c b/src/libAtomVM/atom_table.c index f9739e932..e89b0fd91 100644 --- a/src/libAtomVM/atom_table.c +++ b/src/libAtomVM/atom_table.c @@ -31,13 +31,13 @@ #include "utils.h" #ifndef AVM_NO_SMP -#define SMP_RDLOCK(htable) smp_rwlock_rdlock(htable->lock) -#define SMP_WRLOCK(htable) smp_rwlock_wrlock(htable->lock) -#define SMP_UNLOCK(htable) smp_rwlock_unlock(htable->lock) +# define SMP_RDLOCK(htable) smp_rwlock_rdlock(htable->lock) +# define SMP_WRLOCK(htable) smp_rwlock_wrlock(htable->lock) +# define SMP_UNLOCK(htable) smp_rwlock_unlock(htable->lock) #else -#define SMP_RDLOCK(htable) UNUSED(htable) -#define SMP_WRLOCK(htable) UNUSED(htable) -#define SMP_UNLOCK(htable) UNUSED(htable) +# define SMP_RDLOCK(htable) UNUSED(htable) +# define SMP_WRLOCK(htable) UNUSED(htable) +# define SMP_UNLOCK(htable) UNUSED(htable) #endif #define DEFAULT_SIZE 8 @@ -427,7 +427,7 @@ static inline int read_encoded_len(const uint8_t **len_bytes) } // -1 is not a valid atom index as we're limited to 2^20 -#define ATOM_TABLE_NOT_FOUND_MARKER ((atom_index_t) -1) +#define ATOM_TABLE_NOT_FOUND_MARKER ((atom_index_t) - 1) enum AtomTableEnsureAtomResult atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, size_t count, atom_index_t *translate_table, enum EnsureAtomsOpt opt) diff --git a/src/libAtomVM/bif.c b/src/libAtomVM/bif.c index 29be2d680..c7eda85a4 100644 --- a/src/libAtomVM/bif.c +++ b/src/libAtomVM/bif.c @@ -72,11 +72,11 @@ * do not use `(avm_float_t) INT64_MIN` or `(avm_float_t) INT64_MAX`. */ #ifdef AVM_USE_SINGLE_PRECISION -#define INT64_MIN_AS_AVM_FLOAT -9223372586610590720.0 // 0xDF000000 = -2^63 -#define INT64_MAX_AS_AVM_FLOAT 9223371761976868863.0 // 0x5F000000 = 2^63 +# define INT64_MIN_AS_AVM_FLOAT -9223372586610590720.0 // 0xDF000000 = -2^63 +# define INT64_MAX_AS_AVM_FLOAT 9223371761976868863.0 // 0x5F000000 = 2^63 #else -#define INT64_MIN_AS_AVM_FLOAT -9223372036854776832.0 // 0xC3E0000000000000 = -2^63 -#define INT64_MAX_AS_AVM_FLOAT 9223372036854775295.0 // 0x43DFFFFFFFFFFFFF = 2^62 * 1.1...1b +# define INT64_MIN_AS_AVM_FLOAT -9223372036854776832.0 // 0xC3E0000000000000 = -2^63 +# define INT64_MAX_AS_AVM_FLOAT 9223372036854775295.0 // 0x43DFFFFFFFFFFFFF = 2^62 * 1.1...1b #endif const struct ExportedFunction *bif_registry_get_handler(const char *mfa) @@ -645,7 +645,7 @@ static term add_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 return add_int64_to_bigint(ctx, fail_label, live, val1, val2); #else -#error "Unsupported configuration." +# error "Unsupported configuration." #endif } @@ -784,7 +784,7 @@ static term sub_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 return sub_int64_to_bigint(ctx, fail_label, live, val1, val2); #else -#error "Unsupported configuration." +# error "Unsupported configuration." #endif } @@ -927,7 +927,7 @@ static term mul_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 return mul_int64_to_bigint(ctx, fail_label, live, val1, val2); #else -#error "Unsupported configuration." +# error "Unsupported configuration." #endif } @@ -1177,7 +1177,7 @@ static term neg_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return int64_max_plus_one(ctx, fail_label, live); #else -#error "Unsupported configuration." +# error "Unsupported configuration." #endif default: @@ -1279,7 +1279,7 @@ static term abs_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return int64_max_plus_one(ctx, fail_label, live); #else -#error "Unsupported configuration." +# error "Unsupported configuration." #endif } else { diff --git a/src/libAtomVM/bitstring.c b/src/libAtomVM/bitstring.c index 0382086fa..e4ae67bcd 100644 --- a/src/libAtomVM/bitstring.c +++ b/src/libAtomVM/bitstring.c @@ -26,7 +26,14 @@ static inline uint64_t from_le64(uint64_t value) { - return ((((value) &0xFF) << 56) | (((value) &0xFF00) << 40) | (((value) &0xFF0000) << 24) | (((value) &0xFF000000) << 8) | (((value) &0xFF00000000) >> 8) | (((value) &0xFF0000000000) >> 24) | (((value) &0xFF000000000000) >> 40) | (((value) &0xFF00000000000000) >> 56)); + return ((((value) & 0xFF) << 56) + | (((value) & 0xFF00) << 40) + | (((value) & 0xFF0000) << 24) + | (((value) & 0xFF000000) << 8) + | (((value) & 0xFF00000000) >> 8) + | (((value) & 0xFF0000000000) >> 24) + | (((value) & 0xFF000000000000) >> 40) + | (((value) & 0xFF00000000000000) >> 56)); } bool bitstring_extract_any_integer(const uint8_t *src, size_t offset, avm_int_t n, diff --git a/src/libAtomVM/context.c b/src/libAtomVM/context.c index f682e8c34..09dd58a48 100644 --- a/src/libAtomVM/context.c +++ b/src/libAtomVM/context.c @@ -38,12 +38,12 @@ #include "utils.h" #ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" +# include "platform_atomic.h" #else -#if defined(HAVE_ATOMIC) -#include -#define ATOMIC_COMPARE_EXCHANGE_WEAK_INT atomic_compare_exchange_weak -#endif +# if defined(HAVE_ATOMIC) +# include +# define ATOMIC_COMPARE_EXCHANGE_WEAK_INT atomic_compare_exchange_weak +# endif #endif #define IMPL_EXECUTE_LOOP @@ -467,15 +467,15 @@ void context_process_code_server_resume_signal(Context *ctx) // jit_trap_and_load stores the label in saved_function_ptr uint32_t label = (uint32_t) (uintptr_t) ctx->saved_function_ptr; Module *module = ctx->saved_module; -#ifndef AVM_NO_EMU +# ifndef AVM_NO_EMU if (module->native_code) { ctx->saved_function_ptr = module_get_native_entry_point(module, label); } else { ctx->saved_ip = module->labels[label]; } -#else +# else ctx->saved_function_ptr = module_get_native_entry_point(module, label); -#endif +# endif // Fix CP to OP_INT_CALL_END if (ctx->cp == module_address(module->module_index, 0)) { ctx->cp = module_address(module->module_index, module->end_instruction_ii); diff --git a/src/libAtomVM/context.h b/src/libAtomVM/context.h index bcdf04c3e..fd4d40ca0 100644 --- a/src/libAtomVM/context.h +++ b/src/libAtomVM/context.h @@ -43,7 +43,7 @@ extern "C" { struct Module; #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif @@ -160,7 +160,7 @@ struct Context }; #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif diff --git a/src/libAtomVM/debug.h b/src/libAtomVM/debug.h index 55b52d196..bbe6a9df3 100644 --- a/src/libAtomVM/debug.h +++ b/src/libAtomVM/debug.h @@ -95,9 +95,9 @@ char reg_type_c(int reg_type); void debug_print_processes_list(struct ListHead *processes); #ifdef ENABLE_STACK_TRACE -#define DEBUG_DUMP_STACK debug_dump_stack +# define DEBUG_DUMP_STACK debug_dump_stack #else -#define DEBUG_DUMP_STACK(...) +# define DEBUG_DUMP_STACK(...) #endif #ifdef __cplusplus diff --git a/src/libAtomVM/ets.c b/src/libAtomVM/ets.c index 36f51e83b..2649912a8 100644 --- a/src/libAtomVM/ets.c +++ b/src/libAtomVM/ets.c @@ -32,20 +32,20 @@ #define ETS_ANY_PROCESS -1 #ifndef AVM_NO_SMP -#define SMP_RDLOCK(table) smp_rwlock_rdlock(table->lock) -#define SMP_WRLOCK(table) smp_rwlock_wrlock(table->lock) -#define SMP_UNLOCK(table) smp_rwlock_unlock(table->lock) +# define SMP_RDLOCK(table) smp_rwlock_rdlock(table->lock) +# define SMP_WRLOCK(table) smp_rwlock_wrlock(table->lock) +# define SMP_UNLOCK(table) smp_rwlock_unlock(table->lock) #else -#define SMP_RDLOCK(table) -#define SMP_WRLOCK(table) -#define SMP_UNLOCK(table) +# define SMP_RDLOCK(table) +# define SMP_WRLOCK(table) +# define SMP_UNLOCK(table) #endif #ifndef AVM_NO_SMP -#ifndef TYPEDEF_RWLOCK -#define TYPEDEF_RWLOCK +# ifndef TYPEDEF_RWLOCK +# define TYPEDEF_RWLOCK typedef struct RWLock RWLock; -#endif +# endif #endif struct EtsTable diff --git a/src/libAtomVM/exportedfunction.h b/src/libAtomVM/exportedfunction.h index 0b2d4fb37..694121875 100644 --- a/src/libAtomVM/exportedfunction.h +++ b/src/libAtomVM/exportedfunction.h @@ -31,17 +31,17 @@ #include "term.h" #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif #ifndef TYPEDEF_MODULENATIVEINTERFACE -#define TYPEDEF_MODULENATIVEINTERFACE +# define TYPEDEF_MODULENATIVEINTERFACE typedef struct ModuleNativeInterface ModuleNativeInterface; #endif #ifndef TYPEDEF_JITSTATE -#define TYPEDEF_JITSTATE +# define TYPEDEF_JITSTATE typedef struct JITState JITState; #endif diff --git a/src/libAtomVM/externalterm.c b/src/libAtomVM/externalterm.c index 72b3226b6..6f46fa8ba 100644 --- a/src/libAtomVM/externalterm.c +++ b/src/libAtomVM/externalterm.c @@ -1157,7 +1157,7 @@ static int calculate_heap_usage(const uint8_t *external_term_buf, size_t remaini #elif TERM_BYTES == 8 int size_in_terms = ((binary_size + 8 - 1) >> 3); #else -#error +# error #endif if (copy && term_binary_size_is_heap_binary(binary_size)) { diff --git a/src/libAtomVM/globalcontext.c b/src/libAtomVM/globalcontext.c index ad1a2d028..ce3523ff8 100644 --- a/src/libAtomVM/globalcontext.c +++ b/src/libAtomVM/globalcontext.c @@ -43,12 +43,12 @@ #include "valueshashtable.h" #ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" +# include "platform_atomic.h" #else -#if defined(HAVE_ATOMIC) -#include -#define ATOMIC_COMPARE_EXCHANGE_WEAK_PTR atomic_compare_exchange_weak -#endif +# if defined(HAVE_ATOMIC) +# include +# define ATOMIC_COMPARE_EXCHANGE_WEAK_PTR atomic_compare_exchange_weak +# endif #endif struct RegisteredProcess @@ -134,9 +134,9 @@ GlobalContext *globalcontext_new(void) #if HAVE_OPEN && HAVE_CLOSE glb->posix_fd_resource_type = enif_init_resource_type(&env, "posix_fd", &posix_fd_resource_type_init, ERL_NIF_RT_CREATE, NULL); if (IS_NULL_PTR(glb->posix_fd_resource_type)) { -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP smp_rwlock_destroy(glb->modules_lock); -#endif +# endif free(glb->modules_table); atom_table_destroy(glb->atom_table); free(glb); @@ -149,9 +149,9 @@ GlobalContext *globalcontext_new(void) erl_nif_env_partial_init_from_globalcontext(&dir_env, glb); glb->posix_dir_resource_type = enif_init_resource_type(&env, "posix_dir", &posix_dir_resource_type_init, ERL_NIF_RT_CREATE, NULL); if (IS_NULL_PTR(glb->posix_dir_resource_type)) { -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP smp_rwlock_destroy(glb->modules_lock); -#endif +# endif free(glb->modules_table); atom_table_destroy(glb->atom_table); free(glb); @@ -164,9 +164,9 @@ GlobalContext *globalcontext_new(void) #ifndef AVM_NO_SMP glb->schedulers_mutex = smp_mutex_create(); if (IS_NULL_PTR(glb->schedulers_mutex)) { -#if HAVE_OPEN && HAVE_CLOSE +# if HAVE_OPEN && HAVE_CLOSE resource_type_destroy(glb->posix_fd_resource_type); -#endif +# endif smp_rwlock_destroy(glb->modules_lock); free(glb->modules_table); atom_table_destroy(glb->atom_table); @@ -176,9 +176,9 @@ GlobalContext *globalcontext_new(void) glb->schedulers_cv = smp_condvar_create(); if (IS_NULL_PTR(glb->schedulers_cv)) { smp_mutex_destroy(glb->schedulers_mutex); -#if HAVE_OPEN && HAVE_CLOSE +# if HAVE_OPEN && HAVE_CLOSE resource_type_destroy(glb->posix_fd_resource_type); -#endif +# endif smp_rwlock_destroy(glb->modules_lock); free(glb->modules_table); atom_table_destroy(glb->atom_table); @@ -372,7 +372,7 @@ static inline enum SendMessageResult globalcontext_send_message_from_task_common { enum SendMessageResult result = SEND_MESSAGE_PROCESS_NOT_FOUND; bool postponed = false; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP Context *p = NULL; if (globalcontext_get_process_trylock(glb, process_id, &p)) { if (p) { @@ -394,10 +394,10 @@ static inline enum SendMessageResult globalcontext_send_message_from_task_common } else { postponed = true; } -#else +# else // Without SMP, we have no lock, so we must always enqueue. postponed = true; -#endif +# endif if (postponed) { if (message == NULL) { message = mailbox_message_create_from_term(type, t); diff --git a/src/libAtomVM/globalcontext.h b/src/libAtomVM/globalcontext.h index 42a756f50..380073c98 100644 --- a/src/libAtomVM/globalcontext.h +++ b/src/libAtomVM/globalcontext.h @@ -51,29 +51,29 @@ extern "C" { struct Context; #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif struct Module; #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif #ifndef TYPEDEF_MAILBOXMESSAGE -#define TYPEDEF_MAILBOXMESSAGE +# define TYPEDEF_MAILBOXMESSAGE typedef struct MailboxMessage MailboxMessage; #endif #ifndef TYPEDEF_MESSAGE -#define TYPEDEF_MESSAGE +# define TYPEDEF_MESSAGE typedef struct Message Message; #endif @@ -144,9 +144,9 @@ struct GlobalContext unsigned long long ATOMIC ref_ticks; #else unsigned long long ref_ticks; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP SpinLock ref_ticks_spinlock; -#endif +# endif #endif #ifndef AVM_NO_SMP @@ -537,14 +537,14 @@ run_result_t globalcontext_run(GlobalContext *global, Module *start_module, FILE #ifndef __cplusplus static inline uint64_t globalcontext_get_ref_ticks(GlobalContext *global) { -#if defined(AVM_NO_SMP) || ATOMIC_LLONG_LOCK_FREE == 2 +# if defined(AVM_NO_SMP) || ATOMIC_LLONG_LOCK_FREE == 2 return ++global->ref_ticks; -#else +# else smp_spinlock_lock(&global->ref_ticks_spinlock); unsigned long long value = ++global->ref_ticks; smp_spinlock_unlock(&global->ref_ticks_spinlock); return value; -#endif +# endif } #endif diff --git a/src/libAtomVM/intn.c b/src/libAtomVM/intn.c index 9c331919c..9055e4510 100644 --- a/src/libAtomVM/intn.c +++ b/src/libAtomVM/intn.c @@ -177,13 +177,13 @@ static void mulmnu16(const uint16_t u[], size_t m, const uint16_t v[], size_t n, void intn_mulu(const uint32_t m[], size_t m_len, const uint32_t n[], size_t n_len, uint32_t out[]) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ mulmnu16((const uint16_t *) m, m_len * 2, (const uint16_t *) n, n_len * 2, (uint16_t *) out); -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -#error "Big endian not yet supported" -#else -#error "Unsupported endianness" -#endif +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# error "Big endian not yet supported" +# else +# error "Unsupported endianness" +# endif } #endif @@ -224,9 +224,9 @@ static inline uint32_t uint32_nlz(uint32_t x) ASSUME(x != 0); #ifdef __has_builtin -#define HAS_BUILTIN(x) __has_builtin(x) +# define HAS_BUILTIN(x) __has_builtin(x) #else -#define HAS_BUILTIN(x) 0 +# define HAS_BUILTIN(x) 0 #endif #if defined(__GNUC__) \ @@ -1053,7 +1053,7 @@ char *intn_to_string( // "If realloc() fails, the original block is left untouched; it is not freed or moved." #pragma GCC diagnostic push #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 12 -#pragma GCC diagnostic ignored "-Wuse-after-free" +# pragma GCC diagnostic ignored "-Wuse-after-free" #endif free(outbuf); #pragma GCC diagnostic pop @@ -1243,7 +1243,7 @@ int intn_to_integer_bytes(const intn_digit_t in[], size_t in_len, intn_integer_s out[i * 4 + 3] = (in[i] >> 24) & 0xFF; } #else -#error "Unsupported endianess" +# error "Unsupported endianess" #endif intn_digit_t last_in = in[to_copy]; diff --git a/src/libAtomVM/intn.h b/src/libAtomVM/intn.h index 253b91446..47d1b86fa 100644 --- a/src/libAtomVM/intn.h +++ b/src/libAtomVM/intn.h @@ -899,7 +899,7 @@ static inline void intn_from_uint64(uint64_t absu64, intn_digit_t out[]) out[0] = i32[1]; out[1] = i32[0]; #else -#error "Unsupported endianness" +# error "Unsupported endianness" #endif } @@ -940,7 +940,7 @@ static inline uint64_t intn_to_uint64(const intn_digit_t num[]) #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ utmp = (((uint64_t) num[1] << 32) | (uint64_t) num[0]); #else -#error "Unsupported endianness" +# error "Unsupported endianness" #endif return utmp; diff --git a/src/libAtomVM/jit.c b/src/libAtomVM/jit.c index 009f7795a..1326bd3c5 100644 --- a/src/libAtomVM/jit.c +++ b/src/libAtomVM/jit.c @@ -23,24 +23,24 @@ #ifndef AVM_NO_JIT -#include "bif.h" -#include "bitstring.h" -#include "context.h" -#include "debug.h" -#include "defaultatoms.h" -#include "dist_nifs.h" -#include "module.h" -#include "nifs.h" -#include "scheduler.h" -#include "stacktrace.h" -#include "term.h" -#include "utils.h" - -#include -#include +# include "bif.h" +# include "bitstring.h" +# include "context.h" +# include "debug.h" +# include "defaultatoms.h" +# include "dist_nifs.h" +# include "module.h" +# include "nifs.h" +# include "scheduler.h" +# include "stacktrace.h" +# include "term.h" +# include "utils.h" + +# include +# include // #define ENABLE_TRACE -#include "trace.h" +# include "trace.h" // Verify matching atom index in default_atoms.hrl _Static_assert(OK_ATOM_INDEX == 2, "OK_ATOM_INDEX is 2 in libs/jit/src/default_atoms.hrl "); @@ -61,7 +61,7 @@ _Static_assert(LOWERCASE_EXIT_ATOM_INDEX == 16, "LOWERCASE_EXIT_ATOM_INDEX is 16 _Static_assert(BADRECORD_ATOM_INDEX == 17, "BADRECORD_ATOM_INDEX is 17 in libs/jit/src/default_atoms.hrl "); // Verify offsets in jit_x86_64.erl -#if JIT_ARCH_TARGET == JIT_ARCH_X86_64 || JIT_ARCH_TARGET == JIT_ARCH_AARCH64 +# if JIT_ARCH_TARGET == JIT_ARCH_X86_64 || JIT_ARCH_TARGET == JIT_ARCH_AARCH64 _Static_assert(offsetof(Context, e) == 0x28, "ctx->e is 0x28 in jit/src/jit_{aarch64,x86_64}.erl"); _Static_assert(offsetof(Context, x) == 0x30, "ctx->x is 0x30 in jit/src/jit_{aarch64,x86_64}.erl"); _Static_assert(offsetof(Context, cp) == 0xB8, "ctx->cp is 0xB8 in jit/src/jit_{aarch64,x86_64}.erl"); @@ -72,7 +72,7 @@ _Static_assert(offsetof(Context, bs_offset) == 0xD0, "ctx->bs_offset is 0xD0 in _Static_assert(offsetof(JITState, module) == 0x0, "jit_state->module is 0x0 in jit/src/jit_{aarch64,x86_64}.erl"); _Static_assert(offsetof(JITState, continuation) == 0x8, "jit_state->continuation is 0x8 in jit/src/jit_{aarch64,x86_64}.erl"); _Static_assert(offsetof(JITState, remaining_reductions) == 0x10, "jit_state->remaining_reductions is 0x10 in jit/src/jit_{aarch64,x86_64}.erl"); -#elif JIT_ARCH_TARGET == JIT_ARCH_ARMV6M +# elif JIT_ARCH_TARGET == JIT_ARCH_ARMV6M _Static_assert(offsetof(Context, e) == 0x14, "ctx->e is 0x14 in jit/src/jit_armv6m.erl"); _Static_assert(offsetof(Context, x) == 0x18, "ctx->x is 0x18 in jit/src/jit_armv6m.erl"); _Static_assert(offsetof(Context, cp) == 0x5C, "ctx->cp is 0x5C in jit/src/jit_armv6m.erl"); @@ -86,37 +86,37 @@ _Static_assert(offsetof(JITState, remaining_reductions) == 0x8, "jit_state->rema _Static_assert(sizeof(size_t) == 4, "size_t is expected to be 32 bits"); -#else -#error Unknown jit target -#endif +# else +# error Unknown jit target +# endif -#ifdef AVM_USE_SINGLE_PRECISION +# ifdef AVM_USE_SINGLE_PRECISION _Static_assert(sizeof(avm_float_t) == 0x4, "sizeof(avm_float_t) is 0x4 for single precision"); -#else +# else _Static_assert(sizeof(avm_float_t) == 0x8, "sizeof(avm_float_t) is 0x8 for double precision"); -#endif - -#define PROCESS_MAYBE_TRAP_RETURN_VALUE(return_value, offset) \ - if (term_is_invalid_term(return_value)) { \ - if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ - return jit_handle_error(ctx, jit_state, offset); \ - } else { \ - return jit_schedule_wait_cp(ctx, jit_state); \ - } \ - } +# endif + +# define PROCESS_MAYBE_TRAP_RETURN_VALUE(return_value, offset) \ + if (term_is_invalid_term(return_value)) { \ + if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ + return jit_handle_error(ctx, jit_state, offset); \ + } else { \ + return jit_schedule_wait_cp(ctx, jit_state); \ + } \ + } -#define PROCESS_MAYBE_TRAP_RETURN_VALUE_LAST(return_value, offset) \ - if (term_is_invalid_term(return_value)) { \ - if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ - return jit_handle_error(ctx, jit_state, offset); \ - } else { \ - return jit_schedule_wait_cp(jit_return(ctx, jit_state), jit_state); \ - } \ - } +# define PROCESS_MAYBE_TRAP_RETURN_VALUE_LAST(return_value, offset) \ + if (term_is_invalid_term(return_value)) { \ + if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ + return jit_handle_error(ctx, jit_state, offset); \ + } else { \ + return jit_schedule_wait_cp(jit_return(ctx, jit_state), jit_state); \ + } \ + } -#ifndef MIN -#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) -#endif +# ifndef MIN +# define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +# endif static void destroy_extended_registers(Context *ctx, unsigned int live) { @@ -139,11 +139,11 @@ static void jit_trim_live_regs(Context *ctx, uint32_t live) } } -#define TRIM_LIVE_REGS(live_regs_no) \ - jit_trim_live_regs(ctx, live_regs_no); \ - if (UNLIKELY(live_regs_no > MAX_REG)) { \ - live_regs_no = MAX_REG; \ - } +# define TRIM_LIVE_REGS(live_regs_no) \ + jit_trim_live_regs(ctx, live_regs_no); \ + if (UNLIKELY(live_regs_no > MAX_REG)) { \ + live_regs_no = MAX_REG; \ + } // Update jit_state->module and jit_state->continuation static Context *jit_return(Context *ctx, JITState *jit_state) @@ -153,20 +153,20 @@ static Context *jit_return(Context *ctx, JITState *jit_state) Module *mod = globalcontext_get_module_by_index(ctx->global, module_index); // Native case -#ifndef AVM_NO_EMU +# ifndef AVM_NO_EMU if (mod->native_code == NULL) { // return to emulated const uint8_t *code = mod->code->code; const uint8_t *pc = code + ((ctx->cp & 0xFFFFFF) >> 2); jit_state->continuation = pc; } else { -#endif +# endif // return to native using pointer arithmetics on function pointers const void *native_pc = ((const uint8_t *) mod->native_code) + ((ctx->cp & 0xFFFFFF) >> 2); jit_state->continuation = (ModuleNativeEntryPoint) native_pc; -#ifndef AVM_NO_EMU +# ifndef AVM_NO_EMU } -#endif +# endif jit_state->module = mod; return ctx; } @@ -207,11 +207,11 @@ static Context *jit_handle_error(Context *ctx, JITState *jit_state, int offset) } // Do not print crash dump if reason is normal or shutdown. -#ifdef AVM_PRINT_PROCESS_CRASH_DUMPS +# ifdef AVM_PRINT_PROCESS_CRASH_DUMPS if (ctx->x[0] != LOWERCASE_EXIT_ATOM || (ctx->x[1] != NORMAL_ATOM && ctx->x[1] != SHUTDOWN_ATOM)) { context_dump(ctx); } -#endif +# endif if (ctx->x[0] == LOWERCASE_EXIT_ATOM) { ctx->exit_reason = ctx->x[1]; @@ -322,10 +322,10 @@ enum TrapAndLoadResult jit_trap_and_load(Context *ctx, Module *mod, uint32_t lab END_WITH_STACK_HEAP(heap, ctx->global); ctx->saved_module = mod; // We exceptionally store the label in this field, used by context_process_code_server_resume_signal -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpedantic" ctx->saved_function_ptr = (void *) (uintptr_t) label; -#pragma GCC diagnostic pop +# pragma GCC diagnostic pop context_update_flags(ctx, ~NoFlags, Trap); return TRAP_AND_LOAD_OK; } @@ -575,7 +575,7 @@ static term jit_module_load_literal(Context *ctx, JITState *jit_state, int index static term jit_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) { TRACE("jit_alloc_boxed_integer_fragment: value=%lld\n", (long long) value); -#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 +# if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { Heap heap; if (UNLIKELY(memory_init_heap(&heap, BOXED_INT64_SIZE) != MEMORY_GC_OK)) { @@ -587,7 +587,7 @@ static term jit_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) return term_make_boxed_int64(value, &heap); } -#endif +# endif Heap heap; if (UNLIKELY(memory_init_heap(&heap, BOXED_INT_SIZE) != MEMORY_GC_OK)) { ctx->x[0] = ERROR_ATOM; @@ -601,7 +601,7 @@ static term jit_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) static term maybe_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) { -#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 +# if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { Heap heap; if (UNLIKELY(memory_init_heap(&heap, BOXED_INT64_SIZE) != MEMORY_GC_OK)) { @@ -613,7 +613,7 @@ static term maybe_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) return term_make_boxed_int64(value, &heap); } else -#endif +# endif if ((value < MIN_NOT_BOXED_INT) || (value > MAX_NOT_BOXED_INT)) { Heap heap; if (UNLIKELY(memory_init_heap(&heap, BOXED_INT_SIZE) != MEMORY_GC_OK)) { @@ -1017,8 +1017,8 @@ static bool maybe_call_native(Context *ctx, atom_index_t module_name, atom_index return false; } -#define MAXI(A, B) ((A > B) ? (A) : (B)) -#define MINI(A, B) ((A > B) ? (B) : (A)) +# define MAXI(A, B) ((A > B) ? (A) : (B)) +# define MINI(A, B) ((A > B) ? (B) : (A)) static Context *jit_call_fun(Context *ctx, JITState *jit_state, int offset, term fun, unsigned int args_count) { @@ -1113,80 +1113,80 @@ static void jit_term_conv_to_float(Context *ctx, term t, int freg) static bool jit_fadd(Context *ctx, int freg1, int freg2, int freg3) { TRACE("jit_fadd: freg1=%d [%f] freg2=%d [%f] freg3=%d\n", freg1, ctx->fr[freg1], freg2, ctx->fr[freg2], freg3); -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# pragma STDC FENV_ACCESS ON feclearexcept(FE_OVERFLOW); -#endif +# endif ctx->fr[freg3] = ctx->fr[freg1] + ctx->fr[freg2]; -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS if (fetestexcept(FE_OVERFLOW)) { return false; } -#else +# else if (!isfinite(ctx->fr[freg3])) { return false; } -#endif +# endif return true; } static bool jit_fsub(Context *ctx, int freg1, int freg2, int freg3) { TRACE("jit_fsub: freg1=%d freg2=%d freg3=%d\n", freg1, freg2, freg3); -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# pragma STDC FENV_ACCESS ON feclearexcept(FE_OVERFLOW); -#endif +# endif ctx->fr[freg3] = ctx->fr[freg1] - ctx->fr[freg2]; -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS if (fetestexcept(FE_OVERFLOW)) { return false; } -#else +# else if (!isfinite(ctx->fr[freg3])) { return false; } -#endif +# endif return true; } static bool jit_fmul(Context *ctx, int freg1, int freg2, int freg3) { TRACE("jit_fmul: freg1=%d freg2=%d freg3=%d\n", freg1, freg2, freg3); -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# pragma STDC FENV_ACCESS ON feclearexcept(FE_OVERFLOW); -#endif +# endif ctx->fr[freg3] = ctx->fr[freg1] * ctx->fr[freg2]; -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS if (fetestexcept(FE_OVERFLOW)) { return false; } -#else +# else if (!isfinite(ctx->fr[freg3])) { return false; } -#endif +# endif return true; } static bool jit_fdiv(Context *ctx, int freg1, int freg2, int freg3) { TRACE("jit_fdiv: freg1=%d freg2=%d freg3=%d\n", freg1, freg2, freg3); -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# pragma STDC FENV_ACCESS ON feclearexcept(FE_OVERFLOW | FE_DIVBYZERO); -#endif +# endif ctx->fr[freg3] = ctx->fr[freg1] / ctx->fr[freg2]; -#ifdef HAVE_PRAGMA_STDC_FENV_ACCESS +# ifdef HAVE_PRAGMA_STDC_FENV_ACCESS if (fetestexcept(FE_OVERFLOW | FE_DIVBYZERO)) { return false; } -#else +# else if (!isfinite(ctx->fr[freg3])) { return false; } -#endif +# endif return true; } diff --git a/src/libAtomVM/jit.h b/src/libAtomVM/jit.h index ec11860a8..b274dfa3f 100644 --- a/src/libAtomVM/jit.h +++ b/src/libAtomVM/jit.h @@ -35,31 +35,31 @@ extern "C" { struct Context; #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif struct Module; #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif #ifndef TYPEDEF_JITSTATE -#define TYPEDEF_JITSTATE +# define TYPEDEF_JITSTATE typedef struct JITState JITState; #endif #ifndef TYPEDEF_MODULENATIVEINTERFACE -#define TYPEDEF_MODULENATIVEINTERFACE +# define TYPEDEF_MODULENATIVEINTERFACE typedef struct ModuleNativeInterface ModuleNativeInterface; #endif struct Module; #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif @@ -181,24 +181,24 @@ enum TrapAndLoadResult #ifndef AVM_NO_JIT -#ifdef __x86_64__ -#define JIT_ARCH_TARGET JIT_ARCH_X86_64 -#define JIT_JUMPTABLE_ENTRY_SIZE 5 -#endif +# ifdef __x86_64__ +# define JIT_ARCH_TARGET JIT_ARCH_X86_64 +# define JIT_JUMPTABLE_ENTRY_SIZE 5 +# endif -#if defined(__arm64__) || defined(__aarch64__) -#define JIT_ARCH_TARGET JIT_ARCH_AARCH64 -#define JIT_JUMPTABLE_ENTRY_SIZE 4 -#endif +# if defined(__arm64__) || defined(__aarch64__) +# define JIT_ARCH_TARGET JIT_ARCH_AARCH64 +# define JIT_JUMPTABLE_ENTRY_SIZE 4 +# endif -#ifdef __arm__ -#define JIT_ARCH_TARGET JIT_ARCH_ARMV6M -#define JIT_JUMPTABLE_ENTRY_SIZE 12 -#endif +# ifdef __arm__ +# define JIT_ARCH_TARGET JIT_ARCH_ARMV6M +# define JIT_JUMPTABLE_ENTRY_SIZE 12 +# endif -#ifndef JIT_ARCH_TARGET -#error Unknown JIT target -#endif +# ifndef JIT_ARCH_TARGET +# error Unknown JIT target +# endif #endif /** diff --git a/src/libAtomVM/jit_stream_flash.c b/src/libAtomVM/jit_stream_flash.c index 65b9c1342..a4812fb0d 100644 --- a/src/libAtomVM/jit_stream_flash.c +++ b/src/libAtomVM/jit_stream_flash.c @@ -907,12 +907,12 @@ bool sys_get_cache_native_code(GlobalContext *global, Module *mod, uint16_t *ver *entry_point = (ModuleNativeEntryPoint) ep_addr; *labels = jit_entry->labels; -#ifdef ENABLE_TRACE +# ifdef ENABLE_TRACE // Compute CRC of entire module for verification uint32_t module_crc = crc32((const uint8_t *) jit_entry, sizeof(struct JITEntry) + jit_entry->size); TRACE("Loading from cache - jit_entry=%p CRC32=0x%08x (entry+code size=%u)\n", (void *) jit_entry, (unsigned int) module_crc, (unsigned int) (sizeof(struct JITEntry) + jit_entry->size)); -#endif +# endif return true; } diff --git a/src/libAtomVM/mailbox.c b/src/libAtomVM/mailbox.c index efe93923d..ac8521226 100644 --- a/src/libAtomVM/mailbox.c +++ b/src/libAtomVM/mailbox.c @@ -28,12 +28,12 @@ #include "trace.h" #ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" +# include "platform_atomic.h" #else -#if defined(HAVE_ATOMIC) -#include -#define ATOMIC_COMPARE_EXCHANGE_WEAK_PTR atomic_compare_exchange_weak -#endif +# if defined(HAVE_ATOMIC) +# include +# define ATOMIC_COMPARE_EXCHANGE_WEAK_PTR atomic_compare_exchange_weak +# endif #endif #define ADDITIONAL_PROCESSING_MEMORY_SIZE 4 diff --git a/src/libAtomVM/mailbox.h b/src/libAtomVM/mailbox.h index 7d95a18e7..ceac5d363 100644 --- a/src/libAtomVM/mailbox.h +++ b/src/libAtomVM/mailbox.h @@ -36,14 +36,14 @@ #include "utils.h" #ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" +# include "platform_atomic.h" #endif #if defined(HAVE_ATOMIC) && !defined(__cplusplus) -#include -#define ATOMIC _Atomic +# include +# define ATOMIC _Atomic #else -#define ATOMIC +# define ATOMIC #endif #ifdef __cplusplus @@ -53,29 +53,29 @@ extern "C" { struct Context; #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif struct Heap; #ifndef TYPEDEF_HEAP -#define TYPEDEF_HEAP +# define TYPEDEF_HEAP typedef struct Heap Heap; #endif #ifndef TYPEDEF_MAILBOXMESSAGE -#define TYPEDEF_MAILBOXMESSAGE +# define TYPEDEF_MAILBOXMESSAGE typedef struct MailboxMessage MailboxMessage; #endif #ifndef TYPEDEF_MESSAGE -#define TYPEDEF_MESSAGE +# define TYPEDEF_MESSAGE typedef struct Message Message; #endif diff --git a/src/libAtomVM/memory.c b/src/libAtomVM/memory.c index 25a6f83ee..7591247f3 100644 --- a/src/libAtomVM/memory.c +++ b/src/libAtomVM/memory.c @@ -40,13 +40,13 @@ #include "utils.h" #ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) +# define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif #ifdef ENABLE_REALLOC_GC -#define MEMORY_SHRINK memory_shrink +# define MEMORY_SHRINK memory_shrink #else -#define MEMORY_SHRINK memory_gc +# define MEMORY_SHRINK memory_gc #endif static void memory_scan_and_copy(HeapFragment *old_fragment, term *mem_start, const term *mem_end, term **new_heap_pos, term *mso_list, bool move); diff --git a/src/libAtomVM/memory.h b/src/libAtomVM/memory.h index e53ef5c9d..aeb012621 100644 --- a/src/libAtomVM/memory.h +++ b/src/libAtomVM/memory.h @@ -26,7 +26,7 @@ #include #include #ifdef DEBUG_HEAP_ALLOC -#include +# include #endif #include "erl_nif.h" @@ -41,12 +41,12 @@ extern "C" { #define MIN_FREE_SPACE_SIZE 16 #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif @@ -88,7 +88,7 @@ struct Heap }; #ifndef TYPEDEF_HEAP -#define TYPEDEF_HEAP +# define TYPEDEF_HEAP typedef struct Heap Heap; #endif diff --git a/src/libAtomVM/module.c b/src/libAtomVM/module.c index 9618370be..37883f1c0 100644 --- a/src/libAtomVM/module.c +++ b/src/libAtomVM/module.c @@ -39,7 +39,7 @@ #include #ifdef WITH_ZLIB -#include +# include #endif // BEAM Type constants from OTP source code: @@ -92,10 +92,10 @@ struct LineRefOffset }; #ifndef AVM_NO_EMU -#define IMPL_CODE_LOADER 1 -#include "opcodesswitch.h" -#undef TRACE -#undef IMPL_CODE_LOADER +# define IMPL_CODE_LOADER 1 +# include "opcodesswitch.h" +# undef TRACE +# undef IMPL_CODE_LOADER #endif static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, uint8_t *table_data, GlobalContext *glb) @@ -337,11 +337,11 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary } else { for (int arch_index = 0; arch_index < ENDIAN_SWAP_16(native_code->architectures_count); arch_index++) { uint16_t runtime_variant; -#ifdef AVM_USE_SINGLE_PRECISION +# ifdef AVM_USE_SINGLE_PRECISION runtime_variant = JIT_VARIANT_FLOAT32 | JIT_VARIANT_PIC; -#else +# else runtime_variant = JIT_VARIANT_PIC; -#endif +# endif if (ENDIAN_SWAP_16(native_code->architectures[arch_index].architecture) == JIT_ARCH_TARGET && ENDIAN_SWAP_16(native_code->architectures[arch_index].variant) == runtime_variant) { size_t offset = ENDIAN_SWAP_32(native_code->info_size) + ENDIAN_SWAP_32(native_code->architectures[arch_index].offset) + sizeof(native_code->info_size); ModuleNativeEntryPoint module_entry_point = sys_map_native_code((const uint8_t *) &native_code->info_size, ENDIAN_SWAP_32(native_code->size), offset); @@ -1141,9 +1141,9 @@ bool module_find_line(Module *mod, unsigned int offset, uint32_t *line, size_t * } return module_find_line_ref(mod, prev_line_ref, line, filename_len, filename); } else { -#if defined(AVM_NO_EMU) +# if defined(AVM_NO_EMU) return false; -#endif +# endif #endif #ifndef AVM_NO_EMU uint32_t line_ref; diff --git a/src/libAtomVM/module.h b/src/libAtomVM/module.h index 2f7b56321..96d64d7a4 100644 --- a/src/libAtomVM/module.h +++ b/src/libAtomVM/module.h @@ -45,19 +45,19 @@ extern "C" { #ifndef AVM_NO_SMP -#ifndef TYPEDEF_MUTEX -#define TYPEDEF_MUTEX +# ifndef TYPEDEF_MUTEX +# define TYPEDEF_MUTEX typedef struct Mutex Mutex; -#endif +# endif #endif #ifndef AVM_NO_SMP -#define SMP_MODULE_LOCK(mod) smp_mutex_lock(mod->mutex) -#define SMP_MODULE_UNLOCK(mod) smp_mutex_unlock(mod->mutex) +# define SMP_MODULE_LOCK(mod) smp_mutex_lock(mod->mutex) +# define SMP_MODULE_UNLOCK(mod) smp_mutex_unlock(mod->mutex) #else -#define SMP_MODULE_LOCK(mod) -#define SMP_MODULE_UNLOCK(mod) +# define SMP_MODULE_LOCK(mod) +# define SMP_MODULE_UNLOCK(mod) #endif typedef struct @@ -155,7 +155,7 @@ struct Module }; #ifndef TYPEDEF_MODULE -#define TYPEDEF_MODULE +# define TYPEDEF_MODULE typedef struct Module Module; #endif diff --git a/src/libAtomVM/nifs.c b/src/libAtomVM/nifs.c index d6b5ef8bc..887e00305 100644 --- a/src/libAtomVM/nifs.c +++ b/src/libAtomVM/nifs.c @@ -19,7 +19,7 @@ */ #ifndef _GNU_SOURCE -#define _GNU_SOURCE +# define _GNU_SOURCE #endif #include "nifs.h" @@ -64,7 +64,7 @@ #include "unicode.h" #include "utils.h" #ifdef WITH_ZLIB -#include "zlib.h" +# include "zlib.h" #endif #define FLOAT_BUF_SIZE 64 @@ -75,7 +75,7 @@ return term_invalid_term(); #ifndef MAX -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +# define MAX(x, y) (((x) > (y)) ? (x) : (y)) #endif #define NOT_FOUND (0xFF) @@ -885,47 +885,47 @@ DEFINE_MATH_NIF(tanh) // Handle optional nifs #if HAVE_OPEN && HAVE_CLOSE -#define IF_HAVE_OPEN_CLOSE(expr) (expr) -#if HAVE_EXECVE -#define IF_HAVE_EXECVE(expr) (expr) +# define IF_HAVE_OPEN_CLOSE(expr) (expr) +# if HAVE_EXECVE +# define IF_HAVE_EXECVE(expr) (expr) +# else +# define IF_HAVE_EXECVE(expr) NULL +# endif #else -#define IF_HAVE_EXECVE(expr) NULL -#endif -#else -#define IF_HAVE_OPEN_CLOSE(expr) NULL -#define IF_HAVE_EXECVE(expr) NULL +# define IF_HAVE_OPEN_CLOSE(expr) NULL +# define IF_HAVE_EXECVE(expr) NULL #endif #if HAVE_MKFIFO -#define IF_HAVE_MKFIFO(expr) (expr) +# define IF_HAVE_MKFIFO(expr) (expr) #else -#define IF_HAVE_MKFIFO(expr) NULL +# define IF_HAVE_MKFIFO(expr) NULL #endif #if HAVE_UNLINK -#define IF_HAVE_UNLINK(expr) (expr) +# define IF_HAVE_UNLINK(expr) (expr) #else -#define IF_HAVE_UNLINK(expr) NULL +# define IF_HAVE_UNLINK(expr) NULL #endif #if HAVE_CLOCK_SETTIME -#define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) (expr) +# define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) (expr) #elif HAVE_SETTIMEOFDAY -#define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) (expr) +# define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) (expr) #else -#define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) NULL +# define IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(expr) NULL #endif #if HAVE_OPENDIR && HAVE_READDIR && HAVE_CLOSEDIR -#define IF_HAVE_OPENDIR_READDIR_CLOSEDIR(expr) (expr) +# define IF_HAVE_OPENDIR_READDIR_CLOSEDIR(expr) (expr) #else -#define IF_HAVE_OPENDIR_READDIR_CLOSEDIR(expr) NULL +# define IF_HAVE_OPENDIR_READDIR_CLOSEDIR(expr) NULL #endif #if defined(HAVE_GETCWD) && defined(HAVE_PATH_MAX) -#define IF_HAVE_GETCWD_PATHMAX(expr) (expr) +# define IF_HAVE_GETCWD_PATHMAX(expr) (expr) #else -#define IF_HAVE_GETCWD_PATHMAX(expr) NULL +# define IF_HAVE_GETCWD_PATHMAX(expr) NULL #endif #ifndef AVM_NO_JIT -#define IF_HAVE_JIT(expr) (expr) +# define IF_HAVE_JIT(expr) (expr) #else -#define IF_HAVE_JIT(expr) NULL +# define IF_HAVE_JIT(expr) NULL #endif // Ignore warning caused by gperf generated code @@ -4121,7 +4121,9 @@ static term nif_erlang_fun_info_2(Context *ctx, int argc, term argv[]) RAISE_ERROR(BADARG_ATOM); } - if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]){ key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { + if (UNLIKELY(memory_ensure_free_with_roots( + ctx, TUPLE_SIZE(2), 2, (term[]) { key, value }, MEMORY_CAN_SHRINK) + != MEMORY_GC_OK)) { RAISE_ERROR(OUT_OF_MEMORY_ATOM); } term fun_info_tuple = term_alloc_tuple(2, &ctx->heap); @@ -5697,15 +5699,15 @@ static term nif_jit_backend_module(Context *ctx, int argc, term argv[]) UNUSED(argc); UNUSED(argv); -#if JIT_ARCH_TARGET == JIT_ARCH_X86_64 +# if JIT_ARCH_TARGET == JIT_ARCH_X86_64 return JIT_X86_64_ATOM; -#elif JIT_ARCH_TARGET == JIT_ARCH_AARCH64 +# elif JIT_ARCH_TARGET == JIT_ARCH_AARCH64 return JIT_AARCH64_ATOM; -#elif JIT_ARCH_TARGET == JIT_ARCH_ARMV6M +# elif JIT_ARCH_TARGET == JIT_ARCH_ARMV6M return JIT_ARMV6M_ATOM; -#else -#error Unknown JIT target -#endif +# else +# error Unknown JIT target +# endif } static term nif_jit_variant(Context *ctx, int argc, term argv[]) @@ -5714,11 +5716,11 @@ static term nif_jit_variant(Context *ctx, int argc, term argv[]) UNUSED(argc); UNUSED(argv); -#ifdef AVM_USE_SINGLE_PRECISION +# ifdef AVM_USE_SINGLE_PRECISION return term_from_int(JIT_VARIANT_FLOAT32 | JIT_VARIANT_PIC); -#else +# else return term_from_int(JIT_VARIANT_PIC); -#endif +# endif } #endif @@ -6438,7 +6440,7 @@ static void maybe_clear_exceptions(void) static term get_exception(avm_float_t f) { #ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# pragma STDC FENV_ACCESS ON UNUSED(f) if (fetestexcept(FE_DIVBYZERO | FE_INVALID)) { return BADARITH_ATOM; @@ -6453,7 +6455,7 @@ static term get_exception(avm_float_t f) static term math_unary_op(Context *ctx, term x_term, unary_math_f f) { #ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# pragma STDC FENV_ACCESS ON #endif avm_float_t x = term_conv_to_float(x_term); maybe_clear_exceptions(); @@ -6472,7 +6474,7 @@ static term math_unary_op(Context *ctx, term x_term, unary_math_f f) static term math_binary_op(Context *ctx, term x_term, term y_term, binary_math_f f) { #ifdef HAVE_PRAGMA_STDC_FENV_ACCESS -#pragma STDC FENV_ACCESS ON +# pragma STDC FENV_ACCESS ON #endif avm_float_t x = term_conv_to_float(x_term); avm_float_t y = term_conv_to_float(y_term); diff --git a/src/libAtomVM/otp_net.c b/src/libAtomVM/otp_net.c index 5dccfc859..2be6801a2 100644 --- a/src/libAtomVM/otp_net.c +++ b/src/libAtomVM/otp_net.c @@ -86,10 +86,10 @@ static term eai_errno_to_term(int err, GlobalContext *glb) #ifdef HAVE_EXTENDED_EAI_ERRNO case EAI_BADHINTS: return globalcontext_make_atom(glb, ATOM_STR("\xB", "eaibadhints")); -#ifdef HAVE_EAI_OVERFLOW +# ifdef HAVE_EAI_OVERFLOW case EAI_OVERFLOW: return globalcontext_make_atom(glb, ATOM_STR("\xB", "eaioverflow")); -#endif +# endif case EAI_PROTOCOL: return globalcontext_make_atom(glb, ATOM_STR("\xB", "eaiprotocol")); case EAI_SYSTEM: diff --git a/src/libAtomVM/otp_socket.c b/src/libAtomVM/otp_socket.c index c8acbcdf8..68d0b1338 100644 --- a/src/libAtomVM/otp_socket.c +++ b/src/libAtomVM/otp_socket.c @@ -43,22 +43,22 @@ #include #if OTP_SOCKET_BSD -#include -#include -#include -#include - -#if HAVE_SIGNAL -#include -#endif +# include +# include +# include +# include + +# if HAVE_SIGNAL +# include +# endif #elif OTP_SOCKET_LWIP -#include -#include -#if LWIP_IGMP -#include "lwip/igmp.h" -#endif +# include +# include +# if LWIP_IGMP +# include "lwip/igmp.h" +# endif #else -#error OTP Socket requires BSD Socket or lwIP +# error OTP Socket requires BSD Socket or lwIP #endif // #define ENABLE_TRACE @@ -68,9 +68,9 @@ // Check some LWIP options #if OTP_SOCKET_LWIP -#if !TCP_LISTEN_BACKLOG -#error TCP_LISTEN_BACKLOG is undefined -#endif +# if !TCP_LISTEN_BACKLOG +# error TCP_LISTEN_BACKLOG is undefined +# endif #endif // To factorize parsing of erlang term options, we define few constants for lwIP. @@ -159,9 +159,9 @@ struct SocketResource int32_t selecting_process_id; ErlNifMonitor selecting_process_monitor; size_t buf_size; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP RWLock *socket_lock; -#endif +# endif }; #elif OTP_SOCKET_LWIP struct SocketResource @@ -181,9 +181,9 @@ struct SocketResource size_t pos; struct ListHead received_list; size_t buf_size; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP RWLock *socket_lock; -#endif +# endif }; #endif @@ -240,7 +240,7 @@ static const AtomStringIntPair otp_socket_setopt_level_table[] = { #define DEFAULT_BUFFER_SIZE 512 #ifndef MIN -#define MIN(A, B) (((A) < (B)) ? (A) : (B)) +# define MIN(A, B) (((A) < (B)) ? (A) : (B)) #endif static ErlNifResourceType *socket_resource_type; @@ -848,14 +848,14 @@ static struct SocketResource *make_accepted_socket_resource(struct tcp_pcb *newp conn_rsrc_obj->linger_on = false; conn_rsrc_obj->linger_sec = 0; conn_rsrc_obj->buf_size = DEFAULT_BUFFER_SIZE; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP conn_rsrc_obj->socket_lock = smp_rwlock_create(); if (IS_NULL_PTR(conn_rsrc_obj->socket_lock)) { // destroy resource without calling destructor free(conn_rsrc_obj); return NULL; } -#endif +# endif list_init(&conn_rsrc_obj->received_list); tcp_arg(newpcb, conn_rsrc_obj); @@ -1432,7 +1432,7 @@ static term nif_socket_setopt(Context *ctx, int argc, term argv[]) return OK_ATOM; } #elif OTP_SOCKET_LWIP -#if LWIP_IGMP +# if LWIP_IGMP ip_addr_t interface_addr; ip_addr_set_ip4_u32(&interface_addr, htonl(inet_addr4_to_uint32(interface))); ip_addr_t multicast_addr; @@ -1444,12 +1444,12 @@ static term nif_socket_setopt(Context *ctx, int argc, term argv[]) } else { return OK_ATOM; } -#else +# else TRACE("socket:setopt: Unsupported ip option (LWIP_IGMP is not enabled)"); SMP_RWLOCK_UNLOCK(rsrc_obj->socket_lock); RAISE_ERROR(BADARG_ATOM); make_lwip_err_tuple -#endif +# endif #else TRACE("socket:setopt: Unsupported ip option"); SMP_RWLOCK_UNLOCK(rsrc_obj->socket_lock); @@ -1886,13 +1886,13 @@ static term nif_socket_accept(Context *ctx, int argc, term argv[]) conn_rsrc_obj->fd = fd; conn_rsrc_obj->selecting_process_id = INVALID_PROCESS_ID; conn_rsrc_obj->buf_size = DEFAULT_BUFFER_SIZE; -#ifndef AVM_NO_SMP +# ifndef AVM_NO_SMP conn_rsrc_obj->socket_lock = smp_rwlock_create(); if (IS_NULL_PTR(conn_rsrc_obj->socket_lock)) { free(conn_rsrc_obj); RAISE_ERROR(OUT_OF_MEMORY_ATOM); } -#endif +# endif TRACE("nif_socket_accept: Created socket on accept fd=%i\n", rsrc_obj->fd); term new_resource = enif_make_resource(erl_nif_env_from_context(ctx), conn_rsrc_obj); diff --git a/src/libAtomVM/otp_socket.h b/src/libAtomVM/otp_socket.h index cfd962653..01a149e4c 100644 --- a/src/libAtomVM/otp_socket.h +++ b/src/libAtomVM/otp_socket.h @@ -30,13 +30,13 @@ extern "C" { #endif #if !defined(OTP_SOCKET_BSD) && !defined(OTP_SOCKET_LWIP) -#if HAVE_SOCKET && HAVE_SELECT -#define OTP_SOCKET_BSD 1 -#elif HAVE_LWIP_RAW -#define OTP_SOCKET_LWIP 1 -#else -#error OTP Socket requires BSD Socket or lwIP -#endif +# if HAVE_SOCKET && HAVE_SELECT +# define OTP_SOCKET_BSD 1 +# elif HAVE_LWIP_RAW +# define OTP_SOCKET_LWIP 1 +# else +# error OTP Socket requires BSD Socket or lwIP +# endif #endif enum SocketErrors diff --git a/src/libAtomVM/otp_ssl.c b/src/libAtomVM/otp_ssl.c index fe774ce53..73c9af259 100644 --- a/src/libAtomVM/otp_ssl.c +++ b/src/libAtomVM/otp_ssl.c @@ -40,7 +40,7 @@ #include #if defined(MBEDTLS_PSA_CRYPTO_C) -#include +# include #endif // #define ENABLE_TRACE @@ -49,7 +49,7 @@ #define TAG "otp_ssl" #ifndef MBEDTLS_PRIVATE -#define MBEDTLS_PRIVATE(member) member +# define MBEDTLS_PRIVATE(member) member #endif // Default read buffer if mbedtls_ssl_get_max_in_record_payload fails @@ -57,7 +57,7 @@ #if defined(MBEDTLS_DEBUG_C) && defined(ENABLE_TRACE) -#include +# include static void mbedtls_debug_cb(void *ctx, int level, const char *filename, int line, const char *msg) { @@ -534,10 +534,10 @@ static term make_err_result(int err, Context *ctx) #if MBEDTLS_VERSION_NUMBER >= 0x020B0000 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: return globalcontext_make_atom(ctx->global, ATOM_STR("\xA", "async_in_progress")); -#if MBEDTLS_VERSION_NUMBER >= 0x020E0000 +# if MBEDTLS_VERSION_NUMBER >= 0x020E0000 case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: return globalcontext_make_atom(ctx->global, ATOM_STR("\xA", "crypto_in_progress")); -#endif +# endif #endif default: { if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2)) != MEMORY_GC_OK)) { diff --git a/src/libAtomVM/overflow_helpers.h b/src/libAtomVM/overflow_helpers.h index 0d2bec6b0..9da762846 100644 --- a/src/libAtomVM/overflow_helpers.h +++ b/src/libAtomVM/overflow_helpers.h @@ -30,43 +30,43 @@ extern "C" { #endif #ifdef __GNUC__ -#if __GNUC__ >= 5 -#define BUILTIN_ADD_OVERFLOW __builtin_add_overflow -#define BUILTIN_SUB_OVERFLOW __builtin_sub_overflow -#define BUILTIN_MUL_OVERFLOW __builtin_mul_overflow - -#define BUILTIN_ADD_OVERFLOW_INT __builtin_add_overflow -#define BUILTIN_SUB_OVERFLOW_INT __builtin_sub_overflow -#define BUILTIN_MUL_OVERFLOW_INT __builtin_mul_overflow - -#define BUILTIN_ADD_OVERFLOW_INT64 __builtin_add_overflow -#define BUILTIN_SUB_OVERFLOW_INT64 __builtin_sub_overflow -#define BUILTIN_MUL_OVERFLOW_INT64 __builtin_mul_overflow -#endif +# if __GNUC__ >= 5 +# define BUILTIN_ADD_OVERFLOW __builtin_add_overflow +# define BUILTIN_SUB_OVERFLOW __builtin_sub_overflow +# define BUILTIN_MUL_OVERFLOW __builtin_mul_overflow + +# define BUILTIN_ADD_OVERFLOW_INT __builtin_add_overflow +# define BUILTIN_SUB_OVERFLOW_INT __builtin_sub_overflow +# define BUILTIN_MUL_OVERFLOW_INT __builtin_mul_overflow + +# define BUILTIN_ADD_OVERFLOW_INT64 __builtin_add_overflow +# define BUILTIN_SUB_OVERFLOW_INT64 __builtin_sub_overflow +# define BUILTIN_MUL_OVERFLOW_INT64 __builtin_mul_overflow +# endif #endif #ifdef __has_builtin -#if __has_builtin(__builtin_add_overflow) -#define BUILTIN_ADD_OVERFLOW __builtin_add_overflow -#define BUILTIN_ADD_OVERFLOW_INT __builtin_add_overflow -#define BUILTIN_ADD_OVERFLOW_INT64 __builtin_add_overflow -#endif -#if __has_builtin(__builtin_sub_overflow) -#define BUILTIN_SUB_OVERFLOW __builtin_sub_overflow -#define BUILTIN_SUB_OVERFLOW_INT __builtin_sub_overflow -#define BUILTIN_SUB_OVERFLOW_INT64 __builtin_sub_overflow -#endif -#if __has_builtin(__builtin_mul_overflow) -#define BUILTIN_MUL_OVERFLOW __builtin_mul_overflow -#define BUILTIN_MUL_OVERFLOW_INT __builtin_mul_overflow -#define BUILTIN_MUL_OVERFLOW_INT64 __builtin_mul_overflow -#endif +# if __has_builtin(__builtin_add_overflow) +# define BUILTIN_ADD_OVERFLOW __builtin_add_overflow +# define BUILTIN_ADD_OVERFLOW_INT __builtin_add_overflow +# define BUILTIN_ADD_OVERFLOW_INT64 __builtin_add_overflow +# endif +# if __has_builtin(__builtin_sub_overflow) +# define BUILTIN_SUB_OVERFLOW __builtin_sub_overflow +# define BUILTIN_SUB_OVERFLOW_INT __builtin_sub_overflow +# define BUILTIN_SUB_OVERFLOW_INT64 __builtin_sub_overflow +# endif +# if __has_builtin(__builtin_mul_overflow) +# define BUILTIN_MUL_OVERFLOW __builtin_mul_overflow +# define BUILTIN_MUL_OVERFLOW_INT __builtin_mul_overflow +# define BUILTIN_MUL_OVERFLOW_INT64 __builtin_mul_overflow +# endif #endif #ifndef BUILTIN_ADD_OVERFLOW -#define BUILTIN_ADD_OVERFLOW atomvm_add_overflow -#define BUILTIN_ADD_OVERFLOW_INT atomvm_add_overflow_int -#define BUILTIN_ADD_OVERFLOW_INT64 atomvm_add_overflow_int64 +# define BUILTIN_ADD_OVERFLOW atomvm_add_overflow +# define BUILTIN_ADD_OVERFLOW_INT atomvm_add_overflow_int +# define BUILTIN_ADD_OVERFLOW_INT64 atomvm_add_overflow_int64 static inline int atomvm_add_overflow(avm_int_t a, avm_int_t b, avm_int_t *res) { @@ -91,11 +91,11 @@ static inline int atomvm_add_overflow_int64(avm_int64_t a, avm_int64_t b, avm_in #endif #ifndef BUILTIN_SUB_OVERFLOW -#define BUILTIN_SUB_OVERFLOW atomvm_sub_overflow -#define BUILTIN_SUB_OVERFLOW_INT atomvm_sub_overflow_int -#define BUILTIN_SUB_OVERFLOW_INT64 atomvm_sub_overflow_int64 +# define BUILTIN_SUB_OVERFLOW atomvm_sub_overflow +# define BUILTIN_SUB_OVERFLOW_INT atomvm_sub_overflow_int +# define BUILTIN_SUB_OVERFLOW_INT64 atomvm_sub_overflow_int64 -#include +# include static inline int atomvm_sub_overflow(avm_int_t a, avm_int_t b, avm_int_t *res) { @@ -121,9 +121,9 @@ static inline int atomvm_sub_overflow_int64(avm_int64_t a, avm_int64_t b, avm_in #endif #ifndef BUILTIN_MUL_OVERFLOW -#define BUILTIN_MUL_OVERFLOW atomvm_mul_overflow -#define BUILTIN_MUL_OVERFLOW_INT atomvm_mul_overflow_int -#define BUILTIN_MUL_OVERFLOW_INT64 atomvm_mul_overflow_int64 +# define BUILTIN_MUL_OVERFLOW atomvm_mul_overflow +# define BUILTIN_MUL_OVERFLOW_INT atomvm_mul_overflow_int +# define BUILTIN_MUL_OVERFLOW_INT64 atomvm_mul_overflow_int64 static inline int atomvm_mul_overflow_int(avm_int_t a, avm_int_t b, avm_int_t *res) { @@ -147,18 +147,18 @@ static inline int atomvm_mul_overflow_int64(avm_int64_t a, avm_int64_t b, avm_in static inline int atomvm_mul_overflow(avm_int_t a, avm_int_t b, avm_int_t *res) { -#if AVM_INT_MAX < INT64_MAX +# if AVM_INT_MAX < INT64_MAX avm_int64_t mul = (avm_int64_t) (a >> 2) * (avm_int64_t) (b >> 2); *res = mul << 4; return ((mul > MAX_NOT_BOXED_INT) || (mul < MIN_NOT_BOXED_INT)); -#elif AVM_INT_MAX == INT64_MAX +# elif AVM_INT_MAX == INT64_MAX int64_t mul; int ovf = atomvm_mul_overflow_int64(a >> 2, b >> 2, &mul); *res = mul << 4; return ovf || ((mul > MAX_NOT_BOXED_INT) || (mul < MIN_NOT_BOXED_INT)); -#else -#error "Unsupported AVM_INT_MAX size" -#endif +# else +# error "Unsupported AVM_INT_MAX size" +# endif } #endif diff --git a/src/libAtomVM/posix_nifs.c b/src/libAtomVM/posix_nifs.c index a40b79bf9..7036a2627 100644 --- a/src/libAtomVM/posix_nifs.c +++ b/src/libAtomVM/posix_nifs.c @@ -24,33 +24,33 @@ */ #if HAVE_OPEN && HAVE_CLOSE -#include +# include #endif #if HAVE_OPEN && HAVE_CLOSE || defined(HAVE_GETCWD) && defined(HAVE_PATH_MAX) -#include +# include #endif #if HAVE_MKFIFO -#include -#include +# include +# include #endif #if HAVE_CLOCK_SETTIME -#include +# include #elif HAVE_SETTIMEOFDAY -#include +# include #endif #if HAVE_OPEN && HAVE_CLOSE || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_SETTIMEOFDAY) \ || HAVE_OPENDIR && HAVE_READDIR && HAVE_CLOSEDIR || defined(HAVE_GETCWD) && defined(HAVE_PATH_MAX) -#include +# include #endif #if HAVE_OPENDIR && HAVE_READDIR && HAVE_CLOSEDIR -#include +# include #endif #if HAVE_POSIX_SPAWN_CLOEXEC_DEFAULT -#include +# include #endif #include "defaultatoms.h" @@ -159,7 +159,7 @@ static term errno_to_error_tuple_maybe_gc(Context *ctx) } #if HAVE_OPEN && HAVE_CLOSE -#define CLOSED_FD (-1) +# define CLOSED_FD (-1) struct PosixFd { @@ -209,25 +209,25 @@ const ErlNifResourceTypeInit posix_fd_resource_type_init = { .down = posix_fd_down, }; -#define O_EXEC_ATOM_STR ATOM_STR("\x6", "o_exec") -#define O_RDONLY_ATOM_STR ATOM_STR("\x8", "o_rdonly") -#define O_RDWR_ATOM_STR ATOM_STR("\x6", "o_rdwr") -#define O_SEARCH_ATOM_STR ATOM_STR("\x8", "o_search") -#define O_WRONLY_ATOM_STR ATOM_STR("\x8", "o_wronly") - -#define O_APPEND_ATOM_STR ATOM_STR("\x8", "o_append") -#define O_CLOEXEC_ATOM_STR ATOM_STR("\x9", "o_cloexec") -#define O_CREAT_ATOM_STR ATOM_STR("\x7", "o_creat") -#define O_DIRECTORY_ATOM_STR ATOM_STR("\xB", "o_directory") -#define O_DSYNC_ATOM_STR ATOM_STR("\x7", "o_dsync") -#define O_EXCL_ATOM_STR ATOM_STR("\x6", "o_excl") -#define O_NOCTTY_ATOM_STR ATOM_STR("\x8", "o_noctty") -#define O_NOFOLLOW_ATOM_STR ATOM_STR("\xA", "o_nofollow") +# define O_EXEC_ATOM_STR ATOM_STR("\x6", "o_exec") +# define O_RDONLY_ATOM_STR ATOM_STR("\x8", "o_rdonly") +# define O_RDWR_ATOM_STR ATOM_STR("\x6", "o_rdwr") +# define O_SEARCH_ATOM_STR ATOM_STR("\x8", "o_search") +# define O_WRONLY_ATOM_STR ATOM_STR("\x8", "o_wronly") + +# define O_APPEND_ATOM_STR ATOM_STR("\x8", "o_append") +# define O_CLOEXEC_ATOM_STR ATOM_STR("\x9", "o_cloexec") +# define O_CREAT_ATOM_STR ATOM_STR("\x7", "o_creat") +# define O_DIRECTORY_ATOM_STR ATOM_STR("\xB", "o_directory") +# define O_DSYNC_ATOM_STR ATOM_STR("\x7", "o_dsync") +# define O_EXCL_ATOM_STR ATOM_STR("\x6", "o_excl") +# define O_NOCTTY_ATOM_STR ATOM_STR("\x8", "o_noctty") +# define O_NOFOLLOW_ATOM_STR ATOM_STR("\xA", "o_nofollow") // #define O_NONBLOCK_ATOM_STR ATOM_STR("\xA", "o_nonblock") -#define O_RSYNC_ATOM_STR ATOM_STR("\x8", "o_rsync") -#define O_SYNC_ATOM_STR ATOM_STR("\x7", "o_sync") -#define O_TRUNC_ATOM_STR ATOM_STR("\x8", "o_trunc") -#define O_TTY_INIT_ATOM_STR ATOM_STR("\xA", "o_tty_init") +# define O_RSYNC_ATOM_STR ATOM_STR("\x8", "o_rsync") +# define O_SYNC_ATOM_STR ATOM_STR("\x7", "o_sync") +# define O_TRUNC_ATOM_STR ATOM_STR("\x8", "o_trunc") +# define O_TTY_INIT_ATOM_STR ATOM_STR("\xA", "o_tty_init") static term make_posix_fd_resource(Context *ctx, int fd) { @@ -275,38 +275,38 @@ static term nif_atomvm_posix_open(Context *ctx, int argc, term argv[]) } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_TRUNC_ATOM_STR)) { posix_flags |= O_TRUNC; // SUSv4/2018 edition flags -#if HAVE_O_EXEC +# if HAVE_O_EXEC } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_EXEC_ATOM_STR)) { posix_flags |= O_EXEC; -#endif -#if HAVE_O_SEARCH +# endif +# if HAVE_O_SEARCH } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_SEARCH_ATOM_STR)) { posix_flags |= O_SEARCH; -#endif -#if HAVE_O_CLOEXEC +# endif +# if HAVE_O_CLOEXEC } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_CLOEXEC_ATOM_STR)) { posix_flags |= O_CLOEXEC; -#endif -#if HAVE_O_DIRECTORY +# endif +# if HAVE_O_DIRECTORY } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_DIRECTORY_ATOM_STR)) { posix_flags |= O_DIRECTORY; -#endif -#if HAVE_O_DSYNC +# endif +# if HAVE_O_DSYNC } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_DSYNC_ATOM_STR)) { posix_flags |= O_DSYNC; -#endif -#if HAVE_O_NOFOLLOW +# endif +# if HAVE_O_NOFOLLOW } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_NOFOLLOW_ATOM_STR)) { posix_flags |= O_NOFOLLOW; -#endif -#if HAVE_O_RSYNC +# endif +# if HAVE_O_RSYNC } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_RSYNC_ATOM_STR)) { posix_flags |= O_RSYNC; -#endif -#if HAVE_O_TTY_INIT +# endif +# if HAVE_O_TTY_INIT } else if (globalcontext_is_term_equal_to_atom_string(glb, flag, O_TTY_INIT_ATOM_STR)) { posix_flags |= O_TTY_INIT; -#endif +# endif } else { RAISE_ERROR(BADARG_ATOM); } @@ -521,7 +521,7 @@ static term nif_atomvm_posix_select_stop(Context *ctx, int argc, term argv[]) return OK_ATOM; } -#if HAVE_EXECVE +# if HAVE_EXECVE static void free_string_list(char **list) { if (IS_NULL_PTR(list)) { @@ -603,7 +603,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[]) return errno_to_error_tuple_maybe_gc(ctx); } pid_t pid; -#if HAVE_POSIX_SPAWN_CLOEXEC_DEFAULT +# if HAVE_POSIX_SPAWN_CLOEXEC_DEFAULT do { posix_spawn_file_actions_t file_actions; posix_spawnattr_t spawn_attrs; @@ -637,7 +637,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[]) close(pstdout[1]); return error_tuple_maybe_gc(r, ctx); } -#else +# else r = fork(); if (r < 0) { int err = errno; @@ -653,19 +653,19 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[]) close(0); // close stdin of the child close(pstdout[0]); // close read end of the pipe dup2(pstdout[1], 1); // make stdout the write-end of the pipe -#if HAVE_CLOSEFROM +# if HAVE_CLOSEFROM closefrom(2); -#else +# else int maxfd = sysconf(_SC_OPEN_MAX); for (int fd = 3; fd < maxfd; fd++) { close(fd); } -#endif +# endif execve(path, args, envp); exit(1); } pid = r; -#endif +# endif // parent close(pstdout[1]); // close write-end of the pipe free(path); @@ -687,7 +687,7 @@ static term nif_atomvm_subprocess(Context *ctx, int argc, term argv[]) return result; } -#endif +# endif #endif #if HAVE_MKFIFO @@ -765,14 +765,14 @@ static term nif_atomvm_posix_clock_settime(Context *ctx, int argc, term argv[]) VALIDATE_VALUE(nsecs, term_is_any_integer); avm_int64_t ns = term_maybe_unbox_int64(nsecs); -#ifdef HAVE_CLOCK_SETTIME +# ifdef HAVE_CLOCK_SETTIME struct timespec tp = { .tv_sec = s, .tv_nsec = ns }; int res = clock_settime(CLOCK_REALTIME, &tp); -#else +# else // Use settimeofday as a fallback struct timeval tv = { .tv_sec = s, @@ -780,7 +780,7 @@ static term nif_atomvm_posix_clock_settime(Context *ctx, int argc, term argv[]) }; int res = settimeofday(&tv, NULL); -#endif +# endif if (res != 0) { if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2)) != MEMORY_GC_OK)) { RAISE_ERROR(OUT_OF_MEMORY_ATOM); @@ -1014,12 +1014,12 @@ const struct Nif atomvm_posix_select_stop_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_posix_select_stop }; -#if HAVE_EXECVE +# if HAVE_EXECVE const struct Nif atomvm_subprocess_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_subprocess }; -#endif +# endif #endif #if HAVE_MKFIFO const struct Nif atomvm_posix_mkfifo_nif = { diff --git a/src/libAtomVM/posix_nifs.h b/src/libAtomVM/posix_nifs.h index 9a88997c2..41690109f 100644 --- a/src/libAtomVM/posix_nifs.h +++ b/src/libAtomVM/posix_nifs.h @@ -43,9 +43,9 @@ extern const struct Nif atomvm_posix_write_nif; extern const struct Nif atomvm_posix_select_read_nif; extern const struct Nif atomvm_posix_select_write_nif; extern const struct Nif atomvm_posix_select_stop_nif; -#if HAVE_EXECVE +# if HAVE_EXECVE extern const struct Nif atomvm_subprocess_nif; -#endif +# endif #endif #if HAVE_MKFIFO extern const struct Nif atomvm_posix_mkfifo_nif; diff --git a/src/libAtomVM/refc_binary.h b/src/libAtomVM/refc_binary.h index 3fc1784bd..3ac4030f6 100644 --- a/src/libAtomVM/refc_binary.h +++ b/src/libAtomVM/refc_binary.h @@ -28,14 +28,14 @@ #include "resources.h" #ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" +# include "platform_atomic.h" #endif #if defined(HAVE_ATOMIC) && !defined(__cplusplus) -#include -#define ATOMIC _Atomic +# include +# define ATOMIC _Atomic #else -#define ATOMIC +# define ATOMIC #endif #ifdef __cplusplus @@ -43,12 +43,12 @@ extern "C" { #endif #ifndef TYPEDEF_CONTEXT -#define TYPEDEF_CONTEXT +# define TYPEDEF_CONTEXT typedef struct Context Context; #endif #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif diff --git a/src/libAtomVM/resources.h b/src/libAtomVM/resources.h index 958396ede..040aaa8c8 100644 --- a/src/libAtomVM/resources.h +++ b/src/libAtomVM/resources.h @@ -34,7 +34,7 @@ extern "C" { #endif #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif diff --git a/src/libAtomVM/smp.h b/src/libAtomVM/smp.h index 0f9fa6aa4..306ae9e91 100644 --- a/src/libAtomVM/smp.h +++ b/src/libAtomVM/smp.h @@ -30,76 +30,76 @@ #define _SMP_H_ #if defined(__has_feature) -#if __has_feature(thread_sanitizer) -#define CLANG_THREAD_SANITIZE_SAFE __attribute__((no_sanitize("thread"))) -#endif +# if __has_feature(thread_sanitizer) +# define CLANG_THREAD_SANITIZE_SAFE __attribute__((no_sanitize("thread"))) +# endif #endif #ifndef CLANG_THREAD_SANITIZE_SAFE -#define CLANG_THREAD_SANITIZE_SAFE +# define CLANG_THREAD_SANITIZE_SAFE #endif #ifndef AVM_NO_SMP -#include +# include -#ifdef HAVE_PLATFORM_SMP_H -#include "platform_smp.h" -#endif +# ifdef HAVE_PLATFORM_SMP_H +# include "platform_smp.h" +# endif -#ifdef HAVE_PLATFORM_ATOMIC_H -#include "platform_atomic.h" -#else -#if defined(HAVE_ATOMIC) -#include -#define ATOMIC_COMPARE_EXCHANGE_WEAK_INT atomic_compare_exchange_weak -#endif -#endif +# ifdef HAVE_PLATFORM_ATOMIC_H +# include "platform_atomic.h" +# else +# if defined(HAVE_ATOMIC) +# include +# define ATOMIC_COMPARE_EXCHANGE_WEAK_INT atomic_compare_exchange_weak +# endif +# endif // spinlocks are implemented using atomics -#if !defined(SMP_PLATFORM_SPINLOCK) -#if defined(HAVE_ATOMIC) && !defined(__cplusplus) -#include -#define ATOMIC _Atomic -#else -#define ATOMIC -#endif -#endif - -#ifdef __cplusplus +# if !defined(SMP_PLATFORM_SPINLOCK) +# if defined(HAVE_ATOMIC) && !defined(__cplusplus) +# include +# define ATOMIC _Atomic +# else +# define ATOMIC +# endif +# endif + +# ifdef __cplusplus extern "C" { -#endif +# endif -#ifndef TYPEDEF_MUTEX -#define TYPEDEF_MUTEX +# ifndef TYPEDEF_MUTEX +# define TYPEDEF_MUTEX typedef struct Mutex Mutex; -#endif +# endif -#ifndef TYPEDEF_SPINLOCK -#define TYPEDEF_SPINLOCK +# ifndef TYPEDEF_SPINLOCK +# define TYPEDEF_SPINLOCK typedef struct SpinLock SpinLock; -#endif +# endif -#ifndef TYPEDEF_CONDVAR -#define TYPEDEF_CONDVAR +# ifndef TYPEDEF_CONDVAR +# define TYPEDEF_CONDVAR typedef struct CondVar CondVar; -#endif +# endif -#ifndef TYPEDEF_RWLOCK -#define TYPEDEF_RWLOCK +# ifndef TYPEDEF_RWLOCK +# define TYPEDEF_RWLOCK typedef struct RWLock RWLock; -#endif +# endif -#ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# ifndef TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; -#endif +# endif -#if !defined(SMP_PLATFORM_SPINLOCK) +# if !defined(SMP_PLATFORM_SPINLOCK) struct SpinLock { int ATOMIC lock; }; -#endif +# endif /** * @brief Create a new mutex. @@ -196,7 +196,7 @@ void smp_rwlock_wrlock(RWLock *lock); */ void smp_rwlock_unlock(RWLock *lock); -#if !defined(__cplusplus) && !defined(SMP_PLATFORM_SPINLOCK) +# if !defined(__cplusplus) && !defined(SMP_PLATFORM_SPINLOCK) /** * @brief Initialize a spinlock based on atomics. @@ -239,7 +239,7 @@ static inline void smp_spinlock_unlock(SpinLock *lock) lock->lock = 0; } -#endif +# endif /** * @brief Get the number of online processors to configure schedulers. @@ -262,33 +262,33 @@ void smp_scheduler_start(GlobalContext *glb); */ bool smp_is_main_thread(GlobalContext *glb); -#define SMP_SPINLOCK_LOCK(spinlock) smp_spinlock_lock(spinlock) -#define SMP_SPINLOCK_TRYLOCK(spinlock) smp_spinlock_trylock(spinlock) -#define SMP_SPINLOCK_UNLOCK(spinlock) smp_spinlock_unlock(spinlock) -#define SMP_MUTEX_LOCK(mutex) smp_mutex_lock(mutex) -#define SMP_MUTEX_TRYLOCK(mutex) smp_mutex_trylock(mutex) -#define SMP_MUTEX_UNLOCK(mutex) smp_mutex_unlock(mutex) -#define SMP_RWLOCK_RDLOCK(lock) smp_rwlock_rdlock(lock) -#define SMP_RWLOCK_TRYRDLOCK(lock) smp_rwlock_tryrdlock(lock) -#define SMP_RWLOCK_WRLOCK(lock) smp_rwlock_wrlock(lock) -#define SMP_RWLOCK_UNLOCK(lock) smp_rwlock_unlock(lock) - -#ifdef __cplusplus +# define SMP_SPINLOCK_LOCK(spinlock) smp_spinlock_lock(spinlock) +# define SMP_SPINLOCK_TRYLOCK(spinlock) smp_spinlock_trylock(spinlock) +# define SMP_SPINLOCK_UNLOCK(spinlock) smp_spinlock_unlock(spinlock) +# define SMP_MUTEX_LOCK(mutex) smp_mutex_lock(mutex) +# define SMP_MUTEX_TRYLOCK(mutex) smp_mutex_trylock(mutex) +# define SMP_MUTEX_UNLOCK(mutex) smp_mutex_unlock(mutex) +# define SMP_RWLOCK_RDLOCK(lock) smp_rwlock_rdlock(lock) +# define SMP_RWLOCK_TRYRDLOCK(lock) smp_rwlock_tryrdlock(lock) +# define SMP_RWLOCK_WRLOCK(lock) smp_rwlock_wrlock(lock) +# define SMP_RWLOCK_UNLOCK(lock) smp_rwlock_unlock(lock) + +# ifdef __cplusplus } -#endif +# endif #else -#define SMP_SPINLOCK_LOCK(spinlock) -#define SMP_SPINLOCK_TRYLOCK(spinlock) -#define SMP_SPINLOCK_UNLOCK(spinlock) -#define SMP_MUTEX_LOCK(mutex) -#define SMP_MUTEX_TRYLOCK(mutex) -#define SMP_MUTEX_UNLOCK(mutex) -#define SMP_RWLOCK_RDLOCK(lock) -#define SMP_RWLOCK_TRYRDLOCK(lock) -#define SMP_RWLOCK_WRLOCK(lock) -#define SMP_RWLOCK_UNLOCK(lock) +# define SMP_SPINLOCK_LOCK(spinlock) +# define SMP_SPINLOCK_TRYLOCK(spinlock) +# define SMP_SPINLOCK_UNLOCK(spinlock) +# define SMP_MUTEX_LOCK(mutex) +# define SMP_MUTEX_TRYLOCK(mutex) +# define SMP_MUTEX_UNLOCK(mutex) +# define SMP_RWLOCK_RDLOCK(lock) +# define SMP_RWLOCK_TRYRDLOCK(lock) +# define SMP_RWLOCK_WRLOCK(lock) +# define SMP_RWLOCK_UNLOCK(lock) #endif #endif diff --git a/src/libAtomVM/stacktrace.c b/src/libAtomVM/stacktrace.c index 1f38e1ca8..447316e3f 100644 --- a/src/libAtomVM/stacktrace.c +++ b/src/libAtomVM/stacktrace.c @@ -68,12 +68,12 @@ static bool location_sets_append(GlobalContext *global, Module *mod, const uint8 const void **new_locations_set = realloc(locations_set, (locations_set_size + 1) * sizeof(const uint8_t *)); if (IS_NULL_PTR(new_locations_set)) { // Some versions of gcc don't know that if allocation fails, original pointer should still be freed -#pragma GCC diagnostic push -#if (defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12) -#pragma GCC diagnostic ignored "-Wuse-after-free" -#endif +# pragma GCC diagnostic push +# if (defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12) +# pragma GCC diagnostic ignored "-Wuse-after-free" +# endif free(locations_set); -#pragma GCC diagnostic pop +# pragma GCC diagnostic pop *io_locations_set = NULL; fprintf(stderr, "Unable to allocate space for locations set. No stacktrace will be created\n"); return false; diff --git a/src/libAtomVM/synclist.h b/src/libAtomVM/synclist.h index c4dd2ca57..ec9e23a4f 100644 --- a/src/libAtomVM/synclist.h +++ b/src/libAtomVM/synclist.h @@ -27,16 +27,16 @@ #ifndef AVM_NO_SMP -#include "smp.h" +# include "smp.h" -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif -#ifndef TYPEDEF_RWLOCK -#define TYPEDEF_RWLOCK +# ifndef TYPEDEF_RWLOCK +# define TYPEDEF_RWLOCK typedef struct RWLock RWLock; -#endif +# endif struct SyncList { @@ -115,24 +115,24 @@ static inline bool synclist_is_empty(struct SyncList *synclist) return result; } -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif #else -#define SyncList ListHead -#define synclist_init(list) list_init(list) -#define synclist_rdlock(list) list -#define synclist_tryrdlock(list) list -#define synclist_wrlock(list) list -#define synclist_nolock(list) list -#define synclist_unlock(list) UNUSED(list) -#define synclist_destroy(list) UNUSED(list) -#define synclist_prepend(list, new_item) list_prepend(list, new_item) -#define synclist_append(list, new_item) list_append(list, new_item) -#define synclist_remove(list, new_item) list_remove(new_item) -#define synclist_is_empty(list) list_is_empty(list) +# define SyncList ListHead +# define synclist_init(list) list_init(list) +# define synclist_rdlock(list) list +# define synclist_tryrdlock(list) list +# define synclist_wrlock(list) list +# define synclist_nolock(list) list +# define synclist_unlock(list) UNUSED(list) +# define synclist_destroy(list) UNUSED(list) +# define synclist_prepend(list, new_item) list_prepend(list, new_item) +# define synclist_append(list, new_item) list_append(list, new_item) +# define synclist_remove(list, new_item) list_remove(new_item) +# define synclist_is_empty(list) list_is_empty(list) #endif diff --git a/src/libAtomVM/term.c b/src/libAtomVM/term.c index 29e3a92af..a626d7722 100644 --- a/src/libAtomVM/term.c +++ b/src/libAtomVM/term.c @@ -650,7 +650,7 @@ TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalC } } #else -#error "Unsupported endianness" +# error "Unsupported endianness" #endif } CMP_POP_AND_CONTINUE(); @@ -1059,7 +1059,7 @@ term term_reuse_binary(term src, size_t size, Heap *heap, GlobalContext *glb) // original pointer is still valid #pragma GCC diagnostic push #if (defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12) -#pragma GCC diagnostic ignored "-Wuse-after-free" +# pragma GCC diagnostic ignored "-Wuse-after-free" #endif list_append(refc_binaries, &old_refc->head); #pragma GCC diagnostic pop diff --git a/src/libAtomVM/term.h b/src/libAtomVM/term.h index 6156aec10..9d1984ccd 100644 --- a/src/libAtomVM/term.h +++ b/src/libAtomVM/term.h @@ -130,13 +130,13 @@ extern "C" { #define TERM_BOXED_SUB_BINARY_SIZE 4 #define TERM_BOXED_RESOURCE_SIZE TERM_BOXED_REFC_BINARY_SIZE #if TERM_BYTES == 8 -#define REFC_BINARY_MIN 64 -#define SUB_BINARY_MIN 16 +# define REFC_BINARY_MIN 64 +# define SUB_BINARY_MIN 16 #elif TERM_BYTES == 4 -#define REFC_BINARY_MIN 32 -#define SUB_BINARY_MIN 8 +# define REFC_BINARY_MIN 32 +# define SUB_BINARY_MIN 8 #else -#error +# error #endif #define TERM_MAX_LOCAL_PROCESS_ID ((1 << 28) - 1) @@ -149,19 +149,19 @@ extern "C" { #define FLOAT_SIZE (sizeof(float_term_t) / sizeof(term) + 1) #define REF_SIZE ((int) ((sizeof(uint64_t) / sizeof(term)) + 1)) #if TERM_BYTES == 8 -#define EXTERNAL_PID_SIZE 3 +# define EXTERNAL_PID_SIZE 3 #elif TERM_BYTES == 4 -#define EXTERNAL_PID_SIZE 5 +# define EXTERNAL_PID_SIZE 5 #else -#error +# error #endif #define EXTERNAL_PORT_SIZE EXTERNAL_PID_SIZE #if TERM_BYTES == 8 -#define EXTERNAL_REF_SIZE(words) (3 + (words / 2)) +# define EXTERNAL_REF_SIZE(words) (3 + (words / 2)) #elif TERM_BYTES == 4 -#define EXTERNAL_REF_SIZE(words) (3 + words) +# define EXTERNAL_REF_SIZE(words) (3 + words) #else -#error +# error #endif #define TUPLE_SIZE(elems) ((int) (elems + 1)) #define CONS_SIZE 2 @@ -177,11 +177,11 @@ extern "C" { #define TERM_BINARY_SIZE_IS_HEAP(size) ((size) < REFC_BINARY_MIN) #if TERM_BYTES == 4 -#define TERM_BINARY_DATA_SIZE_IN_TERMS(size) \ - (TERM_BINARY_SIZE_IS_HEAP(size) ? (((size) + 4 - 1) >> 2) + 1 : TERM_BOXED_REFC_BINARY_SIZE) +# define TERM_BINARY_DATA_SIZE_IN_TERMS(size) \ + (TERM_BINARY_SIZE_IS_HEAP(size) ? (((size) + 4 - 1) >> 2) + 1 : TERM_BOXED_REFC_BINARY_SIZE) #elif TERM_BYTES == 8 -#define TERM_BINARY_DATA_SIZE_IN_TERMS(size) \ - (TERM_BINARY_SIZE_IS_HEAP(size) ? (((size) + 8 - 1) >> 3) + 1 : TERM_BOXED_REFC_BINARY_SIZE) +# define TERM_BINARY_DATA_SIZE_IN_TERMS(size) \ + (TERM_BINARY_SIZE_IS_HEAP(size) ? (((size) + 8 - 1) >> 3) + 1 : TERM_BOXED_REFC_BINARY_SIZE) #endif #define TERM_BINARY_HEAP_SIZE(size) \ @@ -231,7 +231,7 @@ extern "C" { #define PORT_AS_CSTRING_LEN 37 #ifndef TYPEDEF_GLOBALCONTEXT -#define TYPEDEF_GLOBALCONTEXT +# define TYPEDEF_GLOBALCONTEXT typedef struct GlobalContext GlobalContext; #endif @@ -1059,7 +1059,7 @@ static inline term term_from_int32(int32_t value) return (value << 4) | TERM_INTEGER_TAG; #else -#error "Wrong TERM_BITS define" +# error "Wrong TERM_BITS define" #endif } @@ -1088,7 +1088,7 @@ static inline term term_from_int64(int64_t value) } #else -#error "Wrong TERM_BITS define" +# error "Wrong TERM_BITS define" #endif } @@ -1210,15 +1210,15 @@ static inline avm_int64_t term_unbox_int64(term boxed_long) return (avm_int64_t) boxed_value[1]; #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 2 -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ return (avm_int64_t) ((avm_uint64_t) boxed_value[1] | ((avm_uint64_t) boxed_value[2] << 32)); -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ return (avm_int64_t) ((avm_uint64_t) boxed_value[1] << 32) | (avm_uint64_t) boxed_value[2]; +# else +# error "unsupported endianness." +# endif #else -#error "unsupported endianness." -#endif -#else -#error "unsupported configuration." +# error "unsupported configuration." #endif } @@ -1267,17 +1267,17 @@ static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap) #if BOXED_TERMS_REQUIRED_FOR_INT64 == 1 boxed_int[1] = large_int64; #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 2 -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ boxed_int[1] = large_int64; boxed_int[2] = large_int64 >> 32; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ boxed_int[2] = large_int64; boxed_int[1] = large_int64 >> 32; +# else +# error "unsupported endianness." +# endif #else -#error "unsupported endianness." -#endif -#else -#error "unsupported configuration." +# error "unsupported configuration." #endif return ((term) boxed_int) | TERM_PRIMARY_BOXED; } @@ -1574,7 +1574,7 @@ static inline size_t term_binary_data_size_in_terms(size_t size) #elif TERM_BYTES == 8 return ((size + 8 - 1) >> 3) + 1; #else -#error +# error #endif } else { return TERM_BOXED_REFC_BINARY_SIZE; @@ -1807,7 +1807,7 @@ static inline bool term_is_nomatch_binary_pos_len(BinaryPosLen pos_len) static inline BinaryPosLen term_nomatch_binary_pos_len(void) { - return (BinaryPosLen){ .pos = -1, .len = -1 }; + return (BinaryPosLen) { .pos = -1, .len = -1 }; } /** @@ -1871,7 +1871,7 @@ static inline term term_from_ref_ticks(uint64_t ref_ticks, Heap *heap) boxed_value[2] = (ref_ticks & 0xFFFFFFFF); #else -#error "terms must be either 32 or 64 bit wide" +# error "terms must be either 32 or 64 bit wide" #endif return ((term) boxed_value) | TERM_PRIMARY_BOXED; @@ -1890,7 +1890,7 @@ static inline uint64_t term_to_ref_ticks(term rt) return (boxed_value[1] << 4) | boxed_value[2]; #else -#error "terms must be either 32 or 64 bit wide" +# error "terms must be either 32 or 64 bit wide" #endif } @@ -2055,7 +2055,7 @@ static inline term term_make_external_reference(term node, uint16_t len, uint32_ } #else -#error "terms must be either 32 or 64 bit wide" +# error "terms must be either 32 or 64 bit wide" #endif return ((term) boxed_value) | TERM_PRIMARY_BOXED; @@ -2081,7 +2081,7 @@ static inline uint32_t term_get_external_reference_len(term t) return (uint32_t) (boxed_value[0] >> 6) - 2; #else -#error "terms must be either 32 or 64 bit wide" +# error "terms must be either 32 or 64 bit wide" #endif } @@ -2105,7 +2105,7 @@ static inline const uint32_t *term_get_external_reference_words(term t) return external_thing_words + 2; #else -#error "terms must be either 32 or 64 bit wide" +# error "terms must be either 32 or 64 bit wide" #endif } diff --git a/src/libAtomVM/term_typedef.h b/src/libAtomVM/term_typedef.h index be4ce9bcc..05a67e31b 100644 --- a/src/libAtomVM/term_typedef.h +++ b/src/libAtomVM/term_typedef.h @@ -29,10 +29,10 @@ #define _TERM_TYPEDEF_H_ #ifdef __cplusplus -#include +# include #else -#include -#include +# include +# include #endif #include #include @@ -47,19 +47,19 @@ typedef uintptr_t term; #if ((UINT32_MAX != 4294967295ULL) || (UINT64_MAX != 18446744073709551615ULL) \ || (INT32_MAX != 2147483647LL) || (INT64_MAX != 9223372036854775807LL)) -#error "limits.h or preprocessor is not sane." +# error "limits.h or preprocessor is not sane." #endif #if UINTPTR_MAX == UINT32_MAX -#define TERM_BITS 32 -#define TERM_BYTES 4 +# define TERM_BITS 32 +# define TERM_BYTES 4 #elif UINTPTR_MAX == UINT64_MAX -#define TERM_BITS 64 -#define TERM_BYTES 8 +# define TERM_BITS 64 +# define TERM_BYTES 8 #else -#error "Term size must be either 32 bit or 64 bit." +# error "Term size must be either 32 bit or 64 bit." #endif typedef intptr_t avm_int_t; @@ -69,21 +69,21 @@ typedef int64_t avm_int64_t; typedef uint64_t avm_uint64_t; #if UINTPTR_MAX == UINT32_MAX -#define AVM_INT_MIN INT32_MIN -#define AVM_INT_MAX INT32_MAX -#define INT64_IS_ALWAYS_BOXED 1 -#define BOXED_TERMS_REQUIRED_FOR_INT 1 -#define BOXED_TERMS_REQUIRED_FOR_INT64 2 +# define AVM_INT_MIN INT32_MIN +# define AVM_INT_MAX INT32_MAX +# define INT64_IS_ALWAYS_BOXED 1 +# define BOXED_TERMS_REQUIRED_FOR_INT 1 +# define BOXED_TERMS_REQUIRED_FOR_INT64 2 #elif UINTPTR_MAX == UINT64_MAX -#define AVM_INT_MIN INT64_MIN -#define AVM_INT_MAX INT64_MAX -#define INT64_IS_ALWAYS_BOXED 0 -#define BOXED_TERMS_REQUIRED_FOR_INT 1 -#define BOXED_TERMS_REQUIRED_FOR_INT64 1 +# define AVM_INT_MIN INT64_MIN +# define AVM_INT_MAX INT64_MAX +# define INT64_IS_ALWAYS_BOXED 0 +# define BOXED_TERMS_REQUIRED_FOR_INT 1 +# define BOXED_TERMS_REQUIRED_FOR_INT64 1 #else -#error "term size must be either 32 bit or 64 bit." +# error "term size must be either 32 bit or 64 bit." #endif #define UNICODE_CHAR_MAX 0x10FFFF @@ -92,46 +92,46 @@ typedef uint64_t avm_uint64_t; #define MAX_NOT_BOXED_INT (AVM_INT_MAX >> 4) #if AVM_INT_MAX == INT_MAX -#define AVM_INT_FMT "%i" +# define AVM_INT_FMT "%i" #elif AVM_INT_MAX == LONG_MAX -#define AVM_INT_FMT "%li" +# define AVM_INT_FMT "%li" #elif AVM_INT_MAX == LLONG_INT_MAX -#define AVM_INT_FMT "%lli" +# define AVM_INT_FMT "%lli" #else -#error "cannot define AVM_INT_MAX: invalid build env." +# error "cannot define AVM_INT_MAX: invalid build env." #endif #if INT64_MAX == INT_MAX -#define AVM_INT64_FMT "%i" +# define AVM_INT64_FMT "%i" #elif INT64_MAX == LONG_MAX -#if defined(__clang__) && defined(__APPLE__) -#define AVM_INT64_FMT "%lli" -#else -#define AVM_INT64_FMT "%li" -#endif +# if defined(__clang__) && defined(__APPLE__) +# define AVM_INT64_FMT "%lli" +# else +# define AVM_INT64_FMT "%li" +# endif #elif INT64_MAX == LLONG_MAX -#define AVM_INT64_FMT "%lli" +# define AVM_INT64_FMT "%lli" #else -#error "cannot define AVM_INT64_FMT: invalid build env." +# error "cannot define AVM_INT64_FMT: invalid build env." #endif // %f and %lf are the same since C99 when using printf // this is not true for scanf. #ifdef AVM_USE_SINGLE_PRECISION typedef float avm_float_t; -#define AVM_FLOAT_FMT "%f" +# define AVM_FLOAT_FMT "%f" -#ifndef __cplusplus +# ifndef __cplusplus _Static_assert(sizeof(avm_float_t) == 4, "avm_float_t must be a 32-bit float"); -#endif +# endif #else typedef double avm_float_t; -#define AVM_FLOAT_FMT "%lf" +# define AVM_FLOAT_FMT "%lf" -#ifndef __cplusplus +# ifndef __cplusplus _Static_assert(sizeof(avm_float_t) == 8, "avm_float_t must be a 64-bit float"); -#endif +# endif #endif diff --git a/src/libAtomVM/trace.h b/src/libAtomVM/trace.h index 511ef3502..a3f1b0541 100644 --- a/src/libAtomVM/trace.h +++ b/src/libAtomVM/trace.h @@ -22,13 +22,13 @@ #define _TRACE_H_ #ifndef TRACE -#ifdef ENABLE_TRACE -#define TRACE printf -#define DEBUG_FAIL_NULL(expr) assert((expr) != NULL) -#else -#define TRACE(...) -#define DEBUG_FAIL_NULL(expr) -#endif +# ifdef ENABLE_TRACE +# define TRACE printf +# define DEBUG_FAIL_NULL(expr) assert((expr) != NULL) +# else +# define TRACE(...) +# define DEBUG_FAIL_NULL(expr) +# endif #endif #define USED_BY_TRACE(x) \ diff --git a/src/libAtomVM/utils.c b/src/libAtomVM/utils.c index cfb1aea96..7f9f25ceb 100644 --- a/src/libAtomVM/utils.c +++ b/src/libAtomVM/utils.c @@ -30,12 +30,12 @@ #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #if INTPTR_MAX == 2147483647 // INT32_MAX -#define INTPTR_MAX_BASE_10_DIGITS 10 -#define INTPTR_MAX_BASE_16_DIGITS 8 +# define INTPTR_MAX_BASE_10_DIGITS 10 +# define INTPTR_MAX_BASE_16_DIGITS 8 #elif INTPTR_MAX == 9223372036854775807 // INT64_MAX -#define INTPTR_MAX_BASE_10_DIGITS 19 -#define INTPTR_MAX_BASE_16_DIGITS 16 +# define INTPTR_MAX_BASE_10_DIGITS 19 +# define INTPTR_MAX_BASE_16_DIGITS 16 #endif #define INT64_MAX_BASE_10_DIGITS 19 @@ -316,7 +316,7 @@ static int buf10_to_int64( return pos; #else -#error "INTPTR_MAX is not either a 32 or 64 bit signed integer" +# error "INTPTR_MAX is not either a 32 or 64 bit signed integer" #endif } @@ -400,7 +400,7 @@ static int buf16_to_int64( return pos; #else -#error "INTPTR_MAX is not either a 32 or 64 bit signed integer" +# error "INTPTR_MAX is not either a 32 or 64 bit signed integer" #endif } diff --git a/src/libAtomVM/utils.h b/src/libAtomVM/utils.h index 76bec7769..0d0225774 100644 --- a/src/libAtomVM/utils.h +++ b/src/libAtomVM/utils.h @@ -39,166 +39,176 @@ extern "C" { #endif #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -#ifdef __GNUC__ -#define READ_32_ALIGNED(ptr) \ - __builtin_bswap32(*((uint32_t *) (ptr))) -#else -#define READ_32_ALIGNED(ptr) \ - ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) -#endif - -#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) || defined(__aarch64__) || defined(_M_ARM64) -#define READ_64_UNALIGNED(ptr) \ - __builtin_bswap64(*((uint64_t *) (ptr))) - -#define WRITE_64_UNALIGNED(ptr, val) \ - *((uint64_t *) (ptr)) = __builtin_bswap64(val) - -#define READ_32_UNALIGNED(ptr) \ - __builtin_bswap32(*((uint32_t *) (ptr))) - -#define WRITE_32_UNALIGNED(ptr, val) \ - *((uint32_t *) (ptr)) = __builtin_bswap32(val) - -#define READ_16_UNALIGNED(ptr) \ - __builtin_bswap16(*((uint16_t *) (ptr))) - -#define WRITE_16_UNALIGNED(ptr, val) \ - *((uint16_t *) (ptr)) = __builtin_bswap16(val) - -#else -#define READ_64_UNALIGNED(ptr) \ - ((((uint64_t) ((uint8_t *) (ptr))[0]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[1]) << 48) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[3]) << 32) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[5]) << 16) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[7]))) - -#define WRITE_64_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint64_t) val) >> 56) & 0xff; \ - ((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 48) & 0xff; \ - ((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 40) & 0xff; \ - ((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 32) & 0xff; \ - ((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 24) & 0xff; \ - ((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 16) & 0xff; \ - ((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[7] = ((uint64_t) val) & 0xff; \ - } - -#define READ_32_UNALIGNED(ptr) \ - ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) - -#define WRITE_32_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint32_t) val) >> 24) & 0xff; \ - ((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 16) & 0xff; \ - ((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[3] = ((uint32_t) val) & 0xff; \ - } - -#define READ_16_UNALIGNED(ptr) \ - ((((uint8_t *) (ptr))[0] << 8) | ((uint8_t *) (ptr))[1]) - -#define WRITE_16_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint16_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[1] = ((uint16_t) val) & 0xff; \ - } -#endif - -#ifdef __GNUC__ -#define ENDIAN_SWAP_32(value) __builtin_bswap32(value) -#else -#define ENDIAN_SWAP_32(value) ((((value) &0xFF) << 24) | (((value) &0xFF00) << 8) | (((value) &0xFF0000) >> 8) | (((value) &0xFF000000) >> 24)) -#endif - -#ifdef __GNUC__ -#define ENDIAN_SWAP_16(value) __builtin_bswap16(value) -#else -#define ENDIAN_SWAP_16(value) ((((value) &0xFF) << 8) | (((value) &0xFF00) >> 8)) -#endif +# ifdef __GNUC__ +# define READ_32_ALIGNED(ptr) \ + __builtin_bswap32(*((uint32_t *) (ptr))) +# else +# define READ_32_ALIGNED(ptr) \ + ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) +# endif + +# if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) || defined(__aarch64__) || defined(_M_ARM64) +# define READ_64_UNALIGNED(ptr) \ + __builtin_bswap64(*((uint64_t *) (ptr))) + +# define WRITE_64_UNALIGNED(ptr, val) \ + *((uint64_t *) (ptr)) = __builtin_bswap64(val) + +# define READ_32_UNALIGNED(ptr) \ + __builtin_bswap32(*((uint32_t *) (ptr))) + +# define WRITE_32_UNALIGNED(ptr, val) \ + *((uint32_t *) (ptr)) = __builtin_bswap32(val) + +# define READ_16_UNALIGNED(ptr) \ + __builtin_bswap16(*((uint16_t *) (ptr))) + +# define WRITE_16_UNALIGNED(ptr, val) \ + *((uint16_t *) (ptr)) = __builtin_bswap16(val) + +# else +# define READ_64_UNALIGNED(ptr) \ + ((((uint64_t) ((uint8_t *) (ptr))[0]) << 56) \ + | (((uint64_t) ((uint8_t *) (ptr))[1]) << 48) \ + | (((uint64_t) ((uint8_t *) (ptr))[2]) << 40) \ + | (((uint64_t) ((uint8_t *) (ptr))[3]) << 32) \ + | (((uint64_t) ((uint8_t *) (ptr))[4]) << 24) \ + | (((uint64_t) ((uint8_t *) (ptr))[5]) << 16) \ + | (((uint64_t) ((uint8_t *) (ptr))[6]) << 8) \ + | (((uint64_t) ((uint8_t *) (ptr))[7]))) + +# define WRITE_64_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint64_t) val) >> 56) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 48) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 40) & 0xff; \ + ((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 32) & 0xff; \ + ((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 24) & 0xff; \ + ((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[7] = ((uint64_t) val) & 0xff; \ + } + +# define READ_32_UNALIGNED(ptr) \ + ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) \ + | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) + +# define WRITE_32_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint32_t) val) >> 24) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[3] = ((uint32_t) val) & 0xff; \ + } + +# define READ_16_UNALIGNED(ptr) \ + ((((uint8_t *) (ptr))[0] << 8) | ((uint8_t *) (ptr))[1]) + +# define WRITE_16_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint16_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[1] = ((uint16_t) val) & 0xff; \ + } +# endif + +# ifdef __GNUC__ +# define ENDIAN_SWAP_32(value) __builtin_bswap32(value) +# else +# define ENDIAN_SWAP_32(value) \ + ((((value) & 0xFF) << 24) | (((value) & 0xFF00) << 8) | (((value) & 0xFF0000) >> 8) \ + | (((value) & 0xFF000000) >> 24)) +# endif + +# ifdef __GNUC__ +# define ENDIAN_SWAP_16(value) __builtin_bswap16(value) +# else +# define ENDIAN_SWAP_16(value) ((((value) & 0xFF) << 8) | (((value) & 0xFF00) >> 8)) +# endif #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -#define READ_64_UNALIGNED(ptr) \ - ((((uint64_t) ((uint8_t *) (ptr))[0]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[1]) << 48) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[3]) << 32) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[5]) << 16) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[7]))) - -#define WRITE_64_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint64_t) val) >> 56) & 0xff; \ - ((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 48) & 0xff; \ - ((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 40) & 0xff; \ - ((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 32) & 0xff; \ - ((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 24) & 0xff; \ - ((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 16) & 0xff; \ - ((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[7] = ((uint64_t) val) & 0xff; \ - } - -#define READ_32_ALIGNED(ptr) \ - (*((uint32_t *) (ptr))) - -#define READ_32_UNALIGNED(ptr) \ - ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) - -#define WRITE_32_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint32_t) val) >> 24) & 0xff; \ - ((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 16) & 0xff; \ - ((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[3] = ((uint32_t) val) & 0xff; \ - } - -#define READ_16_UNALIGNED(ptr) \ - ((((uint8_t *) (ptr))[0] << 8) | ((uint8_t *) (ptr))[1]) - -#define WRITE_16_UNALIGNED(ptr, val) \ - { \ - ((uint8_t *) (ptr))[0] = (((uint16_t) val) >> 8) & 0xff; \ - ((uint8_t *) (ptr))[1] = ((uint16_t) val) & 0xff; \ - } - -#define ENDIAN_SWAP_32(value) (value) -#define ENDIAN_SWAP_16(value) (value) +# define READ_64_UNALIGNED(ptr) \ + ((((uint64_t) ((uint8_t *) (ptr))[0]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[1]) << 48) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[3]) << 32) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[5]) << 16) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[7]))) + +# define WRITE_64_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint64_t) val) >> 56) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 48) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 40) & 0xff; \ + ((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 32) & 0xff; \ + ((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 24) & 0xff; \ + ((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[7] = ((uint64_t) val) & 0xff; \ + } + +# define READ_32_ALIGNED(ptr) \ + (*((uint32_t *) (ptr))) + +# define READ_32_UNALIGNED(ptr) \ + ((((uint8_t *) (ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *) (ptr))[2] << 8) | ((uint8_t *) (ptr))[3]) + +# define WRITE_32_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint32_t) val) >> 24) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[3] = ((uint32_t) val) & 0xff; \ + } + +# define READ_16_UNALIGNED(ptr) \ + ((((uint8_t *) (ptr))[0] << 8) | ((uint8_t *) (ptr))[1]) + +# define WRITE_16_UNALIGNED(ptr, val) \ + { \ + ((uint8_t *) (ptr))[0] = (((uint16_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[1] = ((uint16_t) val) & 0xff; \ + } + +# define ENDIAN_SWAP_32(value) (value) +# define ENDIAN_SWAP_16(value) (value) #else -#error "Unsupported __BYTE_ORDER__ value." +# error "Unsupported __BYTE_ORDER__ value." #endif #define UNUSED(x) (void) (x); #ifdef __GNUC__ -#define IS_NULL_PTR(x) __builtin_expect((x) == NULL, 0) -#define LIKELY(x) __builtin_expect(!!(x), 1) -#define UNLIKELY(x) __builtin_expect(!!(x), 0) +# define IS_NULL_PTR(x) __builtin_expect((x) == NULL, 0) +# define LIKELY(x) __builtin_expect(!!(x), 1) +# define UNLIKELY(x) __builtin_expect(!!(x), 0) #else -#define IS_NULL_PTR(x) ((x) == NULL) -#define LIKELY(x) (x) -#define UNLIKELY(x) (x) +# define IS_NULL_PTR(x) ((x) == NULL) +# define LIKELY(x) (x) +# define UNLIKELY(x) (x) #endif #ifdef __GNUC__ -#define HOT_FUNC __attribute__((hot)) -#define COLD_FUNC __attribute__((cold)) +# define HOT_FUNC __attribute__((hot)) +# define COLD_FUNC __attribute__((cold)) #else -#define HOT_FUNC -#define COLD_FUNC +# define HOT_FUNC +# define COLD_FUNC #endif #ifdef __GNUC__ -#define MALLOC_LIKE __attribute__((malloc)) +# define MALLOC_LIKE __attribute__((malloc)) #else -#define MALLOC_LIKE +# define MALLOC_LIKE #endif #ifdef __GNUC__ -#define MUST_CHECK __attribute__((warn_unused_result)) +# define MUST_CHECK __attribute__((warn_unused_result)) #else -#define MUST_CHECK +# define MUST_CHECK #endif #ifdef ALLOC_RANDOM_FAILURE -#ifndef RAND_MODULO -#define RAND_MODULO 31 -#endif +# ifndef RAND_MODULO +# define RAND_MODULO 31 +# endif static inline void *rand_fail_malloc(unsigned long malloc_size) { @@ -210,19 +220,19 @@ static inline void *rand_fail_calloc(int n, unsigned long alloc_size) return ((rand() % RAND_MODULO) == 0) ? 0L : calloc(n, alloc_size); } -#define malloc(x) rand_fail_malloc(x) -#define calloc(x, y) rand_fail_calloc(x, y) +# define malloc(x) rand_fail_malloc(x) +# define calloc(x, y) rand_fail_calloc(x, y) #endif #ifdef AVM_VERBOSE_ABORT -#define AVM_ABORT() \ - { \ - fprintf(stderr, "Abort in file %s at line %i\n", __FILE__, __LINE__); \ - abort(); \ - } +# define AVM_ABORT() \ + { \ + fprintf(stderr, "Abort in file %s at line %i\n", __FILE__, __LINE__); \ + abort(); \ + } #else -#define AVM_ABORT() abort() +# define AVM_ABORT() abort() #endif /* @@ -238,9 +248,9 @@ typedef void (*func_ptr_t)(void); static inline __attribute__((always_inline)) void *cast_func_to_void_ptr(func_ptr_t func) { -#if __STDC_VERSION__ >= 201112L +# if __STDC_VERSION__ >= 201112L _Static_assert(sizeof(void *) >= sizeof(func_ptr_t), "function ptr cannot be casted to void *"); -#endif +# endif /* The following workaround is way more standard, but overcomplicated: @@ -256,21 +266,21 @@ static inline __attribute__((always_inline)) void *cast_func_to_void_ptr(func_pt */ // Let's rather play with GCC diagnostic -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpedantic" return (void *) func; -#pragma GCC diagnostic pop +# pragma GCC diagnostic pop } /** * Cast any function pointer to \c void * */ -#define CAST_FUNC_TO_VOID_PTR(f) cast_func_to_void_ptr((func_ptr_t) (f)) +# define CAST_FUNC_TO_VOID_PTR(f) cast_func_to_void_ptr((func_ptr_t) (f)) // else ifdef __GNUC__ #else -#define CAST_FUNC_TO_VOID_PTR(f) ((void *) (f)) +# define CAST_FUNC_TO_VOID_PTR(f) ((void *) (f)) #endif @@ -278,25 +288,25 @@ static inline __attribute__((always_inline)) void *cast_func_to_void_ptr(func_pt static inline __attribute__((always_inline)) func_ptr_t cast_void_to_func_ptr(void *ptr) { -#if __STDC_VERSION__ >= 201112L +# if __STDC_VERSION__ >= 201112L _Static_assert(sizeof(func_ptr_t) >= sizeof(void *), "void * cannot be casted to function ptr"); -#endif +# endif -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpedantic" return (func_ptr_t) ptr; -#pragma GCC diagnostic pop +# pragma GCC diagnostic pop } /** * Cast any function pointer to \c void * */ -#define CAST_VOID_TO_FUNC_PTR(f) cast_void_to_func_ptr((void *) (f)) +# define CAST_VOID_TO_FUNC_PTR(f) cast_void_to_func_ptr((void *) (f)) // else ifdef __GNUC__ #else -#define CAST_VOID_TO_FUNC_PTR(f) ((func_ptr_t) (f)) +# define CAST_VOID_TO_FUNC_PTR(f) ((func_ptr_t) (f)) #endif @@ -309,44 +319,44 @@ static inline __attribute__((always_inline)) func_ptr_t cast_void_to_func_ptr(vo ((type *) (((char *) (ptr)) - offsetof(type, member))) #ifdef __GNUC__ -#define PRINTF_FORMAT_ARGS(str_pos, arg_pos) \ - __attribute__((format(printf, str_pos, arg_pos))) +# define PRINTF_FORMAT_ARGS(str_pos, arg_pos) \ + __attribute__((format(printf, str_pos, arg_pos))) #else -#define PRINTF_FORMAT_ARGS(...) +# define PRINTF_FORMAT_ARGS(...) #endif #ifdef __GNUC__ -#define NO_DISCARD \ - __attribute__((warn_unused_result)) +# define NO_DISCARD \ + __attribute__((warn_unused_result)) #else -#define NO_DISCARD(...) +# define NO_DISCARD(...) #endif #ifdef __GNUC__ -#define UNREACHABLE() \ - __builtin_unreachable() +# define UNREACHABLE() \ + __builtin_unreachable() #else -#define UNREACHABLE(...) +# define UNREACHABLE(...) #endif #if defined(__GNUC__) && !defined(__clang__) -#if __GNUC__ >= 13 -#define HAVE_ASSUME 1 -#define ASSUME(x) __attribute__((assume((x)))) -#endif +# if __GNUC__ >= 13 +# define HAVE_ASSUME 1 +# define ASSUME(x) __attribute__((assume((x)))) +# endif #endif #ifndef HAVE_ASSUME -#if defined __has_builtin -#if __has_builtin(__builtin_assume) -#define HAVE_ASSUME 1 -#define ASSUME(x) __builtin_assume((x)) -#endif -#endif +# if defined __has_builtin +# if __has_builtin(__builtin_assume) +# define HAVE_ASSUME 1 +# define ASSUME(x) __builtin_assume((x)) +# endif +# endif #endif #ifndef ASSUME -#define ASSUME(...) +# define ASSUME(...) #endif #define MAXI(A, B) ((A > B) ? (A) : (B)) @@ -593,7 +603,7 @@ static inline bool uint64_does_overflow_int64(uint64_t u64, bool is_negative) */ static inline uint32_t int32_safe_unsigned_abs(int32_t i32) { - return (i32 < 0) ? ((uint32_t) - (i32 + 1)) + 1 : (uint32_t) i32; + return (i32 < 0) ? ((uint32_t) -(i32 + 1)) + 1 : (uint32_t) i32; } /** @@ -611,7 +621,7 @@ static inline uint32_t int32_safe_unsigned_abs(int32_t i32) */ static inline uint64_t int64_safe_unsigned_abs(int64_t i64) { - return (i64 < 0) ? ((uint64_t) - (i64 + 1)) + 1 : (uint64_t) i64; + return (i64 < 0) ? ((uint64_t) -(i64 + 1)) + 1 : (uint64_t) i64; } /** @@ -799,9 +809,9 @@ static inline bool int64_bsl_overflow(int64_t n, size_t lshift, int64_t *out) * @see intptr_write_to_ascii_buf() */ #if INTPTR_MAX <= INT32_MAX -#define INTPTR_WRITE_TO_ASCII_BUF_LEN (32 + 1) +# define INTPTR_WRITE_TO_ASCII_BUF_LEN (32 + 1) #elif INTPTR_MAX <= INT64_MAX -#define INTPTR_WRITE_TO_ASCII_BUF_LEN (64 + 1) +# define INTPTR_WRITE_TO_ASCII_BUF_LEN (64 + 1) #endif /** diff --git a/src/libAtomVM/valueshashtable.c b/src/libAtomVM/valueshashtable.c index 553042148..989a36598 100644 --- a/src/libAtomVM/valueshashtable.c +++ b/src/libAtomVM/valueshashtable.c @@ -25,14 +25,14 @@ #include #ifndef AVM_NO_SMP -#include "smp.h" -#define SMP_RDLOCK(htable) smp_rwlock_rdlock(htable->lock) -#define SMP_WRLOCK(htable) smp_rwlock_wrlock(htable->lock) -#define SMP_UNLOCK(htable) smp_rwlock_unlock(htable->lock) +# include "smp.h" +# define SMP_RDLOCK(htable) smp_rwlock_rdlock(htable->lock) +# define SMP_WRLOCK(htable) smp_rwlock_wrlock(htable->lock) +# define SMP_UNLOCK(htable) smp_rwlock_unlock(htable->lock) #else -#define SMP_RDLOCK(htable) UNUSED(htable) -#define SMP_WRLOCK(htable) UNUSED(htable) -#define SMP_UNLOCK(htable) UNUSED(htable) +# define SMP_RDLOCK(htable) UNUSED(htable) +# define SMP_WRLOCK(htable) UNUSED(htable) +# define SMP_UNLOCK(htable) UNUSED(htable) #endif #define DEFAULT_SIZE 8 diff --git a/src/libAtomVM/valueshashtable.h b/src/libAtomVM/valueshashtable.h index 3aec410f1..4dead6c9a 100644 --- a/src/libAtomVM/valueshashtable.h +++ b/src/libAtomVM/valueshashtable.h @@ -31,10 +31,10 @@ extern "C" { #define TO_VALUESHASHTABLE_VALUE(value) ((uintptr_t) (value)) #ifndef AVM_NO_SMP -#ifndef TYPEDEF_RWLOCK -#define TYPEDEF_RWLOCK +# ifndef TYPEDEF_RWLOCK +# define TYPEDEF_RWLOCK typedef struct RWLock RWLock; -#endif +# endif #endif struct ValuesHashTable diff --git a/src/platforms/.clang-format b/src/platforms/.clang-format new file mode 100644 index 000000000..282555edc --- /dev/null +++ b/src/platforms/.clang-format @@ -0,0 +1,7 @@ +# Copyright 2025 Davide Bettio +# +# SPDX-License-Identifier: CC0-1.0 + +--- +BasedOnStyle: InheritParentConfig +IndentPPDirectives: None