From f3a63aa55f4989d6806b2faa8086bd2f23752b01 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 1 Nov 2018 00:02:45 -0300 Subject: [PATCH] Use unique_ptr for instruction implementations --- include/sirit/sirit.h | 6 +++--- src/CMakeLists.txt | 1 - src/insts.h | 18 ------------------ src/insts/annotation.cpp | 23 +++++++++++++---------- src/insts/constant.cpp | 37 +++++++++++++++++++++---------------- src/insts/debug.cpp | 8 +++++--- src/insts/flow.cpp | 33 +++++++++++++++++---------------- src/insts/function.cpp | 12 ++++++------ src/sirit.cpp | 28 ++++++++++++---------------- 9 files changed, 77 insertions(+), 89 deletions(-) delete mode 100644 src/insts.h diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 2fa1d24..21cde3d 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -233,13 +233,13 @@ class Module { Id OpUndef(Id result_type); private: - Id AddCode(Op* op); + Id AddCode(std::unique_ptr op); Id AddCode(spv::Op opcode, std::optional id = {}); - Id AddDeclaration(Op* op); + Id AddDeclaration(std::unique_ptr op); - Id AddAnnotation(Op* op); + Id AddAnnotation(std::unique_ptr op); std::uint32_t bound{1}; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 86e33d3..3f3ff24 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,6 @@ add_library(sirit literal_string.cpp literal_string.h common_types.h - insts.h insts/type.cpp insts/constant.cpp insts/function.cpp diff --git a/src/insts.h b/src/insts.h deleted file mode 100644 index b08b58a..0000000 --- a/src/insts.h +++ /dev/null @@ -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 inline void AddEnum(Op* op, T value) { - op->Add(static_cast(value)); -} - -} // namespace Sirit diff --git a/src/insts/annotation.cpp b/src/insts/annotation.cpp index 7fe9d37..400af8d 100644 --- a/src/insts/annotation.cpp +++ b/src/insts/annotation.cpp @@ -4,29 +4,32 @@ * 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 +#include namespace Sirit { Id Module::Decorate(Id target, spv::Decoration decoration, - const std::vector& literals) { - auto op{new Op(spv::Op::OpDecorate)}; + const std::vector& literals) { + auto op{std::make_unique(spv::Op::OpDecorate)}; op->Add(target); - AddEnum(op, decoration); + op->Add(static_cast(decoration)); op->Add(literals); - return AddAnnotation(op); + return AddAnnotation(std::move(op)); } Id Module::MemberDecorate(Id structure_type, Literal member, - spv::Decoration decoration, - const std::vector& literals) { - auto op{new Op(spv::Op::OpMemberDecorate)}; + spv::Decoration decoration, + const std::vector& literals) { + auto op{std::make_unique(spv::Op::OpMemberDecorate)}; op->Add(structure_type); op->Add(member); - AddEnum(op, decoration); + op->Add(static_cast(decoration)); op->Add(literals); - return AddAnnotation(op); + return AddAnnotation(std::move(op)); } } // namespace Sirit diff --git a/src/insts/constant.cpp b/src/insts/constant.cpp index ec0c730..507b3f4 100644 --- a/src/insts/constant.cpp +++ b/src/insts/constant.cpp @@ -4,48 +4,53 @@ * Lesser General Public License version 2.1 or any later version. */ -#include "insts.h" +#include "op.h" #include "sirit/sirit.h" #include namespace Sirit { Id Module::OpConstantTrue(Id result_type) { - return AddDeclaration(new Op(spv::Op::OpConstantTrue, bound, result_type)); + return AddDeclaration( + std::make_unique(spv::Op::OpConstantTrue, bound, result_type)); } Id Module::OpConstantFalse(Id result_type) { - return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type)); + return AddDeclaration( + std::make_unique(spv::Op::OpConstantFalse, bound, result_type)); } Id Module::OpConstant(Id result_type, const Literal& literal) { - auto op{new Op(spv::Op::OpConstant, bound, result_type)}; + auto op{std::make_unique(spv::Op::OpConstant, bound, result_type)}; op->Add(literal); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpConstantComposite(Id result_type, - const std::vector& constituents) { - auto op{new Op(spv::Op::OpConstantComposite, bound, result_type)}; + const std::vector& constituents) { + auto op{ + std::make_unique(spv::Op::OpConstantComposite, bound, result_type)}; op->Add(constituents); - return AddDeclaration(op); + return AddDeclaration(std::move(op)); } Id Module::OpConstantSampler(Id result_type, - spv::SamplerAddressingMode addressing_mode, - bool normalized, - spv::SamplerFilterMode filter_mode) { + spv::SamplerAddressingMode addressing_mode, + bool normalized, + spv::SamplerFilterMode filter_mode) { AddCapability(spv::Capability::LiteralSampler); AddCapability(spv::Capability::Kernel); - auto op{new Op(spv::Op::OpConstantSampler, bound, result_type)}; - AddEnum(op, addressing_mode); + auto op{ + std::make_unique(spv::Op::OpConstantSampler, bound, result_type)}; + op->Add(static_cast(addressing_mode)); op->Add(normalized ? 1 : 0); - AddEnum(op, filter_mode); - return AddDeclaration(op); + op->Add(static_cast(filter_mode)); + return AddDeclaration(std::move(op)); } Id Module::OpConstantNull(Id result_type) { - return AddDeclaration(new Op(spv::Op::OpConstantNull, bound, result_type)); + return AddDeclaration( + std::make_unique(spv::Op::OpConstantNull, bound, result_type)); } } // namespace Sirit diff --git a/src/insts/debug.cpp b/src/insts/debug.cpp index 04b544a..072833b 100644 --- a/src/insts/debug.cpp +++ b/src/insts/debug.cpp @@ -4,16 +4,18 @@ * Lesser General Public License version 2.1 or any later version. */ -#include "insts.h" +#include "op.h" #include "sirit/sirit.h" +#include +#include namespace Sirit { Id Module::Name(Id target, const std::string& name) { - auto op{new Op(spv::Op::OpName)}; + auto op{std::make_unique(spv::Op::OpName)}; op->Add(target); op->Add(name); - debug.push_back(std::unique_ptr(op)); + debug.push_back(std::move(op)); return target; } diff --git a/src/insts/flow.cpp b/src/insts/flow.cpp index f956d0c..afd0244 100644 --- a/src/insts/flow.cpp +++ b/src/insts/flow.cpp @@ -4,42 +4,43 @@ * 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 namespace Sirit { Id Module::OpLoopMerge(Id merge_block, Id continue_target, - spv::LoopControlMask loop_control, - const std::vector& literals) { - auto op{new Op(spv::Op::OpLoopMerge)}; + spv::LoopControlMask loop_control, + const std::vector& literals) { + auto op{std::make_unique(spv::Op::OpLoopMerge)}; op->Add(merge_block); op->Add(continue_target); - AddEnum(op, loop_control); + op->Add(static_cast(loop_control)); op->Add(literals); - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpSelectionMerge(Id merge_block, - spv::SelectionControlMask selection_control) { - auto op{new Op(spv::Op::OpSelectionMerge)}; + spv::SelectionControlMask selection_control) { + auto op{std::make_unique(spv::Op::OpSelectionMerge)}; op->Add(merge_block); - AddEnum(op, selection_control); - return AddCode(op); + op->Add(static_cast(selection_control)); + return AddCode(std::move(op)); } Id Module::OpLabel() { return AddCode(spv::Op::OpLabel, bound++); } Id Module::OpBranch(Id target_label) { - auto op{new Op(spv::Op::OpBranch)}; + auto op{std::make_unique(spv::Op::OpBranch)}; op->Add(target_label); - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label, - std::uint32_t true_weight, - std::uint32_t false_weight) { - auto op{new Op(spv::Op::OpBranchConditional)}; + u32 true_weight, u32 false_weight) { + auto op{std::make_unique(spv::Op::OpBranchConditional)}; op->Add(condition); op->Add(true_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(false_weight); } - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpReturn() { return AddCode(spv::Op::OpReturn); } diff --git a/src/insts/function.cpp b/src/insts/function.cpp index f686d64..dcd803a 100644 --- a/src/insts/function.cpp +++ b/src/insts/function.cpp @@ -4,18 +4,18 @@ * 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" namespace Sirit { -Id Module::OpFunction(Id result_type, - spv::FunctionControlMask function_control, - Id function_type) { - auto op{new Op{spv::Op::OpFunction, bound++, result_type}}; +Id Module::OpFunction(Id result_type, spv::FunctionControlMask function_control, + Id function_type) { + auto op{std::make_unique(spv::Op::OpFunction, bound++, result_type)}; op->Add(static_cast(function_control)); op->Add(function_type); - return AddCode(op); + return AddCode(std::move(op)); } Id Module::OpFunctionEnd() { return AddCode(spv::Op::OpFunctionEnd); } diff --git a/src/sirit.cpp b/src/sirit.cpp index bb92a13..d4b7fdd 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -84,7 +84,6 @@ void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, } Id Module::Emit(Id op) { - assert(op); code.push_back(op); return op; } @@ -95,34 +94,31 @@ Id Module::AddGlobalVariable(Id variable) { return variable; } -Id Module::AddCode(Op* op) { - assert(op); - code_store.push_back(std::unique_ptr(op)); - return op; +Id Module::AddCode(std::unique_ptr op) { + code_store.push_back(std::move(op)); + return op.get(); } Id Module::AddCode(spv::Op opcode, std::optional id) { - return AddCode(new Op(opcode, id)); + return AddCode(std::make_unique(opcode, id)); } -Id Module::AddDeclaration(Op* op) { +Id Module::AddDeclaration(std::unique_ptr op) { const auto& found{ std::find_if(declarations.begin(), declarations.end(), [&op](const auto& other) { return *other == *op; })}; if (found != declarations.end()) { - delete op; return found->get(); - } else { - declarations.push_back(std::unique_ptr(op)); - bound++; - return op; } + const auto id = op.get(); + declarations.push_back(std::move(op)); + bound++; + return id; } -Id Module::AddAnnotation(Op* op) { - assert(op); - annotations.push_back(std::unique_ptr(op)); - return op; +Id Module::AddAnnotation(std::unique_ptr op) { + annotations.push_back(std::move(op)); + return op.get(); } } // namespace Sirit