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