blob: c9976ca4bf60a51e5eaf03ef7f3873fb0fd43de0 [file] [log] [blame]
Lei Zhangabf8f642016-06-28 10:23:13 -04001// Copyright (c) 2016 Google Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Lei Zhangabf8f642016-06-28 10:23:13 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhangabf8f642016-06-28 10:23:13 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Lei Zhangabf8f642016-06-28 10:23:13 -040014
Lei Zhang620f05e2016-09-16 16:12:04 -040015#include "spirv-tools/libspirv.hpp"
Lei Zhangabf8f642016-06-28 10:23:13 -040016
Lei Zhang755f97f2016-09-02 18:06:18 -040017#include "table.h"
Lei Zhangabf8f642016-06-28 10:23:13 -040018
19namespace spvtools {
20
Pierre Moreau7183ad52018-01-03 01:54:55 +010021Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {}
22
23Context::Context(Context&& other) : context_(other.context_) {
24 other.context_ = nullptr;
25}
26
27Context& Context::operator=(Context&& other) {
28 spvContextDestroy(context_);
29 context_ = other.context_;
30 other.context_ = nullptr;
31
32 return *this;
33}
34
35Context::~Context() { spvContextDestroy(context_); }
36
37void Context::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040038 SetContextMessageConsumer(context_, std::move(consumer));
Pierre Moreau7183ad52018-01-03 01:54:55 +010039}
40
41spv_context& Context::CContext() { return context_; }
42
43const spv_context& Context::CContext() const { return context_; }
44
Lei Zhang8654caa2016-09-09 10:46:23 -040045// Structs for holding the data members for SpvTools.
Lei Zhang92411ca2016-09-16 15:56:30 -040046struct SpirvTools::Impl {
Lei Zhang8654caa2016-09-09 10:46:23 -040047 explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
48 // The default consumer in spv_context_t is a null consumer, which provides
49 // equivalent functionality (from the user's perspective) as a real consumer
50 // does nothing.
51 }
52 ~Impl() { spvContextDestroy(context); }
53
54 spv_context context; // C interface context object.
55};
56
Lei Zhang92411ca2016-09-16 15:56:30 -040057SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
Lei Zhang8654caa2016-09-09 10:46:23 -040058
Lei Zhang92411ca2016-09-16 15:56:30 -040059SpirvTools::~SpirvTools() {}
Lei Zhang8654caa2016-09-09 10:46:23 -040060
Lei Zhang92411ca2016-09-16 15:56:30 -040061void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040062 SetContextMessageConsumer(impl_->context, std::move(consumer));
Lei Zhang755f97f2016-09-02 18:06:18 -040063}
64
Lei Zhang92411ca2016-09-16 15:56:30 -040065bool SpirvTools::Assemble(const std::string& text,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040066 std::vector<uint32_t>* binary,
67 uint32_t options) const {
68 return Assemble(text.data(), text.size(), binary, options);
Lei Zhang5edf0542016-09-20 18:03:37 -040069}
70
71bool SpirvTools::Assemble(const char* text, const size_t text_size,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040072 std::vector<uint32_t>* binary,
73 uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040074 spv_binary spvbinary = nullptr;
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040075 spv_result_t status = spvTextToBinaryWithOptions(
76 impl_->context, text, text_size, options, &spvbinary, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040077 if (status == SPV_SUCCESS) {
78 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
79 }
Lei Zhangabf8f642016-06-28 10:23:13 -040080 spvBinaryDestroy(spvbinary);
Lei Zhang8654caa2016-09-09 10:46:23 -040081 return status == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040082}
83
Lei Zhang92411ca2016-09-16 15:56:30 -040084bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
85 std::string* text, uint32_t options) const {
Lei Zhang5edf0542016-09-20 18:03:37 -040086 return Disassemble(binary.data(), binary.size(), text, options);
87}
88
89bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
90 std::string* text, uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040091 spv_text spvtext = nullptr;
Lei Zhang5edf0542016-09-20 18:03:37 -040092 spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
93 options, &spvtext, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040094 if (status == SPV_SUCCESS) {
95 text->assign(spvtext->str, spvtext->str + spvtext->length);
96 }
Lei Zhangabf8f642016-06-28 10:23:13 -040097 spvTextDestroy(spvtext);
Lei Zhang8654caa2016-09-09 10:46:23 -040098 return status == SPV_SUCCESS;
99}
Lei Zhangabf8f642016-06-28 10:23:13 -0400100
Lei Zhang92411ca2016-09-16 15:56:30 -0400101bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
Lei Zhang5edf0542016-09-20 18:03:37 -0400102 return Validate(binary.data(), binary.size());
103}
104
105bool SpirvTools::Validate(const uint32_t* binary,
106 const size_t binary_size) const {
107 return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
108 SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -0400109}
110
David Neto0066a362017-03-16 15:06:12 -0400111bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
dan sinclair3dad1cd2018-07-07 09:38:00 -0400112 const ValidatorOptions& options) const {
David Neto0066a362017-03-16 15:06:12 -0400113 spv_const_binary_t the_binary{binary, binary_size};
114 return spvValidateWithOptions(impl_->context, options, &the_binary,
115 nullptr) == SPV_SUCCESS;
116}
117
Lei Zhangabf8f642016-06-28 10:23:13 -0400118} // namespace spvtools