diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index be921c3..2fe9a1b 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -62,6 +62,13 @@ class Module { */ Ref Emit(Ref op); + /** + * Adds a global variable + * @param variable Global variable to add. + * @return Returns variable. + */ + Ref AddGlobalVariable(Ref variable); + // Types /// Returns type void. @@ -233,6 +240,8 @@ class Module { std::vector> declarations; + std::vector global_variables; + std::vector code; std::vector> code_store; diff --git a/src/insts/type.cpp b/src/insts/type.cpp index c9ada14..3eac6b7 100644 --- a/src/insts/type.cpp +++ b/src/insts/type.cpp @@ -6,6 +6,7 @@ #include #include + #include "sirit/sirit.h" #include "insts.h" diff --git a/src/sirit.cpp b/src/sirit.cpp index 3d73399..46a7ac7 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -20,6 +20,13 @@ static void WriteEnum(Stream& stream, spv::Op opcode, T value) { op.Write(stream); } +template +static void WriteSet(Stream& stream, const T& set) { + for (const auto& item : set) { + item->Write(stream); + } +} + Module::Module() {} Module::~Module() = default; @@ -37,9 +44,7 @@ std::vector Module::Assemble() const { for (auto capability : capabilities) { WriteEnum(stream, spv::Op::OpCapability, capability); } - // TODO write extensions - // TODO write ext inst imports Op memory_model_ref{spv::Op::OpMemoryModel}; @@ -47,24 +52,13 @@ std::vector Module::Assemble() const { memory_model_ref.Add(static_cast(memory_model)); memory_model_ref.Write(stream); - for (const auto& entry_point : entry_points) { - entry_point->Write(stream); - } - + WriteSet(stream, entry_points); // TODO write execution mode - - for (const auto& debug_symbol : debug) { - debug_symbol->Write(stream); - } - + WriteSet(stream, debug); // TODO write annotations - - for (const auto& decl : declarations) { - decl->Write(stream); - } - for (const auto& line : code) { - line->Write(stream); - } + WriteSet(stream, declarations); + WriteSet(stream, global_variables); + WriteSet(stream, code); return bytes; } @@ -98,6 +92,12 @@ Ref Module::Emit(Ref op) { return op; } +Ref Module::AddGlobalVariable(Ref variable) { + assert(variable); + global_variables.push_back(variable); + return variable; +} + Ref Module::AddCode(Op* op) { code_store.push_back(std::unique_ptr(op)); return op;