1// Copyright (c) 2016 Google Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17//    https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27#ifndef SPIRV_TOOLS_LIBSPIRV_HPP_
28#define SPIRV_TOOLS_LIBSPIRV_HPP_
29
30#include <memory>
31#include <string>
32#include <vector>
33
34#include "module.h"
35#include "spirv-tools/libspirv.h"
36
37namespace spvtools {
38
39// C++ interface for SPIRV-Tools functionalities. It wraps the context
40// (including target environment and the corresponding SPIR-V grammar) and
41// provides methods for assembling, disassembling, validating, and optimizing.
42//
43// Instances of this class are thread-safe.
44class SpvTools {
45 public:
46  // Creates an instance targeting the given environment |env|.
47  SpvTools(spv_target_env env) : context_(spvContextCreate(env)) {}
48
49  ~SpvTools() { spvContextDestroy(context_); }
50
51  // TODO(antiagainst): handle error message in the following APIs.
52
53  // Assembles the given assembly |text| and writes the result to |binary|.
54  // Returns SPV_SUCCESS on successful assembling.
55  spv_result_t Assemble(const std::string& text, std::vector<uint32_t>* binary);
56
57  // Disassembles the given SPIR-V |binary| and returns the assembly. Returns
58  // SPV_SUCCESS on successful disassembling.
59  spv_result_t Disassemble(const std::vector<uint32_t>& binary,
60                           std::string* text);
61
62  // Builds and returns a Module from the given SPIR-V |binary|.
63  std::unique_ptr<ir::Module> BuildModule(const std::vector<uint32_t>& binary);
64
65  // Builds and returns a Module from the given SPIR-V assembly |text|.
66  std::unique_ptr<ir::Module> BuildModule(const std::string& text);
67
68 private:
69  // Context for the current invocation. Thread-safety of this class depends on
70  // the constness of this field.
71  spv_context context_;
72};
73
74}  // namespace spvtools
75
76#endif  // SPIRV_TOOLS_LIBSPIRV_HPP_
77