Sen API
Sen Libraries
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1// === vm.h ============================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_LANG_VM_H
9#define SEN_CORE_LANG_VM_H
10
11// sen
14#include "sen/core/base/span.h"
17
18// std
19#include <cstdint>
20#include <functional>
21#include <stack>
22#include <string>
23#include <variant>
24#include <vector>
25
26namespace sen::lang
27{
28
31
32constexpr std::size_t stackMax = 256U;
33
57
60{
61};
62
64using Value = std::variant<float32_t,
66 int32_t,
67 uint32_t,
68 int64_t,
69 uint64_t,
70 bool,
71 std::string,
72 uint8_t,
73 int16_t,
74 uint16_t,
76
78using ValueGetter = std::function<Value()>;
79
82
84template <typename T>
85[[nodiscard]] inline T extract(const Value& value)
86{
87 return std::get<T>(value);
88}
89
91template <typename T>
92[[nodiscard]] inline bool holds(const Value& value) noexcept
93{
94 return std::holds_alternative<T>(value);
95}
96
100class Chunk
101{
102 SEN_MOVE_ONLY(Chunk)
103
104public: // special members
105 Chunk() = default;
106 ~Chunk() = default;
107
108public:
110 void disassemble(const std::string& name) const;
111
113 [[nodiscard]] uint8_t addConstant(const Value& value);
114
116 [[nodiscard]] const Value& getConstant(uint8_t offset) const;
117
119 void addCode(uint8_t byte);
120
123
125 [[nodiscard]] uint8_t getOrRegisterVariable(const std::string& name);
126
128 [[nodiscard]] bool isValid() const noexcept;
129
131 [[nodiscard]] const uint8_t* code() const noexcept;
132
134 [[nodiscard]] int count() const noexcept;
135
137 void patch(std::size_t offset, uint8_t byte);
138
140 [[nodiscard]] std::size_t disassembleInstruction(std::size_t offset) const;
141
143 static void printValue(const Value& value);
144
145private:
146 [[nodiscard]] std::size_t printSimpleInstruction(std::string_view name, std::size_t offset) const;
147 [[nodiscard]] std::size_t printConstantInstruction(std::string_view name, std::size_t offset) const;
148 [[nodiscard]] std::size_t printJumpInstruction(std::string_view name, std::size_t offset) const;
149 [[nodiscard]] std::size_t printVariableInstruction(std::size_t offset) const;
150
151private:
152 std::vector<uint8_t> code_;
153 StaticVector<Value, stackMax> constants_;
154 StaticVector<std::string, stackMax> variablesDef_;
155};
156
158class VM
159{
160 SEN_MOVE_ONLY(VM)
161
162public:
163 VM() = default;
164 ~VM() = default;
165
166public:
169 {
170 std::string what;
171 };
172
175 {
176 std::string what;
177 };
178
179public:
181 [[nodiscard]] Result<Value, RuntimeError> interpret(const Chunk& chunk, Environment environment = {});
182
184 [[nodiscard]] QueryStatement parse(const std::string& query) const;
185
187 [[nodiscard]] Result<Chunk, CompileError> compile(const QueryStatement& statement) const;
188
189private:
190 [[nodiscard]] SEN_ALWAYS_INLINE uint8_t readByte();
191 [[nodiscard]] SEN_ALWAYS_INLINE uint16_t readShort();
192 [[nodiscard]] SEN_ALWAYS_INLINE Value readConstant();
193 [[nodiscard]] inline Value pop();
194 inline void push(Value value);
195 inline void negate();
196 inline void add();
197 inline void subtract();
198 inline void multiply();
199 inline void divide();
200 inline void equal();
201 inline void notEqual();
202 inline void lowerThan();
203 inline void lowerOrEqualThan();
204 inline void greaterThan();
205 inline void greaterOrEqualThan();
206 inline void doAnd();
207 inline void doOr();
208 inline void jumpIfFalse(uint16_t offset);
209 inline void jumpIfTrue(uint16_t offset);
210 inline void fetchVariable(uint8_t index);
211 inline void between();
212
213private:
214 template <typename C>
215 inline void mathOperation(const Value& a, const Value& b, C op);
216
217 template <typename C>
218 inline void comparisonOperation(const Value& a, const Value& b, C op);
219
220private:
221 using Stack = std::stack<Value, StaticVector<Value, stackMax>>;
222
223private:
224 const Chunk* currentChunk_ = nullptr;
225 Environment environment_;
226 const uint8_t* ip_ = nullptr;
227 Stack stack_;
228};
229
231
232} // namespace sen::lang
233
234#endif // SEN_CORE_LANG_VM_H
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
Stack-based, exception-free and resizable vector with fixed-capacity.
Definition static_vector.h:571
A chunk of byte code. It holds code and constants needed by the code. Constants are accessed by index...
Definition vm.h:101
const Value & getConstant(uint8_t offset) const
The constant stored at a given slot.
bool isValid() const noexcept
Returns true if it contains code.
uint8_t getOrRegisterVariable(const std::string &name)
Registers a variable and obtains the index.
std::size_t disassembleInstruction(std::size_t offset) const
Prints a human-readable view of the instruction at a given offset.
static void printValue(const Value &value)
Prints a value.
void patch(std::size_t offset, uint8_t byte)
Patches the a code byte at some offset.
Span< const std::string > getVariables() const
The variables used by this chunk.
void addCode(uint8_t byte)
Appends a byte to the code. The line is just for debugging.
void disassemble(const std::string &name) const
Prints a human-readable view of the chunk.
const uint8_t * code() const noexcept
The stored code.
int count() const noexcept
The size of the code.
~Chunk()=default
uint8_t addConstant(const Value &value)
Stores a constant and returns the index that can be used to refer to it.
~VM()=default
std::string what
Definition vm.h:170
QueryStatement parse(const std::string &query) const
Parse a query string into a statement. Throws in case of error.
std::string what
Definition vm.h:176
VM()=default
Result< Chunk, CompileError > compile(const QueryStatement &statement) const
Compile source code into a chunk.
Result< Value, RuntimeError > interpret(const Chunk &chunk, Environment environment={})
Interpret a chunk of code.
Compilation failure report.
Definition vm.h:169
Runtime error report.
Definition vm.h:175
bool holds(const Value &value) noexcept
True if the variant holds T.
Definition vm.h:92
std::function< Value()> ValueGetter
Gets a value from some source.
Definition vm.h:78
Span< ValueGetter > Environment
The environment is an indexed list of value getter functions.
Definition vm.h:81
OpCode
Instructions that can be executed by the virtual machine.
Definition vm.h:36
T extract(const Value &value)
Get T out of the value variant.
Definition vm.h:85
constexpr std::size_t stackMax
Definition vm.h:32
std::variant< float32_t, float64_t, int32_t, uint32_t, int64_t, uint64_t, bool, std::string, uint8_t, int16_t, uint16_t, VariantAccessError > Value
A value that can be in the stack.
Definition vm.h:64
@ multiply
Definition stl_expression.h:40
@ add
Definition stl_expression.h:38
@ divide
Definition stl_expression.h:41
@ subtract
Definition stl_expression.h:39
@ negate
Definition stl_expression.h:25
@ opJumpIfTrue
Jumps to an offset if the top-most value is true.
Definition vm.h:52
@ opJumpIfFalse
Jumps to an offset if the top-most value is false.
Definition vm.h:53
@ opMul
Multiplies the two top-most values on the stack.
Definition vm.h:42
@ opEqual
Compares the two top-most values for equality.
Definition vm.h:44
@ opLowerThan
Compares the two top-most values for <.
Definition vm.h:48
@ opOr
Logical or of the two top-most values.
Definition vm.h:51
@ opDiv
Divides the two top-most values on the stack.
Definition vm.h:43
@ opLowerOrEqualThan
Compares the two top-most values for <=.
Definition vm.h:49
@ opReturn
Finishes execution.
Definition vm.h:37
@ opSub
Subtracts the two top-most values on the stack.
Definition vm.h:41
@ opAnd
Logical and of the two top-most values.
Definition vm.h:50
@ opConstant
Push a constant to the stack. Requires an index as the argument.
Definition vm.h:38
@ opGreaterThan
Compares the two top-most values for >.
Definition vm.h:46
@ opBetween
Checks that the top and second values are <= and >= to the third value.
Definition vm.h:55
@ opNegate
Negates the value at the top of the stack.
Definition vm.h:39
@ opNotEqual
Compares the two top-most values for inequality.
Definition vm.h:45
@ opGreaterOrEqualThan
Compares the two top-most values for >=.
Definition vm.h:47
@ opAdd
Adds the two top-most values on the stack.
Definition vm.h:40
@ opFetchVariable
Fetches a variable in a given index and pushes the value.
Definition vm.h:54
@ equal
Definition stl_token.h:49
Definition stl_statement.h:204
Represents a variant that could not be accessed.
Definition vm.h:60
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Definition code_location.h:14
STL namespace.