From f819ade0efe925a782090dea9e1bf300fedffb39 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sun, 14 Feb 2021 22:45:31 -0300 Subject: [PATCH] Add support for forward declarations --- include/sirit/sirit.h | 10 ++++++++++ src/sirit.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 0a204d7..b675650 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -35,6 +35,10 @@ struct Id { std::uint32_t value; }; +[[nodiscard]] inline bool ValidId(Id id) noexcept { + return id.value != 0; +} + class Module { public: explicit Module(std::uint32_t version = spv::Version); @@ -83,6 +87,12 @@ public: AddExecutionMode(entry_point, mode, std::span({literals...})); } + /// Generate a new id for forward declarations + [[nodiscard]] Id ForwardDeclarationId(); + + /// Assign a new id and return the old one, useful for defining forward declarations + Id ExchangeCurrentId(Id new_current_id); + /** * Adds an existing label to the code * @param label Label to insert into code. diff --git a/src/sirit.cpp b/src/sirit.cpp index 1aa0e66..ee4b76e 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -95,6 +95,15 @@ void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode, *execution_modes << spv::Op::OpExecutionMode << entry_point << mode << literals << EndOp{}; } +Id Module::ForwardDeclarationId() { + return Id{++bound}; +} + +Id Module::ExchangeCurrentId(Id new_current_id) { + const std::uint32_t old_id = std::exchange(bound, new_current_id.value - 1); + return Id{old_id + 1}; +} + Id Module::AddLabel(Id label) { assert(label.value != 0); code->Reserve(2);