blob: 77e03e57c465b21e10cb8d9d6f3159b6cc56a095 [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
Lei Zhang8654caa2016-09-09 10:46:23 -040021// Structs for holding the data members for SpvTools.
Lei Zhang92411ca2016-09-16 15:56:30 -040022struct SpirvTools::Impl {
Lei Zhang8654caa2016-09-09 10:46:23 -040023 explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
24 // The default consumer in spv_context_t is a null consumer, which provides
25 // equivalent functionality (from the user's perspective) as a real consumer
26 // does nothing.
27 }
28 ~Impl() { spvContextDestroy(context); }
29
30 spv_context context; // C interface context object.
31};
32
Lei Zhang92411ca2016-09-16 15:56:30 -040033SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
Lei Zhang8654caa2016-09-09 10:46:23 -040034
Lei Zhang92411ca2016-09-16 15:56:30 -040035SpirvTools::~SpirvTools() {}
Lei Zhang8654caa2016-09-09 10:46:23 -040036
Lei Zhang92411ca2016-09-16 15:56:30 -040037void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
Lei Zhang8654caa2016-09-09 10:46:23 -040038 SetContextMessageConsumer(impl_->context, std::move(consumer));
Lei Zhang755f97f2016-09-02 18:06:18 -040039}
40
Lei Zhang92411ca2016-09-16 15:56:30 -040041bool SpirvTools::Assemble(const std::string& text,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040042 std::vector<uint32_t>* binary,
43 uint32_t options) const {
44 return Assemble(text.data(), text.size(), binary, options);
Lei Zhang5edf0542016-09-20 18:03:37 -040045}
46
47bool SpirvTools::Assemble(const char* text, const size_t text_size,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040048 std::vector<uint32_t>* binary,
49 uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040050 spv_binary spvbinary = nullptr;
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040051 spv_result_t status = spvTextToBinaryWithOptions(
52 impl_->context, text, text_size, options, &spvbinary, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040053 if (status == SPV_SUCCESS) {
54 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
55 }
Lei Zhangabf8f642016-06-28 10:23:13 -040056 spvBinaryDestroy(spvbinary);
Lei Zhang8654caa2016-09-09 10:46:23 -040057 return status == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040058}
59
Lei Zhang92411ca2016-09-16 15:56:30 -040060bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
61 std::string* text, uint32_t options) const {
Lei Zhang5edf0542016-09-20 18:03:37 -040062 return Disassemble(binary.data(), binary.size(), text, options);
63}
64
65bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
66 std::string* text, uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040067 spv_text spvtext = nullptr;
Lei Zhang5edf0542016-09-20 18:03:37 -040068 spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
69 options, &spvtext, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040070 if (status == SPV_SUCCESS) {
71 text->assign(spvtext->str, spvtext->str + spvtext->length);
72 }
Lei Zhangabf8f642016-06-28 10:23:13 -040073 spvTextDestroy(spvtext);
Lei Zhang8654caa2016-09-09 10:46:23 -040074 return status == SPV_SUCCESS;
75}
Lei Zhangabf8f642016-06-28 10:23:13 -040076
Lei Zhang92411ca2016-09-16 15:56:30 -040077bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
Lei Zhang5edf0542016-09-20 18:03:37 -040078 return Validate(binary.data(), binary.size());
79}
80
81bool SpirvTools::Validate(const uint32_t* binary,
82 const size_t binary_size) const {
83 return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
84 SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040085}
86
David Neto0066a362017-03-16 15:06:12 -040087bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
88 const spvtools::ValidatorOptions& options) const {
89 spv_const_binary_t the_binary{binary, binary_size};
90 return spvValidateWithOptions(impl_->context, options, &the_binary,
91 nullptr) == SPV_SUCCESS;
92}
93
Lei Zhangabf8f642016-06-28 10:23:13 -040094} // namespace spvtools