From 56936ab3024a5ca95b3b02819a520e1a094cfa83 Mon Sep 17 00:00:00 2001 From: Paul Guyot Date: Sat, 15 Nov 2025 23:35:15 +0100 Subject: [PATCH] Apply clang-format to more files Signed-off-by: Paul Guyot --- .clang-format-ignore | 28 -- src/libAtomVM/bif.c | 186 +++++------ src/libAtomVM/bitstring.h | 60 ++-- src/libAtomVM/debug.h | 4 +- src/libAtomVM/externalterm.c | 22 +- src/libAtomVM/module.c | 67 ++-- src/libAtomVM/overflow_helpers.h | 60 ++-- src/libAtomVM/term.c | 38 +-- src/libAtomVM/term.h | 316 +++++++++--------- src/libAtomVM/term_typedef.h | 75 +++-- src/libAtomVM/trace.h | 14 +- src/libAtomVM/utils.h | 285 ++++++++-------- .../generic_unix/lib/platform_nifs.c | 11 +- .../generic_unix/lib/socket_driver.c | 2 + src/platforms/stm32/src/lib/platform_nifs.c | 5 +- tests/test-structs.c | 2 + 16 files changed, 571 insertions(+), 604 deletions(-) diff --git a/.clang-format-ignore b/.clang-format-ignore index 92db05c8fd..b1ef74f6e1 100644 --- a/.clang-format-ignore +++ b/.clang-format-ignore @@ -6,35 +6,7 @@ # We have a number of existing files that are quite "re-format unfriendly" # Let's ignore all of them -src/libAtomVM/bif.c -src/libAtomVM/bitstring.h -src/libAtomVM/debug.h -src/libAtomVM/defaultatoms.c -src/libAtomVM/externalterm.c -src/libAtomVM/module.c src/libAtomVM/opcodesswitch.h -src/libAtomVM/overflow_helpers.h -src/libAtomVM/term.c -src/libAtomVM/term.h -src/libAtomVM/term_typedef.h -src/libAtomVM/trace.h -src/libAtomVM/utils.h -src/platforms/esp32/main/gpio_driver.c -src/platforms/esp32/main/i2c_driver.c -src/platforms/esp32/main/ledc_nif.c -src/platforms/esp32/main/network_driver.c -src/platforms/esp32/main/nvs_nif.c -src/platforms/esp32/main/platform_defaultatoms.c -src/platforms/esp32/main/platform_nifs.c -src/platforms/esp32/main/socket_driver.c -src/platforms/esp32/main/spi_driver.c -src/platforms/esp32/main/uart_driver.c src/platforms/esp32/build/* src/platforms/esp32/components/* -src/platforms/generic_unix/lib/platform_defaultatoms.c -src/platforms/generic_unix/lib/platform_nifs.c -src/platforms/generic_unix/lib/socket_driver.c -src/platforms/stm32/src/lib/platform_defaultatoms.c -src/platforms/stm32/src/lib/platform_nifs.c src/platforms/stm32/build/* -tests/test-structs.c diff --git a/src/libAtomVM/bif.c b/src/libAtomVM/bif.c index e6e933b4b0..29be2d6806 100644 --- a/src/libAtomVM/bif.c +++ b/src/libAtomVM/bif.c @@ -20,8 +20,8 @@ #include "bif.h" -#include #include +#include #include "atom.h" #include "bitstring.h" @@ -37,7 +37,7 @@ #include "unicode.h" #include "utils.h" -//Ignore warning caused by gperf generated code +// Ignore warning caused by gperf generated code #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #include "bifs_hash.h" @@ -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) @@ -638,21 +638,21 @@ static term add_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t avm_int_t res; if (BUILTIN_ADD_OVERFLOW_INT(val1, val2, &res)) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - avm_int64_t res64 = (avm_int64_t) val1 + (avm_int64_t) val2; - return make_boxed_int64(ctx, fail_label, live, res64); - - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return add_int64_to_bigint(ctx, fail_label, live, val1, val2); - #else - #error "Unsupported configuration." - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + avm_int64_t res64 = (avm_int64_t) val1 + (avm_int64_t) val2; + return make_boxed_int64(ctx, fail_label, live, res64); + +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return add_int64_to_bigint(ctx, fail_label, live, val1, val2); +#else +#error "Unsupported configuration." +#endif } return make_maybe_boxed_int(ctx, fail_label, live, res); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val1 = term_maybe_unbox_int64(arg1); avm_int64_t val2 = term_maybe_unbox_int64(arg2); @@ -664,7 +664,7 @@ static term add_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, res); } - #endif +#endif default: return add_maybe_bigint(ctx, fail_label, live, arg1, arg2); @@ -689,7 +689,7 @@ term bif_erlang_add_2(Context *ctx, uint32_t fail_label, int live, term arg1, te UNUSED(live); if (LIKELY(term_is_integer(arg1) && term_is_integer(arg2))) { - //TODO: use long integer instead, and term_to_longint + // TODO: use long integer instead, and term_to_longint avm_int_t res; if (!BUILTIN_ADD_OVERFLOW((avm_int_t) (arg1 & ~TERM_INTEGER_TAG), (avm_int_t) (arg2 & ~TERM_INTEGER_TAG), &res)) { return res | TERM_INTEGER_TAG; @@ -777,21 +777,21 @@ static term sub_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t avm_int_t res; if (BUILTIN_SUB_OVERFLOW_INT(val1, val2, &res)) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - avm_int64_t res64 = (avm_int64_t) val1 - (avm_int64_t) val2; - return make_boxed_int64(ctx, fail_label, live, res64); - - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return sub_int64_to_bigint(ctx, fail_label, live, val1, val2); - #else - #error "Unsupported configuration." - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + avm_int64_t res64 = (avm_int64_t) val1 - (avm_int64_t) val2; + return make_boxed_int64(ctx, fail_label, live, res64); + +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return sub_int64_to_bigint(ctx, fail_label, live, val1, val2); +#else +#error "Unsupported configuration." +#endif } return make_maybe_boxed_int(ctx, fail_label, live, res); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val1 = term_maybe_unbox_int64(arg1); avm_int64_t val2 = term_maybe_unbox_int64(arg2); @@ -803,7 +803,7 @@ static term sub_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, res); } - #endif +#endif default: return sub_maybe_bigint(ctx, fail_label, live, arg1, arg2); @@ -828,7 +828,7 @@ term bif_erlang_sub_2(Context *ctx, uint32_t fail_label, int live, term arg1, te UNUSED(live); if (LIKELY(term_is_integer(arg1) && term_is_integer(arg2))) { - //TODO: use long integer instead, and term_to_longint + // TODO: use long integer instead, and term_to_longint avm_int_t res; if (!BUILTIN_SUB_OVERFLOW((avm_int_t) (arg1 & ~TERM_INTEGER_TAG), (avm_int_t) (arg2 & ~TERM_INTEGER_TAG), &res)) { return res | TERM_INTEGER_TAG; @@ -920,21 +920,21 @@ static term mul_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t avm_int_t res; if (BUILTIN_MUL_OVERFLOW_INT(val1, val2, &res)) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - avm_int64_t res64 = (avm_int64_t) val1 * (avm_int64_t) val2; - return make_boxed_int64(ctx, fail_label, live, res64); - - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return mul_int64_to_bigint(ctx, fail_label, live, val1, val2); - #else - #error "Unsupported configuration." - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + avm_int64_t res64 = (avm_int64_t) val1 * (avm_int64_t) val2; + return make_boxed_int64(ctx, fail_label, live, res64); + +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return mul_int64_to_bigint(ctx, fail_label, live, val1, val2); +#else +#error "Unsupported configuration." +#endif } return make_maybe_boxed_int(ctx, fail_label, live, res); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val1 = term_maybe_unbox_int64(arg1); avm_int64_t val2 = term_maybe_unbox_int64(arg2); @@ -946,7 +946,7 @@ static term mul_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, res); } - #endif +#endif default: return mul_maybe_bigint(ctx, fail_label, live, arg1, arg2); @@ -1070,18 +1070,18 @@ static term div_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t RAISE_ERROR_BIF(fail_label, BADARITH_ATOM); } else if (UNLIKELY((val2 == -1) && (val1 == AVM_INT_MIN))) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) AVM_INT_MIN)); +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) AVM_INT_MIN)); - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return int64_max_plus_one(ctx, fail_label, live); - #endif +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return int64_max_plus_one(ctx, fail_label, live); +#endif } return make_maybe_boxed_int(ctx, fail_label, live, val1 / val2); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val1 = term_maybe_unbox_int64(arg1); avm_int64_t val2 = term_maybe_unbox_int64(arg2); @@ -1095,7 +1095,7 @@ static term div_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, val1 / val2); } - #endif +#endif default: return div_maybe_bigint(ctx, fail_label, live, arg1, arg2); @@ -1160,7 +1160,7 @@ static term neg_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t if (term_is_boxed_integer(arg1)) { switch (term_boxed_size(arg1)) { case 0: - //BUG + // BUG AVM_ABORT(); case 1: { @@ -1170,22 +1170,22 @@ static term neg_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return term_from_int(MIN_NOT_BOXED_INT); case AVM_INT_MIN: - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) val)); +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) val)); - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return int64_max_plus_one(ctx, fail_label, live); +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return int64_max_plus_one(ctx, fail_label, live); - #else - #error "Unsupported configuration." - #endif +#else +#error "Unsupported configuration." +#endif default: return make_boxed_int(ctx, fail_label, live, -val); } } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val = term_unbox_int64(arg1); @@ -1198,7 +1198,7 @@ static term neg_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, -val); } } - #endif +#endif default: return neg_bigint(ctx, fail_label, live, arg1); } @@ -1244,11 +1244,11 @@ static term abs_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t if (term_is_float(arg1)) { avm_float_t farg1 = term_conv_to_float(arg1); avm_float_t fresult; - #if AVM_USE_SINGLE_PRECISION - fresult = fabsf(farg1); - #else - fresult = fabs(farg1); - #endif +#if AVM_USE_SINGLE_PRECISION + fresult = fabsf(farg1); +#else + fresult = fabs(farg1); +#endif if (UNLIKELY(!isfinite(fresult))) { RAISE_ERROR_BIF(fail_label, BADARITH_ATOM); @@ -1262,7 +1262,7 @@ static term abs_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t if (term_is_boxed_integer(arg1)) { switch (term_boxed_size(arg1)) { case 0: - //BUG + // BUG AVM_ABORT(); case 1: { @@ -1272,22 +1272,22 @@ static term abs_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t } if (val == AVM_INT_MIN) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) val)); +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + return make_boxed_int64(ctx, fail_label, live, -((avm_int64_t) val)); - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return int64_max_plus_one(ctx, fail_label, live); +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return int64_max_plus_one(ctx, fail_label, live); - #else - #error "Unsupported configuration." - #endif +#else +#error "Unsupported configuration." +#endif } else { return make_boxed_int(ctx, fail_label, live, -val); } } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val = term_unbox_int64(arg1); if (val >= 0) { @@ -1301,7 +1301,7 @@ static term abs_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_boxed_int64(ctx, fail_label, live, -val); } } - #endif +#endif default: return abs_bigint(ctx, fail_label, live, arg1); } @@ -1387,7 +1387,7 @@ static term rem_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t switch (size) { case 0: { - //BUG + // BUG AVM_ABORT(); } @@ -1401,7 +1401,7 @@ static term rem_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int(ctx, fail_label, live, val1 % val2); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: case 3: { avm_int64_t val1 = term_maybe_unbox_int64(arg1); @@ -1412,7 +1412,7 @@ static term rem_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, t return make_maybe_boxed_int64(ctx, fail_label, live, val1 % val2); } - #endif +#endif default: return rem_maybe_bigint(ctx, fail_label, live, arg1, arg2); @@ -1583,9 +1583,9 @@ term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1) typedef int64_t (*bitwise_op)(int64_t a, int64_t b); typedef size_t (*bitwise_big_op)( - const intn_digit_t m[], size_t m_len, intn_integer_sign_t m_sign, - const intn_digit_t n[], size_t n_len, intn_integer_sign_t n_sign, - intn_digit_t out[], intn_integer_sign_t *out_sign); + const intn_digit_t m[], size_t m_len, intn_integer_sign_t m_sign, + const intn_digit_t n[], size_t n_len, intn_integer_sign_t n_sign, + intn_digit_t out[], intn_integer_sign_t *out_sign); static inline term bitwise_helper( Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op, bitwise_big_op big_op) @@ -1677,11 +1677,11 @@ term bif_erlang_bsl_2(Context *ctx, uint32_t fail_label, int live, term arg1, te int64_t a = term_maybe_unbox_int64(arg1); int64_t result; if (!int64_bsl_overflow(a, b, &result)) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 - return make_maybe_boxed_int64(ctx, fail_label, live, result); - #else - return make_maybe_boxed_int(ctx, fail_label, live, result); - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 + return make_maybe_boxed_int64(ctx, fail_label, live, result); +#else + return make_maybe_boxed_int(ctx, fail_label, live, result); +#endif } } @@ -1738,11 +1738,11 @@ term bif_erlang_bsr_2(Context *ctx, uint32_t fail_label, int live, term arg1, te int64_t a = term_maybe_unbox_int64(arg1); int64_t result = int64_bsr_safe(a, b); - #if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 - return make_maybe_boxed_int64(ctx, fail_label, live, result); - #else - return make_maybe_boxed_int(ctx, fail_label, live, result); - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 + return make_maybe_boxed_int64(ctx, fail_label, live, result); +#else + return make_maybe_boxed_int(ctx, fail_label, live, result); +#endif } intn_digit_t tmp_buf1[INTN_INT64_LEN]; @@ -1791,7 +1791,7 @@ static term bnot_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, if (term_is_boxed_integer(arg1)) { switch (term_boxed_size(arg1)) { case 0: - //BUG + // BUG AVM_ABORT(); case 1: { @@ -1799,12 +1799,12 @@ static term bnot_boxed_helper(Context *ctx, uint32_t fail_label, uint32_t live, return make_boxed_int(ctx, fail_label, live, ~val); } - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 case 2: { avm_int64_t val = term_unbox_int64(arg1); return make_boxed_int64(ctx, fail_label, live, ~val); } - #endif +#endif default: { intn_digit_t tmp_buf1[INTN_INT64_LEN]; const intn_digit_t *m; @@ -1934,7 +1934,7 @@ term bif_erlang_not_equal_to_2(Context *ctx, uint32_t fail_label, term arg1, ter term bif_erlang_exactly_equal_to_2(Context *ctx, uint32_t fail_label, term arg1, term arg2) { - //TODO: 5.0 != 5 + // TODO: 5.0 != 5 TermCompareResult result = term_compare(arg1, arg2, TermCompareExact, ctx->global); if (result == TermEquals) { return TRUE_ATOM; diff --git a/src/libAtomVM/bitstring.h b/src/libAtomVM/bitstring.h index f65d415a60..c81cfa7471 100644 --- a/src/libAtomVM/bitstring.h +++ b/src/libAtomVM/bitstring.h @@ -34,42 +34,39 @@ extern "C" { #endif #define READ_16LE_UNALIGNED(ptr) \ - ( (((uint8_t *)(ptr))[1] << 8) | ((uint8_t *)(ptr))[0] ) + ((((uint8_t *) (ptr))[1] << 8) | ((uint8_t *) (ptr))[0]) -#define WRITE_16LE_UNALIGNED(ptr, val) \ - do { \ - ((uint8_t *)(ptr))[0] = ((uint16_t) val) & 0xff; \ - ((uint8_t *)(ptr))[1] = (((uint16_t) val) >> 8) & 0xff; \ - } while(0) +#define WRITE_16LE_UNALIGNED(ptr, val) \ + do { \ + ((uint8_t *) (ptr))[0] = ((uint16_t) val) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint16_t) val) >> 8) & 0xff; \ + } while (0) #define READ_32LE_UNALIGNED(ptr) \ - ( (((uint8_t *)(ptr))[3] << 24) | (((uint8_t *) (ptr))[2] << 16) | (((uint8_t *)(ptr))[1] << 8) | ((uint8_t *)(ptr))[0] ) + ((((uint8_t *) (ptr))[3] << 24) | (((uint8_t *) (ptr))[2] << 16) | (((uint8_t *) (ptr))[1] << 8) | ((uint8_t *) (ptr))[0]) -#define WRITE_32LE_UNALIGNED(ptr, val) \ - do { \ - ((uint8_t *)(ptr))[0] = ((uint32_t) val) & 0xff; \ - ((uint8_t *)(ptr))[1] = (((uint32_t) val) >> 8) & 0xff; \ - ((uint8_t *)(ptr))[2] = (((uint32_t) val) >> 16) & 0xff; \ - ((uint8_t *)(ptr))[3] = (((uint32_t) val) >> 24) & 0xff; \ - } while(0) +#define WRITE_32LE_UNALIGNED(ptr, val) \ + do { \ + ((uint8_t *) (ptr))[0] = ((uint32_t) val) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint32_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint32_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[3] = (((uint32_t) val) >> 24) & 0xff; \ + } while (0) #define READ_64LE_UNALIGNED(ptr) \ - ( (((uint64_t) ((uint8_t *)(ptr))[7]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 48) | \ - (((uint64_t) ((uint8_t *)(ptr))[5]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 32) | \ - (((uint64_t) ((uint8_t *)(ptr))[3]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 16) | \ - (((uint64_t) ((uint8_t *)(ptr))[1]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[0])) ) - -#define WRITE_64LE_UNALIGNED(ptr, val) \ - do { \ - ((uint8_t *)(ptr))[0] = ((uint64_t) val) & 0xff; \ - ((uint8_t *)(ptr))[1] = (((uint64_t) val) >> 8) & 0xff; \ - ((uint8_t *)(ptr))[2] = (((uint64_t) val) >> 16) & 0xff; \ - ((uint8_t *)(ptr))[3] = (((uint64_t) val) >> 24) & 0xff; \ - ((uint8_t *)(ptr))[4] = (((uint64_t) val) >> 32) & 0xff; \ - ((uint8_t *)(ptr))[5] = (((uint64_t) val) >> 40) & 0xff; \ - ((uint8_t *)(ptr))[6] = (((uint64_t) val) >> 48) & 0xff; \ - ((uint8_t *)(ptr))[7] = (((uint64_t) val) >> 56) & 0xff; \ - } while(0) + ((((uint64_t) ((uint8_t *) (ptr))[7]) << 56) | (((uint64_t) ((uint8_t *) (ptr))[6]) << 48) | (((uint64_t) ((uint8_t *) (ptr))[5]) << 40) | (((uint64_t) ((uint8_t *) (ptr))[4]) << 32) | (((uint64_t) ((uint8_t *) (ptr))[3]) << 24) | (((uint64_t) ((uint8_t *) (ptr))[2]) << 16) | (((uint64_t) ((uint8_t *) (ptr))[1]) << 8) | (((uint64_t) ((uint8_t *) (ptr))[0]))) + +#define WRITE_64LE_UNALIGNED(ptr, val) \ + do { \ + ((uint8_t *) (ptr))[0] = ((uint64_t) val) & 0xff; \ + ((uint8_t *) (ptr))[1] = (((uint64_t) val) >> 8) & 0xff; \ + ((uint8_t *) (ptr))[2] = (((uint64_t) val) >> 16) & 0xff; \ + ((uint8_t *) (ptr))[3] = (((uint64_t) val) >> 24) & 0xff; \ + ((uint8_t *) (ptr))[4] = (((uint64_t) val) >> 32) & 0xff; \ + ((uint8_t *) (ptr))[5] = (((uint64_t) val) >> 40) & 0xff; \ + ((uint8_t *) (ptr))[6] = (((uint64_t) val) >> 48) & 0xff; \ + ((uint8_t *) (ptr))[7] = (((uint64_t) val) >> 56) & 0xff; \ + } while (0) // do not change the values // these values are the same used from opcodes such as bs_get_utf16 @@ -370,7 +367,8 @@ static inline bool bitstring_utf8_size(uint32_t c, size_t *out_size) * @return \c true if encoding was successful, \c false if c is not a valid * unicode character */ -static inline bool bitstring_utf16_size(uint32_t c, size_t *out_size) { +static inline bool bitstring_utf16_size(uint32_t c, size_t *out_size) +{ return bitstring_utf16_encode(c, NULL, BigEndianInteger, out_size); } diff --git a/src/libAtomVM/debug.h b/src/libAtomVM/debug.h index f1ae7070f9..55b52d196b 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/externalterm.c b/src/libAtomVM/externalterm.c index e5311b5fdb..72b3226b6a 100644 --- a/src/libAtomVM/externalterm.c +++ b/src/libAtomVM/externalterm.c @@ -243,7 +243,7 @@ static int serialize_term(uint8_t *buf, term t, GlobalContext *glb) buf[1] = num_bytes; buf[2] = sign == IntNNegativeInteger ? 0x01 : 0x00; intn_to_integer_bytes(bigint, bigint_len, IntNPositiveInteger, IntnLittleEndian, - buf + 3, num_bytes); + buf + 3, num_bytes); } return SMALL_BIG_EXT_BASE_SIZE + num_bytes; } @@ -252,7 +252,8 @@ static int serialize_term(uint8_t *buf, term t, GlobalContext *glb) if (!IS_NULL_PTR(buf)) { avm_float_t val = term_to_float(t); buf[0] = NEW_FLOAT_EXT; - union { + union + { uint64_t intvalue; double doublevalue; } v; @@ -543,7 +544,8 @@ static term parse_external_terms(const uint8_t *external_term_buf, size_t *eterm { switch (external_term_buf[0]) { case NEW_FLOAT_EXT: { - union { + union + { uint64_t intvalue; double doublevalue; } v; @@ -1150,13 +1152,13 @@ static int calculate_heap_usage(const uint8_t *external_term_buf, size_t remaini } *eterm_size = BINARY_EXT_BASE_SIZE + binary_size; - #if TERM_BYTES == 4 - int size_in_terms = ((binary_size + 4 - 1) >> 2); - #elif TERM_BYTES == 8 - int size_in_terms = ((binary_size + 8 - 1) >> 3); - #else - #error - #endif +#if TERM_BYTES == 4 + int size_in_terms = ((binary_size + 4 - 1) >> 2); +#elif TERM_BYTES == 8 + int size_in_terms = ((binary_size + 8 - 1) >> 3); +#else +#error +#endif if (copy && term_binary_size_is_heap_binary(binary_size)) { return 2 + size_in_terms; diff --git a/src/libAtomVM/module.c b/src/libAtomVM/module.c index 64ff0569a6..9618370be5 100644 --- a/src/libAtomVM/module.c +++ b/src/libAtomVM/module.c @@ -30,10 +30,10 @@ #include "jit.h" #include "list.h" #include "nifs.h" +#include "smp.h" +#include "sys.h" #include "term.h" #include "utils.h" -#include "sys.h" -#include "smp.h" #include #include @@ -44,22 +44,22 @@ // BEAM Type constants from OTP source code: // /opt/src/otp/lib/compiler/src/beam_types.erl lines 1446-1461 -#define BEAM_TYPE_ATOM (1 << 0) -#define BEAM_TYPE_BITSTRING (1 << 1) -#define BEAM_TYPE_CONS (1 << 2) -#define BEAM_TYPE_FLOAT (1 << 3) -#define BEAM_TYPE_FUN (1 << 4) -#define BEAM_TYPE_INTEGER (1 << 5) -#define BEAM_TYPE_MAP (1 << 6) -#define BEAM_TYPE_NIL (1 << 7) -#define BEAM_TYPE_PID (1 << 8) -#define BEAM_TYPE_PORT (1 << 9) -#define BEAM_TYPE_REFERENCE (1 << 10) -#define BEAM_TYPE_TUPLE (1 << 11) +#define BEAM_TYPE_ATOM (1 << 0) +#define BEAM_TYPE_BITSTRING (1 << 1) +#define BEAM_TYPE_CONS (1 << 2) +#define BEAM_TYPE_FLOAT (1 << 3) +#define BEAM_TYPE_FUN (1 << 4) +#define BEAM_TYPE_INTEGER (1 << 5) +#define BEAM_TYPE_MAP (1 << 6) +#define BEAM_TYPE_NIL (1 << 7) +#define BEAM_TYPE_PID (1 << 8) +#define BEAM_TYPE_PORT (1 << 9) +#define BEAM_TYPE_REFERENCE (1 << 10) +#define BEAM_TYPE_TUPLE (1 << 11) #define BEAM_TYPE_HAS_LOWER_BOUND (1 << 12) #define BEAM_TYPE_HAS_UPPER_BOUND (1 << 13) -#define BEAM_TYPE_HAS_UNIT (1 << 14) +#define BEAM_TYPE_HAS_UNIT (1 << 14) // BEAM Types version from OTP source code: // /opt/src/otp/lib/compiler/src/beam_types.hrl line 22 @@ -76,7 +76,7 @@ static bool module_are_literals_compressed(const uint8_t *litT); #ifdef WITH_ZLIB - static void *module_uncompress_literals(const uint8_t *litT, int size); +static void *module_uncompress_literals(const uint8_t *litT, int size); #endif static struct LiteralEntry *module_build_literals_table(const void *literalsBuf); #ifndef AVM_NO_EMU @@ -387,18 +387,18 @@ Module *module_new_from_iff_binary(GlobalContext *global, const void *iff_binary mod->free_literals_data = 0; } else { - #ifdef WITH_ZLIB - mod->literals_data = module_uncompress_literals(beam_file + offsets[LITT], sizes[LITT]); - if (IS_NULL_PTR(mod->literals_data)) { - module_destroy(mod); - return NULL; - } - mod->free_literals_data = 1; - #else - fprintf(stderr, "Error: zlib required to uncompress literals.\n"); +#ifdef WITH_ZLIB + mod->literals_data = module_uncompress_literals(beam_file + offsets[LITT], sizes[LITT]); + if (IS_NULL_PTR(mod->literals_data)) { module_destroy(mod); return NULL; - #endif + } + mod->free_literals_data = 1; +#else + fprintf(stderr, "Error: zlib required to uncompress literals.\n"); + module_destroy(mod); + return NULL; +#endif } mod->literals_table = module_build_literals_table(mod->literals_data); @@ -590,9 +590,15 @@ term module_get_type_by_index(const Module *mod, int type_index, Context *ctx) pos += 2; // Skip extra data if present - if (type_bits & BEAM_TYPE_HAS_LOWER_BOUND) pos += 8; - if (type_bits & BEAM_TYPE_HAS_UPPER_BOUND) pos += 8; - if (type_bits & BEAM_TYPE_HAS_UNIT) pos += 1; + if (type_bits & BEAM_TYPE_HAS_LOWER_BOUND) { + pos += 8; + } + if (type_bits & BEAM_TYPE_HAS_UPPER_BOUND) { + pos += 8; + } + if (type_bits & BEAM_TYPE_HAS_UNIT) { + pos += 1; + } } // Read the target type @@ -986,7 +992,6 @@ static bool module_get_line_ref(Module *mod, uint16_t line_ref, uint32_t *out_li return false; } - static bool module_get_location(Module *mod, uint16_t location_ix, size_t *filename_len, const uint8_t **filename) { // 0 is module.erl @@ -1001,7 +1006,7 @@ static bool module_get_location(Module *mod, uint16_t location_ix, size_t *filen const uint8_t *pos = mod->locations_table; for (size_t i = 1; i <= mod->locations_count; i++) { uint16_t size = READ_16_UNALIGNED(pos); - pos +=2; + pos += 2; if (i == location_ix) { *filename_len = size; if (filename) { diff --git a/src/libAtomVM/overflow_helpers.h b/src/libAtomVM/overflow_helpers.h index f4e03d1e19..0d2bec6b04 100644 --- a/src/libAtomVM/overflow_helpers.h +++ b/src/libAtomVM/overflow_helpers.h @@ -31,35 +31,35 @@ extern "C" { #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 __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_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 +#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 +#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 +#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 +#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 @@ -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 - 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 - 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 +#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 + 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 } #endif diff --git a/src/libAtomVM/term.c b/src/libAtomVM/term.c index d0e4efd0c1..29e3a92afb 100644 --- a/src/libAtomVM/term.c +++ b/src/libAtomVM/term.c @@ -46,7 +46,8 @@ _Static_assert( // Make sure avm_int_t can always fit into size_t _Static_assert(SIZE_MAX >= AVM_INT_MAX, "SIZE_MAX < AVM_INT_MAX is an unsupported configuration."); -enum TermTypeIndex { +enum TermTypeIndex +{ TERM_TYPE_INDEX_INVALID = 0, TERM_TYPE_INDEX_INTEGER = 1, TERM_TYPE_INDEX_FLOAT = 2, @@ -118,8 +119,7 @@ int term_fprint(FILE *stream, term t, const GlobalContext *global) { struct FprintfFun fprintf_fun = { .base = { - .print = fprintf_printer - }, + .print = fprintf_printer }, .stream = stream }; @@ -130,8 +130,7 @@ int term_snprint(char *buf, size_t size, term t, const GlobalContext *global) { struct SnprintfFun snprintf_fun = { .base = { - .print = snprintf_printer - }, + .print = snprintf_printer }, .buf = buf, .size = size }; @@ -492,8 +491,7 @@ static enum TermTypeIndex term_type_to_index(term t) default: UNREACHABLE(); } - } - break; + } break; case TERM_PRIMARY_IMMED: switch (t & TERM_IMMED_TAG_MASK) { case TERM_PID_TAG: @@ -523,15 +521,15 @@ static enum TermTypeIndex term_type_to_index(term t) #define BEGIN_MAP_KEY TERM_RESERVED_MARKER(1) #define END_MAP_KEY TERM_RESERVED_MARKER(0) -#define CMP_POP_AND_CONTINUE() \ - other = temp_stack_pop(&temp_stack); \ - if (other == BEGIN_MAP_KEY) { \ - map_key_nesting++; \ - other = temp_stack_pop(&temp_stack); \ - } else if (other == END_MAP_KEY) { \ - map_key_nesting--; \ - other = temp_stack_pop(&temp_stack); \ - } \ +#define CMP_POP_AND_CONTINUE() \ + other = temp_stack_pop(&temp_stack); \ + if (other == BEGIN_MAP_KEY) { \ + map_key_nesting++; \ + other = temp_stack_pop(&temp_stack); \ + } else if (other == END_MAP_KEY) { \ + map_key_nesting--; \ + other = temp_stack_pop(&temp_stack); \ + } \ t = temp_stack_pop(&temp_stack); TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalContext *global) @@ -857,8 +855,7 @@ TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalC CMP_POP_AND_CONTINUE(); break; } - } - break; + } break; case TERM_TYPE_INDEX_FLOAT: { avm_float_t t_float = term_to_float(t); avm_float_t other_float = term_to_float(other); @@ -869,8 +866,7 @@ TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalC result = (t_float > other_float) ? TermGreaterThan : TermLessThan; goto unequal; } - } - break; + } break; case TERM_TYPE_INDEX_ATOM: { int t_atom_index = term_to_atom_index(t); int other_atom_index = term_to_atom_index(other); @@ -945,7 +941,7 @@ TermCompareResult term_compare(term t, term other, TermCompareOpts opts, GlobalC UNREACHABLE(); } } else if ((((type_t == TERM_TYPE_INDEX_FLOAT && type_other == TERM_TYPE_INDEX_INTEGER) - || (type_t == TERM_TYPE_INDEX_INTEGER && type_other == TERM_TYPE_INDEX_FLOAT))) + || (type_t == TERM_TYPE_INDEX_INTEGER && type_other == TERM_TYPE_INDEX_FLOAT))) && ((opts & TermCompareExact) != TermCompareExact) && (map_key_nesting == 0)) { avm_float_t t_float = term_conv_to_float(t); avm_float_t other_float = term_conv_to_float(other); diff --git a/src/libAtomVM/term.h b/src/libAtomVM/term.h index 2b7d99c035..6156aec10a 100644 --- a/src/libAtomVM/term.h +++ b/src/libAtomVM/term.h @@ -86,7 +86,7 @@ extern "C" { #define TERM_PRIMARY_BOXED 0x2 #define TERM_PRIMARY_IMMED 0x3 -#define TERM_BOXED_VALUE_TAG _Pragma ("TERM_BOXED_VALUE_TAG is deprecated, use TERM_PRIMARY_BOXED instead") TERM_PRIMARY_BOXED +#define TERM_BOXED_VALUE_TAG _Pragma("TERM_BOXED_VALUE_TAG is deprecated, use TERM_PRIMARY_BOXED instead") TERM_PRIMARY_BOXED #define TERM_IMMED_TAG_MASK 0xF #define TERM_PID_TAG 0x3 @@ -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 @@ -1047,7 +1047,7 @@ static inline term term_from_int32(int32_t value) #if TERM_BITS == 32 // 268435455 == 0x0FFFFFFF if (UNLIKELY((value > 268435455) || (value < -268435455))) { - //TODO: unimplemented on heap integer value + // TODO: unimplemented on heap integer value fprintf(stderr, "term_from_int32: unimplemented: term should be moved to heap."); AVM_ABORT(); @@ -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 } @@ -1068,7 +1068,7 @@ static inline term term_from_int64(int64_t value) #if TERM_BITS == 32 // 268435455 == 0x0FFFFFFF if (UNLIKELY((value > 268435455) || (value < -268435455))) { - //TODO: unimplemented on heap integer value + // TODO: unimplemented on heap integer value fprintf(stderr, "term_from_int64: unimplemented: term should be moved to heap."); AVM_ABORT(); @@ -1079,7 +1079,7 @@ static inline term term_from_int64(int64_t value) #elif TERM_BITS == 64 // 1152921504606846975 = 0x0FFFFFFFFFFFFFFF if (UNLIKELY((value > 1152921504606846975) || (value < -1152921504606846975))) { - //TODO: unimplemented on heap integer value + // TODO: unimplemented on heap integer value fprintf(stderr, "unimplemented: term should be moved to heap."); AVM_ABORT(); @@ -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 } @@ -1206,20 +1206,20 @@ static inline avm_int64_t term_unbox_int64(term boxed_long) const term *boxed_value = term_to_const_term_ptr(boxed_long); - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 1 - return (avm_int64_t) boxed_value[1]; +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 1 + return (avm_int64_t) boxed_value[1]; - #elif BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - #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__ - 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 configuration." - #endif +#elif BOXED_TERMS_REQUIRED_FOR_INT64 == 2 +#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__ + 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 configuration." +#endif } static inline avm_int_t term_maybe_unbox_int(term maybe_boxed_int) @@ -1264,31 +1264,31 @@ static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap) avm_uint64_t sign = (((avm_uint64_t) large_int64) >> 63) << TERM_BOXED_INTEGER_SIGN_BIT_POS; term *boxed_int = memory_heap_alloc(heap, 1 + BOXED_TERMS_REQUIRED_FOR_INT64); boxed_int[0] = (BOXED_TERMS_REQUIRED_FOR_INT64 << 6) | TERM_BOXED_POSITIVE_INTEGER | sign; - #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__ - boxed_int[1] = large_int64; - boxed_int[2] = large_int64 >> 32; - #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 configuration." - #endif +#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__ + boxed_int[1] = large_int64; + boxed_int[2] = large_int64 >> 32; +#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 configuration." +#endif return ((term) boxed_int) | TERM_PRIMARY_BOXED; } static inline term term_make_maybe_boxed_int64(avm_int64_t value, Heap *heap) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { - return term_make_boxed_int64(value, heap); - } - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { + return term_make_boxed_int64(value, heap); + } +#endif if ((value < MIN_NOT_BOXED_INT) || (value > MAX_NOT_BOXED_INT)) { return term_make_boxed_int(value, heap); @@ -1300,11 +1300,11 @@ static inline term term_make_maybe_boxed_int64(avm_int64_t value, Heap *heap) static inline size_t term_boxed_integer_size(avm_int64_t value) { - #if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 - if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { - return BOXED_INT64_SIZE; - } - #endif +#if BOXED_TERMS_REQUIRED_FOR_INT64 == 2 + if ((value < AVM_INT_MIN) || (value > AVM_INT_MAX)) { + return BOXED_INT64_SIZE; + } +#endif if ((value < MIN_NOT_BOXED_INT) || (value > MAX_NOT_BOXED_INT)) { return BOXED_INT_SIZE; } else { @@ -1570,11 +1570,11 @@ static inline size_t term_binary_data_size_in_terms(size_t size) { if (term_binary_size_is_heap_binary(size)) { #if TERM_BYTES == 4 - return ((size + 4 - 1) >> 2) + 1; + return ((size + 4 - 1) >> 2) + 1; #elif TERM_BYTES == 8 - return ((size + 8 - 1) >> 3) + 1; + return ((size + 8 - 1) >> 3) + 1; #else - #error +#error #endif } else { return TERM_BOXED_REFC_BINARY_SIZE; @@ -1648,18 +1648,18 @@ static inline const char *term_binary_data(term t) } /** -* @brief Create an uninitialized binary. -* -* @details Allocates a binary on the heap, and returns a term pointing to it. -* Note that the data in the binary is uninitialized and could contain any garbage. -* Make sure to initialize before use, if needed (e.g., via memset). -* The binary may be allocated as a refc if it is large enough. -* @param size size of binary data buffer. -* @param heap the heap to allocate the binary in -* @param glb the global context as refc binaries are global -* @return a term pointing to the boxed binary pointer or `term_invalid_term()` -* if there isn't enough memory to allocate the refc buffer -*/ + * @brief Create an uninitialized binary. + * + * @details Allocates a binary on the heap, and returns a term pointing to it. + * Note that the data in the binary is uninitialized and could contain any garbage. + * Make sure to initialize before use, if needed (e.g., via memset). + * The binary may be allocated as a refc if it is large enough. + * @param size size of binary data buffer. + * @param heap the heap to allocate the binary in + * @param glb the global context as refc binaries are global + * @return a term pointing to the boxed binary pointer or `term_invalid_term()` + * if there isn't enough memory to allocate the refc buffer + */ static inline term term_create_uninitialized_binary(size_t size, Heap *heap, GlobalContext *glb) { if (term_binary_size_is_heap_binary(size)) { @@ -1753,14 +1753,14 @@ static inline term term_from_const_binary(const void *data, size_t size, Heap *h } /** -* @brief Create an empty binary. All bytes in the binary are initialized to 0x00 -* -* @details Allocates a binary on the heap, and returns a term pointing to it. -* @param size size of binary data buffer. -* @param heap the heap to allocate memory in -* @param glb the global context as refc binaries are global -* @return a term pointing to the boxed binary pointer. -*/ + * @brief Create an empty binary. All bytes in the binary are initialized to 0x00 + * + * @details Allocates a binary on the heap, and returns a term pointing to it. + * @param size size of binary data buffer. + * @param heap the heap to allocate memory in + * @param glb the global context as refc binaries are global + * @return a term pointing to the boxed binary pointer. + */ static inline term term_create_empty_binary(size_t size, Heap *heap, GlobalContext *glb) { term t = term_create_uninitialized_binary(size, heap, glb); @@ -1771,16 +1771,16 @@ static inline term term_create_empty_binary(size_t size, Heap *heap, GlobalConte } /** -* @brief Reuse a binary. If the binary is a refc binary with a ref count of -* 1, try to reuse it. Otherwise, create a new binary and copy the data. -* -* @details Try to reuse a binary and return a term pointing to it. -* @param src binary to reuse. -* @param size size of binary data buffer. -* @param heap the heap to allocate memory in -* @param glb the global context as refc binaries are global -* @return a term pointing to the boxed binary pointer. -*/ + * @brief Reuse a binary. If the binary is a refc binary with a ref count of + * 1, try to reuse it. Otherwise, create a new binary and copy the data. + * + * @details Try to reuse a binary and return a term pointing to it. + * @param src binary to reuse. + * @param size size of binary data buffer. + * @param heap the heap to allocate memory in + * @param glb the global context as refc binaries are global + * @return a term pointing to the boxed binary pointer. + */ term term_reuse_binary(term src, size_t size, Heap *heap, GlobalContext *glb); static inline bool term_normalize_binary_pos_len(term binary, avm_int_t pos, avm_int_t len, BinaryPosLen *pos_len) @@ -1807,25 +1807,25 @@ 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 }; -} - -/** -* @brief Insert an binary into a binary (using bit syntax). -* -* @details Insert the data from the input binary, starting -* at the bit position starting in offset. -* @param t a term pointing to binary data. Fails if t is not a binary term. -* @param offset the bitwise offset in t at which to start writing the integer value -* @param src binary source to insert binary data into. -* @param n the number of low-order bits from value to write. -* @return 0 on success; non-zero value if: -* t is not a binary term -* n is greater than the number of bits in an integer -* there is insufficient capacity in the binary to write these bits -* In general, none of these conditions should apply, if this function is being -* called in the context of generated bit syntax instructions. -*/ + return (BinaryPosLen){ .pos = -1, .len = -1 }; +} + +/** + * @brief Insert an binary into a binary (using bit syntax). + * + * @details Insert the data from the input binary, starting + * at the bit position starting in offset. + * @param t a term pointing to binary data. Fails if t is not a binary term. + * @param offset the bitwise offset in t at which to start writing the integer value + * @param src binary source to insert binary data into. + * @param n the number of low-order bits from value to write. + * @return 0 on success; non-zero value if: + * t is not a binary term + * n is greater than the number of bits in an integer + * there is insufficient capacity in the binary to write these bits + * In general, none of these conditions should apply, if this function is being + * called in the context of generated bit syntax instructions. + */ static inline int term_bs_insert_binary(term t, int offset, term src, int n) { if (!term_is_binary(t)) { @@ -1863,16 +1863,16 @@ static inline term term_from_ref_ticks(uint64_t ref_ticks, Heap *heap) term *boxed_value = memory_heap_alloc(heap, REF_SIZE); boxed_value[0] = ((REF_SIZE - 1) << 6) | TERM_BOXED_REF; - #if TERM_BYTES == 8 - boxed_value[1] = (term) ref_ticks; +#if TERM_BYTES == 8 + boxed_value[1] = (term) ref_ticks; - #elif TERM_BYTES == 4 - boxed_value[1] = (ref_ticks >> 4); - boxed_value[2] = (ref_ticks & 0xFFFFFFFF); +#elif TERM_BYTES == 4 + boxed_value[1] = (ref_ticks >> 4); + boxed_value[2] = (ref_ticks & 0xFFFFFFFF); - #else - #error "terms must be either 32 or 64 bit wide" - #endif +#else +#error "terms must be either 32 or 64 bit wide" +#endif return ((term) boxed_value) | TERM_PRIMARY_BOXED; } @@ -1883,15 +1883,15 @@ static inline uint64_t term_to_ref_ticks(term rt) const term *boxed_value = term_to_const_term_ptr(rt); - #if TERM_BYTES == 8 - return boxed_value[1]; +#if TERM_BYTES == 8 + return boxed_value[1]; - #elif TERM_BYTES == 4 - return (boxed_value[1] << 4) | boxed_value[2]; +#elif TERM_BYTES == 4 + return (boxed_value[1] << 4) | boxed_value[2]; - #else - #error "terms must be either 32 or 64 bit wide" - #endif +#else +#error "terms must be either 32 or 64 bit wide" +#endif } /** @@ -2039,24 +2039,24 @@ static inline term term_make_external_reference(term node, uint16_t len, uint32_ boxed_value[0] = ((EXTERNAL_REF_SIZE(len) - 1) << 6) | TERM_BOXED_EXTERNAL_REF; uint32_t *external_thing_words = (uint32_t *) &boxed_value[1]; - #if TERM_BYTES == 8 - external_thing_words[0] = atom_index; - external_thing_words[1] = creation; - external_thing_words[2] = len; - for (int i = 0; i < len; i++) { - external_thing_words[3 + i] = data[i]; - } +#if TERM_BYTES == 8 + external_thing_words[0] = atom_index; + external_thing_words[1] = creation; + external_thing_words[2] = len; + for (int i = 0; i < len; i++) { + external_thing_words[3 + i] = data[i]; + } - #elif TERM_BYTES == 4 - external_thing_words[0] = atom_index; - external_thing_words[1] = creation; - for (int i = 0; i < len; i++) { - external_thing_words[2 + i] = data[i]; - } +#elif TERM_BYTES == 4 + external_thing_words[0] = atom_index; + external_thing_words[1] = creation; + for (int i = 0; i < len; i++) { + external_thing_words[2 + i] = data[i]; + } - #else - #error "terms must be either 32 or 64 bit wide" - #endif +#else +#error "terms must be either 32 or 64 bit wide" +#endif return ((term) boxed_value) | TERM_PRIMARY_BOXED; } @@ -2073,16 +2073,16 @@ static inline uint32_t term_get_external_reference_len(term t) const term *boxed_value = term_to_const_term_ptr(t); - #if TERM_BYTES == 8 - const uint32_t *external_thing_words = (const uint32_t *) &boxed_value[1]; - return (uint32_t) external_thing_words[2]; +#if TERM_BYTES == 8 + const uint32_t *external_thing_words = (const uint32_t *) &boxed_value[1]; + return (uint32_t) external_thing_words[2]; - #elif TERM_BYTES == 4 - return (uint32_t) (boxed_value[0] >> 6) - 2; +#elif TERM_BYTES == 4 + return (uint32_t) (boxed_value[0] >> 6) - 2; - #else - #error "terms must be either 32 or 64 bit wide" - #endif +#else +#error "terms must be either 32 or 64 bit wide" +#endif } /** @@ -2098,15 +2098,15 @@ static inline const uint32_t *term_get_external_reference_words(term t) const term *boxed_value = term_to_const_term_ptr(t); const uint32_t *external_thing_words = (const uint32_t *) &boxed_value[1]; - #if TERM_BYTES == 8 - return external_thing_words + 3; +#if TERM_BYTES == 8 + return external_thing_words + 3; - #elif TERM_BYTES == 4 - return external_thing_words + 2; +#elif TERM_BYTES == 4 + return external_thing_words + 2; - #else - #error "terms must be either 32 or 64 bit wide" - #endif +#else +#error "terms must be either 32 or 64 bit wide" +#endif } /** @@ -2119,10 +2119,10 @@ static inline const uint32_t *term_get_external_reference_words(term t) */ static inline term term_alloc_tuple(uint32_t size, Heap *heap) { - //TODO: write a real implementation - //align constraints here + // TODO: write a real implementation + // align constraints here term *boxed_value = memory_heap_alloc(heap, 1 + size); - boxed_value[0] = (size << 6); //tuple + boxed_value[0] = (size << 6); // tuple return ((term) boxed_value) | TERM_PRIMARY_BOXED; } @@ -2191,8 +2191,8 @@ static inline int term_get_tuple_arity(term t) */ static inline term term_from_string(const uint8_t *data, uint16_t size, Heap *heap) { - //TODO: write a real implementation - //align constraints here + // TODO: write a real implementation + // align constraints here term *list_cells = memory_heap_alloc(heap, size * 2); for (int i = 0; i < size * 2; i += 2) { list_cells[i] = (term) &list_cells[i + 2] | 0x1; diff --git a/src/libAtomVM/term_typedef.h b/src/libAtomVM/term_typedef.h index 7a55a16ccb..be4ce9bcca 100644 --- a/src/libAtomVM/term_typedef.h +++ b/src/libAtomVM/term_typedef.h @@ -45,21 +45,21 @@ typedef uintptr_t term; #define TERM_U_FMT PRIuPTR #define TERM_X_FMT PRIXPTR -#if ( (UINT32_MAX != 4294967295ULL) || (UINT64_MAX != 18446744073709551615ULL) \ +#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,50 +92,51 @@ 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" +typedef float avm_float_t; +#define AVM_FLOAT_FMT "%f" #ifndef __cplusplus - _Static_assert(sizeof(avm_float_t) == 4, "avm_float_t must be a 32-bit float"); +_Static_assert(sizeof(avm_float_t) == 4, "avm_float_t must be a 32-bit float"); #endif #else - typedef double avm_float_t; - #define AVM_FLOAT_FMT "%lf" +typedef double avm_float_t; +#define AVM_FLOAT_FMT "%lf" #ifndef __cplusplus - _Static_assert(sizeof(avm_float_t) == 8, "avm_float_t must be a 64-bit float"); +_Static_assert(sizeof(avm_float_t) == 8, "avm_float_t must be a 64-bit float"); #endif #endif -typedef union { +typedef union +{ term t; avm_float_t f; } float_term_t; diff --git a/src/libAtomVM/trace.h b/src/libAtomVM/trace.h index 659dd85e93..511ef35022 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.h b/src/libAtomVM/utils.h index 5cd87a29e0..76bec77696 100644 --- a/src/libAtomVM/utils.h +++ b/src/libAtomVM/utils.h @@ -39,166 +39,159 @@ 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 @@ -316,24 +309,24 @@ 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__) diff --git a/src/platforms/generic_unix/lib/platform_nifs.c b/src/platforms/generic_unix/lib/platform_nifs.c index 64f77e548c..242ce6d4d8 100644 --- a/src/platforms/generic_unix/lib/platform_nifs.c +++ b/src/platforms/generic_unix/lib/platform_nifs.c @@ -34,7 +34,7 @@ #include "term.h" #include -//#define ENABLE_TRACE +// #define ENABLE_TRACE #include "trace.h" #define VALIDATE_VALUE(value, verify_function) \ @@ -77,13 +77,11 @@ static term nif_atomvm_random(Context *ctx, int argc, term argv[]) return term_make_boxed_int(value, &ctx->heap); } -static const struct Nif atomvm_rand_bytes_nif = -{ +static const struct Nif atomvm_rand_bytes_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_rand_bytes }; -static const struct Nif atomvm_random_nif = -{ +static const struct Nif atomvm_random_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_random }; @@ -98,8 +96,7 @@ static term nif_atomvm_platform(Context *ctx, int argc, term argv[]) return GENERIC_UNIX_ATOM; } -static const struct Nif atomvm_platform_nif = -{ +static const struct Nif atomvm_platform_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_platform }; diff --git a/src/platforms/generic_unix/lib/socket_driver.c b/src/platforms/generic_unix/lib/socket_driver.c index 7b61f56868..8ebdb0be64 100644 --- a/src/platforms/generic_unix/lib/socket_driver.c +++ b/src/platforms/generic_unix/lib/socket_driver.c @@ -83,6 +83,7 @@ typedef struct SocketDriverData PassiveRecvListener *passive_listener; } SocketDriverData; +// clang-format off // TODO define in defaultatoms const char *const send_a = "\x4" "send"; const char *const sendto_a = "\x6" "sendto"; @@ -100,6 +101,7 @@ const char *const controlling_process_a = "\x13" "controlling_process"; const char *const not_owner_a = "\x9" "not_owner"; const char *const close_internal = "\x14" "$atomvm_socket_close"; +// clang-format on static const char *gen_tcp_moniker_atom = ATOM_STR("\xC", "$avm_gen_tcp"); static const char *native_tcp_module_atom = ATOM_STR("\xC", "gen_tcp_inet"); diff --git a/src/platforms/stm32/src/lib/platform_nifs.c b/src/platforms/stm32/src/lib/platform_nifs.c index 2a5e843837..48b52b787f 100644 --- a/src/platforms/stm32/src/lib/platform_nifs.c +++ b/src/platforms/stm32/src/lib/platform_nifs.c @@ -23,7 +23,7 @@ #include #include -//#define ENABLE_TRACE +// #define ENABLE_TRACE #include #include "stm_sys.h" @@ -36,8 +36,7 @@ static term nif_atomvm_platform(Context *ctx, int argc, term argv[]) return STM32_ATOM; } -static const struct Nif atomvm_platform_nif = -{ +static const struct Nif atomvm_platform_nif = { .base.type = NIFFunctionType, .nif_ptr = nif_atomvm_platform }; diff --git a/tests/test-structs.c b/tests/test-structs.c index 792a91876e..52ce22dcd2 100644 --- a/tests/test-structs.c +++ b/tests/test-structs.c @@ -25,6 +25,7 @@ #include "utils.h" #include "valueshashtable.h" +// clang-format off static const char *const false_atom = "\x05" "false"; static const char *const true_atom = "\x04" "true"; @@ -158,6 +159,7 @@ static const char *const atomvm_heap_growth_atom = "\x12" "atomvm_heap_growth"; static const char *const bounded_free_atom = "\xC" "bounded_free"; static const char *const minimum_atom = "\x7" "minimum"; static const char *const fibonacci_atom = "\x9" "fibonacci"; +// clang-format on void test_valueshashtable(void) {