ArkScript
A small, fast, functional and scripting language for video games
Entity.hpp
Go to the documentation of this file.
1/**
2 * @file Entity.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief An entity in the IR is a bundle of information
5 * @version 1.0
6 * @date 2024-10-05
7 *
8 * @copyright Copyright (c) 2024
9 *
10 */
11
12#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP
13#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP
14
15#include <cinttypes>
16#include <vector>
17
18#include <Ark/Compiler/Word.hpp>
20
22{
23 enum class Kind
24 {
25 Label,
26 Goto,
29 Opcode,
31 };
32
33 using label_t = std::size_t;
34
35 /// The maximum value an argument can have when an IR entity has two arguments
36 constexpr uint16_t MaxValueForDualArg = 0x0fff;
37
38 class Entity
39 {
40 public:
41 explicit Entity(Kind kind);
42
43 explicit Entity(Instruction inst, uint16_t arg = 0);
44
45 Entity(Instruction inst, uint16_t primary_arg, uint16_t secondary_arg);
46
47 static Entity Label(label_t value);
48
49 static Entity Goto(const Entity& label);
50
51 static Entity GotoIf(const Entity& label, bool cond);
52
53 [[nodiscard]] Word bytecode() const;
54
55 [[nodiscard]] inline label_t label() const { return m_label; }
56
57 [[nodiscard]] inline Kind kind() const { return m_kind; }
58
59 [[nodiscard]] inline Instruction inst() const { return m_inst; }
60
61 [[nodiscard]] inline uint16_t primaryArg() const { return m_primary_arg; }
62
63 [[nodiscard]] inline uint16_t secondaryArg() const { return m_secondary_arg; }
64
65 private:
69 uint16_t m_primary_arg { 0 };
70 uint16_t m_secondary_arg { 0 };
71 };
72
73 using Block = std::vector<Entity>;
74}
75
76#endif // ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP
The different instructions used by the compiler and virtual machine.
Describe an instruction and its immediate argument.
uint16_t secondaryArg() const
Definition Entity.hpp:63
label_t label() const
Definition Entity.hpp:55
Instruction inst() const
Definition Entity.hpp:59
static Entity GotoIf(const Entity &label, bool cond)
Definition Entity.cpp:36
uint16_t primaryArg() const
Definition Entity.hpp:61
Word bytecode() const
Definition Entity.cpp:44
std::vector< Entity > Block
Definition Entity.hpp:73
constexpr uint16_t MaxValueForDualArg
The maximum value an argument can have when an IR entity has two arguments.
Definition Entity.hpp:36
std::size_t label_t
Definition Entity.hpp:33
Instruction
The different bytecodes are stored here.