Skip to content

Commit

Permalink
everywhere: Isolated all macros into separeted files.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 14, 2024
1 parent 4016eed commit c721aee
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 103 deletions.
5 changes: 2 additions & 3 deletions src/impls/impl-wasm/externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

#include <karm-base/size.h>

static constexpr usize PAGE_SIZE = kib(64);
#include "macros.h"

#define wasm_import(name) __attribute__((import_module("env"), import_name(#name))) name
#define wasm_export(name) __attribute__((visibility("default"), export_name(#name))) name
static constexpr usize PAGE_SIZE = kib(64);

extern "C" ExternSym __heap_base;
extern "C" ExternSym __heap_end;
Expand Down
4 changes: 4 additions & 0 deletions src/impls/impl-wasm/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define wasm_import(name) __attribute__((import_module("env"), import_name(#name))) name
#define wasm_export(name) __attribute__((visibility("default"), export_name(#name))) name
13 changes: 2 additions & 11 deletions src/libs/karm-base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

#include "_prelude.h"

namespace Karm {

// MARK: Macros ----------------------------------------------------------------
#include "macros.h"

// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
#define lifetimebound [[clang::lifetimebound]]
#define __concat$(LHS, RHS) LHS##RHS
#define concat$(LHS, RHS) __concat$(LHS, RHS)
#define __stringify$(SYM) #SYM
#define stringify$(SYM) __stringify$(SYM)
#define var$(NAME) concat$(__m_##NAME##_, __LINE__)
#define always_inline [[gnu::always_inline]]
namespace Karm {

// MARK: Unsigned --------------------------------------------------------------

Expand Down
39 changes: 3 additions & 36 deletions src/libs/karm-base/enum.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <karm-base/base.h>
#include <karm-base/string.h>
#include <karm-meta/traits.h>

#include "base.h"
#include "string.h"

namespace Karm {

template <Meta::Enum E>
Expand Down Expand Up @@ -127,40 +128,6 @@ struct Flags {
}
};

#define FlagsEnum$(T) \
inline T operator~(T a) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T) ~(U)a; \
} \
inline T operator|(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a | (U)b); \
} \
inline T operator&(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a & (U)b); \
} \
inline T operator^(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a ^ (U)b); \
} \
inline bool operator!(T a) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return not(U) a; \
} \
inline T &operator|=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a |= (U)b); \
} \
inline T &operator&=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a &= (U)b); \
} \
inline T &operator^=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a ^= (U)b); \
}

template <Meta::Enum E, typename U = Meta::UnderlyingType<E>>
constexpr U toUnderlyingType(E value) {
return (U)value;
Expand Down
75 changes: 75 additions & 0 deletions src/libs/karm-base/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

namespace Karm {

#define lifetimebound [[clang::lifetimebound]]
#define __concat$(LHS, RHS) LHS##RHS
#define concat$(LHS, RHS) __concat$(LHS, RHS)
#define __stringify$(SYM) #SYM
#define stringify$(SYM) __stringify$(SYM)
#define var$(NAME) concat$(__m_##NAME##_, __LINE__)
#define always_inline [[gnu::always_inline]]

// MARK: Enum ------------------------------------------------------------------

#define FlagsEnum$(T) \
inline T operator~(T a) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T) ~(U)a; \
} \
inline T operator|(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a | (U)b); \
} \
inline T operator&(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a & (U)b); \
} \
inline T operator^(T a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T)((U)a ^ (U)b); \
} \
inline bool operator!(T a) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return not(U) a; \
} \
inline T &operator|=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a |= (U)b); \
} \
inline T &operator&=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a &= (U)b); \
} \
inline T &operator^=(T &a, T b) { \
using U = ::Karm::Meta::UnderlyingType<T>; \
return (T &)((U &)a ^= (U)b); \
}

// MARK: Try -------------------------------------------------------------------

// Give us a symbole to break one when debbuging error handling.
// This is a no-op in release mode.
#if defined(__ck_debug__) and !defined(KARM_DISABLE_TRY_FAIL_HOOK)
extern "C" void __try_failed();
# define __tryFail() __try_failed()
#else
# define __tryFail() /* NOP */
#endif

#define __try$(EXPR, RET, AWAIT) ({ \
auto __expr = AWAIT(EXPR); \
if (not static_cast<bool>(__expr)) [[unlikely]] { \
__tryFail(); \
RET __expr.none(); \
} \
__expr.take(); \
})

#define try$(EXPR) __try$(EXPR, return, )

#define co_try$(EXPR) __try$(EXPR, co_return, )

#define co_trya$(EXPR) __try$(EXPR, co_return, co_await)

} // namespace Karm
24 changes: 0 additions & 24 deletions src/libs/karm-base/try.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,3 @@ concept Tryable = requires(T t) {
} and Unwrapable<T> and Takeable<T>;

} // namespace Karm

// Give us a symbole to break one when debbuging error handling.
// This is a no-op in release mode.
#if defined(__ck_debug__) and !defined(KARM_DISABLE_TRY_FAIL_HOOK)
extern "C" void __try_failed();
# define __tryFail() __try_failed()
#else
# define __tryFail() /* NOP */
#endif

#define __try$(EXPR, RET, AWAIT) ({ \
auto __expr = AWAIT(EXPR); \
if (not static_cast<bool>(__expr)) [[unlikely]] { \
__tryFail(); \
RET __expr.none(); \
} \
__expr.take(); \
})

#define try$(EXPR) __try$(EXPR, return, )

#define co_try$(EXPR) __try$(EXPR, co_return, )

#define co_trya$(EXPR) __try$(EXPR, co_return, co_await)
19 changes: 19 additions & 0 deletions src/libs/karm-ui/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace Karm::Ui {

#define slot$(EXPR) \
Karm::Ui::Slot { \
[=] { \
return EXPR; \
} \
}

#define slots$(...) \
Karm::Ui::Slots { \
[=] -> Ui::Children { \
return {__VA_ARGS__}; \
} \
}

} // namespace Karm::Ui
15 changes: 1 addition & 14 deletions src/libs/karm-ui/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <karm-sys/async.h>

#include "atoms.h"
#include "macros.h"

namespace Karm::Ui {

Expand Down Expand Up @@ -288,17 +289,3 @@ using Slot = Func<Child()>;
using Slots = Func<Children()>;

} // namespace Karm::Ui

#define slot$(EXPR) \
Karm::Ui::Slot { \
[=] { \
return EXPR; \
} \
}

#define slots$(...) \
Karm::Ui::Slots { \
[=] -> Ui::Children { \
return {__VA_ARGS__}; \
} \
}
20 changes: 20 additions & 0 deletions src/specs/handover/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

namespace Handover {

// clang-format off

#define HandoverSection$() \
[[gnu::used, gnu::section(".handover")]]

#define HandoverRequests$(...) \
HandoverSection$() \
static ::Handover::Request const __handover__[] = { \
{::Handover::Tag::MAGIC, 0, 0}, \
__VA_ARGS__ __VA_OPT__(, ) \
{::Handover::Tag::END, 0, 0}, \
};

// clang-format on

} // namespace Handover
17 changes: 2 additions & 15 deletions src/specs/handover/spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <karm-base/cstr.h>
#include <karm-base/limits.h>

#include "macros.h"

namespace Handover {

#ifdef __ck_bits_64__
Expand Down Expand Up @@ -261,21 +263,6 @@ inline bool valid(u32 magic, Payload const &payload) {

static constexpr char const *REQUEST_SECTION = ".handover";

#define HandoverSection$() \
[[gnu::used, gnu::section(".handover")]]

// clang-format off

#define HandoverRequests$(...) \
HandoverSection$() \
static ::Handover::Request const __handover__[] = { \
{::Handover::Tag::MAGIC, 0, 0}, \
__VA_ARGS__ __VA_OPT__(, ) \
{::Handover::Tag::END, 0, 0}, \
};

// clang-format on

using EntryPoint = void (*)(u64 magic, Payload const *handover);

} // namespace Handover

0 comments on commit c721aee

Please sign in to comment.