aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/op.cpp
blob: a197126b7981cd952d93e64a86b54436275d6742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* This file is part of the sirit project.
 * Copyright (c) 2018 ReinUsesLisp
 * This software may be used and distributed according to the terms of the GNU
 * Lesser General Public License version 2.1 or any later version.
 */

#include <cassert>
#include "common_types.h"
#include "operand.h"
#include "op.h"

namespace Sirit {

Op::Op(spv::Op opcode_, u32 id_, const Op* result_type_) 
    : opcode(opcode_), id(id_), result_type(result_type_) {
    operand_type = OperandType::Ref;
}

Op::~Op() = default;

void Op::Fetch(Stream& stream) const {
    assert(id != UINT32_MAX);
    stream.Write(id);
}

u16 Op::GetWordCount() const {
    return 1;
}

bool Op::operator==(const Operand& other) const {
    if (operand_type != other.GetType()) {
        return false;
    }
    const Op& op = dynamic_cast<const Op&>(other);
    if (op.opcode == opcode && result_type == op.result_type &&
        operands.size() == op.operands.size()) {
        for (std::size_t i{}; i < operands.size(); i++) {
            if (*operands[i] != *op.operands[i]) {
                return false;
            }
        }
        return true;
    }
    return false;
}

void Op::Write(Stream& stream) const {
    stream.Write(static_cast<u16>(opcode));
    stream.Write(WordCount());

    if (result_type) {
        result_type->Fetch(stream);
    }
    if (id != UINT32_MAX) {
        stream.Write(id);
    }
    for (const Operand* operand : operands) {
        operand->Fetch(stream);
    }
}

void Op::Add(Operand* operand) {
    Add(static_cast<const Operand*>(operand));
    operand_store.push_back(std::unique_ptr<Operand>(operand));
}

void Op::Add(const Operand* operand) {
    operands.push_back(operand);
}

void Op::Add(u32 integer) {
    Add(new LiteralInteger(integer));
}

void Op::Add(const std::string& string) {
    Add(new LiteralString(string));
}

void Op::Add(const std::vector<const Op*>& ids) {
    for (const Op* op : ids) {
        Add(op);
    }
}

u16 Op::WordCount() const {
    u16 count{1};
    if (result_type) {
        count++;
    }
    if (id != UINT32_MAX) {
        count++;
    }
    for (const Operand* operand : operands) {
        count += operand->GetWordCount();
    }
    return count;
}

} // namespace Sirit