diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..922a91a --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: LLVM +IndentWidth: 4 + +Language: Cpp +DerivePointerAlignment: false +PointerAlignment: Left diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 728a4b7..5aee8b8 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -7,16 +7,15 @@ #pragma once #include -#include -#include #include +#include +#include #include +#include namespace Sirit { -static const std::uint32_t GeneratorMagicNumber = 0; - -static const std::uint32_t Undefined = UINT32_MAX; +constexpr std::uint32_t GENERATOR_MAGIC_NUMBER = 0; class Op; class Operand; @@ -24,13 +23,14 @@ class Operand; typedef const Op* Ref; class Module { -public: + public: explicit Module(); ~Module(); /** * Assembles current module into a SPIR-V stream. - * It can be called multiple times but it's recommended to copy code externally. + * It can be called multiple times but it's recommended to copy code + * externally. * @return A stream of bytes representing a SPIR-V module. */ std::vector Assemble() const; @@ -46,19 +46,22 @@ public: void AddCapability(spv::Capability capability); /// Sets module memory model. - void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model); + void SetMemoryModel(spv::AddressingModel addressing_model, + spv::MemoryModel memory_model); /// Adds an entry point. void AddEntryPoint(spv::ExecutionModel execution_model, Ref entry_point, - const std::string& name, const std::vector& interfaces = {}); + const std::string& name, + const std::vector& interfaces = {}); /** * Adds an instruction to module's code - * @param op Instruction to insert into code. Types and constants must not be emitted. + * @param op Instruction to insert into code. Types and constants must not + * be emitted. * @return Returns op. */ Ref Emit(Ref op); - + // Types /// Returns type void. @@ -80,9 +83,9 @@ public: Ref TypeMatrix(Ref column_type, int column_count); /// Returns type image. - Ref TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms, - int sampled, spv::ImageFormat image_format, - spv::AccessQualifier access_qualifier = static_cast(Undefined)); + Ref TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, + bool ms, int sampled, spv::ImageFormat image_format, + std::optional access_qualifier = {}); /// Returns type sampler. Ref TypeSampler(); @@ -124,7 +127,7 @@ public: Ref TypePipe(spv::AccessQualifier access_qualifier); // Constant - + /// Returns a true scalar constant. Ref ConstantTrue(Ref result_type); @@ -135,10 +138,12 @@ public: Ref Constant(Ref result_type, Operand* literal); /// Returns a numeric scalar constant. - Ref ConstantComposite(Ref result_type, const std::vector& constituents); + Ref ConstantComposite(Ref result_type, + const std::vector& constituents); /// Returns a sampler constant. - Ref ConstantSampler(Ref result_type, spv::SamplerAddressingMode addressing_mode, + Ref ConstantSampler(Ref result_type, + spv::SamplerAddressingMode addressing_mode, bool normalized, spv::SamplerFilterMode filter_mode); /// Returns a null constant value. @@ -147,7 +152,8 @@ public: // Function /// Declares a function. - Ref Function(Ref result_type, spv::FunctionControlMask function_control, Ref function_type); + Ref Function(Ref result_type, spv::FunctionControlMask function_control, + Ref function_type); /// Ends a function. Ref FunctionEnd(); @@ -155,21 +161,26 @@ public: // Flow /// Declare a structured loop. - Ref LoopMerge(Ref merge_block, Ref continue_target, spv::LoopControlMask loop_control, + Ref LoopMerge(Ref merge_block, Ref continue_target, + spv::LoopControlMask loop_control, const std::vector& literals = {}); /// Declare a structured selection. - Ref SelectionMerge(Ref merge_block, spv::SelectionControlMask selection_control); + Ref SelectionMerge(Ref merge_block, + spv::SelectionControlMask selection_control); - /// The block label instruction: Any reference to a block is through this ref. + /// The block label instruction: Any reference to a block is through this + /// ref. Ref Label(); /// Unconditional jump to label. Ref Branch(Ref target_label); - /// If condition is true branch to true_label, otherwise branch to false_label. + /// If condition is true branch to true_label, otherwise branch to + /// false_label. Ref BranchConditional(Ref condition, Ref true_label, Ref false_label, - std::uint32_t true_weight = 0, std::uint32_t false_weight = 0); + std::uint32_t true_weight = 0, + std::uint32_t false_weight = 0); /// Returns with no value from a function with void return type. Ref Return(); @@ -188,10 +199,10 @@ public: static Operand* Literal(float value); static Operand* Literal(double value); -private: + private: Ref AddCode(Op* op); - Ref AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX); + Ref AddCode(spv::Op opcode, std::optional id = {}); Ref AddDeclaration(Op* op); diff --git a/src/insts.h b/src/insts.h index 68f3998..b08b58a 100644 --- a/src/insts.h +++ b/src/insts.h @@ -6,13 +6,12 @@ #pragma once -#include "stream.h" #include "op.h" +#include "stream.h" namespace Sirit { -template -inline void AddEnum(Op* op, T value) { +template inline void AddEnum(Op* op, T value) { op->Add(static_cast(value)); } diff --git a/src/insts/constant.cpp b/src/insts/constant.cpp index cf3b405..5bae45e 100644 --- a/src/insts/constant.cpp +++ b/src/insts/constant.cpp @@ -19,13 +19,13 @@ Ref Module::ConstantFalse(Ref result_type) { } Ref Module::Constant(Ref result_type, Operand* literal) { - Op* op{new Op(spv::Op::OpConstant, bound, result_type)}; + auto const op{new Op(spv::Op::OpConstant, bound, result_type)}; op->Add(literal); return AddDeclaration(op); } Ref Module::ConstantComposite(Ref result_type, const std::vector& constituents) { - Op* op{new Op(spv::Op::OpConstantComposite, bound, result_type)}; + auto const op{new Op(spv::Op::OpConstantComposite, bound, result_type)}; op->Add(constituents); return AddDeclaration(op); } @@ -34,7 +34,7 @@ Ref Module::ConstantSampler(Ref result_type, spv::SamplerAddressingMode addressi bool normalized, spv::SamplerFilterMode filter_mode) { AddCapability(spv::Capability::LiteralSampler); AddCapability(spv::Capability::Kernel); - Op* op{new Op(spv::Op::OpConstantSampler, bound, result_type)}; + auto const op{new Op(spv::Op::OpConstantSampler, bound, result_type)}; AddEnum(op, addressing_mode); op->Add(normalized ? 1 : 0); AddEnum(op, filter_mode); diff --git a/src/insts/debug.cpp b/src/insts/debug.cpp index 5e7ea0f..974dde1 100644 --- a/src/insts/debug.cpp +++ b/src/insts/debug.cpp @@ -10,7 +10,7 @@ namespace Sirit { Ref Module::Name(Ref target, const std::string& name) { - Op* op{new Op(spv::Op::OpName)}; + auto const op{new Op(spv::Op::OpName)}; op->Add(target); op->Add(name); debug.push_back(std::unique_ptr(op)); diff --git a/src/insts/flow.cpp b/src/insts/flow.cpp index d559fe1..9f0fbb4 100644 --- a/src/insts/flow.cpp +++ b/src/insts/flow.cpp @@ -11,7 +11,7 @@ namespace Sirit { Ref Module::LoopMerge(Ref merge_block, Ref continue_target, spv::LoopControlMask loop_control, const std::vector& literals) { - Op* op{new Op(spv::Op::OpLoopMerge)}; + auto const op{new Op(spv::Op::OpLoopMerge)}; op->Add(merge_block); op->Add(continue_target); AddEnum(op, loop_control); @@ -20,7 +20,7 @@ Ref Module::LoopMerge(Ref merge_block, Ref continue_target, spv::LoopControlMask } Ref Module::SelectionMerge(Ref merge_block, spv::SelectionControlMask selection_control) { - Op* op{new Op(spv::Op::OpSelectionMerge)}; + auto const op{new Op(spv::Op::OpSelectionMerge)}; op->Add(merge_block); AddEnum(op, selection_control); return AddCode(op); @@ -31,14 +31,14 @@ Ref Module::Label() { } Ref Module::Branch(Ref target_label) { - Op* op{new Op(spv::Op::OpBranch)}; + auto const op{new Op(spv::Op::OpBranch)}; op->Add(target_label); return AddCode(op); } Ref Module::BranchConditional(Ref condition, Ref true_label, Ref false_label, std::uint32_t true_weight, std::uint32_t false_weight) { - Op* op{new Op(spv::Op::OpBranchConditional)}; + auto const op{new Op(spv::Op::OpBranchConditional)}; op->Add(condition); op->Add(true_label); op->Add(false_label); diff --git a/src/insts/function.cpp b/src/insts/function.cpp index 210832e..f49ea99 100644 --- a/src/insts/function.cpp +++ b/src/insts/function.cpp @@ -10,7 +10,7 @@ namespace Sirit { Ref Module::Function(Ref result_type, spv::FunctionControlMask function_control, Ref function_type) { - Op* op{new Op{spv::Op::OpFunction, bound++, result_type}}; + auto const op{new Op{spv::Op::OpFunction, bound++, result_type}}; op->Add(static_cast(function_control)); op->Add(function_type); return AddCode(op); diff --git a/src/insts/type.cpp b/src/insts/type.cpp index a3cde94..c9ada14 100644 --- a/src/insts/type.cpp +++ b/src/insts/type.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "sirit/sirit.h" #include "insts.h" @@ -26,7 +27,7 @@ Ref Module::TypeInt(int width, bool is_signed) { } else if (width == 64) { AddCapability(spv::Capability::Int64); } - Op* op{new Op(spv::Op::OpTypeInt, bound)}; + auto const op{new Op(spv::Op::OpTypeInt, bound)}; op->Add(width); op->Add(is_signed ? 1 : 0); return AddDeclaration(op); @@ -38,14 +39,14 @@ Ref Module::TypeFloat(int width) { } else if (width == 64) { AddCapability(spv::Capability::Float64); } - Op* op{new Op(spv::Op::OpTypeFloat, bound)}; + auto const op{new Op(spv::Op::OpTypeFloat, bound)}; op->Add(width); return AddDeclaration(op); } Ref Module::TypeVector(Ref component_type, int component_count) { assert(component_count >= 2); - Op* op{new Op(spv::Op::OpTypeVector, bound)}; + auto const op{new Op(spv::Op::OpTypeVector, bound)}; op->Add(component_type); op->Add(component_count); return AddDeclaration(op); @@ -61,8 +62,8 @@ Ref Module::TypeMatrix(Ref column_type, int column_count) { } Ref Module::TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms, - int sampled, spv::ImageFormat image_format, - spv::AccessQualifier access_qualifier) { + int sampled, spv::ImageFormat image_format, + std::optional access_qualifier) { switch (dim) { case spv::Dim::Dim1D: AddCapability(spv::Capability::Sampled1D); @@ -123,7 +124,7 @@ Ref Module::TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, b AddCapability(spv::Capability::StorageImageExtendedFormats); break; } - Op* op{new Op(spv::Op::OpTypeImage, bound)}; + auto const op{new Op(spv::Op::OpTypeImage, bound)}; op->Add(sampled_type); op->Add(static_cast(dim)); op->Add(depth); @@ -131,9 +132,9 @@ Ref Module::TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, b op->Add(ms ? 1 : 0); op->Add(sampled); op->Add(static_cast(image_format)); - if (static_cast(access_qualifier) != Undefined) { + if (access_qualifier.has_value()) { AddCapability(spv::Capability::Kernel); - op->Add(static_cast(access_qualifier)); + op->Add(static_cast(access_qualifier.value())); } return AddDeclaration(op); } @@ -143,13 +144,13 @@ Ref Module::TypeSampler() { } Ref Module::TypeSampledImage(Ref image_type) { - Op* op{new Op(spv::Op::OpTypeSampledImage, bound)}; + auto const op{new Op(spv::Op::OpTypeSampledImage, bound)}; op->Add(image_type); return AddDeclaration(op); } Ref Module::TypeArray(Ref element_type, Ref length) { - Op* op{new Op(spv::Op::OpTypeArray, bound)}; + auto const op{new Op(spv::Op::OpTypeArray, bound)}; op->Add(element_type); op->Add(length); return AddDeclaration(op); @@ -157,20 +158,20 @@ Ref Module::TypeArray(Ref element_type, Ref length) { Ref Module::TypeRuntimeArray(Ref element_type) { AddCapability(spv::Capability::Shader); - Op* op{new Op(spv::Op::OpTypeRuntimeArray, bound)}; + auto const op{new Op(spv::Op::OpTypeRuntimeArray, bound)}; op->Add(element_type); return AddDeclaration(op); } Ref Module::TypeStruct(const std::vector& members) { - Op* op{new Op(spv::Op::OpTypeStruct, bound)}; + auto const op{new Op(spv::Op::OpTypeStruct, bound)}; op->Add(members); return AddDeclaration(op); } Ref Module::TypeOpaque(const std::string& name) { AddCapability(spv::Capability::Kernel); - Op* op{new Op(spv::Op::OpTypeOpaque, bound)}; + auto const op{new Op(spv::Op::OpTypeOpaque, bound)}; op->Add(name); return AddDeclaration(op); } @@ -191,14 +192,14 @@ Ref Module::TypePointer(spv::StorageClass storage_class, Ref type) { AddCapability(spv::Capability::AtomicStorage); break; } - Op* op{new Op(spv::Op::OpTypePointer, bound)}; + auto const op{new Op(spv::Op::OpTypePointer, bound)}; op->Add(static_cast(storage_class)); op->Add(type); return AddDeclaration(op); } Ref Module::TypeFunction(Ref return_type, const std::vector& arguments) { - Op* op{new Op(spv::Op::OpTypeFunction, bound)}; + auto const op{new Op(spv::Op::OpTypeFunction, bound)}; op->Add(return_type); op->Add(arguments); return AddDeclaration(op); @@ -226,7 +227,7 @@ Ref Module::TypeQueue() { Ref Module::TypePipe(spv::AccessQualifier access_qualifier) { AddCapability(spv::Capability::Pipes); - Op* op{new Op(spv::Op::OpTypePipe, bound)}; + auto const op{new Op(spv::Op::OpTypePipe, bound)}; op->Add(static_cast(access_qualifier)); return AddDeclaration(op); } diff --git a/src/literal.cpp b/src/literal.cpp index a36824c..b1e29bc 100644 --- a/src/literal.cpp +++ b/src/literal.cpp @@ -4,35 +4,23 @@ * Lesser General Public License version 2.1 or any later version. */ -#include "sirit/sirit.h" #include "common_types.h" -#include "operand.h" #include "lnumber.h" +#include "operand.h" +#include "sirit/sirit.h" namespace Sirit { -Operand* Module::Literal(u32 value) { - return new LiteralNumber(value); -} +#define DEFINE_LITERAL(type) \ + Operand* Module::Literal(type value) { \ + return LiteralNumber::Create(value); \ + } -Operand* Module::Literal(u64 value) { - return new LiteralNumber(value); -} - -Operand* Module::Literal(s32 value) { - return new LiteralNumber(value); -} - -Operand* Module::Literal(s64 value) { - return new LiteralNumber(value); -} - -Operand* Module::Literal(f32 value) { - return new LiteralNumber(value); -} - -Operand* Module::Literal(f64 value) { - return new LiteralNumber(value); -} +DEFINE_LITERAL(u32) +DEFINE_LITERAL(u64) +DEFINE_LITERAL(s32) +DEFINE_LITERAL(s64) +DEFINE_LITERAL(f32) +DEFINE_LITERAL(f64) } // namespace Sirit diff --git a/src/lnumber.cpp b/src/lnumber.cpp index 9c347b0..e7f79f1 100644 --- a/src/lnumber.cpp +++ b/src/lnumber.cpp @@ -4,79 +4,26 @@ * Lesser General Public License version 2.1 or any later version. */ -#include #include "lnumber.h" +#include namespace Sirit { -LiteralNumber::LiteralNumber() { +LiteralNumber::LiteralNumber(std::type_index type) : type(type) { operand_type = OperandType::Number; } -LiteralNumber::LiteralNumber(u32 number) - : uint32(number), type(NumberType::U32) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(s32 number) - : int32(number), type(NumberType::S32) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(f32 number) - : float32(number), type(NumberType::F32) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(u64 number) - : uint64(number), type(NumberType::U64) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(s64 number) - : int64(number), type(NumberType::S64) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(f64 number) - : float64(number), type(NumberType::F64) { - LiteralNumber(); -} - LiteralNumber::~LiteralNumber() = default; void LiteralNumber::Fetch(Stream& stream) const { - switch (type) { - case NumberType::S32: - case NumberType::U32: - case NumberType::F32: - stream.Write(uint32); - break; - case NumberType::S64: - case NumberType::U64: - case NumberType::F64: - stream.Write(uint64); - break; - default: - assert(0); + if (is_32) { + stream.Write(static_cast(raw)); + } else { + stream.Write(raw); } } -u16 LiteralNumber::GetWordCount() const { - switch (type) { - case NumberType::S32: - case NumberType::U32: - case NumberType::F32: - return 1; - case NumberType::S64: - case NumberType::U64: - case NumberType::F64: - return 2; - default: - assert(0); - return 0; - } -} +u16 LiteralNumber::GetWordCount() const { return is_32 ? 1 : 2; } bool LiteralNumber::operator==(const Operand& other) const { if (operand_type == other.GetType()) { diff --git a/src/lnumber.h b/src/lnumber.h index 6b86504..8beb169 100644 --- a/src/lnumber.h +++ b/src/lnumber.h @@ -6,19 +6,15 @@ #pragma once -#include "stream.h" #include "operand.h" - +#include "stream.h" +#include + namespace Sirit { class LiteralNumber : public Operand { -public: - LiteralNumber(u32 number); - LiteralNumber(s32 number); - LiteralNumber(f32 number); - LiteralNumber(u64 number); - LiteralNumber(s64 number); - LiteralNumber(f64 number); + public: + LiteralNumber(std::type_index type); ~LiteralNumber(); virtual void Fetch(Stream& stream) const; @@ -26,27 +22,21 @@ public: virtual bool operator==(const Operand& other) const; -private: - LiteralNumber(); + template static LiteralNumber* Create(T value) { + static_assert(sizeof(T) == 4 || sizeof(T) == 8); + LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T))); + if (number->is_32 = sizeof(T) == 4; number->is_32) { + number->raw = *reinterpret_cast(&value); + } else { + number->raw = *reinterpret_cast(&value); + } + return number; + } - enum class NumberType { - U32, - S32, - F32, - U64, - S64, - F64 - } type; - - union { - u64 raw{}; - u32 uint32; - s32 int32; - u64 uint64; - s64 int64; - f32 float32; - f64 float64; - }; + private: + std::type_index type; + bool is_32; + u64 raw; }; } // namespace Sirit diff --git a/src/lstring.cpp b/src/lstring.cpp index d15d933..3843a9a 100644 --- a/src/lstring.cpp +++ b/src/lstring.cpp @@ -8,8 +8,7 @@ namespace Sirit { -LiteralString::LiteralString(const std::string& string_) - : string(string_) { +LiteralString::LiteralString(const std::string& string_) : string(string_) { operand_type = OperandType::String; } diff --git a/src/lstring.h b/src/lstring.h index ea54e23..76fd75b 100644 --- a/src/lstring.h +++ b/src/lstring.h @@ -6,14 +6,14 @@ #pragma once -#include -#include "stream.h" #include "operand.h" +#include "stream.h" +#include namespace Sirit { class LiteralString : public Operand { -public: + public: LiteralString(const std::string& string); ~LiteralString(); @@ -22,7 +22,7 @@ public: virtual bool operator==(const Operand& other) const; -private: + private: std::string string; }; diff --git a/src/op.cpp b/src/op.cpp index 40ce310..41929bf 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -5,29 +5,28 @@ */ #include + #include "common_types.h" -#include "operand.h" -#include "op.h" #include "lnumber.h" #include "lstring.h" +#include "op.h" +#include "operand.h" namespace Sirit { -Op::Op(spv::Op opcode_, u32 id_, Ref result_type_) - : opcode(opcode_), id(id_), result_type(result_type_) { +Op::Op(spv::Op opcode, std::optional id, Ref result_type) + : opcode(opcode), id(id), result_type(result_type) { operand_type = OperandType::Op; } Op::~Op() = default; void Op::Fetch(Stream& stream) const { - assert(id != UINT32_MAX); - stream.Write(id); + assert(id.has_value()); + stream.Write(id.value()); } -u16 Op::GetWordCount() const { - return 1; -} +u16 Op::GetWordCount() const { return 1; } bool Op::operator==(const Operand& other) const { if (operand_type != other.GetType()) { @@ -53,8 +52,8 @@ void Op::Write(Stream& stream) const { if (result_type) { result_type->Fetch(stream); } - if (id != UINT32_MAX) { - stream.Write(id); + if (id.has_value()) { + stream.Write(id.value()); } for (const Operand* operand : operands) { operand->Fetch(stream); @@ -66,17 +65,11 @@ void Op::Add(Operand* operand) { operand_store.push_back(std::unique_ptr(operand)); } -void Op::Add(const Operand* operand) { - operands.push_back(operand); -} +void Op::Add(const Operand* operand) { operands.push_back(operand); } -void Op::Add(u32 integer) { - Add(new LiteralNumber(integer)); -} +void Op::Add(u32 integer) { Add(LiteralNumber::Create(integer)); } -void Op::Add(const std::string& string) { - Add(new LiteralString(string)); -} +void Op::Add(const std::string& string) { Add(new LiteralString(string)); } void Op::Add(const std::vector& ids) { for (Ref op : ids) { @@ -89,7 +82,7 @@ u16 Op::WordCount() const { if (result_type) { count++; } - if (id != UINT32_MAX) { + if (id.has_value()) { count++; } for (const Operand* operand : operands) { diff --git a/src/op.h b/src/op.h index 45d62f9..2a2294d 100644 --- a/src/op.h +++ b/src/op.h @@ -6,16 +6,18 @@ #pragma once -#include "sirit/sirit.h" #include "common_types.h" #include "operand.h" +#include "sirit/sirit.h" #include "stream.h" +#include namespace Sirit { class Op : public Operand { -public: - explicit Op(spv::Op opcode, u32 id = UINT32_MAX, Ref result_type = nullptr); + public: + explicit Op(spv::Op opcode, std::optional id = {}, + Ref result_type = nullptr); ~Op(); virtual void Fetch(Stream& stream) const; @@ -34,15 +36,15 @@ public: void Add(const std::string& string); void Add(const std::vector& ids); - -private: + + private: u16 WordCount() const; spv::Op opcode; Ref result_type; - u32 id; + std::optional id; std::vector operands; diff --git a/src/operand.cpp b/src/operand.cpp index b2ed322..44648c3 100644 --- a/src/operand.cpp +++ b/src/operand.cpp @@ -4,8 +4,8 @@ * Lesser General Public License version 2.1 or any later version. */ -#include #include "operand.h" +#include namespace Sirit { @@ -22,16 +22,12 @@ u16 Operand::GetWordCount() const { return 0; } -bool Operand::operator==(const Operand& other) const { - return false; -} +bool Operand::operator==(const Operand& other) const { return false; } bool Operand::operator!=(const Operand& other) const { return !(*this == other); } -OperandType Operand::GetType() const { - return operand_type; -} +OperandType Operand::GetType() const { return operand_type; } } // namespace Sirit diff --git a/src/operand.h b/src/operand.h index 2583f76..ee9aa81 100644 --- a/src/operand.h +++ b/src/operand.h @@ -10,15 +10,10 @@ namespace Sirit { -enum class OperandType { - Invalid, - Op, - Number, - String -}; +enum class OperandType { Invalid, Op, Number, String }; class Operand { -public: + public: Operand(); virtual ~Operand(); @@ -30,7 +25,7 @@ public: OperandType GetType() const; -protected: + protected: OperandType operand_type{}; }; diff --git a/src/sirit.cpp b/src/sirit.cpp index 7f01cb6..3d73399 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -4,17 +4,17 @@ * Lesser General Public License version 2.1 or any later version. */ -#include -#include #include "sirit/sirit.h" #include "common_types.h" #include "op.h" #include "stream.h" +#include +#include namespace Sirit { -template -inline void WriteEnum(Stream& stream, spv::Op opcode, T value) { +template +static void WriteEnum(Stream& stream, spv::Op opcode, T value) { Op op{opcode}; op.Add(static_cast(value)); op.Write(stream); @@ -30,7 +30,7 @@ std::vector Module::Assemble() const { stream.Write(spv::MagicNumber); stream.Write(spv::Version); - stream.Write(GeneratorMagicNumber); + stream.Write(GENERATOR_MAGIC_NUMBER); stream.Write(bound); stream.Write(static_cast(0)); @@ -69,21 +69,22 @@ std::vector Module::Assemble() const { return bytes; } -void Module::Optimize(int level) { -} +void Module::Optimize(int level) {} void Module::AddCapability(spv::Capability capability) { capabilities.insert(capability); } -void Module::SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model) { +void Module::SetMemoryModel(spv::AddressingModel addressing_model, + spv::MemoryModel memory_model) { this->addressing_model = addressing_model; this->memory_model = memory_model; } void Module::AddEntryPoint(spv::ExecutionModel execution_model, Ref entry_point, - const std::string& name, const std::vector& interfaces) { - Op* op{new Op(spv::Op::OpEntryPoint)}; + const std::string& name, + const std::vector& interfaces) { + auto const op{new Op(spv::Op::OpEntryPoint)}; op->Add(static_cast(execution_model)); op->Add(entry_point); op->Add(name); @@ -102,14 +103,14 @@ Ref Module::AddCode(Op* op) { return op; } -Ref Module::AddCode(spv::Op opcode, u32 id) { - return AddCode(new Op{opcode, id}); +Ref Module::AddCode(spv::Op opcode, std::optional id) { + return AddCode(new Op(opcode, id)); } Ref Module::AddDeclaration(Op* op) { - const auto& found{std::find_if(declarations.begin(), declarations.end(), [=](const auto& other) { - return *other == *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(); diff --git a/src/stream.cpp b/src/stream.cpp index a1144aa..8bfdf65 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -8,42 +8,39 @@ namespace Sirit { -Stream::Stream(std::vector& bytes_) - : bytes(bytes_) {} +Stream::Stream(std::vector& bytes_) : bytes(bytes_) {} Stream::~Stream() = default; void Stream::Write(std::string string) { - std::size_t size{string.size()}; - u8* data{reinterpret_cast(string.data())}; - for (std::size_t i{}; i < size; i++) { + const auto size{string.size()}; + const auto data{reinterpret_cast(string.data())}; + for (std::size_t i = 0; i < size; i++) { Write(data[i]); } - for (std::size_t i{}; i < 4 - size % 4; i++) { + for (std::size_t i = 0; i < 4 - size % 4; i++) { Write(static_cast(0)); } } void Stream::Write(u64 value) { - u32* mem{reinterpret_cast(&value)}; + const auto mem{reinterpret_cast(&value)}; Write(mem[0]); Write(mem[1]); } void Stream::Write(u32 value) { - u16* mem{reinterpret_cast(&value)}; + const auto mem{reinterpret_cast(&value)}; Write(mem[0]); Write(mem[1]); } void Stream::Write(u16 value) { - u8* mem{reinterpret_cast(&value)}; + const auto mem{reinterpret_cast(&value)}; Write(mem[0]); Write(mem[1]); } -void Stream::Write(u8 value) { - bytes.push_back(value); -} +void Stream::Write(u8 value) { bytes.push_back(value); } } // namespace Sirit diff --git a/src/stream.h b/src/stream.h index a87676d..4eec9bf 100644 --- a/src/stream.h +++ b/src/stream.h @@ -6,17 +6,17 @@ #pragma once +#include "common_types.h" #include #include -#include "common_types.h" namespace Sirit { class Stream { -public: + public: explicit Stream(std::vector& bytes); ~Stream(); - + void Write(std::string string); void Write(u64 value); @@ -27,7 +27,7 @@ public: void Write(u8 value); -private: + private: std::vector& bytes; };