ArkScript
A small, lisp-inspired, functional scripting language
Entity.hpp
Go to the documentation of this file.
1/**
2 * @file Entity.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
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,
28 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 constexpr uint16_t MaxValueForSmallNumber = 0x0800;
39
40 class Entity
41 {
42 public:
43 explicit Entity(Kind kind);
44
45 explicit Entity(Instruction inst, uint16_t arg = 0);
46
47 Entity(Instruction inst, uint16_t primary_arg, uint16_t secondary_arg);
48
49 Entity(Instruction inst, uint8_t inst2, uint8_t inst3, uint8_t inst4);
50
51 static Entity Label(label_t value);
52
54
55 static Entity GotoWithArg(const Entity& label, Instruction inst, uint16_t primary_arg);
56
57 static Entity GotoIf(const Entity& label, bool cond);
58
59 [[nodiscard]] Word bytecode() const;
60
61 [[nodiscard]] inline label_t label() const { return m_label; }
62
63 [[nodiscard]] inline Kind kind() const { return m_kind; }
64
65 [[nodiscard]] inline Instruction inst() const { return m_inst; }
66
67 [[nodiscard]] inline uint16_t primaryArg() const { return m_primary_arg; }
68
69 [[nodiscard]] inline uint16_t secondaryArg() const { return m_secondary_arg; }
70
71 [[nodiscard]] inline uint16_t tertiaryArg() const { return m_tertiary_arg; }
72
73 void setSourceLocation(const std::string& filename, std::size_t line);
74
75 [[nodiscard]] inline bool hasValidSourceLocation() const { return !m_source_file.empty(); }
76
77 [[nodiscard]] inline const std::string& filename() const { return m_source_file; }
78
79 [[nodiscard]] inline std::size_t sourceLine() const { return m_source_line; }
80
81 private:
85 uint16_t m_primary_arg { 0 };
86 uint16_t m_secondary_arg { 0 };
87 uint16_t m_tertiary_arg { 0 };
88 std::string m_source_file;
89 std::size_t m_source_line { 0 };
90 };
91
92 using Block = std::vector<Entity>;
93}
94
95#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:69
label_t label() const
Definition Entity.hpp:61
uint16_t tertiaryArg() const
Definition Entity.hpp:71
Instruction inst() const
Definition Entity.hpp:65
std::size_t sourceLine() const
Definition Entity.hpp:79
static Entity GotoIf(const Entity &label, bool cond)
Definition Entity.cpp:52
std::size_t m_source_line
Definition Entity.hpp:89
bool hasValidSourceLocation() const
Definition Entity.hpp:75
uint16_t primaryArg() const
Definition Entity.hpp:67
void setSourceLocation(const std::string &filename, std::size_t line)
Definition Entity.cpp:72
Word bytecode() const
Definition Entity.cpp:57
const std::string & filename() const
Definition Entity.hpp:77
std::string m_source_file
Definition Entity.hpp:88
constexpr uint16_t MaxValueForSmallNumber
Definition Entity.hpp:37
std::vector< Entity > Block
Definition Entity.hpp:92
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.