From 1458bd2c1c628f3143cae1c1d8db63973a303e30 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 23 Oct 2018 04:45:56 -0300 Subject: [PATCH] Add OpDecorate --- include/sirit/sirit.h | 8 ++++++++ src/CMakeLists.txt | 7 ++++--- src/insts/annotation.cpp | 21 +++++++++++++++++++++ src/op.cpp | 6 ++++++ src/op.h | 2 ++ src/sirit.cpp | 9 ++++++++- 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/insts/annotation.cpp diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 2fe9a1b..69ba27d 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -204,6 +204,12 @@ class Module { Ref Variable(Ref result_type, spv::StorageClass storage_class, Ref initializer = nullptr); + // Annotation + + /// Add a decoration to target. + Ref Decorate(Ref target, spv::Decoration decoration, + const std::vector& literals = {}); + // Literals static Operand* Literal(std::uint32_t value); static Operand* Literal(std::uint64_t value); @@ -219,6 +225,8 @@ class Module { Ref AddDeclaration(Op* op); + Ref AddAnnotation(Op* op); + std::uint32_t bound{1}; std::set capabilities; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dd2df1d..0adf917 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,8 @@ add_library(sirit ../include/sirit/sirit.h sirit.cpp - op.cpp - op.h + op.cpp + op.h stream.cpp stream.h operand.cpp @@ -12,7 +12,7 @@ add_library(sirit lnumber.h lstring.cpp lstring.h - common_types.h + common_types.h insts.h insts/type.cpp insts/constant.cpp @@ -20,6 +20,7 @@ add_library(sirit insts/flow.cpp insts/debug.cpp insts/memory.cpp + insts/annotation.cpp ) target_include_directories(sirit PUBLIC ../include diff --git a/src/insts/annotation.cpp b/src/insts/annotation.cpp new file mode 100644 index 0000000..81c9c9b --- /dev/null +++ b/src/insts/annotation.cpp @@ -0,0 +1,21 @@ +/* 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. + */ + +#include "insts.h" +#include "sirit/sirit.h" + +namespace Sirit { + +Ref Module::Decorate(Ref target, spv::Decoration decoration, + const std::vector& literals) { + auto const op{new Op(spv::Op::OpDecorate)}; + op->Add(target); + AddEnum(op, decoration); + op->Add(literals); + return AddAnnotation(op); +} + +} // namespace Sirit diff --git a/src/op.cpp b/src/op.cpp index 41929bf..f5eec52 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -77,6 +77,12 @@ void Op::Add(const std::vector& ids) { } } +void Op::Add(const std::vector& operands) { + for (Operand* operand : operands) { + Add(operand); + } +} + u16 Op::WordCount() const { u16 count{1}; if (result_type) { diff --git a/src/op.h b/src/op.h index 2a2294d..d0ef39a 100644 --- a/src/op.h +++ b/src/op.h @@ -37,6 +37,8 @@ class Op : public Operand { void Add(const std::vector& ids); + void Add(const std::vector& operands); + private: u16 WordCount() const; diff --git a/src/sirit.cpp b/src/sirit.cpp index 46a7ac7..816615a 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -55,7 +55,7 @@ std::vector Module::Assemble() const { WriteSet(stream, entry_points); // TODO write execution mode WriteSet(stream, debug); - // TODO write annotations + WriteSet(stream, annotations); WriteSet(stream, declarations); WriteSet(stream, global_variables); WriteSet(stream, code); @@ -99,6 +99,7 @@ Ref Module::AddGlobalVariable(Ref variable) { } Ref Module::AddCode(Op* op) { + assert(op); code_store.push_back(std::unique_ptr(op)); return op; } @@ -121,4 +122,10 @@ Ref Module::AddDeclaration(Op* op) { } } +Ref Module::AddAnnotation(Op* op) { + assert(op); + annotations.push_back(std::unique_ptr(op)); + return op; +} + } // namespace Sirit