2018-08-26 00:16:37 +01:00
|
|
|
/* 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
|
2018-08-27 03:28:39 +01:00
|
|
|
* Lesser General Public License version 2.1 or any later version.
|
2018-08-26 00:16:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
2018-10-03 04:32:45 +01:00
|
|
|
|
2018-08-26 00:16:37 +01:00
|
|
|
#include "common_types.h"
|
2018-08-28 08:16:52 +01:00
|
|
|
#include "lnumber.h"
|
|
|
|
#include "lstring.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-10-03 04:32:45 +01:00
|
|
|
Op::Op(spv::Op opcode, std::optional<u32> id, Ref result_type)
|
|
|
|
: opcode(opcode), id(id), result_type(result_type) {
|
2018-08-27 04:29:40 +01:00
|
|
|
operand_type = OperandType::Op;
|
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
|
|
|
}
|
|
|
|
|
2018-10-03 04:32:45 +01:00
|
|
|
u16 Op::GetWordCount() const { return 1; }
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-08-26 00:34:06 +01:00
|
|
|
bool Op::operator==(const Operand& other) const {
|
2018-08-26 00:16:37 +01:00
|
|
|
if (operand_type != other.GetType()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-26 00:34:06 +01:00
|
|
|
const Op& op = dynamic_cast<const Op&>(other);
|
|
|
|
if (op.opcode == opcode && result_type == op.result_type &&
|
|
|
|
operands.size() == op.operands.size()) {
|
2018-08-26 00:16:37 +01:00
|
|
|
for (std::size_t i{}; 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 {
|
2018-08-26 00:16:37 +01:00
|
|
|
stream.Write(static_cast<u16>(opcode));
|
|
|
|
stream.Write(WordCount());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
for (const Operand* operand : operands) {
|
|
|
|
operand->Fetch(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 08:52:07 +01:00
|
|
|
void Op::Sink(Operand* operand) {
|
2018-08-26 00:16:37 +01:00
|
|
|
Add(static_cast<const Operand*>(operand));
|
|
|
|
operand_store.push_back(std::unique_ptr<Operand>(operand));
|
|
|
|
}
|
|
|
|
|
2018-10-03 04:32:45 +01:00
|
|
|
void Op::Add(const Operand* operand) { operands.push_back(operand); }
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-10-03 04:32:45 +01:00
|
|
|
void Op::Add(u32 integer) { Add(LiteralNumber::Create<u32>(integer)); }
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-10-03 04:32:45 +01:00
|
|
|
void Op::Add(const std::string& string) { Add(new LiteralString(string)); }
|
2018-08-26 00:16:37 +01:00
|
|
|
|
2018-08-28 08:01:21 +01:00
|
|
|
void Op::Add(const std::vector<Ref>& ids) {
|
|
|
|
for (Ref op : ids) {
|
2018-08-26 00:34:06 +01:00
|
|
|
Add(op);
|
2018-08-26 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 08:45:56 +01:00
|
|
|
void Op::Add(const std::vector<Operand*>& operands) {
|
|
|
|
for (Operand* operand : operands) {
|
|
|
|
Add(operand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:34:06 +01:00
|
|
|
u16 Op::WordCount() const {
|
2018-08-26 00:16:37 +01:00
|
|
|
u16 count{1};
|
|
|
|
if (result_type) {
|
|
|
|
count++;
|
|
|
|
}
|
2018-10-03 04:32:45 +01:00
|
|
|
if (id.has_value()) {
|
2018-08-26 00:16:37 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
for (const Operand* operand : operands) {
|
|
|
|
count += operand->GetWordCount();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sirit
|