/* 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 * General Public License version 2 or any later version. */ #pragma once #include #include #include #include #include namespace Sirit { static const std::uint32_t GeneratorMagicNumber = 0; class Op; class Module { 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. * @return A stream of bytes representing a SPIR-V module. */ std::vector Assembly() const; /** * Optimizes module's IR. * All returned references become invalid. * @param level Level of optimization. */ void Optimize(int level); /// Adds a module capability. void AddCapability(spv::Capability capability); /// Sets module memory model. void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model); /// Adds an entry point. void AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_point, const std::string& name, const std::vector& interfaces = {}); /** * Adds an instruction to module's code * @param op Instruction to insert into code. Types must not be emitted * @return Returns op. */ const Op* Emit(const Op* op); // Types /// Returns type void. const Op* TypeVoid(); /// Returns a function type. const Op* TypeFunction(const Op* return_type, const std::vector& arguments = {}); // Function /// Emits a function. const Op* Function(const Op* result_type, spv::FunctionControlMask function_control, const Op* function_type); /// Emits a function end. const Op* FunctionEnd(); // Flow /// Emits a label. It starts a block. const Op* Label(); /// Emits a return. It ends a block. const Op* Return(); private: const Op* AddCode(Op* op); const Op* AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX); const Op* AddDeclaration(Op* op); std::uint32_t bound{1}; std::set capabilities; std::set extensions; std::set> ext_inst_import; spv::AddressingModel addressing_model{spv::AddressingModel::Logical}; spv::MemoryModel memory_model{spv::MemoryModel::GLSL450}; std::vector> entry_points; std::vector> execution_mode; std::vector> debug; std::vector> annotations; std::vector> declarations; std::vector code; std::vector> code_store; }; } // namespace Sirit