1
0
Fork 0
mirror of https://git.suyu.dev/suyu/sirit.git synced 2024-12-22 12:12:05 +00:00

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);
private:
Id AddCode(Op* op);
Id AddCode(std::unique_ptr<Op> op);
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};

View file

@ -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

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.
*/
#include "insts.h"
#include "common_types.h"
#include "op.h"
#include "sirit/sirit.h"
#include <memory>
#include <vector>
namespace Sirit {
Id Module::Decorate(Id target, spv::Decoration decoration,
const std::vector<Literal>& literals) {
auto op{new Op(spv::Op::OpDecorate)};
const std::vector<Literal>& literals) {
auto op{std::make_unique<Op>(spv::Op::OpDecorate)};
op->Add(target);
AddEnum(op, decoration);
op->Add(static_cast<u32>(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<Literal>& literals) {
auto op{new Op(spv::Op::OpMemberDecorate)};
spv::Decoration decoration,
const std::vector<Literal>& literals) {
auto op{std::make_unique<Op>(spv::Op::OpMemberDecorate)};
op->Add(structure_type);
op->Add(member);
AddEnum(op, decoration);
op->Add(static_cast<u32>(decoration));
op->Add(literals);
return AddAnnotation(op);
return AddAnnotation(std::move(op));
}
} // namespace Sirit

View file

@ -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 <cassert>
namespace Sirit {
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) {
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) {
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);
return AddDeclaration(op);
return AddDeclaration(std::move(op));
}
Id Module::OpConstantComposite(Id result_type,
const std::vector<Id>& constituents) {
auto op{new Op(spv::Op::OpConstantComposite, bound, result_type)};
const std::vector<Id>& constituents) {
auto op{
std::make_unique<Op>(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<Op>(spv::Op::OpConstantSampler, bound, result_type)};
op->Add(static_cast<u32>(addressing_mode));
op->Add(normalized ? 1 : 0);
AddEnum(op, filter_mode);
return AddDeclaration(op);
op->Add(static_cast<u32>(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<Op>(spv::Op::OpConstantNull, bound, result_type));
}
} // namespace Sirit

View file

@ -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 <memory>
#include <string>
namespace Sirit {
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(name);
debug.push_back(std::unique_ptr<Op>(op));
debug.push_back(std::move(op));
return target;
}

View file

@ -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 <vector>
namespace Sirit {
Id Module::OpLoopMerge(Id merge_block, Id continue_target,
spv::LoopControlMask loop_control,
const std::vector<Id>& literals) {
auto op{new Op(spv::Op::OpLoopMerge)};
spv::LoopControlMask loop_control,
const std::vector<Id>& literals) {
auto op{std::make_unique<Op>(spv::Op::OpLoopMerge)};
op->Add(merge_block);
op->Add(continue_target);
AddEnum(op, loop_control);
op->Add(static_cast<u32>(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<Op>(spv::Op::OpSelectionMerge)};
op->Add(merge_block);
AddEnum(op, selection_control);
return AddCode(op);
op->Add(static_cast<u32>(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<Op>(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<Op>(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); }

View file

@ -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<Op>(spv::Op::OpFunction, bound++, result_type)};
op->Add(static_cast<u32>(function_control));
op->Add(function_type);
return AddCode(op);
return AddCode(std::move(op));
}
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) {
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>(op));
return op;
Id Module::AddCode(std::unique_ptr<Op> op) {
code_store.push_back(std::move(op));
return op.get();
}
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{
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>(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>(op));
return op;
Id Module::AddAnnotation(std::unique_ptr<Op> op) {
annotations.push_back(std::move(op));
return op.get();
}
} // namespace Sirit