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 * @date 2024-10-05
6 *
7 * @copyright Copyright (c) 2024-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP
12#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP
13
14#include <cinttypes>
15#include <vector>
16#include <string>
17
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 void setSourceLocation(const std::string& filename, std::size_t line);
66
67 [[nodiscard]] inline bool hasValidSourceLocation() const { return !m_source_file.empty(); }
68
69 [[nodiscard]] inline const std::string& filename() const { return m_source_file; }
70
71 [[nodiscard]] inline std::size_t sourceLine() const { return m_source_line; }
72
73 private:
77 uint16_t m_primary_arg { 0 };
78 uint16_t m_secondary_arg { 0 };
79 std::string m_source_file;
80 std::size_t m_source_line { 0 };
81 };
82
83 using Block = std::vector<Entity>;
84}
85
86#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
std::size_t sourceLine() const
Definition Entity.hpp:71
static Entity GotoIf(const Entity &label, bool cond)
Definition Entity.cpp:36
std::size_t m_source_line
Definition Entity.hpp:80
bool hasValidSourceLocation() const
Definition Entity.hpp:67
uint16_t primaryArg() const
Definition Entity.hpp:61
void setSourceLocation(const std::string &filename, std::size_t line)
Definition Entity.cpp:53
Word bytecode() const
Definition Entity.cpp:44
const std::string & filename() const
Definition Entity.hpp:69
std::string m_source_file
Definition Entity.hpp:79
std::vector< Entity > Block
Definition Entity.hpp:83
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.