From c2215fca0ef8f0e5c56667abab2c0495c343e84c Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sun, 26 Aug 2018 05:01:31 -0300 Subject: [PATCH] Move flow and function opcodes into their own file --- include/sirit/sirit.h | 22 ++++++++++++++------ src/CMakeLists.txt | 4 +++- src/opcodes/flow.cpp | 20 ++++++++++++++++++ src/opcodes/function.cpp | 24 ++++++++++++++++++++++ src/{opcodes_type.cpp => opcodes/type.cpp} | 0 src/sirit.cpp | 20 ------------------ 6 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 src/opcodes/flow.cpp create mode 100644 src/opcodes/function.cpp rename src/{opcodes_type.cpp => opcodes/type.cpp} (100%) diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 90def4b..c38ba79 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -47,18 +47,31 @@ public: 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 = {}); - /// Adds an instruction to module's code block - const Op* Emit(const Op* op); + // Function /// Emits a function. const Op* Function(const Op* result_type, spv::FunctionControlMask function_control, - const Op* function_type); + const Op* function_type); + + /// Emits a function end. + const Op* FunctionEnd(); + + // Flow /// Emits a label. It starts a block. const Op* Label(); @@ -66,9 +79,6 @@ public: /// Emits a return. It ends a block. const Op* Return(); - /// Emits a function end. - const Op* FunctionEnd(); - private: const Op* AddCode(Op* op); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9d32797..25bc732 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,9 @@ add_library(sirit operand.h common_types.h opcodes.h - opcodes_type.cpp + opcodes/type.cpp + opcodes/function.cpp + opcodes/flow.cpp ) target_include_directories(sirit PUBLIC ../include diff --git a/src/opcodes/flow.cpp b/src/opcodes/flow.cpp new file mode 100644 index 0000000..84a6ec0 --- /dev/null +++ b/src/opcodes/flow.cpp @@ -0,0 +1,20 @@ +/* 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. + */ + +#include "sirit/sirit.h" +#include "opcodes.h" + +namespace Sirit { + +const Op* Module::Label() { + return AddCode(spv::Op::OpLabel, bound++); +} + +const Op* Module::Return() { + return AddCode(spv::Op::OpReturn); +} + +} // namespace Sirit diff --git a/src/opcodes/function.cpp b/src/opcodes/function.cpp new file mode 100644 index 0000000..4b861a4 --- /dev/null +++ b/src/opcodes/function.cpp @@ -0,0 +1,24 @@ +/* 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. + */ + +#include "sirit/sirit.h" +#include "opcodes.h" + +namespace Sirit { + +const Op* Module::Function(const Op* result_type, spv::FunctionControlMask function_control, + const Op* function_type) { + Op* op{new Op{spv::Op::OpFunction, bound++, result_type}}; + op->Add(static_cast(function_control)); + op->Add(function_type); + return AddCode(op); +} + +const Op* Module::FunctionEnd() { + return AddCode(spv::Op::OpFunctionEnd); +} + +} // namespace Sirit diff --git a/src/opcodes_type.cpp b/src/opcodes/type.cpp similarity index 100% rename from src/opcodes_type.cpp rename to src/opcodes/type.cpp diff --git a/src/sirit.cpp b/src/sirit.cpp index 2739cf2..a7ced8a 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -89,26 +89,6 @@ const Op* Module::Emit(const Op* op) { return op; } -const Op* Module::Function(const Op* result_type, spv::FunctionControlMask function_control, - const Op* function_type) { - Op* op{new Op{spv::Op::OpFunction, bound++, result_type}}; - op->Add(static_cast(function_control)); - op->Add(function_type); - return AddCode(op); -} - -const Op* Module::Label() { - return AddCode(spv::Op::OpLabel, bound++); -} - -const Op* Module::Return() { - return AddCode(spv::Op::OpReturn); -} - -const Op* Module::FunctionEnd() { - return AddCode(spv::Op::OpFunctionEnd); -} - const Op* Module::AddCode(Op* op) { code_store.push_back(std::unique_ptr(op)); return op;