ArkScript
A small, lisp-inspired, functional scripting language
Entity.cpp
Go to the documentation of this file.
2
3namespace Ark::internal::IR
4{
5 Entity::Entity(const Kind kind) :
6 m_kind(kind),
7 m_inst(NOP)
8 {}
9
10 Entity::Entity(const Instruction inst, const uint16_t arg) :
11 m_kind(Kind::Opcode),
12 m_inst(inst), m_primary_arg(arg)
13 {}
14
15 Entity::Entity(const Instruction inst, const uint16_t primary_arg, const uint16_t secondary_arg) :
16 m_kind(Kind::Opcode2Args),
17 m_inst(inst), m_primary_arg(primary_arg), m_secondary_arg(secondary_arg)
18 {}
19
20 Entity::Entity(const Instruction inst, const uint8_t inst2, const uint8_t inst3, const uint8_t inst4) :
21 m_kind(Kind::Opcode3Args),
22 m_inst(inst), m_primary_arg(inst2), m_secondary_arg(inst3), m_tertiary_arg(inst4)
23 {}
24
26 {
27 m_inst = replacement;
28 }
29
30 void Entity::replaceLabel(const label_t replacement)
31 {
32 m_label = replacement;
33 }
34
36 {
37 auto entity = Entity(Kind::Label);
38 entity.m_label = value;
39
40 return entity;
41 }
42
43 Entity Entity::Goto(const Entity& label, const Instruction inst)
44 {
45 auto jump = Entity(Kind::Goto);
46 jump.m_label = label.m_label;
47 jump.m_inst = inst;
48
49 return jump;
50 }
51
52 Entity Entity::GotoWithArg(const Entity& label, const Instruction inst, const uint16_t primary_arg)
53 {
54 auto jump = Entity(Kind::GotoWithArg);
55 jump.m_label = label.m_label;
56 jump.m_inst = inst;
57 jump.m_primary_arg = primary_arg;
58
59 return jump;
60 }
61
62 Entity Entity::GotoIf(const Entity& label, const bool cond)
63 {
64 return Goto(label, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE);
65 }
66
68 {
69 if (m_kind == Kind::Opcode)
70 return Word(m_inst, m_primary_arg);
74 return Word(
75 m_inst,
76 static_cast<uint8_t>(m_primary_arg),
77 static_cast<uint8_t>(m_secondary_arg),
78 static_cast<uint8_t>(m_tertiary_arg));
79 return Word(0, 0);
80 }
81
82 void Entity::setSourceLocation(const std::string& filename, const std::size_t line)
83 {
84 m_metadata.source_file = filename;
85 m_metadata.source_line = line;
86 }
87
88 void Entity::setRelatedResourceId(const std::optional<uint16_t> id)
89 {
90 m_metadata.related_res_id = id;
91 }
92}
An entity in the IR is a bundle of information.
static Entity Goto(const Entity &label, Instruction inst=Instruction::JUMP)
Create a new Goto IR Entity.
Definition Entity.cpp:43
static Entity Label(label_t value)
Create a new Label IR Entity.
Definition Entity.cpp:35
label_t label() const
Return the label of the IR Entity.
Definition Entity.hpp:160
Entity(Kind kind)
Create a new IR Entity.
Definition Entity.cpp:5
Instruction inst() const
Return the underlying instruction of the IR Entity.
Definition Entity.hpp:174
void replaceLabel(label_t replacement)
Definition Entity.cpp:30
static Entity GotoIf(const Entity &label, bool cond)
Create a new Goto IR Entity.
Definition Entity.cpp:62
void setRelatedResourceId(std::optional< uint16_t > id)
Definition Entity.cpp:88
static Entity GotoWithArg(const Entity &label, Instruction inst, uint16_t primary_arg)
Create a new Goto IR Entity.
Definition Entity.cpp:52
void setSourceLocation(const std::string &filename, std::size_t line)
Set the source location for an IR Entity, which is used to generate the file loc table.
Definition Entity.cpp:82
Word bytecode() const
Return the bytecode representation of the IR Entity if it's an Opcode.
Definition Entity.cpp:67
const std::string & filename() const
Definition Entity.hpp:209
void replaceInstruction(Instruction replacement)
Definition Entity.cpp:25
struct Ark::internal::IR::Entity::@0 m_metadata
std::size_t label_t
Definition Entity.hpp:35
Instruction
The different bytecodes are stored here.