runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
frame.hpp
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_RUNTIME_DETAIL_FRAME_HPP
3#define CEL_RUNTIME_DETAIL_FRAME_HPP
4#include <cel/runtime/raw_stack.hpp>
5
6namespace cel::detail {
22struct borrowed_value {
23 const value_type& type;
24 const std::byte* data;
25private:
33 void require_type(const value_type& expected) const;
34
35public:
49 template <class T>
50 T as() const {
51 const auto expected = detail::boundary_adapter<T>::type();
52 require_type(expected);
53 return detail::boundary_adapter<T>::read(type, data);
54 }
55};
56
73class frame {
74 // [knowledge] _checkpoint protects both live owners and their preceding padding:
75 // each slot's previous cursor restores the byte origin when its suffix retires.
77 raw_stack* _stack;
79 frame* _parent;
81 std::size_t _checkpoint;
83 std::span<const value> _arguments;
85 std::span<const borrowed_value> _canonical_arguments;
87 const void* _context;
88 friend class raw_segment;
89 friend class raw_stack;
90 friend struct detail::frame_access;
91
95 void require_active() const;
103 void reset() noexcept;
112 template <class T>
113 T read_operand(std::size_t from_top, const value_type& expected) const {
114 return detail::boundary_adapter<T>::read(
115 expected, _stack->borrow_trusted(_checkpoint, from_top, expected));
116 }
128 template <class... T, std::size_t... I>
129 std::tuple<T...> acquire_operands(const std::array<value_type, sizeof...(T)>& types,
130 std::index_sequence<I...>) const {
131 require_active();
132 raw_stack::phase_guard phase(*_stack);
133 return {read_operand<T>(sizeof...(T) - 1 - I, types[I])...};
134 }
142 void discard(std::size_t count);
153 template <class T>
154 void push_result(const value_type& type, const T& source) {
155 require_active();
156 _stack->push_written(
157 type, &source, [](const void* input, const value_type& shape, std::byte* destination) {
158 detail::boundary_adapter<T>::write(*static_cast<const T*>(input), shape,
159 destination);
160 });
161 }
162
163public:
170 explicit frame(raw_stack& stack,
171 std::span<const value> arguments = {},
172 const void* context = nullptr);
186 frame(raw_stack& stack,
187 std::span<const borrowed_value> arguments,
188 const void* context = nullptr);
198 ~frame();
200 frame(const frame&) = delete;
202 frame& operator=(const frame&) = delete;
204 frame(frame&&) = delete;
206 frame& operator=(frame&&) = delete;
210 std::size_t depth() const noexcept;
216 raw_stack& stack() const noexcept { return *_stack; }
222 std::span<const value> arguments() const noexcept { return _arguments; }
228 std::span<const borrowed_value> canonical_arguments() const noexcept {
229 return _canonical_arguments;
230 }
238 borrowed_value argument(std::size_t index) const;
244 const void* context() const noexcept { return _context; }
257 void push_argument(std::size_t index);
269 template <class T>
270 void push(const T& source) {
271 push_result(detail::boundary_adapter<T>::type(), source);
272 }
287 template <class T>
288 T pop() {
289 require_active();
290 auto result = _stack->read_value<T>(_checkpoint);
291 discard(1);
292 return result;
293 }
304 void push_clone(const value& source);
313 void push_take(value& source);
323 value pop_value();
324};
325} // namespace cel::detail
326#endif