ArkScript
A small, lisp-inspired, functional scripting language
Entity.hpp
Go to the documentation of this file.
1/**
2 * @file Entity.hpp
3 * @author Alexandre 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,
30 };
31
32 using label_t = std::size_t;
33
34 /// The maximum value an argument can have when an IR entity has two arguments
35 constexpr uint16_t MaxValueForDualArg = 0x0fff;
36
37 class Entity
38 {
39 public:
40 explicit Entity(Kind kind);
41
42 explicit Entity(Instruction inst, uint16_t arg = 0);
43
44 Entity(Instruction inst, uint16_t primary_arg, uint16_t secondary_arg);
45
46 static Entity Label(label_t value);
47
49
50 static Entity GotoWithArg(const Entity& label, Instruction inst, uint16_t primary_arg);
51
52 static Entity GotoIf(const Entity& label, bool cond);
53
54 [[nodiscard]] Word bytecode() const;
55
56 [[nodiscard]] inline label_t label() const { return m_label; }
57
58 [[nodiscard]] inline Kind kind() const { return m_kind; }
59
60 [[nodiscard]] inline Instruction inst() const { return m_inst; }
61
62 [[nodiscard]] inline uint16_t primaryArg() const { return m_primary_arg; }
63
64 [[nodiscard]] inline uint16_t secondaryArg() const { return m_secondary_arg; }
65
66 void setSourceLocation(const std::string& filename, std::size_t line);
67
68 [[nodiscard]] inline bool hasValidSourceLocation() const { return !m_source_file.empty(); }
69
70 [[nodiscard]] inline const std::string& filename() const { return m_source_file; }
71
72 [[nodiscard]] inline std::size_t sourceLine() const { return m_source_line; }
73
74 private:
78 uint16_t m_primary_arg { 0 };
79 uint16_t m_secondary_arg { 0 };
80 std::string m_source_file;
81 std::size_t m_source_line { 0 };
82 };
83
84 using Block = std::vector<Entity>;
85}
86
87#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:64
label_t label() const
Definition Entity.hpp:56
Instruction inst() const
Definition Entity.hpp:60
std::size_t sourceLine() const
Definition Entity.hpp:72
static Entity GotoIf(const Entity &label, bool cond)
Definition Entity.cpp:47
std::size_t m_source_line
Definition Entity.hpp:81
bool hasValidSourceLocation() const
Definition Entity.hpp:68
uint16_t primaryArg() const
Definition Entity.hpp:62
void setSourceLocation(const std::string &filename, std::size_t line)
Definition Entity.cpp:61
Word bytecode() const
Definition Entity.cpp:52
const std::string & filename() const
Definition Entity.hpp:70
std::string m_source_file
Definition Entity.hpp:80
std::vector< Entity > Block
Definition Entity.hpp:84
constexpr uint16_t MaxValueForDualArg
The maximum value an argument can have when an IR entity has two arguments.
Definition Entity.hpp:35
std::size_t label_t
Definition Entity.hpp:32
Instruction
The different bytecodes are stored here.