Rename Ref -> Op

This commit is contained in:
ReinUsesLisp 2018-08-25 20:34:06 -03:00
parent 34d215d3d8
commit 1de01c95ae
6 changed files with 88 additions and 88 deletions

View File

@ -16,7 +16,7 @@ namespace Sirit {
static const std::uint32_t GeneratorMagicNumber = 0; static const std::uint32_t GeneratorMagicNumber = 0;
class Ref; class Op;
class Module { class Module {
public: public:
@ -44,37 +44,37 @@ public:
void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model); void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model);
/// Adds an entry point. /// Adds an entry point.
void AddEntryPoint(spv::ExecutionModel execution_model, const Ref* entry_point, void AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_point,
const std::string& name, const std::vector<const Ref*>& interfaces = {}); const std::string& name, const std::vector<const Op*>& interfaces = {});
/// Returns type void. /// Returns type void.
const Ref* TypeVoid(); const Op* TypeVoid();
/// Returns a function type. /// Returns a function type.
const Ref* TypeFunction(const Ref* return_type, const std::vector<const Ref*>& arguments = {}); const Op* TypeFunction(const Op* return_type, const std::vector<const Op*>& arguments = {});
/// Adds a reference to code block /// Adds an instruction to module's code block
void Add(const Ref* ref); const Op* Emit(const Op* op);
/// Emits a function. /// Emits a function.
const Ref* EmitFunction(const Ref* result_type, spv::FunctionControlMask function_control, const Op* Function(const Op* result_type, spv::FunctionControlMask function_control,
const Ref* function_type); const Op* function_type);
/// Emits a label. It starts a block. /// Emits a label. It starts a block.
const Ref* EmitLabel(); const Op* Label();
/// Emits a return. It ends a block. /// Emits a return. It ends a block.
const Ref* EmitReturn(); const Op* Return();
/// Emits a function end. /// Emits a function end.
const Ref* EmitFunctionEnd(); const Op* FunctionEnd();
private: private:
const Ref* AddCode(Ref* ref); const Op* AddCode(Op* op);
const Ref* AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX); const Op* AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX);
const Ref* AddDeclaration(Ref* ref); const Op* AddDeclaration(Op* op);
std::uint32_t bound{1}; std::uint32_t bound{1};
@ -82,24 +82,24 @@ private:
std::set<std::string> extensions; std::set<std::string> extensions;
std::set<std::unique_ptr<Ref>> ext_inst_import; std::set<std::unique_ptr<Op>> ext_inst_import;
spv::AddressingModel addressing_model{spv::AddressingModel::Logical}; spv::AddressingModel addressing_model{spv::AddressingModel::Logical};
spv::MemoryModel memory_model{spv::MemoryModel::GLSL450}; spv::MemoryModel memory_model{spv::MemoryModel::GLSL450};
std::vector<std::unique_ptr<Ref>> entry_points; std::vector<std::unique_ptr<Op>> entry_points;
std::vector<std::unique_ptr<Ref>> execution_mode; std::vector<std::unique_ptr<Op>> execution_mode;
std::vector<std::unique_ptr<Ref>> debug; std::vector<std::unique_ptr<Op>> debug;
std::vector<std::unique_ptr<Ref>> annotations; std::vector<std::unique_ptr<Op>> annotations;
std::vector<std::unique_ptr<Ref>> declarations; std::vector<std::unique_ptr<Op>> declarations;
std::vector<const Ref*> code; std::vector<const Op*> code;
std::vector<std::unique_ptr<Ref>> code_store; std::vector<std::unique_ptr<Op>> code_store;
}; };
} // namespace Sirit } // namespace Sirit

View File

@ -1,8 +1,8 @@
add_library(sirit add_library(sirit
../include/sirit/sirit.h ../include/sirit/sirit.h
sirit.cpp sirit.cpp
ref.cpp op.cpp
ref.h op.h
stream.cpp stream.cpp
stream.h stream.h
operand.cpp operand.cpp

View File

@ -7,35 +7,35 @@
#include <cassert> #include <cassert>
#include "common_types.h" #include "common_types.h"
#include "operand.h" #include "operand.h"
#include "ref.h" #include "op.h"
namespace Sirit { namespace Sirit {
Ref::Ref(spv::Op opcode_, u32 id_, const Ref* result_type_) Op::Op(spv::Op opcode_, u32 id_, const Op* result_type_)
: opcode(opcode_), id(id_), result_type(result_type_) { : opcode(opcode_), id(id_), result_type(result_type_) {
operand_type = OperandType::Ref; operand_type = OperandType::Ref;
} }
Ref::~Ref() = default; Op::~Op() = default;
void Ref::Fetch(Stream& stream) const { void Op::Fetch(Stream& stream) const {
assert(id != UINT32_MAX); assert(id != UINT32_MAX);
stream.Write(id); stream.Write(id);
} }
u16 Ref::GetWordCount() const { u16 Op::GetWordCount() const {
return 1; return 1;
} }
bool Ref::operator==(const Operand& other) const { bool Op::operator==(const Operand& other) const {
if (operand_type != other.GetType()) { if (operand_type != other.GetType()) {
return false; return false;
} }
const Ref& ref = dynamic_cast<const Ref&>(other); const Op& op = dynamic_cast<const Op&>(other);
if (ref.opcode == opcode && result_type == ref.result_type && if (op.opcode == opcode && result_type == op.result_type &&
operands.size() == ref.operands.size()) { operands.size() == op.operands.size()) {
for (std::size_t i{}; i < operands.size(); i++) { for (std::size_t i{}; i < operands.size(); i++) {
if (*operands[i] != *ref.operands[i]) { if (*operands[i] != *op.operands[i]) {
return false; return false;
} }
} }
@ -44,7 +44,7 @@ bool Ref::operator==(const Operand& other) const {
return false; return false;
} }
void Ref::Write(Stream& stream) const { void Op::Write(Stream& stream) const {
stream.Write(static_cast<u16>(opcode)); stream.Write(static_cast<u16>(opcode));
stream.Write(WordCount()); stream.Write(WordCount());
@ -59,30 +59,30 @@ void Ref::Write(Stream& stream) const {
} }
} }
void Ref::Add(Operand* operand) { void Op::Add(Operand* operand) {
Add(static_cast<const Operand*>(operand)); Add(static_cast<const Operand*>(operand));
operand_store.push_back(std::unique_ptr<Operand>(operand)); operand_store.push_back(std::unique_ptr<Operand>(operand));
} }
void Ref::Add(const Operand* operand) { void Op::Add(const Operand* operand) {
operands.push_back(operand); operands.push_back(operand);
} }
void Ref::Add(u32 integer) { void Op::Add(u32 integer) {
Add(new LiteralInteger(integer)); Add(new LiteralInteger(integer));
} }
void Ref::Add(const std::string& string) { void Op::Add(const std::string& string) {
Add(new LiteralString(string)); Add(new LiteralString(string));
} }
void Ref::Add(const std::vector<const Ref*>& ids) { void Op::Add(const std::vector<const Op*>& ids) {
for (const Ref* ref : ids) { for (const Op* op : ids) {
Add(ref); Add(op);
} }
} }
u16 Ref::WordCount() const { u16 Op::WordCount() const {
u16 count{1}; u16 count{1};
if (result_type) { if (result_type) {
count++; count++;

View File

@ -13,10 +13,10 @@
namespace Sirit { namespace Sirit {
class Ref : public Operand { class Op : public Operand {
public: public:
explicit Ref(spv::Op opcode, u32 id = UINT32_MAX, const Ref* result_type = nullptr); explicit Op(spv::Op opcode, u32 id = UINT32_MAX, const Op* result_type = nullptr);
~Ref(); ~Op();
virtual void Fetch(Stream& stream) const; virtual void Fetch(Stream& stream) const;
virtual u16 GetWordCount() const; virtual u16 GetWordCount() const;
@ -33,14 +33,14 @@ public:
void Add(const std::string& string); void Add(const std::string& string);
void Add(const std::vector<const Ref*>& ids); void Add(const std::vector<const Op*>& ids);
private: private:
u16 WordCount() const; u16 WordCount() const;
spv::Op opcode; spv::Op opcode;
const Ref* result_type; const Op* result_type;
u32 id; u32 id;

View File

@ -8,16 +8,16 @@
#include <cassert> #include <cassert>
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include "common_types.h" #include "common_types.h"
#include "ref.h" #include "op.h"
#include "stream.h" #include "stream.h"
namespace Sirit { namespace Sirit {
template<typename T> template<typename T>
static void WriteEnum(Stream& stream, spv::Op op, T value) { static void WriteEnum(Stream& stream, spv::Op opcode, T value) {
Ref ref{op}; Op op{opcode};
ref.Add(static_cast<u32>(value)); op.Add(static_cast<u32>(value));
ref.Write(stream); op.Write(stream);
} }
Module::Module() {} Module::Module() {}
@ -42,7 +42,7 @@ std::vector<u8> Module::Assembly() const {
// TODO write ext inst imports // TODO write ext inst imports
Ref memory_model_ref{spv::Op::OpMemoryModel}; Op memory_model_ref{spv::Op::OpMemoryModel};
memory_model_ref.Add(static_cast<u32>(addressing_model)); memory_model_ref.Add(static_cast<u32>(addressing_model));
memory_model_ref.Add(static_cast<u32>(memory_model)); memory_model_ref.Add(static_cast<u32>(memory_model));
memory_model_ref.Write(stream); memory_model_ref.Write(stream);
@ -79,74 +79,75 @@ void Module::SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryMo
this->memory_model = memory_model; this->memory_model = memory_model;
} }
void Module::AddEntryPoint(spv::ExecutionModel execution_model, const Ref* entry_point, void Module::AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_point,
const std::string& name, const std::vector<const Ref*>& interfaces) { const std::string& name, const std::vector<const Op*>& interfaces) {
Ref* op{new Ref(spv::Op::OpEntryPoint)}; Op* op{new Op(spv::Op::OpEntryPoint)};
op->Add(static_cast<u32>(execution_model)); op->Add(static_cast<u32>(execution_model));
op->Add(entry_point); op->Add(entry_point);
op->Add(name); op->Add(name);
op->Add(interfaces); op->Add(interfaces);
entry_points.push_back(std::unique_ptr<Ref>(op)); entry_points.push_back(std::unique_ptr<Op>(op));
} }
const Ref* Module::TypeVoid() { const Op* Module::TypeVoid() {
return AddDeclaration(new Ref(spv::Op::OpTypeVoid, bound)); return AddDeclaration(new Op(spv::Op::OpTypeVoid, bound));
} }
const Ref* Module::TypeFunction(const Ref* return_type, const std::vector<const Ref*>& arguments) { const Op* Module::TypeFunction(const Op* return_type, const std::vector<const Op*>& arguments) {
Ref* type_func{new Ref(spv::Op::OpTypeFunction, bound)}; Op* type_func{new Op(spv::Op::OpTypeFunction, bound)};
type_func->Add(return_type); type_func->Add(return_type);
for (const Ref* arg : arguments) { for (const Op* arg : arguments) {
type_func->Add(arg); type_func->Add(arg);
} }
return AddDeclaration(type_func); return AddDeclaration(type_func);
} }
void Module::Add(const Ref* ref) { const Op* Module::Emit(const Op* op) {
assert(ref); assert(op);
code.push_back(ref); code.push_back(op);
return op;
} }
const Ref* Module::EmitFunction(const Ref* result_type, spv::FunctionControlMask function_control, const Op* Module::Function(const Op* result_type, spv::FunctionControlMask function_control,
const Ref* function_type) { const Op* function_type) {
Ref* op{new Ref{spv::Op::OpFunction, bound++, result_type}}; Op* 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(op);
} }
const Ref* Module::EmitLabel() { const Op* Module::Label() {
return AddCode(spv::Op::OpLabel, bound++); return AddCode(spv::Op::OpLabel, bound++);
} }
const Ref* Module::EmitReturn() { const Op* Module::Return() {
return AddCode(spv::Op::OpReturn); return AddCode(spv::Op::OpReturn);
} }
const Ref* Module::EmitFunctionEnd() { const Op* Module::FunctionEnd() {
return AddCode(spv::Op::OpFunctionEnd); return AddCode(spv::Op::OpFunctionEnd);
} }
const Ref* Module::AddCode(Ref* ref) { const Op* Module::AddCode(Op* op) {
code_store.push_back(std::unique_ptr<Ref>(ref)); code_store.push_back(std::unique_ptr<Op>(op));
return ref; return op;
} }
const Ref* Module::AddCode(spv::Op opcode, u32 id) { const Op* Module::AddCode(spv::Op opcode, u32 id) {
return AddCode(new Ref{opcode, id}); return AddCode(new Op{opcode, id});
} }
const Ref* Module::AddDeclaration(Ref* ref) { const Op* Module::AddDeclaration(Op* op) {
const auto& found{std::find_if(declarations.begin(), declarations.end(), [=](const auto& other) { const auto& found{std::find_if(declarations.begin(), declarations.end(), [=](const auto& other) {
return *other == *ref; return *other == *op;
})}; })};
if (found != declarations.end()) { if (found != declarations.end()) {
delete ref; delete op;
return found->get(); return found->get();
} else { } else {
declarations.push_back(std::unique_ptr<Ref>(ref)); declarations.push_back(std::unique_ptr<Op>(op));
bound++; bound++;
return ref; return op;
} }
} }

View File

@ -18,11 +18,10 @@ public:
SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450); SetMemoryModel(spv::AddressingModel::Logical, spv::MemoryModel::GLSL450);
auto main_type{TypeFunction(TypeVoid())}; auto main_type{TypeFunction(TypeVoid())};
auto main_func{EmitFunction(TypeVoid(), spv::FunctionControlMask::MaskNone, main_type)}; auto main_func{Emit(Function(TypeVoid(), spv::FunctionControlMask::MaskNone, main_type))};
Add(main_func); Emit(Label());
Add(EmitLabel()); Emit(Return());
Add(EmitReturn()); Emit(FunctionEnd());
Add(EmitFunctionEnd());
AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main"); AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main");
} }