2#ifndef CEL_RUNTIME_RAW_SEGMENT_HPP
3#define CEL_RUNTIME_RAW_SEGMENT_HPP
5#include <cel/runtime/detail/frame.hpp>
6#include <cel/runtime/detail/retirement.hpp>
14namespace cel::detail {
15struct instruction_storage;
37 detail::retirement_ptr<detail::instruction_storage> _storage;
38 friend struct detail::segment_access;
46 void* allocate_capture(std::size_t size, std::size_t alignment);
56 void append(
const void* capture,
57 void (*execute)(
const void*, frame&),
58 void (*destroy)(
const void*)
noexcept,
59 void (*retire)(
const void*, detail::retirement_queue&)
noexcept =
nullptr);
62 template <
class F,
class R,
class... Args>
64 std::array<value_type,
sizeof...(Args)> inputs;
65 std::array<value_type, std::is_void_v<R> ? 0 : 1>
71 static auto result_types() {
72 if constexpr (std::is_void_v<R>)
73 return std::array<value_type, 0>{};
75 return std::array{detail::boundary_adapter<R>::type()};
81 requires std::constructible_from<F, G&&>
explicit operation(G&& input) :
82 inputs{detail::boundary_adapter<Args>::type()...}, outputs(result_types()),
83 callback(std::forward<G>(input)) {}
91 template <std::size_t... I>
92 void invoke(frame& scope, std::index_sequence<I...>)
const {
94 scope.template acquire_operands<Args...>(inputs, std::index_sequence<I...>{});
95 scope.discard(
sizeof...(Args));
96 if constexpr (std::is_void_v<R>) {
97 std::apply([&](
auto&... args) { std::invoke(callback, scope, std::move(args)...); },
100 R result = std::apply(
101 [&](
auto&... args) -> R {
102 return std::invoke(callback, scope, std::move(args)...);
105 scope.push_result(outputs[0], result);
113 static void execute(
const void* capture, frame& scope) {
114 static_cast<const operation*
>(capture)->invoke(scope,
115 std::index_sequence_for<Args...>{});
122 static void destroy(
const void* capture)
noexcept {
123 std::destroy_at(
static_cast<const operation*
>(capture));
129 raw_segment() noexcept;
136 raw_segment(const raw_segment&) = delete;
137 raw_segment& operator=(const raw_segment&) = delete;
139 raw_segment(raw_segment&& other) noexcept;
145 raw_segment& operator=(raw_segment&& other) noexcept;
163 void execute(frame& scope) const;
182 template <class R, class... Args, class F>
183 void push_op(F&& callback) {
184 using capture_type = std::decay_t<F>;
185 static_assert(std::is_nothrow_destructible_v<capture_type>);
186 static_assert(std::is_invocable_r_v<R,
const capture_type&, frame&, Args...>);
187 using stored = operation<capture_type, R, Args...>;
188 auto* location =
static_cast<stored*
>(allocate_capture(
sizeof(stored),
alignof(stored)));
189 auto* capture = std::construct_at(location, std::forward<F>(callback));
191 append(capture, stored::execute, stored::destroy);
193 std::destroy_at(capture);
202 template <
class R,
class F>
203 void push_op0(F&& callback) {
204 push_op<R>(std::forward<F>(callback));
211 template <
class R,
class A,
class F>
212 void push_op1(F&& callback) {
213 push_op<R, A>(std::forward<F>(callback));
221 template <
class R,
class A,
class B,
class F>
222 void push_op2(F&& callback) {
223 push_op<R, A, B>(std::forward<F>(callback));
231 template <
class R,
class A,
class B,
class C,
class F>
232 void push_op3(F&& callback) {
233 push_op<R, A, B, C>(std::forward<F>(callback));