blob: dcbfa154f98b3e331fcdf4cbe5e1f1f3953c7e4a [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 Zhang8654caa2016-09-09 10:46:23 -040017#include "message.h"
Lei Zhang755f97f2016-09-02 18:06:18 -040018#include "table.h"
Lei Zhangabf8f642016-06-28 10:23:13 -040019
20namespace spvtools {
21
Lei Zhang8654caa2016-09-09 10:46:23 -040022// Structs for holding the data members for SpvTools.
Lei Zhang92411ca2016-09-16 15:56:30 -040023struct SpirvTools::Impl {
Lei Zhang8654caa2016-09-09 10:46:23 -040024 explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
25 // The default consumer in spv_context_t is a null consumer, which provides
26 // equivalent functionality (from the user's perspective) as a real consumer
27 // does nothing.
28 }
29 ~Impl() { spvContextDestroy(context); }
30
31 spv_context context; // C interface context object.
32};
33
Lei Zhang92411ca2016-09-16 15:56:30 -040034SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
Lei Zhang8654caa2016-09-09 10:46:23 -040035
Lei Zhang92411ca2016-09-16 15:56:30 -040036SpirvTools::~SpirvTools() {}
Lei Zhang8654caa2016-09-09 10:46:23 -040037
Lei Zhang92411ca2016-09-16 15:56:30 -040038void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
Lei Zhang8654caa2016-09-09 10:46:23 -040039 SetContextMessageConsumer(impl_->context, std::move(consumer));
Lei Zhang755f97f2016-09-02 18:06:18 -040040}
41
Lei Zhang92411ca2016-09-16 15:56:30 -040042bool SpirvTools::Assemble(const std::string& text,
43 std::vector<uint32_t>* binary) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040044 spv_binary spvbinary = nullptr;
Lei Zhang8654caa2016-09-09 10:46:23 -040045 spv_result_t status = spvTextToBinary(impl_->context, text.data(),
46 text.size(), &spvbinary, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040047 if (status == SPV_SUCCESS) {
48 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
49 }
Lei Zhangabf8f642016-06-28 10:23:13 -040050 spvBinaryDestroy(spvbinary);
Lei Zhang8654caa2016-09-09 10:46:23 -040051 return status == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040052}
53
Lei Zhang92411ca2016-09-16 15:56:30 -040054bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
55 std::string* text, uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040056 spv_text spvtext = nullptr;
Lei Zhang8654caa2016-09-09 10:46:23 -040057 spv_result_t status = spvBinaryToText(
58 impl_->context, binary.data(), binary.size(), options, &spvtext, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040059 if (status == SPV_SUCCESS) {
60 text->assign(spvtext->str, spvtext->str + spvtext->length);
61 }
Lei Zhangabf8f642016-06-28 10:23:13 -040062 spvTextDestroy(spvtext);
Lei Zhang8654caa2016-09-09 10:46:23 -040063 return status == SPV_SUCCESS;
64}
Lei Zhangabf8f642016-06-28 10:23:13 -040065
Lei Zhang92411ca2016-09-16 15:56:30 -040066bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
Lei Zhang8654caa2016-09-09 10:46:23 -040067 spv_const_binary_t b = {binary.data(), binary.size()};
68 return spvValidate(impl_->context, &b, nullptr) == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040069}
70
Lei Zhangabf8f642016-06-28 10:23:13 -040071} // namespace spvtools