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
31 {
32 auto entity = Entity(Kind::Label);
33 entity.m_label = value;
34
35 return entity;
36 }
37
38 Entity Entity::Goto(const Entity& label, const Instruction inst)
39 {
40 auto jump = Entity(Kind::Goto);
41 jump.m_label = label.m_label;
42 jump.m_inst = inst;
43
44 return jump;
45 }
46
47 Entity Entity::GotoWithArg(const Entity& label, const Instruction inst, const uint16_t primary_arg)
48 {
49 auto jump = Entity(Kind::GotoWithArg);
50 jump.m_label = label.m_label;
51 jump.m_inst = inst;
52 jump.m_primary_arg = primary_arg;
53
54 return jump;
55 }
56
57 Entity Entity::GotoIf(const Entity& label, const bool cond)
58 {
60 }
61
63 {
64 if (m_kind == Kind::Opcode)
65 return Word(m_inst, m_primary_arg);
69 return Word(
70 m_inst,
71 static_cast<uint8_t>(m_primary_arg),
72 static_cast<uint8_t>(m_secondary_arg),
73 static_cast<uint8_t>(m_tertiary_arg));
74 return Word(0, 0);
75 }
76
77 void Entity::setSourceLocation(const std::string& filename, std::size_t line)
78 {
80 m_source_line = line;
81 }
82}
An entity in the IR is a bundle of information.
static Entity Goto(const Entity &label, Instruction inst=Instruction::JUMP)
Definition Entity.cpp:38
static Entity Label(label_t value)
Definition Entity.cpp:30
label_t label() const
Definition Entity.hpp:63
Instruction inst() const
Definition Entity.hpp:67
static Entity GotoIf(const Entity &label, bool cond)
Definition Entity.cpp:57
static Entity GotoWithArg(const Entity &label, Instruction inst, uint16_t primary_arg)
Definition Entity.cpp:47
std::size_t m_source_line
Definition Entity.hpp:91
void setSourceLocation(const std::string &filename, std::size_t line)
Definition Entity.cpp:77
Word bytecode() const
Definition Entity.cpp:62
const std::string & filename() const
Definition Entity.hpp:79
void replaceInstruction(Instruction replacement)
Definition Entity.cpp:25
std::string m_source_file
Definition Entity.hpp:90
std::size_t label_t
Definition Entity.hpp:33
Instruction
The different bytecodes are stored here.