Use unique_ptr for instruction implementations

This commit is contained in:
ReinUsesLisp 2018-11-01 00:02:45 -03:00
parent 63ca1b5243
commit f3a63aa55f
9 changed files with 77 additions and 89 deletions

View File

@ -233,13 +233,13 @@ class Module {
Id OpUndef(Id result_type); Id OpUndef(Id result_type);
private: private:
Id AddCode(Op* op); Id AddCode(std::unique_ptr<Op> op);
Id AddCode(spv::Op opcode, std::optional<std::uint32_t> id = {}); Id AddCode(spv::Op opcode, std::optional<std::uint32_t> id = {});
Id AddDeclaration(Op* op); Id AddDeclaration(std::unique_ptr<Op> op);
Id AddAnnotation(Op* op); Id AddAnnotation(std::unique_ptr<Op> op);
std::uint32_t bound{1}; std::uint32_t bound{1};

View File

@ -12,7 +12,6 @@ add_library(sirit
literal_string.cpp literal_string.cpp
literal_string.h literal_string.h
common_types.h common_types.h
insts.h
insts/type.cpp insts/type.cpp
insts/constant.cpp insts/constant.cpp
insts/function.cpp insts/function.cpp

View File

@ -1,18 +0,0 @@
/* 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.
*/
#pragma once
#include "op.h"
#include "stream.h"
namespace Sirit {
template <typename T> inline void AddEnum(Op* op, T value) {
op->Add(static_cast<u32>(value));
}
} // namespace Sirit

View File

@ -4,29 +4,32 @@
* Lesser General Public License version 2.1 or any later version. * Lesser General Public License version 2.1 or any later version.
*/ */
#include "insts.h" #include "common_types.h"
#include "op.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include <memory>
#include <vector>
namespace Sirit { namespace Sirit {
Id Module::Decorate(Id target, spv::Decoration decoration, Id Module::Decorate(Id target, spv::Decoration decoration,
const std::vector<Literal>& literals) { const std::vector<Literal>& literals) {
auto op{new Op(spv::Op::OpDecorate)}; auto op{std::make_unique<Op>(spv::Op::OpDecorate)};
op->Add(target); op->Add(target);
AddEnum(op, decoration); op->Add(static_cast<u32>(decoration));
op->Add(literals); op->Add(literals);
return AddAnnotation(op); return AddAnnotation(std::move(op));
} }
Id Module::MemberDecorate(Id structure_type, Literal member, Id Module::MemberDecorate(Id structure_type, Literal member,
spv::Decoration decoration, spv::Decoration decoration,
const std::vector<Literal>& literals) { const std::vector<Literal>& literals) {
auto op{new Op(spv::Op::OpMemberDecorate)}; auto op{std::make_unique<Op>(spv::Op::OpMemberDecorate)};
op->Add(structure_type); op->Add(structure_type);
op->Add(member); op->Add(member);
AddEnum(op, decoration); op->Add(static_cast<u32>(decoration));
op->Add(literals); op->Add(literals);
return AddAnnotation(op); return AddAnnotation(std::move(op));
} }
} // namespace Sirit } // namespace Sirit

View File

@ -4,48 +4,53 @@
* Lesser General Public License version 2.1 or any later version. * Lesser General Public License version 2.1 or any later version.
*/ */
#include "insts.h" #include "op.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include <cassert> #include <cassert>
namespace Sirit { namespace Sirit {
Id Module::OpConstantTrue(Id result_type) { Id Module::OpConstantTrue(Id result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantTrue, bound, result_type)); return AddDeclaration(
std::make_unique<Op>(spv::Op::OpConstantTrue, bound, result_type));
} }
Id Module::OpConstantFalse(Id result_type) { Id Module::OpConstantFalse(Id result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type)); return AddDeclaration(
std::make_unique<Op>(spv::Op::OpConstantFalse, bound, result_type));
} }
Id Module::OpConstant(Id result_type, const Literal& literal) { Id Module::OpConstant(Id result_type, const Literal& literal) {
auto op{new Op(spv::Op::OpConstant, bound, result_type)}; auto op{std::make_unique<Op>(spv::Op::OpConstant, bound, result_type)};
op->Add(literal); op->Add(literal);
return AddDeclaration(op); return AddDeclaration(std::move(op));
} }
Id Module::OpConstantComposite(Id result_type, Id Module::OpConstantComposite(Id result_type,
const std::vector<Id>& constituents) { const std::vector<Id>& constituents) {
auto op{new Op(spv::Op::OpConstantComposite, bound, result_type)}; auto op{
std::make_unique<Op>(spv::Op::OpConstantComposite, bound, result_type)};
op->Add(constituents); op->Add(constituents);
return AddDeclaration(op); return AddDeclaration(std::move(op));
} }
Id Module::OpConstantSampler(Id result_type, Id Module::OpConstantSampler(Id result_type,
spv::SamplerAddressingMode addressing_mode, spv::SamplerAddressingMode addressing_mode,
bool normalized, bool normalized,
spv::SamplerFilterMode filter_mode) { spv::SamplerFilterMode filter_mode) {
AddCapability(spv::Capability::LiteralSampler); AddCapability(spv::Capability::LiteralSampler);
AddCapability(spv::Capability::Kernel); AddCapability(spv::Capability::Kernel);
auto op{new Op(spv::Op::OpConstantSampler, bound, result_type)}; auto op{
AddEnum(op, addressing_mode); std::make_unique<Op>(spv::Op::OpConstantSampler, bound, result_type)};
op->Add(static_cast<u32>(addressing_mode));
op->Add(normalized ? 1 : 0); op->Add(normalized ? 1 : 0);
AddEnum(op, filter_mode); op->Add(static_cast<u32>(filter_mode));
return AddDeclaration(op); return AddDeclaration(std::move(op));
} }
Id Module::OpConstantNull(Id result_type) { Id Module::OpConstantNull(Id result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantNull, bound, result_type)); return AddDeclaration(
std::make_unique<Op>(spv::Op::OpConstantNull, bound, result_type));
} }
} // namespace Sirit } // namespace Sirit

View File

@ -4,16 +4,18 @@
* Lesser General Public License version 2.1 or any later version. * Lesser General Public License version 2.1 or any later version.
*/ */
#include "insts.h" #include "op.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include <memory>
#include <string>
namespace Sirit { namespace Sirit {
Id Module::Name(Id target, const std::string& name) { Id Module::Name(Id target, const std::string& name) {
auto op{new Op(spv::Op::OpName)}; auto op{std::make_unique<Op>(spv::Op::OpName)};
op->Add(target); op->Add(target);
op->Add(name); op->Add(name);
debug.push_back(std::unique_ptr<Op>(op)); debug.push_back(std::move(op));
return target; return target;
} }

View File

@ -4,42 +4,43 @@
* Lesser General Public License version 2.1 or any later version. * Lesser General Public License version 2.1 or any later version.
*/ */
#include "insts.h" #include "common_types.h"
#include "op.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include <vector>
namespace Sirit { namespace Sirit {
Id Module::OpLoopMerge(Id merge_block, Id continue_target, Id Module::OpLoopMerge(Id merge_block, Id continue_target,
spv::LoopControlMask loop_control, spv::LoopControlMask loop_control,
const std::vector<Id>& literals) { const std::vector<Id>& literals) {
auto op{new Op(spv::Op::OpLoopMerge)}; auto op{std::make_unique<Op>(spv::Op::OpLoopMerge)};
op->Add(merge_block); op->Add(merge_block);
op->Add(continue_target); op->Add(continue_target);
AddEnum(op, loop_control); op->Add(static_cast<u32>(loop_control));
op->Add(literals); op->Add(literals);
return AddCode(op); return AddCode(std::move(op));
} }
Id Module::OpSelectionMerge(Id merge_block, Id Module::OpSelectionMerge(Id merge_block,
spv::SelectionControlMask selection_control) { spv::SelectionControlMask selection_control) {
auto op{new Op(spv::Op::OpSelectionMerge)}; auto op{std::make_unique<Op>(spv::Op::OpSelectionMerge)};
op->Add(merge_block); op->Add(merge_block);
AddEnum(op, selection_control); op->Add(static_cast<u32>(selection_control));
return AddCode(op); return AddCode(std::move(op));
} }
Id Module::OpLabel() { return AddCode(spv::Op::OpLabel, bound++); } Id Module::OpLabel() { return AddCode(spv::Op::OpLabel, bound++); }
Id Module::OpBranch(Id target_label) { Id Module::OpBranch(Id target_label) {
auto op{new Op(spv::Op::OpBranch)}; auto op{std::make_unique<Op>(spv::Op::OpBranch)};
op->Add(target_label); op->Add(target_label);
return AddCode(op); return AddCode(std::move(op));
} }
Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label, Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label,
std::uint32_t true_weight, u32 true_weight, u32 false_weight) {
std::uint32_t false_weight) { auto op{std::make_unique<Op>(spv::Op::OpBranchConditional)};
auto op{new Op(spv::Op::OpBranchConditional)};
op->Add(condition); op->Add(condition);
op->Add(true_label); op->Add(true_label);
op->Add(false_label); op->Add(false_label);
@ -47,7 +48,7 @@ Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label,
op->Add(true_weight); op->Add(true_weight);
op->Add(false_weight); op->Add(false_weight);
} }
return AddCode(op); return AddCode(std::move(op));
} }
Id Module::OpReturn() { return AddCode(spv::Op::OpReturn); } Id Module::OpReturn() { return AddCode(spv::Op::OpReturn); }

View File

@ -4,18 +4,18 @@
* Lesser General Public License version 2.1 or any later version. * Lesser General Public License version 2.1 or any later version.
*/ */
#include "insts.h" #include "common_types.h"
#include "op.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
namespace Sirit { namespace Sirit {
Id Module::OpFunction(Id result_type, Id Module::OpFunction(Id result_type, spv::FunctionControlMask function_control,
spv::FunctionControlMask function_control, Id function_type) {
Id function_type) { auto op{std::make_unique<Op>(spv::Op::OpFunction, bound++, result_type)};
auto op{new Op{spv::Op::OpFunction, bound++, result_type}};
op->Add(static_cast<u32>(function_control)); op->Add(static_cast<u32>(function_control));
op->Add(function_type); op->Add(function_type);
return AddCode(op); return AddCode(std::move(op));
} }
Id Module::OpFunctionEnd() { return AddCode(spv::Op::OpFunctionEnd); } Id Module::OpFunctionEnd() { return AddCode(spv::Op::OpFunctionEnd); }

View File

@ -84,7 +84,6 @@ void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point,
} }
Id Module::Emit(Id op) { Id Module::Emit(Id op) {
assert(op);
code.push_back(op); code.push_back(op);
return op; return op;
} }
@ -95,34 +94,31 @@ Id Module::AddGlobalVariable(Id variable) {
return variable; return variable;
} }
Id Module::AddCode(Op* op) { Id Module::AddCode(std::unique_ptr<Op> op) {
assert(op); code_store.push_back(std::move(op));
code_store.push_back(std::unique_ptr<Op>(op)); return op.get();
return op;
} }
Id Module::AddCode(spv::Op opcode, std::optional<u32> id) { Id Module::AddCode(spv::Op opcode, std::optional<u32> id) {
return AddCode(new Op(opcode, id)); return AddCode(std::make_unique<Op>(opcode, id));
} }
Id Module::AddDeclaration(Op* op) { Id Module::AddDeclaration(std::unique_ptr<Op> op) {
const auto& found{ const auto& found{
std::find_if(declarations.begin(), declarations.end(), std::find_if(declarations.begin(), declarations.end(),
[&op](const auto& other) { return *other == *op; })}; [&op](const auto& other) { return *other == *op; })};
if (found != declarations.end()) { if (found != declarations.end()) {
delete op;
return found->get(); return found->get();
} else {
declarations.push_back(std::unique_ptr<Op>(op));
bound++;
return op;
} }
const auto id = op.get();
declarations.push_back(std::move(op));
bound++;
return id;
} }
Id Module::AddAnnotation(Op* op) { Id Module::AddAnnotation(std::unique_ptr<Op> op) {
assert(op); annotations.push_back(std::move(op));
annotations.push_back(std::unique_ptr<Op>(op)); return op.get();
return op;
} }
} // namespace Sirit } // namespace Sirit