runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
raw_segment.hpp
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_RUNTIME_RAW_SEGMENT_HPP
3#define CEL_RUNTIME_RAW_SEGMENT_HPP
4#include <array>
5#include <cel/runtime/detail/frame.hpp>
6#include <cel/runtime/detail/retirement.hpp>
7#include <concepts>
8#include <functional>
9#include <memory>
10#include <tuple>
11#include <type_traits>
12#include <utility>
13
14namespace cel::detail {
15struct instruction_storage;
16struct segment_access;
36class raw_segment {
37 detail::retirement_ptr<detail::instruction_storage> _storage;
38 friend struct detail::segment_access;
39
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);
60
62 template <class F, class R, class... Args>
63 struct operation {
64 std::array<value_type, sizeof...(Args)> inputs;
65 std::array<value_type, std::is_void_v<R> ? 0 : 1>
66 outputs;
67 F callback;
71 static auto result_types() {
72 if constexpr (std::is_void_v<R>)
73 return std::array<value_type, 0>{};
74 else
75 return std::array{detail::boundary_adapter<R>::type()};
76 }
80 template <class G>
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 {
93 auto arguments =
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)...); },
98 arguments);
99 } else {
100 R result = std::apply(
101 [&](auto&... args) -> R {
102 return std::invoke(callback, scope, std::move(args)...);
103 },
104 arguments);
105 scope.push_result(outputs[0], result);
106 }
107 }
113 static void execute(const void* capture, frame& scope) {
114 static_cast<const operation*>(capture)->invoke(scope,
115 std::index_sequence_for<Args...>{});
116 }
122 static void destroy(const void* capture) noexcept {
123 std::destroy_at(static_cast<const operation*>(capture));
124 }
125 };
126
127public:
129 raw_segment() noexcept;
135 ~raw_segment();
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;
162 // [knowledge] Retain release-build depth/layout checks even for trusted instruction signatures.
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));
190 try {
191 append(capture, stored::execute, stored::destroy);
192 } catch (...) {
193 std::destroy_at(capture);
194 throw;
195 }
196 }
202 template <class R, class F>
203 void push_op0(F&& callback) {
204 push_op<R>(std::forward<F>(callback));
205 }
211 template <class R, class A, class F>
212 void push_op1(F&& callback) {
213 push_op<R, A>(std::forward<F>(callback));
214 }
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));
224 }
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));
234 }
235};
236} // namespace cel::detail
237#endif