2018-08-26 00:16:37 +01:00
|
|
|
/* This file is part of the sirit project.
|
2019-07-14 22:48:59 +01:00
|
|
|
* Copyright (c) 2019 sirit
|
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* 3-Clause BSD License
|
2018-08-26 00:16:37 +01:00
|
|
|
*/
|
|
|
|
|
2019-09-11 05:19:53 +01:00
|
|
|
#include <algorithm>
|
2018-08-26 00:16:37 +01:00
|
|
|
#include <cassert>
|
2019-11-27 08:25:27 +00:00
|
|
|
#include <limits>
|
2018-10-03 04:32:45 +01:00
|
|
|
|
2018-08-26 00:16:37 +01:00
|
|
|
#include "common_types.h"
|
2018-11-01 00:22:00 +00:00
|
|
|
#include "literal_number.h"
|
|
|
|
#include "literal_string.h"
|
2018-10-03 04:32:45 +01:00
|
|
|
#include "op.h"
|
|
|
|
#include "operand.h"
|
2018-08-26 00:16:37 +01:00
|
|
|
|
|
|
|
namespace Sirit {
|
|
|
|
|
2018-11-01 01:20:49 +00:00
|
|
|
Op::Op(spv::Op opcode, std::optional<u32> id, Id result_type)
|
2019-11-01 08:52:49 +00:00
|
|
|
: Operand{OperandType::Op}, opcode(opcode), result_type(result_type), id(id) {}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-08-26 00:34:06 +01:00
|
|
|
Op::~Op() = default;
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-08-26 00:34:06 +01:00
|
|
|
void Op::Fetch(Stream& stream) const {
|
2018-10-03 04:32:45 +01:00
|
|
|
assert(id.has_value());
|
|
|
|
stream.Write(id.value());
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:25:27 +00:00
|
|
|
std::size_t Op::GetWordCount() const noexcept {
|
2019-03-11 06:26:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2019-11-01 09:07:53 +00:00
|
|
|
bool Op::operator==(const Operand& other) const noexcept {
|
|
|
|
if (!EqualType(other)) {
|
2018-08-26 00:16:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-01 08:44:27 +00:00
|
|
|
const auto& op = static_cast<const Op&>(other);
|
2018-08-26 00:34:06 +01:00
|
|
|
if (op.opcode == opcode && result_type == op.result_type &&
|
|
|
|
operands.size() == op.operands.size()) {
|
2018-10-23 09:09:17 +01:00
|
|
|
for (std::size_t i = 0; i < operands.size(); i++) {
|
2018-08-26 00:34:06 +01:00
|
|
|
if (*operands[i] != *op.operands[i]) {
|
2018-08-26 00:16:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:34:06 +01:00
|
|
|
void Op::Write(Stream& stream) const {
|
2019-11-27 08:25:27 +00:00
|
|
|
stream.Write(static_cast<u16>(opcode), CalculateTotalWords());
|
2018-08-26 00:16:37 +01:00
|
|
|
|
|
|
|
if (result_type) {
|
|
|
|
result_type->Fetch(stream);
|
|
|
|
}
|
2018-10-03 04:32:45 +01:00
|
|
|
if (id.has_value()) {
|
|
|
|
stream.Write(id.value());
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
2018-10-23 09:09:17 +01:00
|
|
|
for (const auto* operand : operands) {
|
2018-08-26 00:16:37 +01:00
|
|
|
operand->Fetch(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 08:33:53 +00:00
|
|
|
void Op::Sink(std::unique_ptr<Operand> operand) {
|
|
|
|
Add(static_cast<const Operand*>(operand.get()));
|
|
|
|
operand_store.push_back(std::move(operand));
|
2018-10-23 09:02:18 +01:00
|
|
|
}
|
|
|
|
|
2018-10-28 16:44:12 +00:00
|
|
|
void Op::Add(const Literal& literal) {
|
2019-11-01 08:33:53 +00:00
|
|
|
Sink([&] {
|
2018-10-28 16:44:12 +00:00
|
|
|
switch (literal.index()) {
|
|
|
|
case 0:
|
|
|
|
return LiteralNumber::Create(std::get<0>(literal));
|
|
|
|
case 1:
|
|
|
|
return LiteralNumber::Create(std::get<1>(literal));
|
|
|
|
case 2:
|
|
|
|
return LiteralNumber::Create(std::get<2>(literal));
|
|
|
|
case 3:
|
|
|
|
return LiteralNumber::Create(std::get<3>(literal));
|
|
|
|
case 4:
|
|
|
|
return LiteralNumber::Create(std::get<4>(literal));
|
|
|
|
case 5:
|
|
|
|
return LiteralNumber::Create(std::get<5>(literal));
|
|
|
|
default:
|
2018-11-16 07:17:37 +00:00
|
|
|
assert(!"Invalid literal type");
|
2018-11-01 00:22:00 +00:00
|
|
|
abort();
|
2018-10-28 16:44:12 +00:00
|
|
|
}
|
2019-11-01 08:33:53 +00:00
|
|
|
}());
|
2018-10-28 16:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Op::Add(const std::vector<Literal>& literals) {
|
|
|
|
for (const auto& literal : literals) {
|
|
|
|
Add(literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 06:26:21 +00:00
|
|
|
void Op::Add(const Operand* operand) {
|
2019-09-11 05:19:53 +01:00
|
|
|
assert(operand);
|
2019-03-11 06:26:21 +00:00
|
|
|
operands.push_back(operand);
|
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2019-03-11 06:26:21 +00:00
|
|
|
void Op::Add(u32 integer) {
|
2019-11-27 08:25:27 +00:00
|
|
|
Sink(LiteralNumber::Create(integer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Op::Add(s32 integer) {
|
|
|
|
Sink(LiteralNumber::Create(integer));
|
2019-03-11 06:26:21 +00:00
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2019-03-14 06:52:54 +00:00
|
|
|
void Op::Add(std::string string) {
|
2019-11-01 08:33:53 +00:00
|
|
|
Sink(std::make_unique<LiteralString>(std::move(string)));
|
2019-03-11 06:26:21 +00:00
|
|
|
}
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-11-01 01:20:49 +00:00
|
|
|
void Op::Add(const std::vector<Id>& ids) {
|
2019-09-11 05:19:53 +01:00
|
|
|
assert(std::all_of(ids.begin(), ids.end(), [](auto id) { return id; }));
|
2019-03-14 07:12:55 +00:00
|
|
|
operands.insert(operands.end(), ids.begin(), ids.end());
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:25:27 +00:00
|
|
|
u16 Op::CalculateTotalWords() const noexcept {
|
|
|
|
std::size_t count = 1;
|
2018-08-26 00:16:37 +01:00
|
|
|
if (result_type) {
|
2019-11-27 08:25:27 +00:00
|
|
|
++count;
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
2018-10-03 04:32:45 +01:00
|
|
|
if (id.has_value()) {
|
2019-11-27 08:25:27 +00:00
|
|
|
++count;
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
for (const Operand* operand : operands) {
|
|
|
|
count += operand->GetWordCount();
|
|
|
|
}
|
2019-11-27 08:25:27 +00:00
|
|
|
assert(count < std::numeric_limits<u16>::max());
|
|
|
|
return static_cast<u16>(count);
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sirit
|